From 12cf0dd868cc38eb6198d716110d6e186025d411 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Tue, 12 Aug 2025 15:43:07 -0700 Subject: [PATCH] Better implementation of interrupt on Esc (#2111) Use existing abstractions --- codex-rs/tui/src/app.rs | 14 -------------- .../tui/src/bottom_pane/status_indicator_view.rs | 9 +++++++++ codex-rs/tui/src/chatwidget.rs | 8 -------- codex-rs/tui/src/status_indicator_widget.rs | 13 +++++++------ 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index ebce2699..cd2b308d 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -235,20 +235,6 @@ impl App<'_> { self.app_event_tx.send(AppEvent::ExitRequest); } }, - KeyEvent { - code: KeyCode::Esc, - kind: KeyEventKind::Press, - .. - } => match &mut self.app_state { - AppState::Chat { widget } => { - if !widget.on_esc() { - self.dispatch_key_event(key_event); - } - } - AppState::Onboarding { .. } => { - self.dispatch_key_event(key_event); - } - }, KeyEvent { code: KeyCode::Char('z'), modifiers: crossterm::event::KeyModifiers::CONTROL, diff --git a/codex-rs/tui/src/bottom_pane/status_indicator_view.rs b/codex-rs/tui/src/bottom_pane/status_indicator_view.rs index a944271e..cad4f0f2 100644 --- a/codex-rs/tui/src/bottom_pane/status_indicator_view.rs +++ b/codex-rs/tui/src/bottom_pane/status_indicator_view.rs @@ -1,7 +1,10 @@ +use crossterm::event::KeyCode; +use crossterm::event::KeyEvent; use ratatui::buffer::Buffer; use ratatui::widgets::WidgetRef; use crate::app_event_sender::AppEventSender; +use crate::bottom_pane::BottomPane; use crate::status_indicator_widget::StatusIndicatorWidget; use super::BottomPaneView; @@ -40,4 +43,10 @@ impl BottomPaneView<'_> for StatusIndicatorView { fn render(&self, area: ratatui::layout::Rect, buf: &mut Buffer) { self.view.render_ref(area, buf); } + + fn handle_key_event(&mut self, _pane: &mut BottomPane<'_>, key_event: KeyEvent) { + if key_event.code == KeyCode::Esc { + self.view.interrupt(); + } + } } diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 3d312ffc..2c28446f 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -624,14 +624,6 @@ impl ChatWidget<'_> { self.bottom_pane.on_file_search_result(query, matches); } - pub(crate) fn on_esc(&mut self) -> bool { - if self.bottom_pane.is_task_running() { - self.interrupt_running_task(); - return true; - } - false - } - /// Handle Ctrl-C key press. /// Returns CancellationEvent::Handled if the event was consumed by the UI, or /// CancellationEvent::Ignored if the caller should handle it (e.g. exit). diff --git a/codex-rs/tui/src/status_indicator_widget.rs b/codex-rs/tui/src/status_indicator_widget.rs index 6ef29320..b686f45d 100644 --- a/codex-rs/tui/src/status_indicator_widget.rs +++ b/codex-rs/tui/src/status_indicator_widget.rs @@ -9,6 +9,7 @@ use std::thread; use std::time::Duration; use std::time::Instant; +use codex_core::protocol::Op; use ratatui::buffer::Buffer; use ratatui::layout::Rect; use ratatui::style::Color; @@ -44,11 +45,7 @@ pub(crate) struct StatusIndicatorWidget { frame_idx: Arc, running: Arc, start_time: Instant, - // Keep one sender alive to prevent the channel from closing while the - // animation thread is still running. The field itself is currently not - // accessed anywhere, therefore the leading underscore silences the - // `dead_code` warning without affecting behavior. - _app_event_tx: AppEventSender, + app_event_tx: AppEventSender, } impl StatusIndicatorWidget { @@ -82,7 +79,7 @@ impl StatusIndicatorWidget { running, start_time: Instant::now(), - _app_event_tx: app_event_tx, + app_event_tx, } } @@ -120,6 +117,10 @@ impl StatusIndicatorWidget { self.reveal_len_at_base = shown_now.min(new_len); } + pub(crate) fn interrupt(&self) { + self.app_event_tx.send(AppEvent::CodexOp(Op::Interrupt)); + } + /// Reset the animation and start revealing `text` from the beginning. #[cfg(test)] pub(crate) fn restart_with_text(&mut self, text: String) {