Merge pull request #35 from jonas-schievink/repl
Use rustyline for the REPL
This commit is contained in:
commit
b458f91aac
|
@ -20,3 +20,6 @@ builtin-lua = ["gcc"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
gcc = { version = "0.3", optional = true }
|
gcc = { version = "0.3", optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
rustyline = "1.0.0"
|
||||||
|
|
|
@ -1,27 +1,28 @@
|
||||||
//! This example shows a simple read-evaluate-print-loop (REPL).
|
//! This example shows a simple read-evaluate-print-loop (REPL).
|
||||||
|
|
||||||
extern crate rlua;
|
extern crate rlua;
|
||||||
|
extern crate rustyline;
|
||||||
|
|
||||||
use rlua::{Lua, MultiValue, Error};
|
use rlua::{Lua, MultiValue, Error};
|
||||||
use std::io::prelude::*;
|
use rustyline::Editor;
|
||||||
use std::io::{stdin, stdout, stderr, BufReader};
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let mut stdout = stdout();
|
let mut editor = Editor::<()>::new();
|
||||||
let mut stdin = BufReader::new(stdin());
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
write!(stdout, "> ").unwrap();
|
let mut prompt = "> ";
|
||||||
stdout.flush().unwrap();
|
|
||||||
|
|
||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
|
|
||||||
loop {
|
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) {
|
match lua.eval::<MultiValue>(&line, None) {
|
||||||
Ok(values) => {
|
Ok(values) => {
|
||||||
|
editor.add_history_entry(&line);
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
values
|
values
|
||||||
|
@ -34,11 +35,11 @@ fn main() {
|
||||||
}
|
}
|
||||||
Err(Error::IncompleteStatement(_)) => {
|
Err(Error::IncompleteStatement(_)) => {
|
||||||
// continue reading input and append it to `line`
|
// continue reading input and append it to `line`
|
||||||
write!(stdout, ">> ").unwrap();
|
line.push_str("\n"); // separate input lines
|
||||||
stdout.flush().unwrap();
|
prompt = ">> ";
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
writeln!(stderr(), "error: {}", e).unwrap();
|
eprintln!("error: {}", e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue