Skip to content

Commit b458f91

Browse files
authored
Merge pull request #35 from jonas-schievink/repl
Use rustyline for the REPL
2 parents f9f3d05 + 9569873 commit b458f91

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ builtin-lua = ["gcc"]
2020

2121
[build-dependencies]
2222
gcc = { version = "0.3", optional = true }
23+
24+
[dev-dependencies]
25+
rustyline = "1.0.0"

examples/repl.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
//! This example shows a simple read-evaluate-print-loop (REPL).
22
33
extern crate rlua;
4+
extern crate rustyline;
45

56
use rlua::{Lua, MultiValue, Error};
6-
use std::io::prelude::*;
7-
use std::io::{stdin, stdout, stderr, BufReader};
7+
use rustyline::Editor;
88

99
fn main() {
1010
let lua = Lua::new();
11-
let mut stdout = stdout();
12-
let mut stdin = BufReader::new(stdin());
11+
let mut editor = Editor::<()>::new();
1312

1413
loop {
15-
write!(stdout, "> ").unwrap();
16-
stdout.flush().unwrap();
17-
14+
let mut prompt = "> ";
1815
let mut line = String::new();
1916

2017
loop {
21-
stdin.read_line(&mut line).unwrap();
18+
match editor.readline(prompt) {
19+
Ok(input) => line.push_str(&input),
20+
Err(_) => return,
21+
}
2222

2323
match lua.eval::<MultiValue>(&line, None) {
2424
Ok(values) => {
25+
editor.add_history_entry(&line);
2526
println!(
2627
"{}",
2728
values
@@ -34,11 +35,11 @@ fn main() {
3435
}
3536
Err(Error::IncompleteStatement(_)) => {
3637
// continue reading input and append it to `line`
37-
write!(stdout, ">> ").unwrap();
38-
stdout.flush().unwrap();
38+
line.push_str("\n"); // separate input lines
39+
prompt = ">> ";
3940
}
4041
Err(e) => {
41-
writeln!(stderr(), "error: {}", e).unwrap();
42+
eprintln!("error: {}", e);
4243
break;
4344
}
4445
}

0 commit comments

Comments
 (0)