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", t!("app.commands.quit")),
22 CommandInfo::new("dquit", t!("app.commands.dquit")),
23 CommandInfo::new("xquit", t!("app.commands.xquit")),
24 CommandInfo::new("save", t!("app.commands.save")),
25 CommandInfo::new("saveas", t!("app.commands.saveas")),
26 CommandInfo::new("csave", t!("app.commands.csave")),
27 CommandInfo::new("help", t!("app.commands.help")),
28 CommandInfo::new("open", t!("app.commands.open")),
29 CommandInfo::new("log", t!("app.commands.log")),
30 CommandInfo::new("run", t!("app.commands.run")),
31 CommandInfo::new("ftext", t!("app.commands.ftext")),
32 CommandInfo::new("fsym", t!("app.commands.fsym")),
33 CommandInfo::new("fcom", t!("app.commands.fcom")),
34 CommandInfo::new("ecom", t!("app.commands.ecom")),
35 CommandInfo::new("text", t!("app.commands.text")),
36 CommandInfo::new("patch", t!("app.commands.patch")),
37 CommandInfo::new("jump", t!("app.commands.jump")),
38 CommandInfo::new("view", t!("app.commands.view")),
39 CommandInfo::new("undo", t!("app.commands.undo")),
40 CommandInfo::new("redo", t!("app.commands.redo")),
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}