Improve readline history.

This commit is contained in:
Oleksandr Kozachuk
2023-07-18 14:22:15 +02:00
parent b3260e04fb
commit d4eeea73d8
2 changed files with 9 additions and 3 deletions
+7 -1
View File
@@ -41,11 +41,13 @@ impl LKRead {
} }
pub fn read(&mut self) -> LKEval { pub fn read(&mut self) -> LKEval {
let history_file = HISTORY_FILE.to_str().unwrap();
self.cmd = match &self.input { self.cmd = match &self.input {
Some(cmd) => cmd.to_string(), Some(cmd) => cmd.to_string(),
None => match self.rl.lock().readline(&*self.prompt) { None => match self.rl.lock().readline(&*self.prompt) {
Ok(str) => str, Ok(str) => str,
Err(LKErr::EOF) => "quit".to_string(), Err(LKErr::EOF) => "quit".to_string(),
Err(LKErr::Error(_)) => "quit".to_string(),
Err(err) => { Err(err) => {
return LKEval::new( return LKEval::new(
self.rl.clone(), self.rl.clone(),
@@ -58,7 +60,11 @@ impl LKRead {
}; };
match command_parser::cmd(&self.cmd) { match command_parser::cmd(&self.cmd) {
Ok(cmd) => LKEval::new(self.rl.clone(), cmd, self.state.clone(), self.read_password), Ok(cmd) => LKEval::new(self.rl.clone(), cmd, self.state.clone(), self.read_password),
Err(err) => LKEval::new(self.rl.clone(), Command::Error(LKErr::ParseError(err)), self.state.clone(), self.read_password), Err(err) => {
self.rl.lock().add_history_entry(&self.cmd);
self.rl.lock().save_history(&history_file).ok();
LKEval::new(self.rl.clone(), Command::Error(LKErr::ParseError(err)), self.state.clone(), self.read_password)
},
} }
} }
+2 -2
View File
@@ -113,14 +113,14 @@ impl<'a> PartialEq for Command<'a> {
impl<'a> std::fmt::Display for Command<'a> { impl<'a> std::fmt::Display for Command<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Command::Add(s) => write!(f, "add {}", s.lock().borrow().to_string()), Command::Add(s) => write!(f, "add {}", s.lock().borrow().to_string().trim()),
Command::Keep(s) => write!(f, "keep {}", s), Command::Keep(s) => write!(f, "keep {}", s),
Command::Ls(s) => write!(f, "ls {}", s), Command::Ls(s) => write!(f, "ls {}", s),
Command::Ld(s) => write!(f, "ld {}", s), Command::Ld(s) => write!(f, "ld {}", s),
Command::Mv(a, b) => write!(f, "mv {} {}", a, b), Command::Mv(a, b) => write!(f, "mv {} {}", a, b),
Command::Rm(s) => write!(f, "rm {}", s), Command::Rm(s) => write!(f, "rm {}", s),
Command::Enc(s) => write!(f, "enc {}", s), Command::Enc(s) => write!(f, "enc {}", s),
Command::Gen(a, b) => write!(f, "gen{} {}", a, b.lock().borrow().to_string()), Command::Gen(a, b) => write!(f, "gen{} {}", a, b.lock().borrow().to_string().trim()),
Command::Pass(a, None) => write!(f, "pass {}", a), Command::Pass(a, None) => write!(f, "pass {}", a),
Command::Pass(a, Some(b)) => write!(f, "pass {} {}", a, b), Command::Pass(a, Some(b)) => write!(f, "pass {} {}", a, b),
Command::UnPass(s) => write!(f, "unpass {}", s), Command::UnPass(s) => write!(f, "unpass {}", s),