Skip to content

Commit

Permalink
Adding a persistent command history
Browse files Browse the repository at this point in the history
  • Loading branch information
Katana-Steel committed Sep 18, 2024
1 parent 144a8de commit 09d4b38
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/shell/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::Path;
use std::path::PathBuf;

use anyhow::Context;
Expand Down Expand Up @@ -42,6 +43,14 @@ async fn interactive() -> anyhow::Result<()> {
let mut state = init_state();

let home = dirs::home_dir().context("Couldn't get home directory")?;
let history_file: PathBuf = [home.as_path(), Path::new(".shell_history")]
.iter()
.collect();
if Path::new(history_file.as_path()).exists() {
let _ = rl
.load_history(history_file.as_path())
.context("Failed to read the command history");
}

let mut _prev_exit_code = 0;
loop {
Expand Down Expand Up @@ -117,6 +126,9 @@ async fn interactive() -> anyhow::Result<()> {
}
}
}
let _ = rl
.save_history(history_file.as_path())
.context("Failed to write the command history");

Ok(())
}
Expand Down

0 comments on commit 09d4b38

Please sign in to comment.