From 3f20319a840307753ae8c34ab4d8443e70761310 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 2 Aug 2017 14:32:45 +0200 Subject: [PATCH 1/2] Use rustyline for the REPL This makes the REPL more usable as you can now edit lines and recall previously executed statements. --- Cargo.toml | 3 +++ examples/repl.rs | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) 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..aca78f0 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,10 @@ fn main() { } Err(Error::IncompleteStatement(_)) => { // continue reading input and append it to `line` - write!(stdout, ">> ").unwrap(); - stdout.flush().unwrap(); + prompt = ">> "; } Err(e) => { - writeln!(stderr(), "error: {}", e).unwrap(); + eprintln!("error: {}", e); break; } } From 95698735a2fc3296a13534656be7734a432ce528 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 2 Aug 2017 14:36:10 +0200 Subject: [PATCH 2/2] Fix multiline inputs --- examples/repl.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/repl.rs b/examples/repl.rs index aca78f0..3cde4d5 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -35,6 +35,7 @@ fn main() { } Err(Error::IncompleteStatement(_)) => { // continue reading input and append it to `line` + line.push_str("\n"); // separate input lines prompt = ">> "; } Err(e) => {