use crate::utils::{fuzzy_match, FuzzyMatch};
#[derive(Clone, Debug)]
pub struct Command {
pub id: String,
pub label: String,
pub description: Option<String>,
pub shortcut: Option<String>,
pub category: Option<String>,
pub icon: Option<char>,
pub recent: bool,
pub pinned: bool,
}
impl Command {
pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self {
Self {
id: id.into(),
label: label.into(),
description: None,
shortcut: None,
category: None,
icon: None,
recent: false,
pinned: false,
}
}
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
pub fn shortcut(mut self, shortcut: impl Into<String>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
pub fn category(mut self, category: impl Into<String>) -> Self {
self.category = Some(category.into());
self
}
pub fn icon(mut self, icon: char) -> Self {
self.icon = Some(icon);
self
}
pub fn recent(mut self) -> Self {
self.recent = true;
self
}
pub fn pinned(mut self) -> Self {
self.pinned = true;
self
}
pub fn matches(&self, query: &str) -> bool {
self.fuzzy_match(query).is_some()
}
pub fn fuzzy_match(&self, query: &str) -> Option<FuzzyMatch> {
if query.is_empty() {
return Some(FuzzyMatch::new(0, vec![]));
}
if let Some(m) = fuzzy_match(query, &self.label) {
return Some(m);
}
if let Some(ref desc) = self.description {
if let Some(m) = fuzzy_match(query, desc) {
return Some(m);
}
}
if let Some(ref cat) = self.category {
if let Some(m) = fuzzy_match(query, cat) {
return Some(m);
}
}
None
}
pub fn match_score(&self, query: &str) -> i32 {
if query.is_empty() {
return if self.pinned {
100
} else if self.recent {
50
} else {
0
};
}
let mut score = 0;
if let Some(m) = fuzzy_match(query, &self.label) {
score += m.score;
}
if self.pinned {
score += 50;
}
if self.recent {
score += 25;
}
score
}
}