hex_patch/app/commands/
command_info.rs1use ratatui::text::{Line, Span};
2
3use crate::app::{plugins::plugin_manager::PluginManager, settings::color_settings::ColorSettings};
4
5#[derive(Debug, Clone)]
6pub struct CommandInfo {
7 pub command: String,
8 pub description: String,
9}
10
11impl CommandInfo {
12 pub fn new(command: impl Into<String>, description: impl Into<String>) -> Self {
13 Self {
14 command: command.into(),
15 description: description.into(),
16 }
17 }
18
19 pub fn default_commands() -> Vec<CommandInfo> {
20 vec![
21 CommandInfo::new("quit", "Quit the program."),
22 CommandInfo::new("dquit", "Quit the program without saving."),
23 CommandInfo::new("xquit", "Save and quit the program."),
24 CommandInfo::new("save", "Save the current file."),
25 CommandInfo::new("saveas", "Save the current file as a new file."),
26 CommandInfo::new("help", "Display the help page."),
27 CommandInfo::new("open", "Open a file."),
28 CommandInfo::new("log", "Open the log."),
29 CommandInfo::new("run", "Run a command."),
30 CommandInfo::new("ftext", "Find text."),
31 CommandInfo::new("fsym", "Find a symbol."),
32 CommandInfo::new("text", "Insert text."),
33 CommandInfo::new("patch", "Patch assembly."),
34 CommandInfo::new("jump", "Jump to address."),
35 CommandInfo::new("view", "Switch between text and assembly."),
36 CommandInfo::new("undo", "Undo the last change."),
37 CommandInfo::new("redo", "Redo the last change."),
38 ]
39 }
40
41 pub fn full_list_of_commands(plugin_manager: &PluginManager) -> Vec<CommandInfo> {
42 let mut commands = Self::default_commands();
43 commands.extend(plugin_manager.get_commands().iter().map(|&c| c.clone()));
44 commands
45 }
46
47 pub fn to_line(&self, color_settings: &ColorSettings, selected: bool) -> Line<'static> {
48 let (s0, s1) = if selected {
49 (
50 color_settings.command_selected,
51 color_settings.command_selected,
52 )
53 } else {
54 (
55 color_settings.command_name,
56 color_settings.command_description,
57 )
58 };
59 Line::from(vec![
60 Span::styled(self.command.clone(), s0),
61 Span::styled(" ", s0),
62 Span::styled(self.description.clone(), s1),
63 ])
64 .left_aligned()
65 }
66}
67
68impl AsRef<str> for CommandInfo {
69 fn as_ref(&self) -> &str {
70 &self.command
71 }
72}