From 579829d091bdca3f3fba891a6ea0954ca6c5bb16 Mon Sep 17 00:00:00 2001 From: Thom Date: Wed, 17 Jan 2018 23:33:22 -0500 Subject: [PATCH] CLI quality-of-life fixes. (#521) r=rnewman * CLI: Update linefeed library to latest version * CLI: Don't store incomplete commands in history * CLI: add curly braces to word separators * Review comment: clean up CLI add_history. Signed-off-by: Thom Chiovoloni --- tools/cli/Cargo.toml | 2 +- tools/cli/src/mentat_cli/input.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/tools/cli/Cargo.toml b/tools/cli/Cargo.toml index 0958a369..3e5104de 100644 --- a/tools/cli/Cargo.toml +++ b/tools/cli/Cargo.toml @@ -14,7 +14,7 @@ test = false [dependencies] getopts = "0.2" env_logger = "0.3" -linefeed = "0.1" +linefeed = "0.4" log = "0.3" tempfile = "1.1" combine = "2.2.2" diff --git a/tools/cli/src/mentat_cli/input.rs b/tools/cli/src/mentat_cli/input.rs index 783db7e7..4b198620 100644 --- a/tools/cli/src/mentat_cli/input.rs +++ b/tools/cli/src/mentat_cli/input.rs @@ -10,8 +10,11 @@ use std::io::stdin; -use linefeed::Reader; -use linefeed::terminal::DefaultTerminal; +use linefeed::{ + DefaultTerminal, + Reader, + ReadResult, +}; use self::InputResult::*; @@ -53,7 +56,7 @@ impl InputReader { pub fn new() -> InputReader { let r = match Reader::new("mentat") { Ok(mut r) => { - r.set_word_break_chars(" \t\n!\"#$%&'()*+,-./:;<=>?@[\\]^`"); + r.set_word_break_chars(" \t\n!\"#$%&'(){}*+,-./:;<=>?@[\\]^`"); Some(r) }, Err(_) => None, @@ -81,14 +84,16 @@ impl InputReader { None => return Ok(Eof), }; + if !self.buffer.is_empty() { + self.buffer.push('\n'); + } + self.buffer.push_str(&line); if self.buffer.is_empty() { return Ok(Empty); } - self.add_history(&line); - // if we have a command in process (i.e. an incomplete query or transaction), // then we already know which type of command it is and so we don't need to parse the // command again, only the content, which we do later. @@ -118,14 +123,18 @@ impl InputReader { Ok(More) }, _ => { + let entry = self.buffer.clone(); self.buffer.clear(); + self.add_history(entry); self.in_process_cmd = None; Ok(InputResult::MetaCommand(cmd)) } } }, Err(e) => { + let entry = self.buffer.clone(); self.buffer.clear(); + self.add_history(entry); self.in_process_cmd = None; Err(e) }, @@ -136,7 +145,12 @@ impl InputReader { match self.reader { Some(ref mut r) => { r.set_prompt(prompt); - r.read_line().ok().and_then(|line| line) + r.read_line().ok().and_then(|line| + match line { + ReadResult::Input(s) => Some(s), + _ => None + }) + }, None => self.read_stdin() } @@ -151,9 +165,9 @@ impl InputReader { } } - fn add_history(&mut self, line: &str) { + fn add_history(&mut self, line: String) { if let Some(ref mut r) = self.reader { - r.add_history(line.to_owned()); + r.add_history(line); } } }