aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs83
1 files changed, 49 insertions, 34 deletions
diff --git a/src/main.rs b/src/main.rs
index 4e9a4e9..37b8d04 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,5 @@
1use std::f64; 1use std::f64;
2use std::env;
2 3
3mod lex; 4mod lex;
4use crate::lex::*; 5use crate::lex::*;
@@ -11,46 +12,60 @@ use crate::error::{ CalcError, handler };
11 12
12use rustyline::error::ReadlineError; 13use rustyline::error::ReadlineError;
13use rustyline::Editor; 14use rustyline::Editor;
14use rustyline::config::{ Config, Builder, ColorMode, EditMode }; 15use rustyline::config::{ Builder, ColorMode, EditMode };
16
15 17
16fn main() { 18fn main() {
17 let config_builder = Builder::new(); 19 let args: Vec<String> = env::args().collect();
18 let config = config_builder.color_mode(ColorMode::Enabled) 20 if args.len() > 1 {
19 .edit_mode(EditMode::Emacs) 21 let mut expr = String::new();
20 .history_ignore_space(true) 22 for arg in args[1..].iter() {
21 .max_history_size(1000) 23 expr.push_str(&arg[..]);
22 .build(); 24 }
23 let mut rl = Editor::<()>::with_config(config); 25 let evaled = eval_math_expression(&expr[..]);
24 if rl.load_history("history.txt").is_err() { 26 match evaled {
25 println!("No previous history."); 27 Ok(ans) => println!("{}", ans),
26 } 28 Err(e) => handler(e),
29 };
30 } else {
31 let config_builder = Builder::new();
32 let config = config_builder.color_mode(ColorMode::Enabled)
33 .edit_mode(EditMode::Emacs)
34 .history_ignore_space(true)
35 .max_history_size(1000)
36 .build();
37 let mut rl = Editor::<()>::with_config(config);
38 if rl.load_history("history.txt").is_err() {
39 println!("No previous history.");
40 }
27 41
28 loop { 42 loop {
29 let readline = rl.readline("> "); 43 let readline = rl.readline("> ");
30 match readline { 44 match readline {
31 Ok(line) => { 45 Ok(line) => {
32 rl.add_history_entry(line.as_ref()); 46 rl.add_history_entry(line.as_ref());
33 let evaled = eval_math_expression(&line[..]); 47 let evaled = eval_math_expression(&line[..]);
34 match evaled { 48 match evaled {
35 Ok(ans) => println!("{}", ans), 49 Ok(ans) => println!("{}", ans),
36 Err(e) => handler(e), 50 Err(e) => handler(e),
37 }; 51 };
38 }, 52 },
39 Err(ReadlineError::Interrupted) => { 53 Err(ReadlineError::Interrupted) => {
40 println!("CTRL-C"); 54 println!("CTRL-C");
41 break 55 break
42 }, 56 },
43 Err(ReadlineError::Eof) => { 57 Err(ReadlineError::Eof) => {
44 println!("CTRL-D"); 58 println!("CTRL-D");
45 break 59 break
46 }, 60 },
47 Err(err) => { 61 Err(err) => {
48 println!("Error: {:?}", err); 62 println!("Error: {:?}", err);
49 break 63 break
64 }
50 } 65 }
51 } 66 }
67 rl.save_history("history.txt").unwrap();
52 } 68 }
53 rl.save_history("history.txt").unwrap();
54} 69}
55 70
56fn autobalance_parens(input: &str) -> Result<String, CalcError> { 71fn autobalance_parens(input: &str) -> Result<String, CalcError> {