leetcode_tui_rs/
executor.rs

1use leetcode_tui_config::{key::Key, utils::get_config_file_path};
2use leetcode_tui_core::emit;
3
4use crate::ctx::Ctx;
5
6pub struct Executor;
7
8impl Executor {
9    pub fn handle(cx: &mut Ctx, key: Key) -> bool {
10        if matches!(key, Key::Char('?')) {
11            return cx.help.toggle();
12        }
13
14        // open config file
15        if matches!(key, Key::Char('c')) && !cx.input.visible {
16            emit!(Open(get_config_file_path()));
17            return false;
18        }
19
20        if cx.help.is_visible() {
21            return match key {
22                Key::Down | Key::Char('j') => cx.help.next(),
23                Key::Up | Key::Char('k') => cx.help.previous(),
24                Key::Char('?') | Key::Esc => cx.help.toggle(),
25                Key::Enter => {
26                    cx.help.toggle()
27                    //TODO: emit key event
28                    // emit!(Key());
29                }
30                _ => false,
31            };
32        }
33
34        if cx.popup.visible {
35            return match key {
36                Key::Enter | Key::Esc => cx.popup.toggle(),
37                Key::Up | Key::Char('k') => cx.popup.scroll_up(),
38                Key::Down | Key::Char('j') => cx.popup.scroll_down(),
39                _ => false,
40            };
41        }
42
43        if cx.select_popup.visible {
44            return match key {
45                Key::Enter => cx.select_popup.close(),
46                Key::Esc => cx.select_popup.close_unselected(),
47                Key::Up | Key::Char('k') => cx.select_popup.prev_item(),
48                Key::Down | Key::Char('j') => cx.select_popup.next_item(),
49                _ => false,
50            };
51        }
52
53        if cx.input.visible {
54            return match key {
55                Key::Esc => cx.input.close(),
56                Key::Char(c) => cx.input.char(c),
57                Key::Backspace => cx.input.remove_char(),
58                Key::Up | Key::Down => {
59                    cx.input.close();
60                    leetcode_tui_core::emit!(Key(key.into()));
61                    true
62                }
63                _ => false,
64            };
65        }
66
67        if cx.content.get_questions().is_stats_visible() {
68            return match key {
69                Key::Char('T') => cx.content.get_topic_mut().prev_topic(),
70                Key::Char('t') => cx.content.get_topic_mut().next_topic(),
71                Key::Ctrl('s') | Key::Esc | Key::Enter => {
72                    cx.content.get_questions_mut().toggle_stats()
73                }
74                _ => false,
75            };
76        }
77
78        if cx.content.is_visible() {
79            return match key {
80                Key::Char('T') => cx.content.get_topic_mut().prev_topic(),
81                Key::Char('t') => cx.content.get_topic_mut().next_topic(),
82                Key::Char('d') => cx.content.get_questions().toggle_daily_question(),
83                Key::Char('e') => cx.content.get_questions_mut().solve_for_language(),
84                Key::Up | Key::Char('k') => cx.content.get_questions_mut().prev_ques(),
85                Key::Down | Key::Char('j') => cx.content.get_questions_mut().next_ques(),
86                Key::Char('r') => cx.content.get_questions_mut().rand_ques(),
87                Key::Enter => cx.content.get_questions_mut().show_question_content(),
88                Key::Char('R') => cx.content.get_questions_mut().run_solution(),
89                Key::Char('s') => cx.content.get_questions_mut().submit_solution(),
90                Key::Ctrl('s') => cx.content.get_questions_mut().toggle_stats(),
91                Key::Char('/') => cx.content.get_questions_mut().toggle_search(),
92                Key::Char('q') => {
93                    emit!(Quit);
94                    false
95                }
96                Key::Char('*') => {
97                    emit!(SyncDb);
98                    true
99                }
100                _ => false,
101            };
102        }
103        false
104    }
105}