toon_format/tui/
keybindings.rs

1//! Keyboard shortcuts and action mapping.
2
3use crossterm::event::{
4    KeyCode,
5    KeyEvent,
6    KeyModifiers,
7};
8
9/// Actions that can be triggered by keyboard shortcuts.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Action {
12    Quit,
13    ToggleMode,
14    SwitchPanel,
15    OpenFile,
16    SaveFile,
17    Refresh,
18    ToggleSettings,
19    ToggleHelp,
20    ToggleFileBrowser,
21    ToggleHistory,
22    ToggleDiff,
23    ToggleTheme,
24    CopyOutput,
25    CopySelection,
26    PasteInput,
27    ClearInput,
28    NewFile,
29    RoundTrip,
30    OpenRepl,
31    None,
32}
33
34pub struct KeyBindings;
35
36impl KeyBindings {
37    /// Map key event to action.
38    pub fn handle(key: KeyEvent) -> Action {
39        match (key.code, key.modifiers) {
40            (KeyCode::Char('c'), KeyModifiers::CONTROL) => Action::Quit,
41            (KeyCode::Char('q'), KeyModifiers::CONTROL) => Action::Quit,
42            (KeyCode::Char('e'), KeyModifiers::CONTROL) => Action::ToggleMode,
43            (KeyCode::Char('m'), KeyModifiers::CONTROL) => Action::ToggleMode,
44            (KeyCode::Tab, KeyModifiers::NONE) => Action::SwitchPanel,
45            (KeyCode::Char('o'), KeyModifiers::CONTROL) => Action::OpenFile,
46            (KeyCode::Char('s'), KeyModifiers::CONTROL) => Action::SaveFile,
47            (KeyCode::Char('n'), KeyModifiers::CONTROL) => Action::NewFile,
48            (KeyCode::Char('p'), KeyModifiers::CONTROL) => Action::ToggleSettings,
49            (KeyCode::F(1), KeyModifiers::NONE) => Action::ToggleHelp,
50            (KeyCode::Char('f'), KeyModifiers::CONTROL) => Action::ToggleFileBrowser,
51            (KeyCode::Char('h'), KeyModifiers::CONTROL) => Action::ToggleHistory,
52            (KeyCode::Char('d'), KeyModifiers::CONTROL) => Action::ToggleDiff,
53            (KeyCode::Char('t'), KeyModifiers::CONTROL) => Action::ToggleTheme,
54            (KeyCode::Char('y'), KeyModifiers::CONTROL) => Action::CopyOutput,
55            (KeyCode::Char('k'), KeyModifiers::CONTROL) => Action::CopySelection,
56            (KeyCode::Char('v'), KeyModifiers::CONTROL) => Action::PasteInput,
57            (KeyCode::Char('b'), KeyModifiers::CONTROL) => Action::RoundTrip,
58            (KeyCode::Char('r'), KeyModifiers::CONTROL) => Action::OpenRepl,
59            (KeyCode::Char('l'), KeyModifiers::CONTROL) => Action::ClearInput,
60
61            _ => Action::None,
62        }
63    }
64
65    /// Get list of shortcuts for help display.
66    pub fn shortcuts() -> Vec<(&'static str, &'static str)> {
67        vec![
68            ("Ctrl+C/Q", "Quit"),
69            ("Ctrl+E/M", "Toggle Mode"),
70            ("Tab", "Switch Panel"),
71            ("Ctrl+R", "Open REPL"),
72            ("Ctrl+O", "Open File"),
73            ("Ctrl+S", "Save File"),
74            ("Ctrl+N", "New File"),
75            ("Ctrl+P", "Settings"),
76            ("F1", "Help"),
77            ("Ctrl+F", "File Browser"),
78            ("Ctrl+H", "History"),
79            ("Ctrl+D", "Diff View"),
80            ("Ctrl+T", "Toggle Theme"),
81            ("Ctrl+Y", "Copy All Output"),
82            ("Ctrl+K", "Copy Selection"),
83            ("Ctrl+V", "Paste Input"),
84            ("Ctrl+B", "Round Trip Test"),
85            ("Ctrl+L", "Clear Input"),
86        ]
87    }
88}