From f1970a657aa4df877a14fd586cb4a8b96d01d01a Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 9 Jul 2024 14:20:04 -0400 Subject: [PATCH] added rustyline --- Cargo.toml | 3 ++- src/main.rs | 26 ++++++++++---------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ee56b44..936bb9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quickmath" -version = "0.2.3" +version = "0.3.0" edition = "2021" authors = [ "Valerie Wolfe " ] description = "A quick command-line math evaluator." @@ -14,6 +14,7 @@ path = "src/main.rs" [dependencies] evalexpr = "11.0.0" +rustyline = "14.0.0" termion = "1.5.6" [profile.release] diff --git a/src/main.rs b/src/main.rs index 8fb110a..251d046 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use std::{ env, io::{ - stdin, stdout, Write @@ -16,6 +15,7 @@ use evalexpr::{ HashMapContext, Value }; +use rustyline::DefaultEditor; use termion::{ color, style @@ -62,33 +62,27 @@ fn main() { for expression in expressions { match expression.as_str() { "help" => help_text(), - _ => do_eval(expression, &mut context) + _ => do_eval(&expression, &mut context) } } } else { + let mut rl = DefaultEditor::new().unwrap(); // enter interactive mode if no args are given println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset); loop { - print!("> "); - stdout().flush().unwrap(); - let mut i_line = String::new(); - let line_result = stdin().read_line(&mut i_line); - if line_result.is_err() { - break; - } - let line = i_line.trim().to_string(); - match line.as_str() { - "" | - "exit" => break, - _ => do_eval(line, &mut context) + match rl.readline("> ").as_deref() { + Ok("") | + Ok("exit") | + Err(_) => break, + + Ok(line) => do_eval(line, &mut context), } reset(); } } } -fn do_eval(i_expression: String, context: &mut HashMapContext) { - let expression = i_expression.as_str(); +fn do_eval(expression: &str, context: &mut HashMapContext) { let i_result = eval_with_context_mut(expression, context); if i_result.is_err() { println!(