Skip to content

Commit 7be7327

Browse files
authored
Adding a persistent command history (#150)
1 parent 144a8de commit 7be7327

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

crates/shell/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::path::Path;
12
use std::path::PathBuf;
23

34
use anyhow::Context;
@@ -42,6 +43,13 @@ async fn interactive() -> anyhow::Result<()> {
4243
let mut state = init_state();
4344

4445
let home = dirs::home_dir().context("Couldn't get home directory")?;
46+
let history_file: PathBuf = [home.as_path(), Path::new(".shell_history")]
47+
.iter()
48+
.collect();
49+
if Path::new(history_file.as_path()).exists() {
50+
rl.load_history(history_file.as_path())
51+
.context("Failed to read the command history")?;
52+
}
4553

4654
let mut _prev_exit_code = 0;
4755
loop {
@@ -117,6 +125,8 @@ async fn interactive() -> anyhow::Result<()> {
117125
}
118126
}
119127
}
128+
rl.save_history(history_file.as_path())
129+
.context("Failed to write the command history")?;
120130

121131
Ok(())
122132
}

0 commit comments

Comments
 (0)