|
| 1 | +use abi_stable::std_types::ROption; |
| 2 | +use anyrun_plugin::Match; |
| 3 | +use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; |
| 4 | +use num_enum::{IntoPrimitive, TryFromPrimitive}; |
| 5 | + |
| 6 | +#[derive(Clone, Copy, IntoPrimitive, TryFromPrimitive)] |
| 7 | +#[repr(u64)] |
| 8 | +pub enum PowerAction { |
| 9 | + Lock, |
| 10 | + Logout, |
| 11 | + Poweroff, |
| 12 | + Reboot, |
| 13 | + Suspend, |
| 14 | + Hibernate, |
| 15 | +} |
| 16 | + |
| 17 | +impl PowerAction { |
| 18 | + const VALUES: [Self; 6] = [ |
| 19 | + Self::Lock, |
| 20 | + Self::Logout, |
| 21 | + Self::Poweroff, |
| 22 | + Self::Reboot, |
| 23 | + Self::Suspend, |
| 24 | + Self::Hibernate, |
| 25 | + ]; |
| 26 | + |
| 27 | + pub const fn get_title(&self) -> &str { |
| 28 | + match self { |
| 29 | + Self::Lock => "Lock", |
| 30 | + Self::Logout => "Log out", |
| 31 | + Self::Poweroff => "Power off", |
| 32 | + Self::Reboot => "Reboot", |
| 33 | + Self::Suspend => "Suspend", |
| 34 | + Self::Hibernate => "Hibernate", |
| 35 | + } |
| 36 | + } |
| 37 | + pub const fn get_description(&self) -> &str { |
| 38 | + match self { |
| 39 | + Self::Lock => "Lock the session screen", |
| 40 | + Self::Logout => "Terminate the session", |
| 41 | + Self::Poweroff => "Shut down the system", |
| 42 | + Self::Reboot => "Restart the system", |
| 43 | + Self::Suspend => "Suspend the system to RAM", |
| 44 | + Self::Hibernate => "Suspend the system to disk", |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub const fn get_icon_name(&self) -> &str { |
| 49 | + match self { |
| 50 | + Self::Lock => "system-lock-screen", |
| 51 | + Self::Logout => "system-log-out", |
| 52 | + Self::Poweroff => "system-shutdown", |
| 53 | + Self::Reboot => "system-reboot", |
| 54 | + Self::Suspend => "system-suspend", |
| 55 | + Self::Hibernate => "system-suspend-hibernate", |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + pub fn as_match(self) -> Match { |
| 60 | + Match { |
| 61 | + title: self.get_title().into(), |
| 62 | + icon: ROption::RSome(self.get_icon_name().into()), |
| 63 | + use_pango: false, |
| 64 | + description: ROption::RSome(self.get_description().into()), |
| 65 | + id: ROption::RSome(self.into()), |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + pub fn get_fuzzy_matching_values(phrase: &str) -> impl Iterator<Item = Self> { |
| 70 | + let fuzzy_matcher = SkimMatcherV2::default().ignore_case(); |
| 71 | + let mut matches_with_scores = Self::VALUES |
| 72 | + .into_iter() |
| 73 | + .filter_map(|action| { |
| 74 | + action |
| 75 | + .get_fuzzy_score(&fuzzy_matcher, phrase) |
| 76 | + .map(|score| (action, score)) |
| 77 | + }) |
| 78 | + .collect::<Vec<_>>(); |
| 79 | + matches_with_scores.sort_by_key(|(_action, score)| *score); |
| 80 | + matches_with_scores |
| 81 | + .into_iter() |
| 82 | + .map(|(action, _score)| action) |
| 83 | + } |
| 84 | + |
| 85 | + fn get_fuzzy_score(self, matcher: &impl FuzzyMatcher, phrase: &str) -> Option<i64> { |
| 86 | + matcher |
| 87 | + .fuzzy_match(self.get_title(), phrase) |
| 88 | + .max(matcher.fuzzy_match(self.get_description(), phrase)) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(PartialEq, Eq, IntoPrimitive, TryFromPrimitive)] |
| 93 | +#[repr(u64)] |
| 94 | +pub enum ConfirmAction { |
| 95 | + Confirm, |
| 96 | + Cancel, |
| 97 | +} |
| 98 | + |
| 99 | +impl ConfirmAction { |
| 100 | + pub fn is_confirmed(&self) -> bool { |
| 101 | + *self == Self::Confirm |
| 102 | + } |
| 103 | +} |
0 commit comments