From 7054e189687f1e9a074c6b5be9fa87191072fb49 Mon Sep 17 00:00:00 2001 From: jupahe64 Date: Thu, 9 Mar 2023 19:59:47 +0100 Subject: [PATCH] add 'include' option --- src/cli.rs | 3 +++ src/tre.rs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index 946a9a6..aed9c09 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -44,6 +44,9 @@ pub struct Interface { /// Limit depth of the tree in output. #[clap(long, short)] pub limit: Option, + /// Include paths matching a regex pattern. Repeatable. + #[clap(long, short = 'I', value_name = "PATTERN")] + pub include: Vec, /// Exclude paths matching a regex pattern. Repeatable. #[clap(long, short = 'E', value_name = "PATTERN")] pub exclude: Vec, diff --git a/src/tre.rs b/src/tre.rs index 6295fd5..4277722 100644 --- a/src/tre.rs +++ b/src/tre.rs @@ -23,6 +23,7 @@ pub struct RunOptions { pub output_json: bool, pub root: String, pub max_depth: Option, + pub include_patterns: Vec, pub exclude_patterns: Vec, pub coloring: cli::Coloring, pub portable_aliases: bool, @@ -58,6 +59,11 @@ impl From for RunOptions { output_json: inputs.json, root: inputs.path, max_depth: inputs.limit, + include_patterns: inputs + .include + .iter() + .filter_map(|p| regex::Regex::new(p).ok()) + .collect(), exclude_patterns: inputs .exclude .iter() @@ -84,6 +90,17 @@ pub fn run(option: RunOptions) { } }; + let paths = if option.include_patterns.is_empty() { + paths + } else { + paths + .into_iter() + .filter(|(path, _)| { + let mut pattern_iters = option.include_patterns.iter(); + pattern_iters.any(|p| p.is_match(path)) + }) + .collect() + }; let paths = if option.exclude_patterns.is_empty() { paths } else {