From abaf7c7590652a67a9eaa5aad8981495c73c913f 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 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/shell/src/main.rs b/crates/shell/src/main.rs index cb30a08..b6c3be3 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,13 @@ 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() { + rl.load_history(history_file.as_path()) + .context("Failed to read the command history")?; + } let mut _prev_exit_code = 0; loop { @@ -117,6 +125,8 @@ async fn interactive() -> anyhow::Result<()> { } } } + rl.save_history(history_file.as_path()) + .context("Failed to write the command history")?; Ok(()) }