1use super::selectors::Selector;
2
3const MAX_COMMAND_COMPLETIONS: usize = 16;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct CommandCompletion {
8 options: Vec<String>,
9 selected: usize,
10}
11
12impl CommandCompletion {
13 #[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 #[must_use]
36 pub fn options(&self) -> &[String] {
37 &self.options
38 }
39
40 #[must_use]
42 pub fn selected(&self) -> Option<&str> {
43 self.options.get(self.selected).map(String::as_str)
44 }
45
46 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 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#[derive(Debug, Clone, PartialEq, Eq)]
70pub enum Overlay {
71 Selector(Selector),
73 CommandCompletion(CommandCompletion),
75}
76
77impl Overlay {
78 #[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 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 #[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 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}