Skip to content

Commit

Permalink
Load and save history on repl start/exit (#199)
Browse files Browse the repository at this point in the history
* Load and save repl history on start/exit

* Addressing linter issues

* Create history file if it does not exist
  • Loading branch information
glenn-m authored May 5, 2024
1 parent 3005daf commit cae11f7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ docs/theme
.vscode/
/result
.direnv/
.steel/
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/steel-repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ rustyline-derive = "0.7.0"
colored = "2.0.0"
steel-core = { workspace = true }
steel-parser = { path = "../steel-parser", version = "0.6.0"}
dirs = "5.0.1"
36 changes: 36 additions & 0 deletions crates/steel-repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ use std::io::Read;

use std::time::Instant;

use std::env;

use std::fs::File;

use dirs;

use crate::highlight::RustylineHelper;

fn display_help() {
Expand Down Expand Up @@ -49,6 +55,25 @@ fn display_startup() {
);
}

fn get_repl_history_path() -> String {
let steel_home = env::var("STEEL_HOME");
let path = match steel_home {
Ok(val) => {
let mut parsed_path = PathBuf::from(&val);
parsed_path = parsed_path.canonicalize().unwrap_or(parsed_path);
parsed_path.push("history");
parsed_path.to_string_lossy().into_owned()
}
Err(_) => {
let mut default_path = dirs::home_dir().unwrap_or_default();
default_path.push(".steel/history");
default_path.to_string_lossy().into_owned()
}
};

path
}

fn finish_load_or_interrupt(vm: &mut Engine, exprs: String, path: PathBuf) {
// let file_name = path.to_str().unwrap().to_string();

Expand Down Expand Up @@ -113,6 +138,14 @@ pub fn repl_base(mut vm: Engine) -> std::io::Result<()> {
let mut rl = Editor::<RustylineHelper>::new().expect("Unable to instantiate the repl!");
rl.set_check_cursor_position(true);

// Load repl history
let history_path = get_repl_history_path();
if let Err(_) = rl.load_history(&history_path) {
if let Err(_) = File::create(&history_path) {
eprintln!("Unable to create repl history file {:?}", history_path)
}
};

let current_dir = std::env::current_dir()?;

let mut print_time = false;
Expand Down Expand Up @@ -218,6 +251,9 @@ pub fn repl_base(mut vm: Engine) -> std::io::Result<()> {
}
}
}
if let Err(err) = rl.save_history(&history_path) {
eprintln!("Failed to save REPL history: {}", err);
}

Ok(())
}

0 comments on commit cae11f7

Please sign in to comment.