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