From 09d4b389aaba8a0c9743cfe95009d4d794d8fb48 Mon Sep 17 00:00:00 2001 From: Rene Kjellerup Date: Wed, 18 Sep 2024 11:16:24 -0700 Subject: [PATCH] Adding a persistent command history --- crates/shell/src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/shell/src/main.rs b/crates/shell/src/main.rs index cb30a08..b9c8668 100644 --- a/crates/shell/src/main.rs +++ b/crates/shell/src/main.rs @@ -1,3 +1,4 @@ +use std::path::Path; use std::path::PathBuf; use anyhow::Context; @@ -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 { @@ -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(()) }