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