@@ -25,6 +25,8 @@ use iced::{
2525use iced:: { event, window} ;
2626
2727use log:: { info, warn} ;
28+ use nucleo_matcher:: pattern:: { CaseMatching , Normalization , Pattern } ;
29+ use nucleo_matcher:: { Matcher , Utf32Str } ;
2830use objc2:: rc:: Retained ;
2931use objc2_app_kit:: NSRunningApplication ;
3032use rayon:: iter:: { IntoParallelRefIterator , ParallelIterator } ;
@@ -34,6 +36,7 @@ use tokio::io::{AsyncBufReadExt, AsyncRead};
3436use tray_icon:: TrayIcon ;
3537use url:: Url ;
3638
39+ use std:: cell:: RefCell ;
3740use std:: collections:: HashMap ;
3841use std:: fmt:: Debug ;
3942use std:: time:: Duration ;
@@ -56,15 +59,22 @@ struct AppIndex {
5659impl 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 }
0 commit comments