hex_patch/app/commands/
command_info.rs

1use 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("csave", "Save the comments."),
27            CommandInfo::new("help", "Display the help page."),
28            CommandInfo::new("open", "Open a file."),
29            CommandInfo::new("log", "Open the log."),
30            CommandInfo::new("run", "Run a command."),
31            CommandInfo::new("ftext", "Find text."),
32            CommandInfo::new("fsym", "Find a symbol."),
33            CommandInfo::new("fcom", "Find a comment."),
34            CommandInfo::new("ecom", "Edit a comment."),
35            CommandInfo::new("text", "Insert text."),
36            CommandInfo::new("patch", "Patch assembly."),
37            CommandInfo::new("jump", "Jump to address."),
38            CommandInfo::new("view", "Switch between text and assembly."),
39            CommandInfo::new("undo", "Undo the last change."),
40            CommandInfo::new("redo", "Redo the last change."),
41        ]
42    }
43
44    pub fn full_list_of_commands(plugin_manager: &PluginManager) -> Vec<CommandInfo> {
45        let mut commands = Self::default_commands();
46        commands.extend(plugin_manager.get_commands().iter().map(|&c| c.clone()));
47        commands
48    }
49
50    pub fn to_line(&self, color_settings: &ColorSettings, selected: bool) -> Line<'static> {
51        let (s0, s1) = if selected {
52            (
53                color_settings.command_selected,
54                color_settings.command_selected,
55            )
56        } else {
57            (
58                color_settings.command_name,
59                color_settings.command_description,
60            )
61        };
62        Line::from(vec![
63            Span::styled(self.command.clone(), s0),
64            Span::styled(" ", s0),
65            Span::styled(self.description.clone(), s1),
66        ])
67        .left_aligned()
68    }
69}
70
71impl AsRef<str> for CommandInfo {
72    fn as_ref(&self) -> &str {
73        &self.command
74    }
75}