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::TabRisk),
90        KeyCode::Char('2') => Some(GridCommand::TabNetwork),
91        KeyCode::Char('3') => Some(GridCommand::TabHistory),
92        KeyCode::Char('4') => Some(GridCommand::TabPredictors),
93        KeyCode::Char('5') => Some(GridCommand::TabSystemLog),
94        KeyCode::Tab => Some(GridCommand::ToggleOnOffPanel),
95        KeyCode::Up => Some(GridCommand::StrategyUp),
96        KeyCode::Down => Some(GridCommand::StrategyDown),
97        KeyCode::Left => Some(GridCommand::SymbolLeft),
98        KeyCode::Right => Some(GridCommand::SymbolRight),
99        KeyCode::Delete => Some(GridCommand::DeleteStrategy),
100        KeyCode::Enter => Some(GridCommand::ActivateStrategy),
101        KeyCode::Esc => Some(GridCommand::CloseGrid),
102        KeyCode::Char(c) => match c.to_ascii_lowercase() {
103            'k' => Some(GridCommand::StrategyUp),
104            'j' => Some(GridCommand::StrategyDown),
105            'h' => Some(GridCommand::SymbolLeft),
106            'l' => Some(GridCommand::SymbolRight),
107            'n' => Some(GridCommand::NewStrategy),
108            'c' => Some(GridCommand::EditStrategyConfig),
109            'x' => Some(GridCommand::DeleteStrategy),
110            'o' => Some(GridCommand::ToggleStrategyOnOff),
111            'f' => Some(GridCommand::ActivateStrategy),
112            'u' => Some(GridCommand::ToggleSmallPositionsFilter),
113            'g' => Some(GridCommand::CloseGrid),
114            _ => None,
115        },
116        _ => None,
117    }
118}
119
120pub fn parse_popup_command(kind: PopupKind, key_code: &KeyCode) -> Option<PopupCommand> {
121    match kind {
122        PopupKind::SymbolSelector => match key_code {
123            KeyCode::Esc | KeyCode::Char('t') | KeyCode::Char('T') => 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::StrategySelector => match key_code {
130            KeyCode::Esc | KeyCode::Char('y') | KeyCode::Char('Y') => Some(PopupCommand::Close),
131            KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('K') => Some(PopupCommand::Up),
132            KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('J') => Some(PopupCommand::Down),
133            KeyCode::Enter => Some(PopupCommand::Confirm),
134            _ => None,
135        },
136        PopupKind::Account => match key_code {
137            KeyCode::Esc | KeyCode::Char('a') | KeyCode::Char('A') | KeyCode::Enter => {
138                Some(PopupCommand::Close)
139            }
140            _ => None,
141        },
142        PopupKind::History => match key_code {
143            KeyCode::Char('d') | KeyCode::Char('D') => Some(PopupCommand::HistoryDay),
144            KeyCode::Char('h') | KeyCode::Char('H') => Some(PopupCommand::HistoryHour),
145            KeyCode::Char('m') | KeyCode::Char('M') => Some(PopupCommand::HistoryMonth),
146            KeyCode::Esc | KeyCode::Char('i') | KeyCode::Char('I') | KeyCode::Enter => {
147                Some(PopupCommand::Close)
148            }
149            _ => None,
150        },
151        PopupKind::Focus => match key_code {
152            KeyCode::Esc | KeyCode::Char('f') | KeyCode::Char('F') | KeyCode::Enter => {
153                Some(PopupCommand::Close)
154            }
155            _ => None,
156        },
157    }
158}