44
55use applications:: { read_applications, Apps } ;
66use arguments:: { get_args, Args } ;
7+ use fuzzy_matcher:: skim:: SkimMatcherV2 ;
8+ use fuzzy_matcher:: FuzzyMatcher ;
79use std:: cmp:: { max, min} ;
810use std:: process:: exit;
911use std:: process:: Command ;
@@ -28,7 +30,7 @@ struct State {
2830 caret_pos : i32 ,
2931 text : String ,
3032 last_text : String ,
31- suggestions : Vec < String > ,
33+ suggestions : Vec < ( i64 , String ) > ,
3234 selected : u8 ,
3335 progress : f32 ,
3436 progress_finished : Option < Instant > ,
@@ -189,13 +191,13 @@ fn render_bar(
189191 // render suggestions
190192 let mut x = ( width as f32 * 0.3 ) . floor ( ) as i32 ;
191193 for ( i, suggestion) in state. suggestions . iter ( ) . enumerate ( ) {
192- let name_width = xc. get_text_dimensions ( & trc, & suggestion) . 0 as i32 ;
194+ let name_width = xc. get_text_dimensions ( & trc, & suggestion. 1 ) . 0 as i32 ;
193195 // if selected, render rectangle below
194196 if state. selected as usize == i {
195197 xc. draw_rect ( & gc, args. color1 , x, 0 , name_width as u32 + 16 , args. height ) ;
196198 }
197199
198- xc. render_text ( & trc, 1 , x + 8 , text_y, suggestion) ;
200+ xc. render_text ( & trc, 1 , x + 8 , text_y, & suggestion. 1 ) ;
199201
200202 x += name_width + 16 ;
201203 }
@@ -213,30 +215,29 @@ fn update_suggestions(
213215 }
214216 state. suggestions . clear ( ) ;
215217 // iterate over application names
216- // and find those that contain the typed text
218+ // and find those that match the typed text
217219 let mut x = 0 ;
218220 let max_width = ( width as f32 * 0.7 ) . floor ( ) as i32 ;
219221 let apps_lock = apps. lock ( ) . unwrap ( ) ;
220- let mut suggestions = Vec :: new ( ) ;
221222 for app in apps_lock. iter ( ) {
222223 let name = & app. 0 ;
223- if let Some ( mtch) =
224- sublime_fuzzy :: best_match ( & state. text . to_lowercase ( ) , & name . to_lowercase ( ) )
224+ if let Some ( mtch) = SkimMatcherV2 :: default ( )
225+ . fuzzy_match ( name , & state. text . split_whitespace ( ) . collect :: < String > ( ) )
225226 {
226- suggestions. push ( ( mtch, name. to_string ( ) ) ) ;
227+ state . suggestions . push ( ( mtch, name. to_string ( ) ) ) ;
227228 }
228229 }
229230 // sort the suggestion by match scores
230- suggestions. sort_unstable ( ) ;
231- suggestions. reverse ( ) ;
231+ state . suggestions . sort_unstable ( ) ;
232+ state . suggestions . reverse ( ) ;
232233
233- for suggestion in & suggestions {
234+ for ( i , suggestion) in state . suggestions . iter ( ) . enumerate ( ) {
234235 let name = & suggestion. 1 ;
235236 let width = xc. get_text_dimensions ( & trc, & name) . 0 as i32 ;
236237 if x + width <= max_width {
237238 x += width + 16 ;
238- state. suggestions . push ( name. to_string ( ) ) ;
239239 } else {
240+ state. suggestions . truncate ( i + 1 ) ;
240241 break ;
241242 }
242243 }
@@ -298,7 +299,7 @@ fn handle_event(
298299 } else {
299300 let apps_lock = apps. lock ( ) . unwrap ( ) ;
300301 let app = & apps_lock
301- . get ( & state. suggestions [ state. selected as usize ] )
302+ . get ( & state. suggestions [ state. selected as usize ] . 1 )
302303 . unwrap ( ) ;
303304 if app. show_terminal {
304305 run_command ( & format ! ( "{} -e \" {}\" " , terminal, app. exec) ) ;
@@ -310,7 +311,7 @@ fn handle_event(
310311 }
311312 KEY_TAB => {
312313 if !state. suggestions . is_empty ( ) {
313- state. text = state. suggestions [ state. selected as usize ] . to_string ( ) ;
314+ state. text = state. suggestions [ state. selected as usize ] . 1 . to_string ( ) ;
314315 state. caret_pos = state. text . len ( ) as i32 ;
315316 state. selected = 0 ;
316317 }
0 commit comments