Added command history and editing using rustyline
[evpf.git] / src / main.rs
index d237193..f46dfab 100644 (file)
@@ -1,15 +1,12 @@
-use std::io;
-use std::io::Write;
 use regex::Regex;
 use colored::*;
 use std::env;
+use rustyline::Editor;
 
 const ILLEGAL_EXP : &'static str = "Illegal Expression";
 const ERR_PARSING_NOT_MATCH : &'static str = "Error parsing expression! \
                                 Must be a number or operator (+,-,* or /)";
 const ERR_POSTFIX_INCOMPLETE : &'static str = "Postfix expression incomplete!";
-const ERR_FLUSHING : &'static str = "Error flushing!";
-const ERR_READING_LINE : &'static str = "Error reading line!";
 const HELP_TEXT : [&str ; 4] = 
                                ["Type a postfix expression to evaluate.",
                                 "Example: 4 5 + 12 -",
@@ -165,15 +162,18 @@ fn run_interactive_mode (match_num : &regex::Regex) {
        // get a line from input and evaluate it 
        let mut expr = String::new ();
 
+       let mut rl = Editor::<()>::new ();
        // loop until a blank line is received  
        loop {
-               expr.clear ();  
-               print!("{}", "evpf>".bold() );
-               io::stdout().flush ().expect (ERR_FLUSHING);
-               // read a line of text
-               io::stdin().read_line (&mut expr).expect (ERR_READING_LINE);
-               // trim the text
-               let expr = expr.trim ();
+               expr.clear ();
+               
+               let line = rl.readline ("evpf> ");
+               if line.is_err () {
+                       break;
+               }
+               let hist = line.unwrap ();
+               rl.add_history_entry (&hist);
+               expr.push_str (&hist);
 
                if expr == "q" || expr == "Q" {
                // quit if the expression is q or Qs