diff --git a/Cargo.lock b/Cargo.lock index 422bc06..955e8ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,6 +96,11 @@ name = "downcast-rs" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fuzzy-matcher" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "1.3.0" @@ -407,6 +412,7 @@ dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "dbus 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "fuzzy-matcher 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "libpulse-binding 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "memmap 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -441,6 +447,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum dbus 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "48b5f0f36f1eebe901b0e6bee369a77ed3396334bf3f09abd46454a576f71819" "checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a" "checksum downcast-rs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f2b92dfd5c2f75260cbf750572f95d387e7ca0ba5e3fbe9e1a33f23025be020f" +"checksum fuzzy-matcher 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2db8c952765d7250f5fa509db7f6510ceef4be8672affc9bdbeffa2695923f67" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb" "checksum libdbus-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "69382cb3be797cfe62ba89ac5e594a0e3022914ff44867ffbcfc03daa24eebd7" diff --git a/Cargo.toml b/Cargo.toml index 6031616..ee8c0d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ wayland-client = { version = "0.23", features = ["dlopen"] } wayland-protocols = { version = "0.23", features = ["client", "unstable_protocols"] } libpulse-binding = { version = "2.6.0"} dbus = "0.6" -atty = "0.2" \ No newline at end of file +atty = "0.2" +fuzzy-matcher = "0.2" \ No newline at end of file diff --git a/src/launcher.rs b/src/launcher.rs index cd1d617..8bd12a2 100644 --- a/src/launcher.rs +++ b/src/launcher.rs @@ -7,9 +7,8 @@ use std::cmp::Ordering; use std::io::Read; use atty::Stream; - use chrono::{DateTime, Local}; - +use fuzzy_matcher::skim::{fuzzy_indices, fuzzy_match}; use smithay_client_toolkit::keyboard::keysyms; pub struct Launcher { @@ -70,7 +69,9 @@ impl ModuleImpl for Launcher { if self.matches.len() == 0 && self.cur.len() > 0 { draw_text( ROBOTO_REGULAR, - &mut buf.subdimensions((64, 0, width_remaining as u32, 32)).unwrap(), + &mut buf + .subdimensions((64, 0, width_remaining as u32, 32)) + .unwrap(), bg, &Color::new(1.0, 0.5, 0.5, 1.0), 32.0, @@ -84,11 +85,11 @@ impl ModuleImpl for Launcher { Err(_) => break, }; let size = if idx == self.offset && self.cur.len() > 0 { - let l = self.cur.len(); - let off = m.to_lowercase().find(&self.cur.to_lowercase()).unwrap(); + let (_, indices) = + fuzzy_indices(&m.to_lowercase(), &self.cur.to_lowercase()).unwrap(); let mut colors = Vec::with_capacity(m.len()); for pos in 0..m.len() { - if pos >= off && pos < off + l { + if indices.contains(&pos) { colors.push(Color::new(1.0, 1.0, 1.0, 1.0)); } else { colors.push(Color::new(0.75, 0.75, 0.75, 1.0)); @@ -123,10 +124,10 @@ impl ModuleImpl for Launcher { let mut m = self .options .iter() - .map(|x| (x.to_lowercase().find(&self.cur.to_lowercase()), x)) + .map(|x| (fuzzy_match(&x.to_lowercase(), &self.cur.to_lowercase()), x)) .filter(|(x, _)| x.is_some()) .map(|(x, y)| (x.unwrap(), y.to_string())) - .collect::>(); + .collect::>(); m.sort_by(|(x1, y1), (x2, y2)| { if x1 < x2 {