Add fuse matcher

This commit is contained in:
Armin Friedl 2021-10-26 09:19:36 +02:00
parent 0f4117b436
commit c382a121a5
6 changed files with 47 additions and 4 deletions

7
Cargo.lock generated
View file

@ -420,6 +420,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
[[package]]
name = "fuse-rust"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d9ff709b73b2baf07e03076ffe6fb9a6e7309e67651d650e2775729a4fc0eaf"
[[package]]
name = "futures-channel"
version = "0.3.17"
@ -1087,6 +1093,7 @@ dependencies = [
"cairo-sys-rs",
"env_logger",
"freedesktop_entry_parser",
"fuse-rust",
"fuzzy-matcher",
"log",
"mlua",

View file

@ -22,3 +22,4 @@ walkdir = "2.3.2"
freedesktop_entry_parser = "1.2.0"
mlua = { version = "0.6", features = ["lua54", "vendored"] }
nix = "0.23.0"
fuse-rust = "0.3.0"

View file

@ -6,7 +6,7 @@ use winit::event_loop::{ControlFlow, EventLoop};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode,
WindowEvent::{CloseRequested, ReceivedCharacter}};
use crate::matcher::SkimMatcher;
use crate::matcher::{FuseMatcher, SkimMatcher};
mod roftl;
mod ui;
@ -24,7 +24,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.add_source(sources::Window::new())
.add_source(sources::Apps::new())
.add_source(sources::LuaHost::new())
.with_matcher(SkimMatcher::new());
.with_matcher(FuseMatcher::new());
debug!{"Source roftl sources"}
roftl.source();

33
src/matcher/fuse.rs Normal file
View file

@ -0,0 +1,33 @@
use std::ops::Range;
use fuse_rust::Fuse;
use crate::roftl::Matcher;
fn flatten_ranges(ranges: Vec<Range<usize>>) -> Vec<usize> {
ranges.into_iter().flat_map(|range| {range.collect::<Vec<usize>>().into_iter()})
.collect()
}
pub struct FuseMatcher {
matcher: Fuse
}
impl FuseMatcher {
pub fn new() -> Box<Self> {
Box::new(FuseMatcher{
matcher: Fuse::default()
})
}
}
impl Matcher for FuseMatcher {
fn try_match(&self, haystack: &str, needle: &str) -> Option<(f64, Vec<usize>)> {
match self.matcher.search_text_in_string(needle, haystack) {
Some(score) => Some((score.score, flatten_ranges(score.ranges))),
None => None
}
}
}

View file

@ -3,3 +3,6 @@ pub use prefix::PrefixMatcher;
mod skim;
pub use skim::SkimMatcher;
mod fuse;
pub use fuse::FuseMatcher;

View file

@ -1,6 +1,5 @@
use std::{collections::HashMap, sync::{Arc, Mutex, atomic::{AtomicUsize, Ordering}}, usize};
use log::debug;
use rayon::{iter::{IndexedParallelIterator, IntoParallelRefIterator, IntoParallelRefMutIterator, ParallelIterator}, slice::ParallelSliceMut};