Add readline history
This commit is contained in:
@@ -15,3 +15,4 @@ regex = "1.6.0"
|
|||||||
rustyline = "10.0.0"
|
rustyline = "10.0.0"
|
||||||
thiserror = "1.0.37"
|
thiserror = "1.0.37"
|
||||||
anyerror = "0.1.7"
|
anyerror = "0.1.7"
|
||||||
|
home = "0.5.4"
|
||||||
|
|||||||
+11
-1
@@ -1,5 +1,6 @@
|
|||||||
use rustyline::Editor;
|
use rustyline::Editor;
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
use home::home_dir;
|
||||||
|
|
||||||
use crate::structs::{LKErr, Command, LK};
|
use crate::structs::{LKErr, Command, LK};
|
||||||
use crate::parser::command_parser;
|
use crate::parser::command_parser;
|
||||||
@@ -27,14 +28,23 @@ pub struct LKPrint {
|
|||||||
|
|
||||||
impl LKRead {
|
impl LKRead {
|
||||||
pub fn new(rl: Editor::<()>, prompt: String, state: Rc<RefCell<LK>>) -> Self {
|
pub fn new(rl: Editor::<()>, prompt: String, state: Rc<RefCell<LK>>) -> Self {
|
||||||
Self { rl, prompt, state, cmd: "".to_string() }
|
Self { rl, prompt, state, cmd: "".to_string() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(&mut self) -> LKEval {
|
pub fn read(&mut self) -> LKEval {
|
||||||
|
let history_file_path = home_dir().unwrap().join(".lesskey_history");
|
||||||
|
let history_file = history_file_path.as_path().to_str().unwrap();
|
||||||
|
self.rl.clear_history();
|
||||||
|
match self.rl.load_history(&history_file) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_) => { self.rl.add_history_entry("ls"); () }
|
||||||
|
}
|
||||||
self.cmd = match self.rl.readline(&*self.prompt) {
|
self.cmd = match self.rl.readline(&*self.prompt) {
|
||||||
Ok(str) => str,
|
Ok(str) => str,
|
||||||
Err(err) => return LKEval::new(Command::Error(LKErr::ReadError(err.to_string())), self.state.clone()),
|
Err(err) => return LKEval::new(Command::Error(LKErr::ReadError(err.to_string())), self.state.clone()),
|
||||||
};
|
};
|
||||||
|
self.rl.add_history_entry(self.cmd.as_str());
|
||||||
|
match self.rl.save_history(&history_file) { Ok(_) => (), Err(_) => () }
|
||||||
match command_parser::cmd(self.cmd.as_str()) {
|
match command_parser::cmd(self.cmd.as_str()) {
|
||||||
Ok(cmd) => LKEval::new(cmd, self.state.clone()),
|
Ok(cmd) => LKEval::new(cmd, self.state.clone()),
|
||||||
Err(err) => LKEval::new(Command::Error(LKErr::ParseError(err)), self.state.clone()),
|
Err(err) => LKEval::new(Command::Error(LKErr::ParseError(err)), self.state.clone()),
|
||||||
|
|||||||
Reference in New Issue
Block a user