Search in current dir

This commit is contained in:
=
2024-01-23 13:40:57 +01:00
parent bc2bae18f4
commit 21aa4d5693
3 changed files with 40 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
#[inline] #[inline]
pub fn fzs(pattern: &str, list: &mut [&str]) { pub fn fzs(pattern: &str, list: &mut Vec<String>) {
list.sort_by_key(|x| distance(pattern, x)); list.sort_by_key(|x| distance(pattern, x));
} }

View File

@@ -1,7 +1,7 @@
use std::{ use std::{
// io::stdout, // io::stdout,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
thread::{self, JoinHandle}, // time::Duration, thread::{self, JoinHandle},
}; };
use self::fuzzy::fzs; use self::fuzzy::fzs;
@@ -9,18 +9,24 @@ use self::fuzzy::fzs;
pub mod fuzzy; pub mod fuzzy;
pub fn test( pub fn test(
list: Vec<String>,
input: Arc<RwLock<(Vec<char>, String)>>, input: Arc<RwLock<(Vec<char>, String)>>,
result: Arc<RwLock<Vec<String>>>, result: Arc<RwLock<Vec<String>>>,
) -> JoinHandle<()> { ) -> JoinHandle<()> {
let list = [ // let list = [
"pear", // "pear",
"tree", // "tree",
"platypus", // "platypus",
"building", // "building",
"test", // "test",
"lime", // "lime",
"stationary", // "stationary",
]; // ];
// let list = read_dir(".")
// .unwrap()
// .map(|x| x.unwrap().file_name().to_str().unwrap().to_string())
// .collect::<Vec<String>>();
let mut list2 = list; let mut list2 = list;
@@ -28,6 +34,6 @@ pub fn test(
let pattern = String::from_iter(input.read().unwrap().0.clone()); let pattern = String::from_iter(input.read().unwrap().0.clone());
fzs(pattern.as_str(), &mut list2); fzs(pattern.as_str(), &mut list2);
*result.write().unwrap() = list2.map(|x| x.to_string()).to_vec(); *result.write().unwrap() = list2;
}) })
} }

View File

@@ -1,4 +1,5 @@
use std::{ use std::{
fs::read_dir,
io::stdout, io::stdout,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
thread::sleep, thread::sleep,
@@ -53,7 +54,12 @@ pub fn run() {
start_get_input(size.0 as usize, input.clone(), cont.clone(), cursor.clone()); 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::<Vec<String>>();
let mut search_thread = test(list.clone(), input.clone(), results.clone());
let mut last_input = String::new(); 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() if String::from_iter(input.read().unwrap().clone().0).as_str() != last_input.as_str()
&& search_thread.is_finished() && 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!( queue!(
stdout(), stdout(),
MoveTo(2, 1), MoveTo(2, 1),