Skip to content

Commit

Permalink
Add immediate saving of every line of input to history file
Browse files Browse the repository at this point in the history
By writing history after each input line, we avoid losing history if numbat fails to exit gracefully.
  • Loading branch information
rben01 committed Mar 4, 2025
1 parent 5753fde commit 9e4be1b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use itertools::Itertools;
use numbat::command::{CommandControlFlow, CommandRunner};
use numbat::compact_str::CompactString;
use numbat::diagnostic::ErrorDiagnostic;
use numbat::markup as m;
use numbat::module_importer::{BuiltinModuleImporter, ChainedImporter, FileSystemImporter};
use numbat::pretty_print::PrettyPrint;
use numbat::resolver::CodeSource;
use numbat::session_history::{ParseEvaluationResult, SessionHistory};
use numbat::{markup as m, RuntimeError};
use numbat::{Context, NumbatError};
use numbat::{InterpreterSettings, NameResolutionError};

Expand All @@ -30,7 +30,7 @@ use rustyline::{
use rustyline::{EventHandler, Highlighter, KeyCode, KeyEvent, Modifiers};

use std::io::IsTerminal;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::{fs, thread};

Expand Down Expand Up @@ -343,7 +343,7 @@ impl Cli {
}
}

let result = self.repl_loop(&mut rl, interactive);
let result = self.repl_loop(&mut rl, interactive, &history_path);

if interactive {
rl.save_history(&history_path).context(format!(
Expand All @@ -359,6 +359,7 @@ impl Cli {
&mut self,
rl: &mut Editor<NumbatHelper, DefaultHistory>,
interactive: bool,
history_path: &Path,
) -> Result<()> {
let mut cmd_runner = CommandRunner::<Editor<NumbatHelper, DefaultHistory>>::new()
.print_with(|m| println!("{}", ansi_format(m, true)))
Expand All @@ -379,6 +380,9 @@ impl Cli {
}

rl.add_history_entry(&line)?;
if interactive && rl.append_history(history_path).is_err() {
self.print_diagnostic(RuntimeError::HistoryWrite(history_path.to_owned()));
}

let mut ctx = self.context.lock().unwrap();
match cmd_runner.try_run_command(&line, &mut ctx, rl) {
Expand Down
5 changes: 5 additions & 0 deletions numbat/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ pub enum RuntimeError {

#[error("Could not write to file: {0:?}")]
FileWrite(std::path::PathBuf),

#[error(
"Could not write to history file {0:?}. History will not be saved until this is fixed."
)]
HistoryWrite(std::path::PathBuf),
}

#[derive(Debug, PartialEq)]
Expand Down

0 comments on commit 9e4be1b

Please sign in to comment.