X-Git-Url: https://harishankar.org/repos/?p=evpf.git;a=blobdiff_plain;f=src%2Fmain.rs;fp=src%2Fmain.rs;h=f46dfab5f3a1c73df48d5e1146323c099c7978dc;hp=d2371937b73204e75fae8da3dc96fba43fbc461e;hb=8a5044a6aba2dea15733f830988887d6d4f5ca2a;hpb=e12e35ce57fb851e14e6589959c622512a9d1091 diff --git a/src/main.rs b/src/main.rs index d237193..f46dfab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 : ®ex::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