summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2017-08-02 14:32:45 +0200
committerJonas Schievink <jonasschievink@gmail.com>2017-08-02 14:32:45 +0200
commit3f20319a840307753ae8c34ab4d8443e70761310 (patch)
tree921af00bcbbbc8d3e2d6f8030fc32f9f0cfa26a6 /examples
parentf6cefca9169d8dcbb0d7e22c209bb6b5d7cc5794 (diff)
downloadmlua-3f20319a840307753ae8c34ab4d8443e70761310.zip
Use rustyline for the REPL
This makes the REPL more usable as you can now edit lines and recall previously executed statements.
Diffstat (limited to 'examples')
-rw-r--r--examples/repl.rs22
1 files changed, 11 insertions, 11 deletions
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::<MultiValue>(&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;
}
}