diff --git a/Cargo.toml b/Cargo.toml index c423229..c204659 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,6 @@ builtin-lua = ["gcc"] [build-dependencies] gcc = { version = "0.3", optional = true } + +[dev-dependencies] +rustyline = "1.0.0" diff --git a/examples/repl.rs b/examples/repl.rs index 27ef0f9..3cde4d5 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -1,27 +1,28 @@ //! This example shows a simple read-evaluate-print-loop (REPL). extern crate rlua; +extern crate rustyline; use rlua::{Lua, MultiValue, Error}; -use std::io::prelude::*; -use std::io::{stdin, stdout, stderr, BufReader}; +use rustyline::Editor; fn main() { let lua = Lua::new(); - let mut stdout = stdout(); - let mut stdin = BufReader::new(stdin()); + let mut editor = Editor::<()>::new(); loop { - write!(stdout, "> ").unwrap(); - stdout.flush().unwrap(); - + let mut prompt = "> "; let mut line = String::new(); loop { - stdin.read_line(&mut line).unwrap(); + match editor.readline(prompt) { + Ok(input) => line.push_str(&input), + Err(_) => return, + } match lua.eval::(&line, None) { Ok(values) => { + editor.add_history_entry(&line); println!( "{}", values @@ -34,11 +35,11 @@ fn main() { } Err(Error::IncompleteStatement(_)) => { // continue reading input and append it to `line` - write!(stdout, ">> ").unwrap(); - stdout.flush().unwrap(); + line.push_str("\n"); // separate input lines + prompt = ">> "; } Err(e) => { - writeln!(stderr(), "error: {}", e).unwrap(); + eprintln!("error: {}", e); break; } }