Skip to content

Commit 842ff1e

Browse files
authored
Merge pull request #190 from unsecretised/fuzzy-search
add fuzzy search
2 parents efd0471 + 634a74a commit 842ff1e

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/app/tile.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ use objc2_app_kit::NSRunningApplication;
2828
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
2929
use tray_icon::TrayIcon;
3030

31+
use std::collections::HashMap;
3132
use std::fmt::Debug;
3233
use std::fs;
33-
use std::ops::Bound;
34+
use std::path::Path;
3435
use std::str::FromStr;
3536
use std::time::Duration;
36-
use std::{collections::BTreeMap, path::Path};
3737

3838
/// This is a wrapper around the sender to disable dropping
3939
#[derive(Clone, Debug)]
@@ -47,17 +47,21 @@ impl Drop for ExtSender {
4747
/// All the indexed apps that rustcast can search for
4848
#[derive(Clone, Debug)]
4949
struct AppIndex {
50-
by_name: BTreeMap<String, App>,
50+
by_name: HashMap<String, App>,
5151
}
5252

5353
impl AppIndex {
5454
/// Search for an element in the index that starts with the provided prefix
55-
fn search_prefix<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item = &'a App> + 'a {
56-
self.by_name
57-
.range::<str, _>((Bound::Included(prefix), Bound::Unbounded))
58-
.take_while(move |(k, _)| k.starts_with(prefix))
59-
.map(|(_, v)| v)
55+
fn search_prefix<'a>(&'a self, prefix: &'a str) -> impl ParallelIterator<Item = &'a App> + 'a {
56+
self.by_name.par_iter().filter_map(move |(name, app)| {
57+
if name.starts_with(prefix) || name.contains(format!(" {prefix}").as_str()) {
58+
Some(app)
59+
} else {
60+
None
61+
}
62+
})
6063
}
64+
6165
fn update_ranking(&mut self, name: &str) {
6266
let app = match self.by_name.get_mut(name) {
6367
Some(a) => a,
@@ -69,18 +73,18 @@ impl AppIndex {
6973

7074
fn empty() -> AppIndex {
7175
AppIndex {
72-
by_name: BTreeMap::new(),
76+
by_name: HashMap::new(),
7377
}
7478
}
7579

7680
/// Factory function for creating
7781
pub fn from_apps(options: Vec<App>) -> Self {
78-
let mut bmap = BTreeMap::new();
82+
let mut hmap = HashMap::new();
7983
for app in options {
80-
bmap.insert(app.search_name.clone(), app);
84+
hmap.insert(app.search_name.clone(), app);
8185
}
8286

83-
AppIndex { by_name: bmap }
87+
AppIndex { by_name: hmap }
8488
}
8589
}
8690

src/app/tile/elm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use iced::{Element, Task};
1111
use iced::{Length::Fill, widget::text_input};
1212

1313
use log::info;
14+
use rayon::iter::ParallelIterator;
1415
use rayon::slice::ParallelSliceMut;
1516

1617
use crate::app::pages::emoji::emoji_page;

src/app/tile/update.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use iced::widget::operation::AbsoluteOffset;
1111
use iced::window;
1212
use iced::window::Id;
1313
use log::info;
14+
use rayon::iter::ParallelIterator;
1415
use rayon::slice::ParallelSliceMut;
1516

1617
use crate::app::ToApp;

0 commit comments

Comments
 (0)