From 21aa4d56932e78fbc1d0c38dc0e388f0ce93d35a Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 23 Jan 2024 13:40:57 +0100 Subject: [PATCH] Search in current dir --- src/fuzzy_search/fuzzy.rs | 2 +- src/fuzzy_search/mod.rs | 28 +++++++++++++++++----------- src/ui/renderer.rs | 33 ++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/fuzzy_search/fuzzy.rs b/src/fuzzy_search/fuzzy.rs index 5f1eea3..4591c4b 100644 --- a/src/fuzzy_search/fuzzy.rs +++ b/src/fuzzy_search/fuzzy.rs @@ -1,5 +1,5 @@ #[inline] -pub fn fzs(pattern: &str, list: &mut [&str]) { +pub fn fzs(pattern: &str, list: &mut Vec) { list.sort_by_key(|x| distance(pattern, x)); } diff --git a/src/fuzzy_search/mod.rs b/src/fuzzy_search/mod.rs index aadc394..8906cbd 100644 --- a/src/fuzzy_search/mod.rs +++ b/src/fuzzy_search/mod.rs @@ -1,7 +1,7 @@ use std::{ // io::stdout, sync::{Arc, RwLock}, - thread::{self, JoinHandle}, // time::Duration, + thread::{self, JoinHandle}, }; use self::fuzzy::fzs; @@ -9,18 +9,24 @@ use self::fuzzy::fzs; pub mod fuzzy; pub fn test( + list: Vec, input: Arc, String)>>, result: Arc>>, ) -> JoinHandle<()> { - let list = [ - "pear", - "tree", - "platypus", - "building", - "test", - "lime", - "stationary", - ]; + // let list = [ + // "pear", + // "tree", + // "platypus", + // "building", + // "test", + // "lime", + // "stationary", + // ]; + + // let list = read_dir(".") + // .unwrap() + // .map(|x| x.unwrap().file_name().to_str().unwrap().to_string()) + // .collect::>(); let mut list2 = list; @@ -28,6 +34,6 @@ pub fn test( let pattern = String::from_iter(input.read().unwrap().0.clone()); fzs(pattern.as_str(), &mut list2); - *result.write().unwrap() = list2.map(|x| x.to_string()).to_vec(); + *result.write().unwrap() = list2; }) } diff --git a/src/ui/renderer.rs b/src/ui/renderer.rs index cb6f3cd..f0ee949 100644 --- a/src/ui/renderer.rs +++ b/src/ui/renderer.rs @@ -1,4 +1,5 @@ use std::{ + fs::read_dir, io::stdout, sync::{Arc, RwLock}, thread::sleep, @@ -53,7 +54,12 @@ pub fn run() { start_get_input(size.0 as usize, input.clone(), cont.clone(), cursor.clone()); - let mut search_thread = test(input.clone(), results.clone()); + let list = read_dir(".") + .unwrap() + .map(|x| x.unwrap().file_name().to_str().unwrap().to_string()) + .collect::>(); + + let mut search_thread = test(list.clone(), input.clone(), results.clone()); let mut last_input = String::new(); @@ -62,18 +68,23 @@ pub fn run() { if String::from_iter(input.read().unwrap().clone().0).as_str() != last_input.as_str() && search_thread.is_finished() { - search_thread = test(input.clone(), results.clone()); + search_thread = test(list.clone(), input.clone(), results.clone()); + let res_list = results.read().unwrap(); + for (i, res) in res_list[0..(size.1 as usize - 4).min(res_list.len())] + .iter() + .enumerate() + { + let spaces = + String::from_iter(vec![' '; (size.0 as usize - 4) - res.chars().count()]); + queue!( + stdout(), + MoveTo(2, 3 + i as u16), + Print(format!("{}{}", res, spaces)) + ) + .unwrap(); + } } - for (i, res) in results.read().unwrap().iter().enumerate() { - let spaces = String::from_iter(vec![' '; (size.0 as usize - 4) - res.chars().count()]); - queue!( - stdout(), - MoveTo(2, 3 + i as u16), - Print(format!("{}{}", res, spaces)) - ) - .unwrap(); - } queue!( stdout(), MoveTo(2, 1),