Search working internally. Results not yet shown in interface
This commit is contained in:
parent
2116549154
commit
9b9c5add18
3 changed files with 71 additions and 35 deletions
|
|
@ -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<String>) ->
|
|||
Err(_) => Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn _search_files(path: &Path, searchstring: &str, filter: &Vec<String>) -> Vec<String> {
|
||||
let mut search_results: Vec<String> = 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<String>) -> bool {
|
||||
if filter.contains(
|
||||
&dir_entry
|
||||
.path()
|
||||
.extension()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
) {
|
||||
let matcher = SkimMatcherV2::default();
|
||||
let search_match: Option<i64> =
|
||||
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<String>) -> Vec<String> {
|
||||
let mut search_results: Vec<String> = 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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue