extern crate datafusion_rustyline;
use self::datafusion_rustyline::error::ReadlineError;
use self::datafusion_rustyline::Editor;
const DEFAULT_PROMPT: &'static str = "datafusion> ";
const CONTINUE_PROMPT: &'static str = "> ";
#[cfg(target_family = "unix")]
pub enum LineResult {
Break,
Input(String),
}
#[cfg(target_family = "unix")]
pub struct LineReader<'a> {
reader: Editor<()>,
prompt: &'a str,
}
#[cfg(target_family = "unix")]
impl<'a> LineReader<'a> {
pub fn new() -> Self {
LineReader {
reader: Editor::<()>::new(),
prompt: DEFAULT_PROMPT,
}
}
pub fn set_prompt(&mut self, prompt: &'a str) {
self.prompt = prompt;
}
pub fn read_lines(&mut self) -> Option<LineResult> {
let mut result = String::new();
loop {
let line = self.reader.readline(self.prompt);
match line {
Ok(i) => {
let j = i.as_str().trim_end();
result.push_str(j);
match j {
"quit" | "exit" => {
return Some(LineResult::Break);
}
_ => {
if j.ends_with(';') {
self.set_prompt(DEFAULT_PROMPT);
break;
} else {
self.set_prompt(CONTINUE_PROMPT);
result.push_str(" ");
continue;
}
}
}
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
break;
}
Err(ReadlineError::Eof) => {
println!("CTRL-D");
return Some(LineResult::Break);
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
};
}
if !result.trim().is_empty() {
}
Some(LineResult::Input(result[..result.len() - 1].to_string()))
}
}