brain: Quit consistently on Ctrl-C
469c62d45083ef24fd0e744cfa48b3166e7a4b64
Route Ctrl-C before mode-specific key handling so terminal interrupts quit from history, commit, and hunk-browse views. Preserve Ctrl-C as editor input while a comment draft is active. Assisted-by: Codex:gpt-5.6-sol
1 parent
7ce49fb1
crates/brain/src/session.rs
+52 -0
| 221 | 221 | } |
|
| 222 | 222 | } |
|
| 223 | 223 | Ok(()) |
|
| 224 | 224 | } |
|
| 225 | 225 | ||
| 226 | + | /// Returns the control flow for a terminal interrupt in the active mode. |
|
| 227 | + | fn terminal_interrupt(context: ActionContext, event: &Event<()>) -> Control { |
|
| 228 | + | if context != ActionContext::HunkEdit && matches!(event, Event::Key(Key::Ctrl('c'))) { |
|
| 229 | + | Control::Quit |
|
| 230 | + | } else { |
|
| 231 | + | Control::Continue |
|
| 232 | + | } |
|
| 233 | + | } |
|
| 234 | + | ||
| 226 | 235 | /// Interactive terminal session state. |
|
| 227 | 236 | struct Session<'a, T: TuiBackend<Error = io::Error>> { |
|
| 228 | 237 | /// Terminal frame target. |
|
| 229 | 238 | terminal: &'a mut Terminal<T>, |
|
| 230 | 239 | /// Per-commit review slots, newest first. |
| 303 | 312 | fn update(&mut self, event: TerminalEvent) -> Result<Update, Error> { |
|
| 304 | 313 | match event { |
|
| 305 | 314 | TerminalEvent::Input(event) => { |
|
| 306 | 315 | self.error = None; |
|
| 307 | 316 | let event = event?; |
|
| 317 | + | if terminal_interrupt(self.action_context(), &event) == Control::Quit { |
|
| 318 | + | return Ok(Update::Quit); |
|
| 319 | + | } |
|
| 308 | 320 | let resize = if matches!(event, Event::Resize(_)) { |
|
| 309 | 321 | Resize::Changed |
|
| 310 | 322 | } else { |
|
| 311 | 323 | Resize::Stable |
|
| 312 | 324 | }; |
| 1485 | 1497 | .map(|hunk| hunk.id) |
|
| 1486 | 1498 | .collect::<Vec<_>>(), |
|
| 1487 | 1499 | vec![hunk_ids[1]] |
|
| 1488 | 1500 | ); |
|
| 1489 | 1501 | } |
|
| 1502 | + | ||
| 1503 | + | #[test] |
|
| 1504 | + | fn control_c_quits_commit_list() { |
|
| 1505 | + | let event = Event::Key(Key::Ctrl('c')); |
|
| 1506 | + | ||
| 1507 | + | assert_eq!( |
|
| 1508 | + | terminal_interrupt(ActionContext::Commits, &event), |
|
| 1509 | + | Control::Quit |
|
| 1510 | + | ); |
|
| 1511 | + | } |
|
| 1512 | + | ||
| 1513 | + | #[test] |
|
| 1514 | + | fn control_c_quits_commit_diff() { |
|
| 1515 | + | let event = Event::Key(Key::Ctrl('c')); |
|
| 1516 | + | ||
| 1517 | + | assert_eq!( |
|
| 1518 | + | terminal_interrupt(ActionContext::Commit, &event), |
|
| 1519 | + | Control::Quit |
|
| 1520 | + | ); |
|
| 1521 | + | } |
|
| 1522 | + | ||
| 1523 | + | #[test] |
|
| 1524 | + | fn control_c_quits_hunk_browser() { |
|
| 1525 | + | let event = Event::Key(Key::Ctrl('c')); |
|
| 1526 | + | ||
| 1527 | + | assert_eq!( |
|
| 1528 | + | terminal_interrupt(ActionContext::HunkBrowse, &event), |
|
| 1529 | + | Control::Quit |
|
| 1530 | + | ); |
|
| 1531 | + | } |
|
| 1532 | + | ||
| 1533 | + | #[test] |
|
| 1534 | + | fn control_c_remains_comment_editor_input() { |
|
| 1535 | + | let event = Event::Key(Key::Ctrl('c')); |
|
| 1536 | + | ||
| 1537 | + | assert_eq!( |
|
| 1538 | + | terminal_interrupt(ActionContext::HunkEdit, &event), |
|
| 1539 | + | Control::Continue |
|
| 1540 | + | ); |
|
| 1541 | + | } |
|
| 1490 | 1542 | } |