Skip to main content

limit_cli/tui/commands/
builtin.rs

1//! Built-in commands (help, clear, exit)
2//!
3//! Simple commands that don't require complex state management.
4
5use super::{Command, CommandContext, CommandResult};
6use crate::error::CliError;
7
8/// Help command - displays available commands
9pub struct HelpCommand;
10
11impl Command for HelpCommand {
12    fn name(&self) -> &str {
13        "help"
14    }
15
16    fn aliases(&self) -> Vec<&str> {
17        vec!["?", "h"]
18    }
19
20    fn description(&self) -> &str {
21        "Show available commands and usage information"
22    }
23
24    fn usage(&self) -> Vec<&str> {
25        vec!["/help", "/help <command>"]
26    }
27
28    fn execute(&self, args: &str, ctx: &mut CommandContext) -> Result<CommandResult, CliError> {
29        if args.is_empty() {
30            // General help
31            let help_text = "Available commands:\n\
32                 /help  - Show this help message\n\
33                 /clear - Clear chat history\n\
34                 /copy  - Copy last AI output to clipboard\n\
35                 /exit  - Exit the application\n\
36                 /quit  - Exit the application\n\
37                 /session list  - List all sessions\n\
38                 /session new   - Create a new session\n\
39                 /session load  <id> - Load a session by ID\n\
40                 /share         - Copy session to clipboard (markdown)\n\
41                 /share md      - Export session as markdown file\n\
42                 /share json    - Export session as JSON file\n\
43                 \n\
44                 Page Up/Down - Scroll chat history\n\
45                 Ctrl+V/Alt+V - Paste image from clipboard";
46            ctx.add_system_message(help_text.to_string());
47        } else {
48            // Help for specific command (future enhancement)
49            ctx.add_system_message(format!("Help for command: {}", args));
50        }
51
52        Ok(CommandResult::Continue)
53    }
54}
55
56/// Clear command - clears the chat view
57pub struct ClearCommand;
58
59impl Command for ClearCommand {
60    fn name(&self) -> &str {
61        "clear"
62    }
63
64    fn aliases(&self) -> Vec<&str> {
65        vec!["cls"]
66    }
67
68    fn description(&self) -> &str {
69        "Clear the chat history"
70    }
71
72    fn usage(&self) -> Vec<&str> {
73        vec!["/clear"]
74    }
75
76    fn execute(&self, _args: &str, ctx: &mut CommandContext) -> Result<CommandResult, CliError> {
77        ctx.clear_chat();
78        ctx.add_system_message("Chat cleared".to_string());
79        Ok(CommandResult::ClearChat)
80    }
81}
82
83/// Exit command - exits the application
84pub struct ExitCommand;
85
86impl Command for ExitCommand {
87    fn name(&self) -> &str {
88        "exit"
89    }
90
91    fn aliases(&self) -> Vec<&str> {
92        vec!["quit", "q"]
93    }
94
95    fn description(&self) -> &str {
96        "Exit the application"
97    }
98
99    fn usage(&self) -> Vec<&str> {
100        vec!["/exit", "/quit"]
101    }
102
103    fn execute(&self, _args: &str, _ctx: &mut CommandContext) -> Result<CommandResult, CliError> {
104        Ok(CommandResult::Exit)
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_help_command() {
114        assert_eq!(HelpCommand.name(), "help");
115        assert!(HelpCommand.aliases().contains(&"?"));
116    }
117
118    #[test]
119    fn test_clear_command() {
120        assert_eq!(ClearCommand.name(), "clear");
121        assert!(ClearCommand.aliases().contains(&"cls"));
122    }
123
124    #[test]
125    fn test_exit_command() {
126        assert_eq!(ExitCommand.name(), "exit");
127        assert!(ExitCommand.aliases().contains(&"quit"));
128    }
129}