1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use ratatui::text::{Line, Span};

use super::{
    settings::{color_settings::ColorSettings, key_settings::KeySettings},
    App,
};

#[derive(Debug, Clone)]
pub struct HelpLine {
    pub command: String,
    pub description: String,
}

impl HelpLine {
    pub fn new(command: &str, description: &str) -> Self {
        Self {
            command: command.to_string(),
            description: description.to_string(),
        }
    }

    pub fn to_line(&self, color_settings: &ColorSettings) -> Line<'static> {
        let mut line = Line::default();
        line.spans.push(Span::styled(
            self.command.to_string(),
            color_settings.help_command,
        ));
        line.spans
            .push(Span::styled(": ", color_settings.menu_text));
        line.spans.push(Span::styled(
            self.description.clone(),
            color_settings.menu_text,
        ));
        line.left_aligned()
    }
}

impl App {
    pub fn key_event_to_string(event: crossterm::event::KeyEvent) -> String {
        let mut result = String::new();
        if event
            .modifiers
            .contains(crossterm::event::KeyModifiers::CONTROL)
        {
            result.push_str("Ctrl+");
        }
        if event
            .modifiers
            .contains(crossterm::event::KeyModifiers::ALT)
        {
            result.push_str("Alt+");
        }
        if event
            .modifiers
            .contains(crossterm::event::KeyModifiers::SHIFT)
        {
            result.push_str("Shift+");
        }
        if event
            .modifiers
            .contains(crossterm::event::KeyModifiers::SUPER)
        {
            result.push_str("Super+");
        }
        if event
            .modifiers
            .contains(crossterm::event::KeyModifiers::HYPER)
        {
            result.push_str("Hyper+");
        }
        if event
            .modifiers
            .contains(crossterm::event::KeyModifiers::META)
        {
            result.push_str("Meta+");
        }

        result.push_str(&match event.code {
            crossterm::event::KeyCode::Left => "←".into(),
            crossterm::event::KeyCode::Right => "→".into(),
            crossterm::event::KeyCode::Up => "↑".into(),
            crossterm::event::KeyCode::Down => "↓".into(),
            crossterm::event::KeyCode::F(n) => {
                format!("F{}", n)
            }
            crossterm::event::KeyCode::Char(c) => match c {
                ' ' => "Space".into(),
                '\t' => "Tab".into(),
                '\n' => "LF".into(),
                '\r' => "CR".into(),
                _ => c.to_ascii_uppercase().to_string(),
            },
            c => format!("{:?}", c),
        });
        result
    }

    pub(super) fn help_list(key_settings: &KeySettings) -> Vec<HelpLine> {
        vec![
            HelpLine::new(&Self::key_event_to_string(key_settings.up), "Move up"),
            HelpLine::new(&Self::key_event_to_string(key_settings.down), "Move down"),
            HelpLine::new(&Self::key_event_to_string(key_settings.left), "Move left"),
            HelpLine::new(&Self::key_event_to_string(key_settings.right), "Move right"),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.next),
                "Next instruction or block",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.previous),
                "Previous instruction or block",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.page_up),
                "Scroll up",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.page_down),
                "Scroll down",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.goto_start),
                "Scroll to start",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.goto_end),
                "Scroll to end",
            ),
            HelpLine::new(&Self::key_event_to_string(key_settings.run), "Run command"),
            HelpLine::new(&Self::key_event_to_string(key_settings.save), "Save"),
            HelpLine::new(&Self::key_event_to_string(key_settings.save_as), "Save as"),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.save_and_quit),
                "Save and quit",
            ),
            HelpLine::new(&Self::key_event_to_string(key_settings.quit), "Quit"),
            HelpLine::new(&Self::key_event_to_string(key_settings.open), "Open file"),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.change_view),
                "Change view",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.jump),
                "Jump to location",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.find_symbol),
                "Search symbol",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.find_text),
                "Search text",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.patch_text),
                "Patch text",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.patch_assembly),
                "Patch assembly",
            ),
            HelpLine::new(&Self::key_event_to_string(key_settings.log), "Open log"),
            HelpLine::new(&Self::key_event_to_string(key_settings.confirm), "Confirm"),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.close_popup),
                "Close popup",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.new_line),
                "Insert new line (with multiline text)",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.clear_log),
                "Clear log (with log open)",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.undo),
                "Undo last change",
            ),
            HelpLine::new(
                &Self::key_event_to_string(key_settings.redo),
                "Redo last change",
            ),
            HelpLine::new(&Self::key_event_to_string(key_settings.help), "Help"),
        ]
    }
}