diff options
author | sigmaSd <bedisnbiba@gmail.com> | 2022-12-17 00:39:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-16 18:39:52 -0500 |
commit | c39550fe522306d049716a0c6aca5a850c841f7e (patch) | |
tree | 5fd880572d90cb245afd2333129aec875be5591e /cli/tools/repl/editor.rs | |
parent | efcb93f8b9610bff896f21ecb5add7d17de40156 (diff) |
fix(repl): doing two history searches exiting with ctrl+c should not exit repl (#17079)
fix https://github.com/denoland/deno/issues/16147
Diffstat (limited to 'cli/tools/repl/editor.rs')
-rw-r--r-- | cli/tools/repl/editor.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/cli/tools/repl/editor.rs b/cli/tools/repl/editor.rs index 2ff9ee0b4..86d4ce4f9 100644 --- a/cli/tools/repl/editor.rs +++ b/cli/tools/repl/editor.rs @@ -372,6 +372,7 @@ pub struct ReplEditor { inner: Arc<Mutex<Editor<EditorHelper>>>, history_file_path: PathBuf, errored_on_history_save: Arc<AtomicBool>, + should_exit_on_interrupt: Arc<AtomicBool>, } impl ReplEditor { @@ -395,6 +396,13 @@ impl ReplEditor { KeyEvent(KeyCode::Tab, Modifiers::NONE), EventHandler::Conditional(Box::new(TabEventHandler)), ); + let should_exit_on_interrupt = Arc::new(AtomicBool::new(false)); + editor.bind_sequence( + KeyEvent(KeyCode::Char('r'), Modifiers::CTRL), + EventHandler::Conditional(Box::new(ReverseSearchHistoryEventHandler { + should_exit_on_interrupt: should_exit_on_interrupt.clone(), + })), + ); let history_file_dir = history_file_path.parent().unwrap(); std::fs::create_dir_all(history_file_dir).with_context(|| { @@ -408,6 +416,7 @@ impl ReplEditor { inner: Arc::new(Mutex::new(editor)), history_file_path, errored_on_history_save: Arc::new(AtomicBool::new(false)), + should_exit_on_interrupt, }) } @@ -426,6 +435,31 @@ impl ReplEditor { eprintln!("Unable to save history file: {}", e); } } + + pub fn should_exit_on_interrupt(&self) -> bool { + self.should_exit_on_interrupt.load(Relaxed) + } + + pub fn set_should_exit_on_interrupt(&self, yes: bool) { + self.should_exit_on_interrupt.store(yes, Relaxed); + } +} + +/// Command to reverse search history , same as rustyline default C-R but that resets repl should_exit flag to false +struct ReverseSearchHistoryEventHandler { + should_exit_on_interrupt: Arc<AtomicBool>, +} +impl ConditionalEventHandler for ReverseSearchHistoryEventHandler { + fn handle( + &self, + _: &Event, + _: RepeatCount, + _: bool, + _: &EventContext, + ) -> Option<Cmd> { + self.should_exit_on_interrupt.store(false, Relaxed); + Some(Cmd::ReverseSearchHistory) + } } /// A custom tab key event handler |