Add possibility to add input instead of calling readline.

This commit is contained in:
Oleksandr Kozachuk
2023-01-06 17:13:42 +01:00
parent e9dba54750
commit 77f7db7dec
2 changed files with 9 additions and 7 deletions
+8 -6
View File
@@ -9,6 +9,7 @@ pub struct LKRead {
pub prompt: String, pub prompt: String,
pub state: LKRef, pub state: LKRef,
pub cmd: String, pub cmd: String,
pub input: Option<String>,
pub read_password: fn(String) -> std::io::Result<String>, pub read_password: fn(String) -> std::io::Result<String>,
} }
@@ -33,6 +34,7 @@ impl LKRead {
prompt, prompt,
state, state,
cmd: "".to_string(), cmd: "".to_string(),
input: None,
read_password: password, read_password: password,
} }
} }
@@ -47,8 +49,9 @@ impl LKRead {
() ()
} }
} }
if self.cmd != "" { self.cmd = match &self.input {
self.cmd = match self.rl.readline(&*self.prompt) { Some(cmd) => cmd.to_string(),
None => match self.rl.readline(&*self.prompt) {
Ok(str) => str, Ok(str) => str,
Err(LKErr::EOF) => "quit".to_string(), Err(LKErr::EOF) => "quit".to_string(),
Err(err) => { Err(err) => {
@@ -58,15 +61,14 @@ impl LKRead {
self.read_password, self.read_password,
) )
} }
}; }
} };
self.rl.add_history_entry(self.cmd.as_str()); self.rl.add_history_entry(self.cmd.as_str());
self.cmd = "".to_string();
match self.rl.save_history(&history_file) { match self.rl.save_history(&history_file) {
Ok(_) => (), Ok(_) => (),
Err(_) => (), Err(_) => (),
} }
match command_parser::cmd(self.cmd.as_str()) { match command_parser::cmd(&self.cmd) {
Ok(cmd) => LKEval::new(cmd, self.state.clone(), self.read_password), Ok(cmd) => LKEval::new(cmd, self.state.clone(), self.read_password),
Err(err) => LKEval::new(Command::Error(LKErr::ParseError(err)), self.state.clone(), self.read_password), Err(err) => LKEval::new(Command::Error(LKErr::ParseError(err)), self.state.clone(), self.read_password),
} }
+1 -1
View File
@@ -14,7 +14,7 @@ lazy_static! {
pub fn hel_command(cmd: String) -> String { pub fn hel_command(cmd: String) -> String {
let editor = Editor::new(); let editor = Editor::new();
let mut lkread = LKRead::new(editor, "> ".to_string(), STATE.clone()); let mut lkread = LKRead::new(editor, "> ".to_string(), STATE.clone());
lkread.cmd = cmd.to_string(); lkread.input = Some(cmd.to_string());
let lkeval = lkread.read(); let lkeval = lkread.read();
let lkprint = lkeval.eval(); let lkprint = lkeval.eval();
lkprint.out.output().join("\n") lkprint.out.output().join("\n")