diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index 140150c..ea9e65a 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -41,9 +41,10 @@ lazy_static! { | |||
41 | } | 41 | } |
42 | 42 | ||
43 | fn main() { | 43 | fn main() { |
44 | env_logger::init(); | ||
44 | if !CONFIGURATION.input.is_empty() { | 45 | if !CONFIGURATION.input.is_empty() { |
45 | // command mode // | 46 | // command mode // |
46 | let evaled = eval_math_expression(&CONFIGURATION.input[..], 0f64); | 47 | let evaled = eval_math_expression(&CONFIGURATION.input[..], Some(0.)); |
47 | match evaled { | 48 | match evaled { |
48 | Ok(ans) => pprint(ans), | 49 | Ok(ans) => pprint(ans), |
49 | Err(e) => { | 50 | Err(e) => { |
@@ -57,17 +58,26 @@ fn main() { | |||
57 | let mut rl = create_readline(); | 58 | let mut rl = create_readline(); |
58 | 59 | ||
59 | // previous answer | 60 | // previous answer |
60 | let mut prev_ans = 0f64; | 61 | let mut prev_ans = None; |
61 | 62 | ||
62 | // handle history storage | 63 | // handle history storage |
63 | let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); | 64 | let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); |
64 | let eva_data_dir = eva_dirs.data_dir(); | 65 | let eva_data_dir = eva_dirs.data_dir(); |
65 | let mut history_path = PathBuf::from(eva_data_dir); | 66 | let mut history_path = PathBuf::from(eva_data_dir); |
67 | let mut previous_ans_path = PathBuf::from(eva_data_dir); | ||
66 | match create_dir_all(eva_data_dir) { | 68 | match create_dir_all(eva_data_dir) { |
67 | Ok(_) => history_path.push("history.txt"), | 69 | Ok(_) => { |
68 | Err(_) => history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()), | 70 | history_path.push("history.txt"); |
71 | previous_ans_path.push("previous_ans.txt"); | ||
72 | }, | ||
73 | Err(_) => { | ||
74 | history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); | ||
75 | previous_ans_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); | ||
76 | } | ||
69 | }; | 77 | }; |
70 | 78 | ||
79 | std::fs::write(&previous_ans_path, "0"); | ||
80 | |||
71 | if rl.load_history(history_path.as_path()).is_err() { | 81 | if rl.load_history(history_path.as_path()).is_err() { |
72 | println!("No previous history.") | 82 | println!("No previous history.") |
73 | }; | 83 | }; |
@@ -81,7 +91,17 @@ fn main() { | |||
81 | let evaled = eval_math_expression(&line[..], prev_ans); | 91 | let evaled = eval_math_expression(&line[..], prev_ans); |
82 | match evaled { | 92 | match evaled { |
83 | Ok(ans) => { | 93 | Ok(ans) => { |
84 | prev_ans = ans; | 94 | use std::io::Write; |
95 | use std::fs::OpenOptions; | ||
96 | let mut file = OpenOptions::new() | ||
97 | .write(true) | ||
98 | .create(true) | ||
99 | .open(&previous_ans_path) | ||
100 | .unwrap(); | ||
101 | |||
102 | prev_ans = Some(ans); | ||
103 | writeln!(file, "{}", ans); | ||
104 | |||
85 | pprint(ans); | 105 | pprint(ans); |
86 | } | 106 | } |
87 | Err(e) => println!("{}", handler(e)), | 107 | Err(e) => println!("{}", handler(e)), |
@@ -148,7 +168,7 @@ fn parse_arguments() -> Configuration { | |||
148 | } | 168 | } |
149 | } | 169 | } |
150 | 170 | ||
151 | pub fn eval_math_expression(input: &str, prev_ans: f64) -> Result<f64, CalcError> { | 171 | pub fn eval_math_expression(input: &str, prev_ans: Option<f64>) -> Result<f64, CalcError> { |
152 | let input = input.trim(); | 172 | let input = input.trim(); |
153 | let input = input.replace(" ", ""); | 173 | let input = input.replace(" ", ""); |
154 | if input.is_empty() { | 174 | if input.is_empty() { |
@@ -203,4 +223,9 @@ mod tests { | |||
203 | let evaled = eval_math_expression("_ + 9", 9f64).unwrap(); | 223 | let evaled = eval_math_expression("_ + 9", 9f64).unwrap(); |
204 | assert_eq!(18.0, evaled); | 224 | assert_eq!(18.0, evaled); |
205 | } | 225 | } |
226 | #[test] | ||
227 | fn eval_with_zero_prev() { | ||
228 | let evaled = eval_math_expression("9 + _ ", 0f64).unwrap(); | ||
229 | assert_eq!(9., evaled); | ||
230 | } | ||
206 | } | 231 | } |