Skip to content

Commit cd52548

Browse files
authored
Merge pull request #314 from MystikoLab/fuzzysearch
Switch to Fuzzysearch
2 parents 3c36441 + 7df308a commit cd52548

6 files changed

Lines changed: 34 additions & 21 deletions

File tree

‎Cargo.lock‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ image = { version = "0.25.9", features = ["tiff"] }
2121
libc = "0.2.180"
2222
log = "0.4.29"
2323
minreq = { version = "2.14.1", features = ["https"] }
24+
nucleo-matcher = "0.3.1"
2425
objc2 = "0.6.3"
2526
objc2-app-kit = { version = "0.3.2", features = ["NSImage", "NSScreen"] }
2627
objc2-application-services = { version = "0.3.2", default-features = false, features = ["HIServices", "Processes"] }

‎src/app/tile.rs‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use iced::{
2525
use iced::{event, window};
2626

2727
use log::{info, warn};
28+
use nucleo_matcher::pattern::{CaseMatching, Normalization, Pattern};
29+
use nucleo_matcher::{Matcher, Utf32Str};
2830
use objc2::rc::Retained;
2931
use objc2_app_kit::NSRunningApplication;
3032
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
@@ -34,6 +36,7 @@ use tokio::io::{AsyncBufReadExt, AsyncRead};
3436
use tray_icon::TrayIcon;
3537
use url::Url;
3638

39+
use std::cell::RefCell;
3740
use std::collections::HashMap;
3841
use std::fmt::Debug;
3942
use std::time::Duration;
@@ -56,15 +59,22 @@ struct AppIndex {
5659
impl AppIndex {
5760
/// Search for an element in the index that starts with the provided prefix
5861
fn search_prefix<'a>(&'a self, prefix: &'a str) -> impl ParallelIterator<Item = &'a App> + 'a {
62+
let pattern = Pattern::parse(prefix, CaseMatching::Ignore, Normalization::Smart);
63+
5964
self.by_name.par_iter().filter_map(move |(name, app)| {
60-
if name.starts_with(prefix)
61-
|| name.contains(format!(" {prefix}").as_str())
62-
|| name.contains(format!("-{prefix}").as_str())
63-
{
64-
Some(app)
65-
} else {
66-
None
65+
thread_local! {
66+
static MATCHER: RefCell<Matcher> = RefCell::new(
67+
Matcher::new(nucleo_matcher::Config::DEFAULT.match_paths())
68+
);
6769
}
70+
71+
MATCHER.with(|m| {
72+
let mut buf = Vec::new();
73+
let haystack = Utf32Str::new(name, &mut buf);
74+
pattern
75+
.score(haystack, &mut m.borrow_mut())
76+
.map(|_score| app)
77+
})
6878
})
6979
}
7080

@@ -706,7 +716,11 @@ mod tests {
706716
.map(|app| app.display_name.clone())
707717
.collect();
708718

709-
assert_eq!(prefix_results, vec!["Safari".to_string()]);
719+
assert!(
720+
prefix_results.contains(&"Visual Studio Code".to_string())
721+
&& prefix_results.contains(&"Signal Desktop".to_string())
722+
&& prefix_results.contains(&"Safari".to_string()),
723+
);
710724
assert_eq!(spaced_results, vec!["Visual Studio Code".to_string()]);
711725
assert_eq!(hyphen_results, vec!["Signal Desktop".to_string()]);
712726
}

‎src/app/tile/elm.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::app::pages::settings::settings_page;
2121
use crate::app::tile::{AppIndex, Hotkeys};
2222
use crate::app::{DEFAULT_WINDOW_HEIGHT, HotkeyCapture, SettingsTab, ToApp, ToApps};
2323
use crate::config::Theme;
24-
use crate::database::load_clipboard;
2524
use crate::debounce::Debouncer;
2625
use crate::platform::macos::events::Event;
2726
use crate::styles::{

‎src/clipboard.rs‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,4 @@ mod tests {
109109
ClipBoardContentType::Text("world".to_string())
110110
);
111111
}
112-
113-
#[test]
114-
fn clipboard_to_app_truncates_and_uses_first_line_for_display() {
115-
let item =
116-
ClipBoardContentType::Text("abcdefghijklmnopqrstuvwxyz\nsecond line".to_string());
117-
118-
// let app = item.to_app();
119-
120-
// assert_eq!(app.display_name, "abcdefghijklmnopqrstuvwxy");
121-
// assert_eq!(app.search_name, "abcdefghijklmnopqrstuvwxy");
122-
// assert_eq!(app.desc, "Clipboard Item");
123-
}
124112
}
File renamed without changes.

0 commit comments

Comments
 (0)