Skip to main content

agent_procs/tui/
input.rs

1use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum Action {
5    SelectNext,
6    SelectPrev,
7    Restart,
8    Stop,
9    StopAll,
10    CycleStream,
11    TogglePause,
12    ScrollUp,
13    ScrollDown,
14    ScrollToTop,
15    ScrollToBottom,
16    StartFilter,
17    ClearFilter,
18    Quit,
19    QuitAndStop,
20    None,
21}
22
23/// Actions available when the filter input prompt is active.
24#[derive(Debug, Clone, PartialEq)]
25pub enum FilterAction {
26    Char(char),
27    Backspace,
28    Confirm,
29    Cancel,
30}
31
32pub fn handle_key(key: KeyEvent) -> Action {
33    match key.code {
34        KeyCode::Down | KeyCode::Char('j') => Action::SelectNext,
35        KeyCode::Up | KeyCode::Char('k') => Action::SelectPrev,
36        KeyCode::Char('r') => Action::Restart,
37        KeyCode::Char('x') => Action::Stop,
38        KeyCode::Char('X') => Action::StopAll,
39        KeyCode::Char('e') => Action::CycleStream,
40        KeyCode::Char(' ') => Action::TogglePause,
41        KeyCode::PageUp | KeyCode::Char('u') => Action::ScrollUp,
42        KeyCode::PageDown | KeyCode::Char('d') => Action::ScrollDown,
43        KeyCode::Home | KeyCode::Char('g') => Action::ScrollToTop,
44        KeyCode::End | KeyCode::Char('G') => Action::ScrollToBottom,
45        KeyCode::Char('/') => Action::StartFilter,
46        KeyCode::Esc => Action::ClearFilter,
47        KeyCode::Char('q') => Action::Quit,
48        KeyCode::Char('Q') => Action::QuitAndStop,
49        KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => Action::Quit,
50        _ => Action::None,
51    }
52}
53
54pub fn handle_filter_key(key: KeyEvent) -> FilterAction {
55    match key.code {
56        KeyCode::Enter => FilterAction::Confirm,
57        KeyCode::Backspace => FilterAction::Backspace,
58        KeyCode::Char(c) => FilterAction::Char(c),
59        _ => FilterAction::Cancel, // Esc or any unrecognized key cancels
60    }
61}