Skip to content

Commit fe1c4a7

Browse files
committed
change fuzzy search algorithm
1 parent 06b180a commit fe1c4a7

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "rlaunch"
3-
version = "1.3.9"
3+
version = "1.3.13"
44
authors = ["Ponas <mykolas.peteraitis@gmail.com>"]
55
edition = "2018"
66

77
[dependencies]
88
x11-dl = "2.18.5"
99
structopt = "0.3.9"
10-
sublime_fuzzy = "0.7"
10+
fuzzy-matcher = "0.3.7"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This should work on all linux distributions and DEs that use X11, but if it does
1212
### Usage
1313

1414
```
15-
rlaunch 1.3.9
15+
rlaunch 1.3.13
1616
A simple and light-weight tool for launching applications and running commands on X11.
1717
1818
USAGE:

src/main.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ mod x11;
44

55
use applications::{read_applications, Apps};
66
use arguments::{get_args, Args};
7+
use fuzzy_matcher::skim::SkimMatcherV2;
8+
use fuzzy_matcher::FuzzyMatcher;
79
use std::cmp::{max, min};
810
use std::process::exit;
911
use 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

Comments
 (0)