summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mod.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index f4b98c1..7aa1801 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -119,8 +119,20 @@ pub fn convert_option_string(value: &str) -> Option<String> {
}
}
-pub fn fuzzy_match(choice: &str, pattern: &str) -> Option<i64> {
- SkimMatcherV2::default().fuzzy_match(choice, pattern)
+pub fn fuzzy_filter<T, F>(values: Vec<T>, get: F, pattern: &str) -> Vec<T>
+where
+ F: Fn(&T) -> &str,
+{
+ let matcher = SkimMatcherV2::default();
+ let mut list: Vec<(T, i64)> = values
+ .into_iter()
+ .filter_map(|v| {
+ let score = matcher.fuzzy_match(get(&v), pattern)?;
+ Some((v, score))
+ })
+ .collect();
+ list.sort_unstable_by(|a, b| b.1.cmp(&a.1));
+ list.into_iter().map(|(v, _)| v).collect()
}
pub fn pretty_error(err: &anyhow::Error) -> String {