diff --git a/tools/cli/Cargo.toml b/tools/cli/Cargo.toml index 58cbc09d..620a8ce4 100644 --- a/tools/cli/Cargo.toml +++ b/tools/cli/Cargo.toml @@ -31,3 +31,6 @@ path = "../.." [dependencies.mentat_parser_utils] path = "../../parser-utils" + +[dependencies.edn] +path = "../../edn" diff --git a/tools/cli/src/mentat_cli/command_parser.rs b/tools/cli/src/mentat_cli/command_parser.rs index a28b0e01..d20b85bb 100644 --- a/tools/cli/src/mentat_cli/command_parser.rs +++ b/tools/cli/src/mentat_cli/command_parser.rs @@ -30,6 +30,8 @@ use combine::combinator::{ use errors as cli; +use edn; + pub static HELP_COMMAND: &'static str = &"help"; pub static OPEN_COMMAND: &'static str = &"open"; pub static CLOSE_COMMAND: &'static str = &"close"; @@ -52,8 +54,10 @@ impl Command { /// TODO: for query and transact commands, they will be considered complete if a parsable EDN has been entered as an argument pub fn is_complete(&self) -> bool { match self { - &Command::Query(_) | - &Command::Transact(_) => false, + &Command::Query(ref args) | + &Command::Transact(ref args) => { + edn::parse::value(&args).is_ok() + }, &Command::Help(_) | &Command::Open(_) | &Command::Close => true diff --git a/tools/cli/src/mentat_cli/input.rs b/tools/cli/src/mentat_cli/input.rs index 0679b3b6..9e580d02 100644 --- a/tools/cli/src/mentat_cli/input.rs +++ b/tools/cli/src/mentat_cli/input.rs @@ -22,6 +22,12 @@ use command_parser::{ use errors as cli; +/// Starting prompt +const DEFAULT_PROMPT: &'static str = "mentat=> "; +/// Prompt when further input is being read +// TODO: Should this actually reflect the current open brace? +const MORE_PROMPT: &'static str = "mentat.> "; + /// Possible results from reading input from `InputReader` #[derive(Clone, Debug)] pub enum InputResult { @@ -66,8 +72,8 @@ impl InputReader { /// Reads a single command, item, or statement from `stdin`. /// Returns `More` if further input is required for a complete result. /// In this case, the input received so far is buffered internally. - pub fn read_input(&mut self, prompt: &str) -> Result { - let line = match self.read_line(prompt) { + pub fn read_input(&mut self, in_process_cmd: Option) -> Result { + let line = match self.read_line(if in_process_cmd.is_some() { MORE_PROMPT } else { DEFAULT_PROMPT }) { Some(s) => s, None => return Ok(Eof), }; @@ -80,7 +86,19 @@ impl InputReader { self.add_history(&line); - let cmd = try!(command(&self.buffer)); + let cmd = match in_process_cmd { + Some(Command::Query(args)) => { + Command::Query(args + " " + &line) + }, + Some(Command::Transact(args)) => { + Command::Transact(args + " " + &line) + }, + _ => { + try!(command(&self.buffer)) + } + }; + + println!("processing {:?}", cmd); match cmd { Command::Query(_) | diff --git a/tools/cli/src/mentat_cli/lib.rs b/tools/cli/src/mentat_cli/lib.rs index 76f7d602..8fd95231 100644 --- a/tools/cli/src/mentat_cli/lib.rs +++ b/tools/cli/src/mentat_cli/lib.rs @@ -21,6 +21,7 @@ extern crate linefeed; extern crate rusqlite; extern crate mentat; +extern crate edn; use getopts::Options; diff --git a/tools/cli/src/mentat_cli/repl.rs b/tools/cli/src/mentat_cli/repl.rs index 0ad0b99e..c9da67a3 100644 --- a/tools/cli/src/mentat_cli/repl.rs +++ b/tools/cli/src/mentat_cli/repl.rs @@ -27,12 +27,6 @@ use store::{ db_output_name }; -/// Starting prompt -const DEFAULT_PROMPT: &'static str = "mentat=> "; -/// Prompt when further input is being read -// TODO: Should this actually reflect the current open brace? -const MORE_PROMPT: &'static str = "mentat.> "; - lazy_static! { static ref COMMAND_HELP: HashMap<&'static str, &'static str> = { let mut map = HashMap::new(); @@ -62,7 +56,8 @@ impl Repl { let mut input = InputReader::new(); loop { - let res = input.read_input(if more.is_some() { MORE_PROMPT } else { DEFAULT_PROMPT }); + let res = input.read_input(more.clone()); + match res { Ok(MetaCommand(cmd)) => { debug!("read command: {:?}", cmd);