diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-04-27 17:59:33 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-04-27 18:06:02 -0500 |
commit | a98b50667bc6a11e4b8be464969adc14601e9e78 (patch) | |
tree | 18c0b2a7858a733a380415a9740e48a6f4ea8c96 /src/search.rs | |
parent | bf58ceab696347f9917f14a5e6a63d8503d28b00 (diff) |
feat: rewrite cli and add --all flag
Diffstat (limited to 'src/search.rs')
-rw-r--r-- | src/search.rs | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/src/search.rs b/src/search.rs index 6b30293..888179e 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,16 +1,76 @@ +use std::path::PathBuf; + +use clap::Args; + +use crate::project::ProjectItem; + use self::entry::SearchEntry; -use crate::{config::SearchEntryConfig, project::ProjectItem}; pub mod entry; -type EntryIter = std::vec::IntoIter<SearchEntryConfig>; +type EntryIter = std::vec::IntoIter<PathBuf>; +#[derive(Debug, Default, Clone, Args)] pub struct Search { + /// Directory to search. + /// + /// Directories are searched recursively based on `--max-depth`. + pub paths: Vec<PathBuf>, + + #[command(flatten)] + pub filter: Filters, +} + +#[derive(Debug, Default, Clone, Args)] +pub struct Filters { + /// Match all child directories + #[arg(long, short, conflicts_with_all = ["pattern", "git"])] + pub all: bool, + + /// Max depth to recurse. + /// + /// Setting to 0 will only use the supplied directory. + #[arg(short = 'd', long, default_value = "1")] + pub max_depth: Option<usize>, + + /// Recurse into hidden directories. + /// + /// Traverse into hidden directories while searching. A directory is considered hidden + /// if its name starts with a `.` sign (dot). If `--max-depth` is 0, this has no effect. + #[arg(long)] + pub hidden: bool, + + /// Match directories containing item named <PATTERN> + #[arg(long, short)] + pub pattern: Option<String>, + + /// Match git repositories + #[cfg(feature = "git")] + #[arg(long, short, default_value_t = true)] + pub git: bool, +} + +impl IntoIterator for Search { + type Item = ProjectItem; + + type IntoIter = SearchIter; + + fn into_iter(self) -> Self::IntoIter { + SearchIter { + iter: self.paths.into_iter(), + config: self.filter, + curr: None, + } + } +} + +pub struct SearchIter { iter: EntryIter, + config: Filters, curr: Option<SearchEntry>, } -impl std::fmt::Debug for Search { +impl std::fmt::Debug for SearchIter { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Projects") .field("paths_iter", &self.iter) @@ -18,19 +78,7 @@ impl std::fmt::Debug for Search { } } -impl<T> From<T> for Search -where - T: IntoIterator<IntoIter = EntryIter>, -{ - fn from(value: T) -> Self { - Self { - iter: value.into_iter(), - curr: None, - } - } -} - -impl Iterator for Search { +impl Iterator for SearchIter { type Item = ProjectItem; #[tracing::instrument] @@ -38,7 +86,7 @@ impl Iterator for Search { match self.curr.as_mut().and_then(|c| c.next()) { Some(proj) => Some(proj), None => { - self.curr = Some(self.iter.next()?.into()); + self.curr = Some(SearchEntry::new(self.iter.next()?, &self.config)); self.next() } } |