diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 9b8e2fb..64d0580 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -144,6 +144,7 @@ name = "apographe" version = "0.1.0" dependencies = [ "comrak", + "fuzzy-matcher", "html_tag", "serde", "serde_json", @@ -1236,6 +1237,15 @@ dependencies = [ "slab", ] +[[package]] +name = "fuzzy-matcher" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54614a3312934d066701a80f20f15fa3b56d67ac7722b39eea5b4c9dd1d66c94" +dependencies = [ + "thread_local", +] + [[package]] name = "fxhash" version = "0.2.1" @@ -4149,6 +4159,16 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.36" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 29393c6..739f12b 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -27,4 +27,4 @@ tauri-plugin-fs = "2" tauri-plugin-log = "2" shellexpand = "3.1.0" html_tag = "0.1.3" - +fuzzy-matcher = "0.3" diff --git a/src-tauri/src/search.rs b/src-tauri/src/search.rs index fd3cf03..b2a97ba 100644 --- a/src-tauri/src/search.rs +++ b/src-tauri/src/search.rs @@ -1,3 +1,4 @@ +use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use html_tag::HtmlTag; use std::{ fs::{self, DirEntry}, @@ -12,6 +13,55 @@ pub fn search_files(basepath: &str, searchstring: &str, filter: Vec) -> Err(_) => Vec::new(), } } +pub fn _search_files(path: &Path, searchstring: &str, filter: &Vec) -> Vec { + let mut search_results: Vec = Vec::new(); + if let Ok(entries) = fs::read_dir(path) { + for dir_entry_res in entries { + if let Ok(dir_entry) = dir_entry_res { + if let Ok(metadata) = fs::metadata(dir_entry.path()) { + if metadata.is_file() { + if entry_match(&dir_entry, searchstring, filter) { + let div = search_result_div_from_dir_entry(&dir_entry).to_html(); + println!("{:?}", div); + search_results.push(div); + } + } else if metadata.is_dir() { + search_results.append(&mut _search_files( + &dir_entry.path(), + searchstring, + &filter, + )); + } + } + } + } + } + println!("{:?}", search_results); + return search_results; +} +fn entry_match(dir_entry: &DirEntry, searchstring: &str, filter: &Vec) -> bool { + if filter.contains( + &dir_entry + .path() + .extension() + .unwrap_or_default() + .to_string_lossy() + .to_string(), + ) { + let matcher = SkimMatcherV2::default(); + let search_match: Option = + matcher.fuzzy_match(&dir_entry.file_name().to_string_lossy(), searchstring); + // debug!( + // "{} = {} : {:?}", + // search_str, &application.name, search_match + // ); + if let Some(score) = search_match { + return true; + } + // self.matches.sort_by(|a, b| b.1.cmp(&a.1)); + } + return false; +} fn search_result_div_from_dir_entry(dir_entry: &DirEntry) -> HtmlTag { let mut file_div = HtmlTag::new("div") @@ -44,37 +94,3 @@ fn search_result_div_from_dir_entry(dir_entry: &DirEntry) -> HtmlTag { file_div.add_child(file_button); return file_div; } -pub fn _search_files(path: &Path, searchstring: &str, filter: &Vec) -> Vec { - let mut search_results: Vec = Vec::new(); - if let Ok(entries) = fs::read_dir(path) { - for dir_entry_res in entries { - if let Ok(dir_entry) = dir_entry_res { - if let Ok(metadata) = fs::metadata(dir_entry.path()) { - if metadata.is_file() { - if filter.contains( - &dir_entry - .path() - .extension() - .unwrap_or_default() - .to_string_lossy() - .to_string() - ) { - let div =search_result_div_from_dir_entry(&dir_entry).to_html(); - println!("{:?}", div); - search_results - .push(div); - } - } else if metadata.is_dir() { - search_results.append(&mut _search_files( - &dir_entry.path(), - searchstring, - &filter, - )); - } - } - } - } - } - println!("{:?}", search_results); - return search_results; -}