summaryrefslogtreecommitdiff
path: root/cli/tools/repl/editor.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-11-25 02:56:47 +0100
committerGitHub <noreply@github.com>2022-11-25 02:56:47 +0100
commit433f38084bba9f18bdb5de22422cbbd5b0c01ff7 (patch)
tree9b8312967987bd9f31d29e70b30764223b1bd3ce /cli/tools/repl/editor.rs
parente6a9588b4375f7ff3f445d13e4cd4b3c334d451c (diff)
fix(repl): more reliable history handling (#16797)
This commit changes history handling of the REPL. There were some situations were history wasn't properly saved and flushed to a file, making history very spotty. This commit changes it to save every line into the history file and flush it to disk before being evaluated. Thanks to this all lines, including "close()" will be stored in the history file. If for any reason we're not able to save history file, a single warning will be printed to the REPL and it will continue to work, even if subsequent tries will fail to save to disk.
Diffstat (limited to 'cli/tools/repl/editor.rs')
-rw-r--r--cli/tools/repl/editor.rs37
1 files changed, 27 insertions, 10 deletions
diff --git a/cli/tools/repl/editor.rs b/cli/tools/repl/editor.rs
index 69fec9df0..73196d3f3 100644
--- a/cli/tools/repl/editor.rs
+++ b/cli/tools/repl/editor.rs
@@ -5,6 +5,7 @@ use crate::colors;
use deno_ast::swc::parser::error::SyntaxError;
use deno_ast::swc::parser::token::Token;
use deno_ast::swc::parser::token::Word;
+use deno_core::anyhow::Context as _;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
@@ -27,6 +28,8 @@ use rustyline::{ConditionalEventHandler, Event, EventContext, RepeatCount};
use rustyline_derive::{Helper, Hinter};
use std::borrow::Cow;
use std::path::PathBuf;
+use std::sync::atomic::AtomicBool;
+use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;
use super::channel::RustylineSyncMessageSender;
@@ -368,10 +371,14 @@ impl Highlighter for EditorHelper {
pub struct ReplEditor {
inner: Arc<Mutex<Editor<EditorHelper>>>,
history_file_path: PathBuf,
+ errored_on_history_save: Arc<AtomicBool>,
}
impl ReplEditor {
- pub fn new(helper: EditorHelper, history_file_path: PathBuf) -> Self {
+ pub fn new(
+ helper: EditorHelper,
+ history_file_path: PathBuf,
+ ) -> Result<Self, AnyError> {
let editor_config = Config::builder()
.completion_type(CompletionType::List)
.build();
@@ -389,25 +396,35 @@ impl ReplEditor {
EventHandler::Conditional(Box::new(TabEventHandler)),
);
- ReplEditor {
+ let history_file_dir = history_file_path.parent().unwrap();
+ std::fs::create_dir_all(history_file_dir).with_context(|| {
+ format!(
+ "Unable to create directory for the history file: {}",
+ history_file_dir.display()
+ )
+ })?;
+
+ Ok(ReplEditor {
inner: Arc::new(Mutex::new(editor)),
history_file_path,
- }
+ errored_on_history_save: Arc::new(AtomicBool::new(false)),
+ })
}
pub fn readline(&self) -> Result<String, ReadlineError> {
self.inner.lock().readline("> ")
}
- pub fn add_history_entry(&self, entry: String) {
+ pub fn update_history(&self, entry: String) {
self.inner.lock().add_history_entry(entry);
- }
-
- pub fn save_history(&self) -> Result<(), AnyError> {
- std::fs::create_dir_all(self.history_file_path.parent().unwrap())?;
+ if let Err(e) = self.inner.lock().append_history(&self.history_file_path) {
+ if self.errored_on_history_save.load(Relaxed) {
+ return;
+ }
- self.inner.lock().save_history(&self.history_file_path)?;
- Ok(())
+ self.errored_on_history_save.store(true, Relaxed);
+ eprintln!("Unable to save history file: {}", e);
+ }
}
}