Skip to main content

luaur_repl_cli/functions/
load_history.rs

1use alloc::string::String;
2
3use luaur_cli_lib::functions::join_paths_file_utils::join_paths_basic_string_ch_ch_ch;
4
5// Faithful rustyline analog of Repl.cpp's `loadHistory`.
6//
7// C++ resolved the history file path from $HOME (falling back to $USERPROFILE)
8// joined with `name`, then handed it to isocline via `ic_set_history(path, -1)`,
9// where -1 selected isocline's default entry count (= 200). isocline then loaded
10// the file and automatically saved history on process exit.
11//
12// rustyline manages history through the `Editor` rather than a global call, so
13// this returns the resolved path; `run_repl_impl` loads it, caps it at the
14// default 200 entries, and saves it when the loop ends.
15pub fn load_history(name: &str) -> Option<String> {
16    let mut path = String::new();
17
18    if let Ok(home) = std::env::var("HOME") {
19        join_paths_basic_string_ch_ch_ch(&mut path, &home, name);
20    } else if let Ok(user_profile) = std::env::var("USERPROFILE") {
21        join_paths_basic_string_ch_ch_ch(&mut path, &user_profile, name);
22    }
23
24    if path.is_empty() {
25        None
26    } else {
27        Some(path)
28    }
29}
30
31// isocline's default history entry count, selected by passing -1 to ic_set_history.
32pub const DEFAULT_HISTORY_ENTRIES: usize = 200;