Skip to main content

tea_cli/tui/
overlay.rs

1use super::selectors::Selector;
2
3const MAX_COMMAND_COMPLETIONS: usize = 16;
4
5/// One bounded slash-command completion menu owned by the local TUI.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct CommandCompletion {
8    options: Vec<String>,
9    selected: usize,
10}
11
12impl CommandCompletion {
13    /// Creates a bounded deterministic completion list.
14    #[must_use]
15    pub fn new<I>(options: I) -> Self
16    where
17        I: IntoIterator<Item = String>,
18    {
19        let options = options
20            .into_iter()
21            .filter(|option| {
22                option.starts_with('/')
23                    && option.len() <= 512
24                    && !option.chars().any(char::is_control)
25            })
26            .take(MAX_COMMAND_COMPLETIONS)
27            .collect();
28        Self {
29            options,
30            selected: 0,
31        }
32    }
33
34    /// Returns all visible completion options in stable order.
35    #[must_use]
36    pub fn options(&self) -> &[String] {
37        &self.options
38    }
39
40    /// Returns the explicit option currently selected by the user.
41    #[must_use]
42    pub fn selected(&self) -> Option<&str> {
43        self.options.get(self.selected).map(String::as_str)
44    }
45
46    /// Moves to the next option with wraparound.
47    pub fn move_next(&mut self) {
48        if !self.options.is_empty() {
49            self.selected = (self.selected + 1) % self.options.len();
50        }
51    }
52
53    /// Moves to the previous option with wraparound.
54    pub fn move_previous(&mut self) {
55        if !self.options.is_empty() {
56            self.selected = self
57                .selected
58                .checked_sub(1)
59                .unwrap_or(self.options.len() - 1);
60        }
61    }
62
63    pub(crate) fn should_show(&self, text: &str) -> bool {
64        self.selected().is_some() && !self.options.iter().any(|option| option == text)
65    }
66}
67
68/// The one top-priority local interaction view currently visible above composer input.
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub enum Overlay {
71    /// A typed selector backed by host queries.
72    Selector(Selector),
73    /// A slash-command completion menu that edits the composer draft.
74    CommandCompletion(CommandCompletion),
75}
76
77impl Overlay {
78    /// Returns the selector when this overlay owns a selector view.
79    #[must_use]
80    pub const fn selector(&self) -> Option<&Selector> {
81        match self {
82            Self::Selector(selector) => Some(selector),
83            Self::CommandCompletion(_) => None,
84        }
85    }
86
87    /// Returns mutable selector state when this overlay owns it.
88    pub(crate) fn selector_mut(&mut self) -> Option<&mut Selector> {
89        match self {
90            Self::Selector(selector) => Some(selector),
91            Self::CommandCompletion(_) => None,
92        }
93    }
94
95    /// Returns the command menu when this overlay owns it.
96    #[must_use]
97    pub const fn command_completion(&self) -> Option<&CommandCompletion> {
98        match self {
99            Self::Selector(_) => None,
100            Self::CommandCompletion(completion) => Some(completion),
101        }
102    }
103
104    /// Returns mutable command-menu state when this overlay owns it.
105    pub(crate) fn command_completion_mut(&mut self) -> Option<&mut CommandCompletion> {
106        match self {
107            Self::Selector(_) => None,
108            Self::CommandCompletion(completion) => Some(completion),
109        }
110    }
111}