Skip to content

Commit 93b738e

Browse files
committed
Implement history loading/saving for REPL.
1 parent 1e35912 commit 93b738e

File tree

3 files changed

+133
-12
lines changed

3 files changed

+133
-12
lines changed

Cargo.lock

Lines changed: 126 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jaq/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ jaq-std = { version = "2.1.0", path = "../jaq-std" }
2020
jaq-json = { version = "1.1.1", path = "../jaq-json" }
2121

2222
codesnake = { version = "0.2" }
23+
dirs = { version = "6.0" }
2324
env_logger = { version = "0.10.0", default-features = false }
2425
hifijson = "0.2.0"
2526
is-terminal = "0.4.13"
2627
log = { version = "0.4.17" }
2728
memmap2 = "0.9"
2829
mimalloc = { version = "0.1.29", default-features = false, optional = true }
29-
rustyline = { version = "13.0.0", default-features = false }
30+
rustyline = { version = "13.0.0", default-features = false, features = ["with-file-history"] }
3031
tempfile = "3.3.0"
3132
unicode-width = "0.1.13"
3233
yansi = "1.0.1"

jaq/src/repl.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ fn repl_with(depth: usize, f: impl Fn(String)) -> Result<(), ReadlineError> {
3838
.auto_add_history(true)
3939
.build();
4040
let mut rl = DefaultEditor::with_config(config)?;
41+
let history = dirs::cache_dir().map(|dir| dir.join("jaq-history"));
42+
let _ = history.iter().try_for_each(|h| rl.load_history(h));
4143
let prompt = format!("{}{} ", str::repeat(" ", depth), '>'.bold());
4244
loop {
4345
match rl.readline(&prompt) {
4446
Ok(line) => f(line),
4547
Err(ReadlineError::Interrupted) => (),
46-
Err(ReadlineError::Eof) => return Ok(()),
48+
Err(ReadlineError::Eof) => break,
4749
Err(err) => Err(err)?,
4850
}
4951
}
52+
let _ = history.iter().try_for_each(|h| rl.append_history(h));
53+
Ok(())
5054
}

0 commit comments

Comments
 (0)