2017-11-21 16:56:16 +00:00
|
|
|
// Copyright 2017 Mozilla
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
|
|
|
use std::io::stdin;
|
|
|
|
|
2018-01-18 04:33:22 +00:00
|
|
|
use linefeed::{
|
|
|
|
DefaultTerminal,
|
|
|
|
Reader,
|
|
|
|
ReadResult,
|
|
|
|
};
|
2017-11-21 16:56:16 +00:00
|
|
|
|
|
|
|
use self::InputResult::*;
|
|
|
|
|
|
|
|
use command_parser::{
|
|
|
|
Command,
|
2017-12-05 22:10:10 +00:00
|
|
|
command,
|
2017-11-21 16:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
|
|
|
/// mentat command as input; (name, rest of line)
|
|
|
|
MetaCommand(Command),
|
|
|
|
/// An empty line
|
|
|
|
Empty,
|
|
|
|
/// Needs more input
|
|
|
|
More,
|
|
|
|
/// End of file reached
|
|
|
|
Eof,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads input from `stdin`
|
|
|
|
pub struct InputReader {
|
|
|
|
buffer: String,
|
|
|
|
reader: Option<Reader<DefaultTerminal>>,
|
|
|
|
in_process_cmd: Option<Command>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InputReader {
|
|
|
|
/// Constructs a new `InputReader` reading from `stdin`.
|
|
|
|
pub fn new() -> InputReader {
|
|
|
|
let r = match Reader::new("mentat") {
|
|
|
|
Ok(mut r) => {
|
2018-01-18 04:33:22 +00:00
|
|
|
r.set_word_break_chars(" \t\n!\"#$%&'(){}*+,-./:;<=>?@[\\]^`");
|
2017-11-21 16:56:16 +00:00
|
|
|
Some(r)
|
2017-12-05 22:10:10 +00:00
|
|
|
},
|
|
|
|
Err(_) => None,
|
2017-11-21 16:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
InputReader{
|
|
|
|
buffer: String::new(),
|
|
|
|
reader: r,
|
|
|
|
in_process_cmd: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the `InputReader` is reading from a TTY.
|
|
|
|
pub fn is_tty(&self) -> bool {
|
|
|
|
self.reader.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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) -> Result<InputResult, cli::Error> {
|
|
|
|
let prompt = if self.in_process_cmd.is_some() { MORE_PROMPT } else { DEFAULT_PROMPT };
|
|
|
|
let line = match self.read_line(prompt) {
|
|
|
|
Some(s) => s,
|
|
|
|
None => return Ok(Eof),
|
|
|
|
};
|
|
|
|
|
2018-01-18 04:33:22 +00:00
|
|
|
if !self.buffer.is_empty() {
|
|
|
|
self.buffer.push('\n');
|
|
|
|
}
|
|
|
|
|
2017-11-21 16:56:16 +00:00
|
|
|
self.buffer.push_str(&line);
|
|
|
|
|
|
|
|
if self.buffer.is_empty() {
|
|
|
|
return Ok(Empty);
|
|
|
|
}
|
|
|
|
|
2017-12-05 22:10:10 +00:00
|
|
|
// if we have a command in process (i.e. an incomplete query or transaction),
|
2017-11-21 16:56:16 +00:00
|
|
|
// 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.
|
|
|
|
// Therefore, we add the newly read in line to the existing command args.
|
|
|
|
// If there is no in process command, we parse the read in line as a new command.
|
|
|
|
let cmd = match &self.in_process_cmd {
|
|
|
|
&Some(Command::Query(ref args)) => {
|
2017-12-05 22:10:10 +00:00
|
|
|
Ok(Command::Query(args.clone() + " " + &line))
|
2017-11-21 16:56:16 +00:00
|
|
|
},
|
|
|
|
&Some(Command::Transact(ref args)) => {
|
2017-12-05 22:10:10 +00:00
|
|
|
Ok(Command::Transact(args.clone() + " " + &line))
|
2017-11-21 16:56:16 +00:00
|
|
|
},
|
|
|
|
_ => {
|
2017-12-05 22:10:10 +00:00
|
|
|
command(&self.buffer)
|
|
|
|
},
|
2017-11-21 16:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match cmd {
|
2017-12-05 22:10:10 +00:00
|
|
|
Ok(cmd) => {
|
|
|
|
match cmd {
|
|
|
|
Command::Query(_) |
|
2018-01-20 00:44:39 +00:00
|
|
|
Command::Transact(_) |
|
|
|
|
Command::QueryExplain(_) if !cmd.is_complete() => {
|
2017-12-05 22:10:10 +00:00
|
|
|
// A query or transact is complete if it contains a valid EDN.
|
|
|
|
// if the command is not complete, ask for more from the REPL and remember
|
|
|
|
// which type of command we've found here.
|
|
|
|
self.in_process_cmd = Some(cmd);
|
|
|
|
Ok(More)
|
|
|
|
},
|
|
|
|
_ => {
|
2018-01-18 04:33:22 +00:00
|
|
|
let entry = self.buffer.clone();
|
2017-12-05 22:10:10 +00:00
|
|
|
self.buffer.clear();
|
2018-01-18 04:33:22 +00:00
|
|
|
self.add_history(entry);
|
2017-12-05 22:10:10 +00:00
|
|
|
self.in_process_cmd = None;
|
|
|
|
Ok(InputResult::MetaCommand(cmd))
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 16:56:16 +00:00
|
|
|
},
|
2017-12-05 22:10:10 +00:00
|
|
|
Err(e) => {
|
2018-01-18 04:33:22 +00:00
|
|
|
let entry = self.buffer.clone();
|
2017-11-21 16:56:16 +00:00
|
|
|
self.buffer.clear();
|
2018-01-18 04:33:22 +00:00
|
|
|
self.add_history(entry);
|
2017-11-21 16:56:16 +00:00
|
|
|
self.in_process_cmd = None;
|
2017-12-05 22:10:10 +00:00
|
|
|
Err(e)
|
|
|
|
},
|
2017-11-21 16:56:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_line(&mut self, prompt: &str) -> Option<String> {
|
|
|
|
match self.reader {
|
|
|
|
Some(ref mut r) => {
|
|
|
|
r.set_prompt(prompt);
|
2018-01-18 04:33:22 +00:00
|
|
|
r.read_line().ok().and_then(|line|
|
|
|
|
match line {
|
|
|
|
ReadResult::Input(s) => Some(s),
|
|
|
|
_ => None
|
|
|
|
})
|
|
|
|
|
2017-11-21 16:56:16 +00:00
|
|
|
},
|
|
|
|
None => self.read_stdin()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_stdin(&self) -> Option<String> {
|
|
|
|
let mut s = String::new();
|
|
|
|
|
|
|
|
match stdin().read_line(&mut s) {
|
|
|
|
Ok(0) | Err(_) => None,
|
|
|
|
Ok(_) => Some(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 04:33:22 +00:00
|
|
|
fn add_history(&mut self, line: String) {
|
2017-11-21 16:56:16 +00:00
|
|
|
if let Some(ref mut r) = self.reader {
|
2018-01-18 04:33:22 +00:00
|
|
|
r.add_history(line);
|
2017-11-21 16:56:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|