limit_cli/tui/commands/
builtin.rs1use super::{Command, CommandContext, CommandResult};
6use crate::error::CliError;
7
8pub 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 let help_text = "Available commands:\n\
32 /help - Show this help message\n\
33 /clear - Clear chat history\n\
34 /exit - Exit the application\n\
35 /quit - Exit the application\n\
36 /session list - List all sessions\n\
37 /session new - Create a new session\n\
38 /session load <id> - Load a session by ID\n\
39 /share - Copy session to clipboard (markdown)\n\
40 /share md - Export session as markdown file\n\
41 /share json - Export session as JSON file\n\
42 \n\
43 Page Up/Down - Scroll chat history";
44 ctx.add_system_message(help_text.to_string());
45 } else {
46 ctx.add_system_message(format!("Help for command: {}", args));
48 }
49
50 Ok(CommandResult::Continue)
51 }
52}
53
54pub struct ClearCommand;
56
57impl Command for ClearCommand {
58 fn name(&self) -> &str {
59 "clear"
60 }
61
62 fn aliases(&self) -> Vec<&str> {
63 vec!["cls"]
64 }
65
66 fn description(&self) -> &str {
67 "Clear the chat history"
68 }
69
70 fn usage(&self) -> Vec<&str> {
71 vec!["/clear"]
72 }
73
74 fn execute(&self, _args: &str, ctx: &mut CommandContext) -> Result<CommandResult, CliError> {
75 ctx.clear_chat();
76 ctx.add_system_message("Chat cleared".to_string());
77 Ok(CommandResult::ClearChat)
78 }
79}
80
81pub struct ExitCommand;
83
84impl Command for ExitCommand {
85 fn name(&self) -> &str {
86 "exit"
87 }
88
89 fn aliases(&self) -> Vec<&str> {
90 vec!["quit", "q"]
91 }
92
93 fn description(&self) -> &str {
94 "Exit the application"
95 }
96
97 fn usage(&self) -> Vec<&str> {
98 vec!["/exit", "/quit"]
99 }
100
101 fn execute(&self, _args: &str, _ctx: &mut CommandContext) -> Result<CommandResult, CliError> {
102 Ok(CommandResult::Exit)
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn test_help_command() {
112 assert_eq!(HelpCommand.name(), "help");
113 assert!(HelpCommand.aliases().contains(&"?"));
114 }
115
116 #[test]
117 fn test_clear_command() {
118 assert_eq!(ClearCommand.name(), "clear");
119 assert!(ClearCommand.aliases().contains(&"cls"));
120 }
121
122 #[test]
123 fn test_exit_command() {
124 assert_eq!(ExitCommand.name(), "exit");
125 assert!(ExitCommand.aliases().contains(&"quit"));
126 }
127}