From 77f7db7dec57841a72c3829e769d340345be1255 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Fri, 6 Jan 2023 17:13:42 +0100 Subject: [PATCH] Add possibility to add input instead of calling readline. --- hel/src/repl.rs | 14 ++++++++------ helwasm/src/hel_state.rs | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/hel/src/repl.rs b/hel/src/repl.rs index d5c8b1f..61b308f 100644 --- a/hel/src/repl.rs +++ b/hel/src/repl.rs @@ -9,6 +9,7 @@ pub struct LKRead { pub prompt: String, pub state: LKRef, pub cmd: String, + pub input: Option, pub read_password: fn(String) -> std::io::Result, } @@ -33,6 +34,7 @@ impl LKRead { prompt, state, cmd: "".to_string(), + input: None, read_password: password, } } @@ -47,8 +49,9 @@ impl LKRead { () } } - if self.cmd != "" { - self.cmd = match self.rl.readline(&*self.prompt) { + self.cmd = match &self.input { + Some(cmd) => cmd.to_string(), + None => match self.rl.readline(&*self.prompt) { Ok(str) => str, Err(LKErr::EOF) => "quit".to_string(), Err(err) => { @@ -58,15 +61,14 @@ impl LKRead { self.read_password, ) } - }; - } + } + }; self.rl.add_history_entry(self.cmd.as_str()); - self.cmd = "".to_string(); match self.rl.save_history(&history_file) { Ok(_) => (), 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), Err(err) => LKEval::new(Command::Error(LKErr::ParseError(err)), self.state.clone(), self.read_password), } diff --git a/helwasm/src/hel_state.rs b/helwasm/src/hel_state.rs index e9e6687..9d3b64a 100644 --- a/helwasm/src/hel_state.rs +++ b/helwasm/src/hel_state.rs @@ -14,7 +14,7 @@ lazy_static! { pub fn hel_command(cmd: String) -> String { let editor = Editor::new(); 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 lkprint = lkeval.eval(); lkprint.out.output().join("\n")