From: Harishankar Date: Sun, 31 May 2020 10:46:26 +0000 (+0530) Subject: Added command history saving with rustyline X-Git-Tag: 0.1-a^0 X-Git-Url: https://harishankar.org/repos/?p=evpf.git;a=commitdiff_plain;h=a86a7c0712840275401e5ccd79322f3575b918fb Added command history saving with rustyline Added functionality to save the command history in evpf with rustyline load_history and save_history --- diff --git a/src/main.rs b/src/main.rs index f46dfab..8e54265 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use regex::Regex; use colored::*; use std::env; use rustyline::Editor; +use std::path::PathBuf; const ILLEGAL_EXP : &'static str = "Illegal Expression"; const ERR_PARSING_NOT_MATCH : &'static str = "Error parsing expression! \ @@ -13,6 +14,13 @@ const HELP_TEXT : [&str ; 4] = "Supported operators: +, -, *, /", "Type q, Q to quit" ]; + +const HOMEDIR_NOT_FOUND : &'static str = "User home directory not found \ + (missing environment?)."; + +const HISTORY_FILE_NOT_FOUND : &'static str = "History file not found."; + +const SAVE_HISTORY_ERROR : &'static str = "Unable to save command history!"; const ERROR : &'static str = "Error"; const ERROR_HELP : &'static str = "Type ? or h or H for help"; @@ -156,6 +164,30 @@ fn run_command_line (args : &Vec, match_num : ®ex::Regex) { } } +// get the history file name as string - home dir + .evpfhistory +fn get_history_file () -> Result { + // get the environment variable HOME + let home_path = env::var ("HOME"); + // if not found, return an error + if home_path.is_err () { + return Err (HOMEDIR_NOT_FOUND.to_string()); + } + // build the path for the history file i.e. homedir + .evpfhistory in + // platform independent way + let mut hist_file = PathBuf::new (); + hist_file.push (home_path.unwrap()); + hist_file.push (".evpfhistory"); + + // if cannot convert to string return error + if hist_file.to_str ().is_none () { + return Err (HOMEDIR_NOT_FOUND.to_string()); + } + + // return the history file path as a string + let hist_file_path = String::from (hist_file.to_str().unwrap()); + return Ok (hist_file_path); +} + // Interactive mode - display the prompt and evaluate expressions entered into // the prompt - until user quits fn run_interactive_mode (match_num : ®ex::Regex) { @@ -163,6 +195,14 @@ fn run_interactive_mode (match_num : ®ex::Regex) { let mut expr = String::new (); let mut rl = Editor::<()>::new (); + let hist_file = get_history_file (); + if hist_file.is_err () { + eprintln! ("{}", &hist_file.unwrap_err()); + } else { + if rl.load_history (&hist_file.unwrap ()).is_err () { + eprintln! ("{}", HISTORY_FILE_NOT_FOUND.purple()); + } + } // loop until a blank line is received loop { expr.clear (); @@ -205,6 +245,13 @@ fn run_interactive_mode (match_num : ®ex::Regex) { eprintln! ("{}", ERROR_HELP.purple()); } } + // save the history + let hist_file = get_history_file (); + if ! hist_file.is_err () { + if rl.save_history (&hist_file.unwrap()).is_err () { + eprintln! ("{}", SAVE_HISTORY_ERROR); + } + } } fn main() {