Skip to main content

sandbox_quant/
input.rs

1use crossterm::event::KeyCode;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum UiCommand {
5    Pause,
6    Resume,
7    ManualBuy,
8    ManualSell,
9    SwitchTimeframe(&'static str),
10    OpenSymbolSelector,
11    OpenStrategySelector,
12    OpenAccountPopup,
13    OpenHistoryPopup,
14    OpenGrid,
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum GridCommand {
19    TabAssets,
20    TabStrategies,
21    TabRisk,
22    TabNetwork,
23    TabHistory,
24    TabSystemLog,
25    ToggleOnOffPanel,
26    StrategyUp,
27    StrategyDown,
28    SymbolLeft,
29    SymbolRight,
30    NewStrategy,
31    EditStrategyConfig,
32    DeleteStrategy,
33    ToggleStrategyOnOff,
34    ActivateStrategy,
35    CloseGrid,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum PopupKind {
40    SymbolSelector,
41    StrategySelector,
42    Account,
43    History,
44    Focus,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum PopupCommand {
49    Close,
50    Up,
51    Down,
52    Confirm,
53    HistoryDay,
54    HistoryHour,
55    HistoryMonth,
56}
57
58pub fn parse_main_command(key_code: &KeyCode) -> Option<UiCommand> {
59    match key_code {
60        KeyCode::Char('0') => Some(UiCommand::SwitchTimeframe("1s")),
61        KeyCode::Char(c) => match c.to_ascii_lowercase() {
62            'p' => Some(UiCommand::Pause),
63            'r' => Some(UiCommand::Resume),
64            'b' => Some(UiCommand::ManualBuy),
65            's' => Some(UiCommand::ManualSell),
66            '1' => Some(UiCommand::SwitchTimeframe("1m")),
67            'h' => Some(UiCommand::SwitchTimeframe("1h")),
68            'd' => Some(UiCommand::SwitchTimeframe("1d")),
69            'w' => Some(UiCommand::SwitchTimeframe("1w")),
70            'm' => Some(UiCommand::SwitchTimeframe("1M")),
71            't' => Some(UiCommand::OpenSymbolSelector),
72            'y' => Some(UiCommand::OpenStrategySelector),
73            'a' => Some(UiCommand::OpenAccountPopup),
74            'i' => Some(UiCommand::OpenHistoryPopup),
75            'g' | 'f' => Some(UiCommand::OpenGrid),
76            _ => None,
77        },
78        _ => None,
79    }
80}
81
82pub fn parse_grid_command(key_code: &KeyCode) -> Option<GridCommand> {
83    match key_code {
84        KeyCode::Char('1') => Some(GridCommand::TabAssets),
85        KeyCode::Char('2') => Some(GridCommand::TabStrategies),
86        KeyCode::Char('3') => Some(GridCommand::TabRisk),
87        KeyCode::Char('4') => Some(GridCommand::TabNetwork),
88        KeyCode::Char('5') => Some(GridCommand::TabHistory),
89        KeyCode::Char('6') => Some(GridCommand::TabSystemLog),
90        KeyCode::Tab => Some(GridCommand::ToggleOnOffPanel),
91        KeyCode::Up => Some(GridCommand::StrategyUp),
92        KeyCode::Down => Some(GridCommand::StrategyDown),
93        KeyCode::Left => Some(GridCommand::SymbolLeft),
94        KeyCode::Right => Some(GridCommand::SymbolRight),
95        KeyCode::Delete => Some(GridCommand::DeleteStrategy),
96        KeyCode::Enter => Some(GridCommand::ActivateStrategy),
97        KeyCode::Esc => Some(GridCommand::CloseGrid),
98        KeyCode::Char(c) => match c.to_ascii_lowercase() {
99            'k' => Some(GridCommand::StrategyUp),
100            'j' => Some(GridCommand::StrategyDown),
101            'h' => Some(GridCommand::SymbolLeft),
102            'l' => Some(GridCommand::SymbolRight),
103            'n' => Some(GridCommand::NewStrategy),
104            'c' => Some(GridCommand::EditStrategyConfig),
105            'x' => Some(GridCommand::DeleteStrategy),
106            'o' => Some(GridCommand::ToggleStrategyOnOff),
107            'f' => Some(GridCommand::ActivateStrategy),
108            'g' => Some(GridCommand::CloseGrid),
109            _ => None,
110        },
111        _ => None,
112    }
113}
114
115pub fn parse_popup_command(kind: PopupKind, key_code: &KeyCode) -> Option<PopupCommand> {
116    match kind {
117        PopupKind::SymbolSelector => match key_code {
118            KeyCode::Esc | KeyCode::Char('t') | KeyCode::Char('T') => Some(PopupCommand::Close),
119            KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('K') => Some(PopupCommand::Up),
120            KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('J') => Some(PopupCommand::Down),
121            KeyCode::Enter => Some(PopupCommand::Confirm),
122            _ => None,
123        },
124        PopupKind::StrategySelector => match key_code {
125            KeyCode::Esc | KeyCode::Char('y') | KeyCode::Char('Y') => Some(PopupCommand::Close),
126            KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('K') => Some(PopupCommand::Up),
127            KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('J') => Some(PopupCommand::Down),
128            KeyCode::Enter => Some(PopupCommand::Confirm),
129            _ => None,
130        },
131        PopupKind::Account => match key_code {
132            KeyCode::Esc | KeyCode::Char('a') | KeyCode::Char('A') | KeyCode::Enter => {
133                Some(PopupCommand::Close)
134            }
135            _ => None,
136        },
137        PopupKind::History => match key_code {
138            KeyCode::Char('d') | KeyCode::Char('D') => Some(PopupCommand::HistoryDay),
139            KeyCode::Char('h') | KeyCode::Char('H') => Some(PopupCommand::HistoryHour),
140            KeyCode::Char('m') | KeyCode::Char('M') => Some(PopupCommand::HistoryMonth),
141            KeyCode::Esc | KeyCode::Char('i') | KeyCode::Char('I') | KeyCode::Enter => {
142                Some(PopupCommand::Close)
143            }
144            _ => None,
145        },
146        PopupKind::Focus => match key_code {
147            KeyCode::Esc | KeyCode::Char('f') | KeyCode::Char('F') | KeyCode::Enter => {
148                Some(PopupCommand::Close)
149            }
150            _ => None,
151        },
152    }
153}