Skip to main content

hx_plugins/
commands.rs

1//! Custom command support for plugins.
2
3/// A custom command registered by a plugin.
4#[derive(Debug, Clone)]
5pub struct CustomCommand {
6    /// Command name (what users type after `hx`).
7    pub name: String,
8
9    /// Description shown in help.
10    pub description: String,
11
12    /// Usage string for help.
13    pub usage: Option<String>,
14
15    /// Source plugin that registered this command.
16    pub source_plugin: Option<String>,
17}
18
19impl CustomCommand {
20    /// Create a new custom command.
21    pub fn new(name: impl Into<String>) -> Self {
22        CustomCommand {
23            name: name.into(),
24            description: String::new(),
25            usage: None,
26            source_plugin: None,
27        }
28    }
29
30    /// Set the description.
31    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
32        self.description = desc.into();
33        self
34    }
35
36    /// Set the usage string.
37    pub fn with_usage(mut self, usage: impl Into<String>) -> Self {
38        self.usage = Some(usage.into());
39        self
40    }
41
42    /// Set the source plugin.
43    pub fn with_source(mut self, source: impl Into<String>) -> Self {
44        self.source_plugin = Some(source.into());
45        self
46    }
47
48    /// Generate help text for this command.
49    pub fn help_text(&self) -> String {
50        let mut text = String::new();
51
52        if !self.description.is_empty() {
53            text.push_str(&self.description);
54            text.push('\n');
55        }
56
57        if let Some(ref usage) = self.usage {
58            text.push('\n');
59            text.push_str("Usage: ");
60            text.push_str(usage);
61            text.push('\n');
62        }
63
64        if let Some(ref source) = self.source_plugin {
65            text.push('\n');
66            text.push_str("(Defined by: ");
67            text.push_str(source);
68            text.push(')');
69        }
70
71        text
72    }
73}
74
75/// Builder for creating custom commands programmatically.
76pub struct CommandBuilder {
77    command: CustomCommand,
78}
79
80impl CommandBuilder {
81    /// Start building a new command.
82    pub fn new(name: impl Into<String>) -> Self {
83        CommandBuilder {
84            command: CustomCommand::new(name),
85        }
86    }
87
88    /// Set the description.
89    pub fn description(mut self, desc: impl Into<String>) -> Self {
90        self.command.description = desc.into();
91        self
92    }
93
94    /// Set the usage string.
95    pub fn usage(mut self, usage: impl Into<String>) -> Self {
96        self.command.usage = Some(usage.into());
97        self
98    }
99
100    /// Set the source plugin.
101    pub fn source(mut self, source: impl Into<String>) -> Self {
102        self.command.source_plugin = Some(source.into());
103        self
104    }
105
106    /// Build the command.
107    pub fn build(self) -> CustomCommand {
108        self.command
109    }
110}