Skip to content

Commit

Permalink
change fuzzy search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
PonasKovas committed May 16, 2021
1 parent 06b180a commit fe1c4a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
34 changes: 26 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "rlaunch"
version = "1.3.9"
version = "1.3.13"
authors = ["Ponas <mykolas.peteraitis@gmail.com>"]
edition = "2018"

[dependencies]
x11-dl = "2.18.5"
structopt = "0.3.9"
sublime_fuzzy = "0.7"
fuzzy-matcher = "0.3.7"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This should work on all linux distributions and DEs that use X11, but if it does
### Usage

```
rlaunch 1.3.9
rlaunch 1.3.13
A simple and light-weight tool for launching applications and running commands on X11.
USAGE:
Expand Down
29 changes: 15 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod x11;

use applications::{read_applications, Apps};
use arguments::{get_args, Args};
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use std::cmp::{max, min};
use std::process::exit;
use std::process::Command;
Expand All @@ -28,7 +30,7 @@ struct State {
caret_pos: i32,
text: String,
last_text: String,
suggestions: Vec<String>,
suggestions: Vec<(i64, String)>,
selected: u8,
progress: f32,
progress_finished: Option<Instant>,
Expand Down Expand Up @@ -189,13 +191,13 @@ fn render_bar(
// render suggestions
let mut x = (width as f32 * 0.3).floor() as i32;
for (i, suggestion) in state.suggestions.iter().enumerate() {
let name_width = xc.get_text_dimensions(&trc, &suggestion).0 as i32;
let name_width = xc.get_text_dimensions(&trc, &suggestion.1).0 as i32;
// if selected, render rectangle below
if state.selected as usize == i {
xc.draw_rect(&gc, args.color1, x, 0, name_width as u32 + 16, args.height);
}

xc.render_text(&trc, 1, x + 8, text_y, suggestion);
xc.render_text(&trc, 1, x + 8, text_y, &suggestion.1);

x += name_width + 16;
}
Expand All @@ -213,30 +215,29 @@ fn update_suggestions(
}
state.suggestions.clear();
// iterate over application names
// and find those that contain the typed text
// and find those that match the typed text
let mut x = 0;
let max_width = (width as f32 * 0.7).floor() as i32;
let apps_lock = apps.lock().unwrap();
let mut suggestions = Vec::new();
for app in apps_lock.iter() {
let name = &app.0;
if let Some(mtch) =
sublime_fuzzy::best_match(&state.text.to_lowercase(), &name.to_lowercase())
if let Some(mtch) = SkimMatcherV2::default()
.fuzzy_match(name, &state.text.split_whitespace().collect::<String>())
{
suggestions.push((mtch, name.to_string()));
state.suggestions.push((mtch, name.to_string()));
}
}
// sort the suggestion by match scores
suggestions.sort_unstable();
suggestions.reverse();
state.suggestions.sort_unstable();
state.suggestions.reverse();

for suggestion in &suggestions {
for (i, suggestion) in state.suggestions.iter().enumerate() {
let name = &suggestion.1;
let width = xc.get_text_dimensions(&trc, &name).0 as i32;
if x + width <= max_width {
x += width + 16;
state.suggestions.push(name.to_string());
} else {
state.suggestions.truncate(i + 1);
break;
}
}
Expand Down Expand Up @@ -298,7 +299,7 @@ fn handle_event(
} else {
let apps_lock = apps.lock().unwrap();
let app = &apps_lock
.get(&state.suggestions[state.selected as usize])
.get(&state.suggestions[state.selected as usize].1)
.unwrap();
if app.show_terminal {
run_command(&format!("{} -e \"{}\"", terminal, app.exec));
Expand All @@ -310,7 +311,7 @@ fn handle_event(
}
KEY_TAB => {
if !state.suggestions.is_empty() {
state.text = state.suggestions[state.selected as usize].to_string();
state.text = state.suggestions[state.selected as usize].1.to_string();
state.caret_pos = state.text.len() as i32;
state.selected = 0;
}
Expand Down

0 comments on commit fe1c4a7

Please sign in to comment.