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 <tchiovoloni@mozilla.com>
This commit is contained in:
Thom 2018-01-17 23:33:22 -05:00 committed by Richard Newman
parent 95e95d735e
commit 579829d091
2 changed files with 23 additions and 9 deletions

View file

@ -14,7 +14,7 @@ test = false
[dependencies] [dependencies]
getopts = "0.2" getopts = "0.2"
env_logger = "0.3" env_logger = "0.3"
linefeed = "0.1" linefeed = "0.4"
log = "0.3" log = "0.3"
tempfile = "1.1" tempfile = "1.1"
combine = "2.2.2" combine = "2.2.2"

View file

@ -10,8 +10,11 @@
use std::io::stdin; use std::io::stdin;
use linefeed::Reader; use linefeed::{
use linefeed::terminal::DefaultTerminal; DefaultTerminal,
Reader,
ReadResult,
};
use self::InputResult::*; use self::InputResult::*;
@ -53,7 +56,7 @@ impl InputReader {
pub fn new() -> InputReader { pub fn new() -> InputReader {
let r = match Reader::new("mentat") { let r = match Reader::new("mentat") {
Ok(mut r) => { Ok(mut r) => {
r.set_word_break_chars(" \t\n!\"#$%&'()*+,-./:;<=>?@[\\]^`"); r.set_word_break_chars(" \t\n!\"#$%&'(){}*+,-./:;<=>?@[\\]^`");
Some(r) Some(r)
}, },
Err(_) => None, Err(_) => None,
@ -81,14 +84,16 @@ impl InputReader {
None => return Ok(Eof), None => return Ok(Eof),
}; };
if !self.buffer.is_empty() {
self.buffer.push('\n');
}
self.buffer.push_str(&line); self.buffer.push_str(&line);
if self.buffer.is_empty() { if self.buffer.is_empty() {
return Ok(Empty); return Ok(Empty);
} }
self.add_history(&line);
// if we have a command in process (i.e. an incomplete query or transaction), // 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 // 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. // command again, only the content, which we do later.
@ -118,14 +123,18 @@ impl InputReader {
Ok(More) Ok(More)
}, },
_ => { _ => {
let entry = self.buffer.clone();
self.buffer.clear(); self.buffer.clear();
self.add_history(entry);
self.in_process_cmd = None; self.in_process_cmd = None;
Ok(InputResult::MetaCommand(cmd)) Ok(InputResult::MetaCommand(cmd))
} }
} }
}, },
Err(e) => { Err(e) => {
let entry = self.buffer.clone();
self.buffer.clear(); self.buffer.clear();
self.add_history(entry);
self.in_process_cmd = None; self.in_process_cmd = None;
Err(e) Err(e)
}, },
@ -136,7 +145,12 @@ impl InputReader {
match self.reader { match self.reader {
Some(ref mut r) => { Some(ref mut r) => {
r.set_prompt(prompt); 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() 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 { if let Some(ref mut r) = self.reader {
r.add_history(line.to_owned()); r.add_history(line);
} }
} }
} }