From cf7dae67bafa50034d39293df1645d6691561fbb Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 24 Jan 2024 10:19:23 +0100 Subject: [PATCH] atributos y recursividad (mas o menos) --- Cargo.lock | 234 ++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/fuzzy_search/fuzzy.rs | 2 +- src/main.rs | 32 +++--- src/ui/renderer.rs | 68 ++++++++--- 5 files changed, 292 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a3818d..a1c5d6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,54 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -26,6 +74,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "crossterm" version = "0.27.0" @@ -55,9 +149,16 @@ dependencies = [ name = "fuzzy-search" version = "0.1.0" dependencies = [ + "clap", "crossterm", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "libc" version = "0.2.152" @@ -89,7 +190,7 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -112,7 +213,25 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "proc-macro2" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", ] [[package]] @@ -166,6 +285,35 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "2.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b7d0a2c048d661a1a59fcd7355baa232f7ed34e0ee4df2eef3c1c1c0d3852d8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -200,7 +348,16 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", ] [[package]] @@ -209,13 +366,28 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", ] [[package]] @@ -224,38 +396,80 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/Cargo.toml b/Cargo.toml index d473782..84269e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "4.4.18", features = ["derive"] } crossterm = "0.27.0" diff --git a/src/fuzzy_search/fuzzy.rs b/src/fuzzy_search/fuzzy.rs index 4591c4b..f2af58d 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 Vec) { +pub fn fzs(pattern: &str, list: &mut [String]) { list.sort_by_key(|x| distance(pattern, x)); } diff --git a/src/main.rs b/src/main.rs index 571440e..2b2a90c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,23 @@ +use clap::Parser; use fuzzy_search::ui::renderer; +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Args { + /// Print contents of directories recursively, + /// specify maximum recursive depth + #[arg(short, long, default_value_t = 0)] + recursive: usize, + + /// Path of the directory to list + #[arg(default_value_t = String::from("."))] + path: String, +} + fn main() { - // let list = [ - // "pear", - // "tree", - // "platypus", - // "building", - // "test", - // "lime", - // "stationary", - // ]; - - // let mut result = list; - - // fzs("aaa", &mut result); - - // println!("{:?}", result); + let args = Args::parse(); renderer::start(); - renderer::run(); + renderer::run(&args.path, args.recursive); renderer::stop(); } diff --git a/src/ui/renderer.rs b/src/ui/renderer.rs index b2d4911..e7381a6 100644 --- a/src/ui/renderer.rs +++ b/src/ui/renderer.rs @@ -1,14 +1,13 @@ use std::{ - fs::read_dir, + fs::{metadata, read_dir}, io::stdout, - os::unix::process, sync::{Arc, RwLock}, thread::sleep, time::Duration, }; use crossterm::{ - cursor::{MoveTo, Show}, + cursor::{Hide, MoveTo, Show}, execute, queue, style::Print, terminal, @@ -41,43 +40,35 @@ pub fn start() -> (usize, usize) { (width, height) } -pub fn run() { +pub fn run(path: &str, recursive: usize) { let size = terminal::size().unwrap(); - let input: Arc, String)>> = Arc::new(RwLock::new((Vec::new(), String::new()))); - let cont: Arc> = Arc::new(RwLock::new(true)); - let cursor: Arc> = Arc::new(RwLock::new(0)); - let results: Arc>> = Arc::new(RwLock::new(Vec::new())); - start_get_input(size.0 as usize, input.clone(), cont.clone(), cursor.clone()); + // let list = read_dir(path) + // .unwrap() + // .map(|x| x.unwrap().file_name().to_str().unwrap().to_string()) + // .collect::>(); - let list = read_dir(".") - .unwrap() - .map(|x| x.unwrap().file_name().to_str().unwrap().to_string()) - .collect::>(); + let list = get_files(Vec::new(), path, recursive); let mut search_thread = test(list.clone(), input.clone(), results.clone()); - - // let mut new_input = true; - let mut processing = false; - let mut last_input = String::new(); while cont.read().unwrap().to_owned() { let input_buff = input.read().unwrap().0.clone(); if String::from_iter(input.read().unwrap().clone().0).as_str() != last_input.as_str() { search_thread = test(list.clone(), input.clone(), results.clone()); - // new_input = false; processing = true; } if processing && search_thread.is_finished() { let res_list = results.read().unwrap(); + queue!(stdout(), Hide).unwrap(); for (i, res) in res_list[0..(size.1 as usize - 4).min(res_list.len())] .iter() .enumerate() @@ -91,6 +82,7 @@ pub fn run() { ) .unwrap(); } + queue!(stdout(), Show).unwrap(); } queue!( @@ -100,7 +92,9 @@ pub fn run() { MoveTo(cursor.read().unwrap().to_owned() as u16 + 2, 1) ) .unwrap(); + execute!(stdout()).unwrap(); + last_input = String::from_iter(input_buff); sleep(Duration::from_millis(1)); } @@ -147,3 +141,41 @@ fn print_frame(width: usize, height: usize) { execute!(stdout()).unwrap(); } + +fn get_files(list: Vec, path: &str, recursive: usize) -> Vec { + let mut list2 = list.clone(); + + let path_format = if path == "." { + "".to_string() + } else { + format!("{}/", path) + }; + + if recursive < 1 { + list2.append( + &mut read_dir(path) + .unwrap() + .map(|x| { + format!( + "{}{}", + path_format, + x.unwrap().file_name().to_str().unwrap() + ) + }) + .collect::>(), + ) + } else { + for e in read_dir(path) + .unwrap() + .map(|x| x.unwrap().file_name().to_str().unwrap().to_string()) + .collect::>() + { + let e_path = format!("{}{}", path_format, e); + list2.push(e_path.clone()); + if metadata(e_path.clone()).unwrap().is_dir() { + list2 = get_files(list2, e_path.as_str().clone(), recursive - 1); + } + } + } + list2 +}