Skip to content

Commit 12cf0dd

Browse files
authored
Better implementation of interrupt on Esc (#2111)
Use existing abstractions
1 parent 6c254ca commit 12cf0dd

File tree

4 files changed

+16
-28
lines changed

4 files changed

+16
-28
lines changed

codex-rs/tui/src/app.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,6 @@ impl App<'_> {
235235
self.app_event_tx.send(AppEvent::ExitRequest);
236236
}
237237
},
238-
KeyEvent {
239-
code: KeyCode::Esc,
240-
kind: KeyEventKind::Press,
241-
..
242-
} => match &mut self.app_state {
243-
AppState::Chat { widget } => {
244-
if !widget.on_esc() {
245-
self.dispatch_key_event(key_event);
246-
}
247-
}
248-
AppState::Onboarding { .. } => {
249-
self.dispatch_key_event(key_event);
250-
}
251-
},
252238
KeyEvent {
253239
code: KeyCode::Char('z'),
254240
modifiers: crossterm::event::KeyModifiers::CONTROL,

codex-rs/tui/src/bottom_pane/status_indicator_view.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use crossterm::event::KeyCode;
2+
use crossterm::event::KeyEvent;
13
use ratatui::buffer::Buffer;
24
use ratatui::widgets::WidgetRef;
35

46
use crate::app_event_sender::AppEventSender;
7+
use crate::bottom_pane::BottomPane;
58
use crate::status_indicator_widget::StatusIndicatorWidget;
69

710
use super::BottomPaneView;
@@ -40,4 +43,10 @@ impl BottomPaneView<'_> for StatusIndicatorView {
4043
fn render(&self, area: ratatui::layout::Rect, buf: &mut Buffer) {
4144
self.view.render_ref(area, buf);
4245
}
46+
47+
fn handle_key_event(&mut self, _pane: &mut BottomPane<'_>, key_event: KeyEvent) {
48+
if key_event.code == KeyCode::Esc {
49+
self.view.interrupt();
50+
}
51+
}
4352
}

codex-rs/tui/src/chatwidget.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,6 @@ impl ChatWidget<'_> {
624624
self.bottom_pane.on_file_search_result(query, matches);
625625
}
626626

627-
pub(crate) fn on_esc(&mut self) -> bool {
628-
if self.bottom_pane.is_task_running() {
629-
self.interrupt_running_task();
630-
return true;
631-
}
632-
false
633-
}
634-
635627
/// Handle Ctrl-C key press.
636628
/// Returns CancellationEvent::Handled if the event was consumed by the UI, or
637629
/// CancellationEvent::Ignored if the caller should handle it (e.g. exit).

codex-rs/tui/src/status_indicator_widget.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::thread;
99
use std::time::Duration;
1010
use std::time::Instant;
1111

12+
use codex_core::protocol::Op;
1213
use ratatui::buffer::Buffer;
1314
use ratatui::layout::Rect;
1415
use ratatui::style::Color;
@@ -44,11 +45,7 @@ pub(crate) struct StatusIndicatorWidget {
4445
frame_idx: Arc<AtomicUsize>,
4546
running: Arc<AtomicBool>,
4647
start_time: Instant,
47-
// Keep one sender alive to prevent the channel from closing while the
48-
// animation thread is still running. The field itself is currently not
49-
// accessed anywhere, therefore the leading underscore silences the
50-
// `dead_code` warning without affecting behavior.
51-
_app_event_tx: AppEventSender,
48+
app_event_tx: AppEventSender,
5249
}
5350

5451
impl StatusIndicatorWidget {
@@ -82,7 +79,7 @@ impl StatusIndicatorWidget {
8279
running,
8380
start_time: Instant::now(),
8481

85-
_app_event_tx: app_event_tx,
82+
app_event_tx,
8683
}
8784
}
8885

@@ -120,6 +117,10 @@ impl StatusIndicatorWidget {
120117
self.reveal_len_at_base = shown_now.min(new_len);
121118
}
122119

120+
pub(crate) fn interrupt(&self) {
121+
self.app_event_tx.send(AppEvent::CodexOp(Op::Interrupt));
122+
}
123+
123124
/// Reset the animation and start revealing `text` from the beginning.
124125
#[cfg(test)]
125126
pub(crate) fn restart_with_text(&mut self, text: String) {

0 commit comments

Comments
 (0)