Skip to content

Commit fdb2cfb

Browse files
committed
[tuify] Add Ctrl + c handling to main event loop
1 parent 4694e1f commit fdb2cfb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

tuify/src/keypress.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub enum KeyPress {
2929
Error,
3030
Space,
3131
Resize(Size),
32+
CtrlC,
3233
}
3334

3435
pub fn read_key_press() -> KeyPress {
@@ -51,6 +52,11 @@ fn read_key_press_unix() -> KeyPress {
5152
col_count: ch!(width),
5253
row_count: ch!(height),
5354
}),
55+
crossterm::event::Event::Key(KeyEvent {
56+
modifiers: KeyModifiers::CONTROL,
57+
code: KeyCode::Char('c'),
58+
..
59+
}) => KeyPress::CtrlC,
5460
crossterm::event::Event::Key(KeyEvent { code, .. }) => {
5561
// Only trap the right code.
5662
match code {
@@ -122,6 +128,14 @@ fn read_key_press_windows() -> KeyPress {
122128
state: KeyEventState::NONE,
123129
}) => KeyPress::Space,
124130

131+
// Ctrl + c.
132+
Event::Key(KeyEvent {
133+
code: KeyCode::Char('c'),
134+
modifiers: KeyModifiers::CONTROL,
135+
kind: KeyEventKind::Press, // This is for Windows.
136+
state: KeyEventState::NONE,
137+
}) => KeyPress::CtrlC,
138+
125139
// Resize.
126140
Event::Resize(width, height) => KeyPress::Resize(Size {
127141
col_count: ch!(width),

tuify/src/public_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ fn keypress_handler(state: &mut State, key_press: KeyPress) -> EventLoopResult {
214214
}
215215
}
216216

217-
// Escape.
218-
KeyPress::Esc => {
217+
// Escape or Ctrl + c.
218+
KeyPress::Esc | KeyPress::CtrlC => {
219219
call_if_true!(DEVELOPMENT_MODE, {
220220
log_debug("Esc".red().to_string());
221221
});

0 commit comments

Comments
 (0)