hex_patch/app/
help.rs

1use ratatui::text::{Line, Span};
2
3use super::{
4    settings::{color_settings::ColorSettings, key_settings::KeySettings},
5    App,
6};
7
8#[derive(Debug, Clone)]
9pub struct HelpLine {
10    pub command: String,
11    pub description: String,
12}
13
14impl HelpLine {
15    pub fn new(command: &str, description: &str) -> Self {
16        Self {
17            command: command.to_string(),
18            description: description.to_string(),
19        }
20    }
21
22    pub fn to_line(&self, color_settings: &ColorSettings) -> Line<'static> {
23        let mut line = Line::default();
24        line.spans.push(Span::styled(
25            self.command.to_string(),
26            color_settings.help_command,
27        ));
28        line.spans
29            .push(Span::styled(": ", color_settings.menu_text));
30        line.spans.push(Span::styled(
31            self.description.clone(),
32            color_settings.help_description,
33        ));
34        line.left_aligned()
35    }
36}
37
38impl App {
39    pub fn key_event_to_string(event: crossterm::event::KeyEvent) -> String {
40        let mut result = String::new();
41        if event
42            .modifiers
43            .contains(crossterm::event::KeyModifiers::CONTROL)
44        {
45            result.push_str("Ctrl+");
46        }
47        if event
48            .modifiers
49            .contains(crossterm::event::KeyModifiers::ALT)
50        {
51            result.push_str("Alt+");
52        }
53        if event
54            .modifiers
55            .contains(crossterm::event::KeyModifiers::SHIFT)
56        {
57            result.push_str("Shift+");
58        }
59        if event
60            .modifiers
61            .contains(crossterm::event::KeyModifiers::SUPER)
62        {
63            result.push_str("Super+");
64        }
65        if event
66            .modifiers
67            .contains(crossterm::event::KeyModifiers::HYPER)
68        {
69            result.push_str("Hyper+");
70        }
71        if event
72            .modifiers
73            .contains(crossterm::event::KeyModifiers::META)
74        {
75            result.push_str("Meta+");
76        }
77
78        result.push_str(&match event.code {
79            crossterm::event::KeyCode::Left => "←".into(),
80            crossterm::event::KeyCode::Right => "→".into(),
81            crossterm::event::KeyCode::Up => "↑".into(),
82            crossterm::event::KeyCode::Down => "↓".into(),
83            crossterm::event::KeyCode::F(n) => {
84                format!("F{}", n)
85            }
86            crossterm::event::KeyCode::Char(c) => match c {
87                ' ' => "Space".into(),
88                '\t' => "Tab".into(),
89                '\n' => "LF".into(),
90                '\r' => "CR".into(),
91                _ => c.to_ascii_uppercase().to_string(),
92            },
93            c => format!("{:?}", c),
94        });
95        result
96    }
97
98    pub(super) fn help_list(key_settings: &KeySettings) -> Vec<HelpLine> {
99        vec![
100            HelpLine::new(&Self::key_event_to_string(key_settings.up), "Move up"),
101            HelpLine::new(&Self::key_event_to_string(key_settings.down), "Move down"),
102            HelpLine::new(&Self::key_event_to_string(key_settings.left), "Move left"),
103            HelpLine::new(&Self::key_event_to_string(key_settings.right), "Move right"),
104            HelpLine::new(
105                &Self::key_event_to_string(key_settings.next),
106                "Next instruction or block",
107            ),
108            HelpLine::new(
109                &Self::key_event_to_string(key_settings.previous),
110                "Previous instruction or block",
111            ),
112            HelpLine::new(
113                &Self::key_event_to_string(key_settings.page_up),
114                "Scroll up",
115            ),
116            HelpLine::new(
117                &Self::key_event_to_string(key_settings.page_down),
118                "Scroll down",
119            ),
120            HelpLine::new(
121                &Self::key_event_to_string(key_settings.goto_start),
122                "Scroll to start",
123            ),
124            HelpLine::new(
125                &Self::key_event_to_string(key_settings.goto_end),
126                "Scroll to end",
127            ),
128            HelpLine::new(&Self::key_event_to_string(key_settings.run), "Run command"),
129            HelpLine::new(&Self::key_event_to_string(key_settings.save), "Save"),
130            HelpLine::new(&Self::key_event_to_string(key_settings.save_as), "Save as"),
131            HelpLine::new(
132                &Self::key_event_to_string(key_settings.save_and_quit),
133                "Save and quit",
134            ),
135            HelpLine::new(&Self::key_event_to_string(key_settings.quit), "Quit"),
136            HelpLine::new(&Self::key_event_to_string(key_settings.open), "Open file"),
137            HelpLine::new(
138                &Self::key_event_to_string(key_settings.change_view),
139                "Switch view from assembly to text or viceversa",
140            ),
141            HelpLine::new(
142                &Self::key_event_to_string(key_settings.change_selected_pane),
143                "Switch from the hex pane to assembly/text or viceversa",
144            ),
145            HelpLine::new(
146                &Self::key_event_to_string(key_settings.fullscreen),
147                "Toggle the fullscreen for the selected view",
148            ),
149            HelpLine::new(
150                &Self::key_event_to_string(key_settings.jump),
151                "Jump to location",
152            ),
153            HelpLine::new(
154                &Self::key_event_to_string(key_settings.find_symbol),
155                "Search symbol",
156            ),
157            HelpLine::new(
158                &Self::key_event_to_string(key_settings.find_text),
159                "Search text",
160            ),
161            HelpLine::new(
162                &Self::key_event_to_string(key_settings.patch_text),
163                "Patch text",
164            ),
165            HelpLine::new(
166                &Self::key_event_to_string(key_settings.patch_assembly),
167                "Patch assembly",
168            ),
169            HelpLine::new(&Self::key_event_to_string(key_settings.log), "Open log"),
170            HelpLine::new(&Self::key_event_to_string(key_settings.confirm), "Confirm"),
171            HelpLine::new(
172                &Self::key_event_to_string(key_settings.close_popup),
173                "Close popup",
174            ),
175            HelpLine::new(
176                &Self::key_event_to_string(key_settings.new_line),
177                "Insert new line (with multiline text)",
178            ),
179            HelpLine::new(
180                &Self::key_event_to_string(key_settings.clear_log),
181                "Clear log (with log open)",
182            ),
183            HelpLine::new(
184                &Self::key_event_to_string(key_settings.undo),
185                "Undo last change",
186            ),
187            HelpLine::new(
188                &Self::key_event_to_string(key_settings.redo),
189                "Redo last change",
190            ),
191            HelpLine::new(&Self::key_event_to_string(key_settings.help), "Help"),
192        ]
193    }
194}