ratatui_toolkit/widgets/ai_chat/methods/
mod.rs

1use crate::widgets::ai_chat::state::{InputState, MessageStore};
2use ratatui::style::{Color, Modifier, Style};
3
4use crate::widgets::ai_chat::AIChat;
5
6impl<'a> AIChat<'a> {
7    /// Register a command.
8    pub fn register_command(&mut self, command: String) {
9        if !self.commands.contains(&command) {
10            self.commands.push(command);
11        }
12    }
13
14    /// Get available commands.
15    pub fn commands(&self) -> &[String] {
16        &self.commands
17    }
18
19    /// Get filtered commands matching the current command input.
20    pub fn filtered_commands(&self) -> Vec<String> {
21        let command_lower = self.input.command().to_lowercase();
22        self.commands
23            .iter()
24            .filter(|c| c.to_lowercase().starts_with(&format!("/{}", command_lower)))
25            .cloned()
26            .collect()
27    }
28
29    /// Get selected command index.
30    pub fn selected_command_index(&self) -> usize {
31        self.selected_command_index
32    }
33
34    /// Set selected command index.
35    pub fn set_selected_command_index(&mut self, index: usize) {
36        self.selected_command_index = index;
37    }
38
39    /// Handle a command string (e.g., "/clear").
40    ///
41    /// Returns true if command was handled, false if unknown.
42    pub fn handle_command(&mut self, command: &str) -> bool {
43        match command {
44            "/clear" => {
45                self.messages.clear();
46                true
47            }
48            _ => false,
49        }
50    }
51
52    /// Set the loading state.
53    pub fn set_loading(&mut self, loading: bool) {
54        self.is_loading = loading;
55    }
56
57    /// Get the loading state.
58    pub fn is_loading(&self) -> bool {
59        self.is_loading
60    }
61
62    /// Set user message style.
63    pub fn with_user_message_style(mut self, style: Style) -> Self {
64        self.user_message_style = style;
65        self
66    }
67
68    /// Set AI message style.
69    pub fn with_ai_message_style(mut self, style: Style) -> Self {
70        self.ai_message_style = style;
71        self
72    }
73
74    /// Set input style.
75    pub fn with_input_style(mut self, style: Style) -> Self {
76        self.input_style = style;
77        self
78    }
79
80    /// Set input prompt text.
81    pub fn with_prompt(mut self, prompt: String) -> Self {
82        self.input_prompt = prompt;
83        self
84    }
85}