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