1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(repl): doing two history searches exiting with ctrl+c should not exit repl (#17079)

fix https://github.com/denoland/deno/issues/16147
This commit is contained in:
sigmaSd 2022-12-17 00:39:52 +01:00 committed by GitHub
parent efcb93f8b9
commit c39550fe52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 4 deletions

View file

@ -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

View file

@ -91,7 +91,6 @@ pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> {
let worker = worker.into_main_worker();
let mut repl_session = ReplSession::initialize(ps.clone(), worker).await?;
let mut rustyline_channel = rustyline_channel();
let mut should_exit_on_interrupt = false;
let helper = EditorHelper {
context_id: repl_session.context_id,
@ -154,7 +153,7 @@ pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> {
.await;
match line {
Ok(line) => {
should_exit_on_interrupt = false;
editor.set_should_exit_on_interrupt(false);
editor.update_history(line.clone());
let output = repl_session.evaluate_line_and_get_output(&line).await;
@ -167,10 +166,10 @@ pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> {
println!("{}", output);
}
Err(ReadlineError::Interrupted) => {
if should_exit_on_interrupt {
if editor.should_exit_on_interrupt() {
break;
}
should_exit_on_interrupt = true;
editor.set_should_exit_on_interrupt(true);
println!("press ctrl+c again to exit");
continue;
}