use super::selectors::Selector;
const MAX_COMMAND_COMPLETIONS: usize = 16;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandCompletion {
options: Vec<String>,
selected: usize,
}
impl CommandCompletion {
#[must_use]
pub fn new<I>(options: I) -> Self
where
I: IntoIterator<Item = String>,
{
let options = options
.into_iter()
.filter(|option| {
option.starts_with('/')
&& option.len() <= 512
&& !option.chars().any(char::is_control)
})
.take(MAX_COMMAND_COMPLETIONS)
.collect();
Self {
options,
selected: 0,
}
}
#[must_use]
pub fn options(&self) -> &[String] {
&self.options
}
#[must_use]
pub fn selected(&self) -> Option<&str> {
self.options.get(self.selected).map(String::as_str)
}
pub fn move_next(&mut self) {
if !self.options.is_empty() {
self.selected = (self.selected + 1) % self.options.len();
}
}
pub fn move_previous(&mut self) {
if !self.options.is_empty() {
self.selected = self
.selected
.checked_sub(1)
.unwrap_or(self.options.len() - 1);
}
}
pub(crate) fn should_show(&self, text: &str) -> bool {
self.selected().is_some() && !self.options.iter().any(|option| option == text)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Overlay {
Selector(Selector),
CommandCompletion(CommandCompletion),
}
impl Overlay {
#[must_use]
pub const fn selector(&self) -> Option<&Selector> {
match self {
Self::Selector(selector) => Some(selector),
Self::CommandCompletion(_) => None,
}
}
pub(crate) fn selector_mut(&mut self) -> Option<&mut Selector> {
match self {
Self::Selector(selector) => Some(selector),
Self::CommandCompletion(_) => None,
}
}
#[must_use]
pub const fn command_completion(&self) -> Option<&CommandCompletion> {
match self {
Self::Selector(_) => None,
Self::CommandCompletion(completion) => Some(completion),
}
}
pub(crate) fn command_completion_mut(&mut self) -> Option<&mut CommandCompletion> {
match self {
Self::Selector(_) => None,
Self::CommandCompletion(completion) => Some(completion),
}
}
}