From 5da4fb25f9bb72771c7f2798daa6339be0bc3900 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Thu, 4 May 2023 19:43:08 -0500 Subject: refactor: join iterators into a single walk --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 7fbed15..33b170d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use anyhow::Result; use clap::Parser; -use projectr::{project::Project, search::Search, Config}; +use projectr::{config::Config, path::PathMatcher, project::Project, search::SearchBuilder}; fn main() -> Result<()> { let config = Config::parse(); @@ -11,7 +11,9 @@ fn main() -> Result<()> { .with_max_level(&config.verbosity) .init(); - let mut projects: Vec = Search::new(config).into_iter().collect(); + let mut projects: Vec = build_search(&config) + .filter(|p| !config.paths.contains(&p.worktree)) + .collect(); projects.sort_unstable_by_key(|p| p.timestamp); @@ -21,3 +23,35 @@ fn main() -> Result<()> { Ok(()) } + +fn build_search(config: &Config) -> impl Iterator { + let (init, paths) = config.paths.split_first().unwrap(); + let mut builder = SearchBuilder::new(init); + + for path in paths { + builder.add(path); + } + + for path in &config.projects { + builder.project(path); + } + + builder.max_depth(config.search.max_depth); + + builder.hidden(!config.search.hidden); + + if config.search.parsers.all { + builder.parser(PathMatcher::All); + } + + if let Some(pattern) = &config.search.parsers.pattern { + builder.parser(PathMatcher::Pattern(pattern.to_owned())); + } + + #[cfg(feature = "git")] + if config.search.parsers.git { + builder.parser(projectr::git::Git); + } + + builder.build() +} -- cgit v1.2.3-70-g09d2