From cebc052f0df6d0c497f438702588d64a5928f14e Mon Sep 17 00:00:00 2001 From: Andrej Benz Date: Sat, 19 Aug 2023 16:52:53 +0200 Subject: [PATCH] feat: changed applications matching/weighting (#88) --- plugins/applications/src/lib.rs | 26 +++++++++++++++++++------- plugins/applications/src/scrubber.rs | 5 ++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/plugins/applications/src/lib.rs b/plugins/applications/src/lib.rs index 7b3a74a..30cebb8 100644 --- a/plugins/applications/src/lib.rs +++ b/plugins/applications/src/lib.rs @@ -105,13 +105,25 @@ pub fn get_matches(input: RString, state: &State) -> RVec { .entries .iter() .filter_map(|(entry, id)| { - let score: i64 = matcher.fuzzy_match(&entry.name, &input).unwrap_or(0) - + matcher.fuzzy_match(&entry.exec, &input).unwrap_or(0) - + entry - .keywords - .iter() - .map(|keyword| matcher.fuzzy_match(keyword, &input).unwrap_or(0)) - .sum::(); + let app_score = match &entry.desc { + None => matcher.fuzzy_match(&entry.name, &input).unwrap_or(0), + Some(val) => matcher + .fuzzy_match(&format!("{} {}", &val, &entry.name).to_string(), &input) + .unwrap_or(0), + }; + + let keyword_score = entry + .keywords + .iter() + .map(|keyword| matcher.fuzzy_match(keyword, &input).unwrap_or(0)) + .sum::(); + + let mut score = (app_score * 25 + keyword_score) - entry.offset; + + // prioritize actions + if entry.desc.is_some() { + score = score * 2; + } if score > 0 { Some((entry, *id, score)) diff --git a/plugins/applications/src/scrubber.rs b/plugins/applications/src/scrubber.rs index 1423c63..d72065c 100644 --- a/plugins/applications/src/scrubber.rs +++ b/plugins/applications/src/scrubber.rs @@ -11,6 +11,7 @@ pub struct DesktopEntry { pub desc: Option, pub icon: String, pub term: bool, + pub offset: i64, } const FIELD_CODE_LIST: &[&str] = &[ @@ -95,6 +96,7 @@ impl DesktopEntry { .get("Terminal") .map(|val| val.to_lowercase() == "true") .unwrap_or(false), + offset: 0, }) } else { None @@ -108,7 +110,7 @@ impl DesktopEntry { }; if config.desktop_actions { - for section in new_sections { + for (i, section) in new_sections.iter().enumerate() { let mut map = HashMap::new(); for line in section.iter().skip(1) { @@ -150,6 +152,7 @@ impl DesktopEntry { .get("Terminal") .map(|val| val.to_lowercase() == "true") .unwrap_or(false), + offset: i as i64, }) } }