Switch to rustyline library

This commit is contained in:
greg 2018-03-01 02:43:11 -08:00
parent df88e33579
commit ad450469a5
2 changed files with 13 additions and 19 deletions

View File

@ -8,7 +8,6 @@ llvm-sys = "*"
take_mut = "0.1.3" take_mut = "0.1.3"
itertools = "0.5.8" itertools = "0.5.8"
getopts = "*" getopts = "*"
linefeed = "0.3"
lazy_static = "0.2.8" lazy_static = "0.2.8"
maplit = "*" maplit = "*"
colored = "1.5" colored = "1.5"
@ -20,6 +19,7 @@ rocket_codegen = "0.3.5"
rocket_contrib = "0.3.5" rocket_contrib = "0.3.5"
phf = "0.7.12" phf = "0.7.12"
includedir = "0.2.0" includedir = "0.2.0"
rustyline = "1.0.0"
[build-dependencies] [build-dependencies]
includedir_codegen = "0.2.0" includedir_codegen = "0.2.0"

View File

@ -3,7 +3,7 @@
#![feature(plugin)] #![feature(plugin)]
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate getopts; extern crate getopts;
extern crate linefeed; extern crate rustyline;
extern crate itertools; extern crate itertools;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
@ -23,6 +23,9 @@ use std::io::{Read, Write};
use std::process::exit; use std::process::exit;
use std::default::Default; use std::default::Default;
use rustyline::error::ReadlineError;
use rustyline::Editor;
mod language; mod language;
mod webapp; mod webapp;
pub mod llvm_wrap; pub mod llvm_wrap;
@ -112,26 +115,26 @@ fn run_noninteractive(filename: &str, languages: Vec<Box<ProgrammingLanguageInte
} }
} }
type LineReader = linefeed::Reader<linefeed::terminal::DefaultTerminal>;
struct Repl { struct Repl {
options: EvalOptions, options: EvalOptions,
languages: Vec<Box<ProgrammingLanguageInterface>>, languages: Vec<Box<ProgrammingLanguageInterface>>,
current_language_index: usize, current_language_index: usize,
interpreter_directive_sigil: char, interpreter_directive_sigil: char,
reader: LineReader, console: rustyline::Editor<()>,
} }
impl Repl { impl Repl {
fn new(languages: Vec<Box<ProgrammingLanguageInterface>>, initial_index: usize) -> Repl { fn new(languages: Vec<Box<ProgrammingLanguageInterface>>, initial_index: usize) -> Repl {
let reader: linefeed::Reader<_> = linefeed::Reader::new("Metainterpreter").unwrap();
let i = if initial_index < languages.len() { initial_index } else { 0 }; let i = if initial_index < languages.len() { initial_index } else { 0 };
let console = Editor::<()>::new();
Repl { Repl {
options: Repl::get_options(), options: Repl::get_options(),
languages: languages, languages: languages,
current_language_index: i, current_language_index: i,
interpreter_directive_sigil: '.', interpreter_directive_sigil: '.',
reader: reader, console
} }
} }
@ -162,23 +165,19 @@ impl Repl {
} }
fn run(&mut self) { fn run(&mut self) {
use linefeed::ReadResult::*;
println!("MetaInterpreter v 0.05"); println!("MetaInterpreter v 0.05");
loop { loop {
let language_name = self.languages[self.current_language_index].get_language_name(); let language_name = self.languages[self.current_language_index].get_language_name();
let prompt_str = format!("{} >> ", language_name); let prompt_str = format!("{} >> ", language_name);
self.reader.set_prompt(&prompt_str);
match self.reader.read_line() { match self.console.readline(&prompt_str) {
Err(ReadlineError::Eof) | Err(ReadlineError::Interrupted) => break,
Err(e) => { Err(e) => {
println!("Terminal read error: {}", e); println!("Terminal read error: {}", e);
}, },
Ok(Eof) => { Ok(ref input) => {
break; self.console.add_history_entry(input);
}
Ok(Input(ref input)) => {
self.reader.add_history(input.clone());
if self.handle_interpreter_directive(input) { if self.handle_interpreter_directive(input) {
continue; continue;
} }
@ -222,11 +221,6 @@ impl Repl {
self.save_options(); self.save_options();
exit(0) exit(0)
}, },
"history" => {
for item in self.reader.history() {
println!("{}", item);
}
},
"help" => { "help" => {
println!("Commands:"); println!("Commands:");
println!("exit | quit"); println!("exit | quit");