diff --git a/Cargo.lock b/Cargo.lock index 2550560..7efa630 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,7 +70,7 @@ dependencies = [ [[package]] name = "find-files" -version = "0.1.7" +version = "0.1.8" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 0dbf44c..4c53ac9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "find-files" -version = "0.1.7" +version = "0.1.8" description = "Find Files (ff) utility recursively searches the files whose names match the specified RegExp pattern in the provided directory (defaults to the current directory if not provided)." authors = ["Vishal Telangre "] license = "Unlicense OR MIT" diff --git a/README.md b/README.md index 6e596cc..19a78bb 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,11 @@ FLAGS: OPTIONS: -x, --exclude Exclude files and directories matching this regular expression from the search results. + -L, --level Recursively search only given level directories + deep. By default no depth restriction is imposed. + A value of 0 would always yield zero results. A + value of 1 searches for the direct children in + the given path. -j, --threads The approximate number of threads to use. A value of 0 (which is the default) results in thread count set to available CPU cores. @@ -105,91 +110,97 @@ Following examples demonstrate just a tip of an iceberg. - List paths of files recursively in the current working directory matching `article` string. -``` -ff article -``` + ``` + ff article + ``` - List files having `.png`, or `.PNG` extension. -``` -ff png$ -``` + ``` + ff png$ + ``` - List files having strict `.PNG` extension. -``` -ff -s PNG$ -``` + ``` + ff -s PNG$ + ``` - Search various image files. -``` -ff "\.(png|jpg|jpeg|gif|svg)$" -``` + ``` + ff "\.(png|jpg|jpeg|gif|svg)$" + ``` - List files whose path matches `controllers` string. -``` -ff controllers -``` + ``` + ff controllers + ``` - Search `.js` files in `./spec` directory. -``` -ff \.js ./spec -``` + ``` + ff \.js ./spec + ``` - Search a file which is expected to be inside hidden `.git` directory whose name contains `commit` or something similar. -```bash -$ ff git.*commit + ```bash + $ ff git.*commit -./.git/COMMIT_EDITMSG -# omitted other results -``` + ./.git/COMMIT_EDITMSG + # omitted other results + ``` - Do not show hidden files and directories in the search results. -``` -ff something -H -``` + ``` + ff something -H + ``` - Do not show those files and directories in the search results which are enlisted in `.gitignore`. -``` -ff src/.*js$ -G -``` + ``` + ff src/.*js$ -G + ``` -Without `-G (--ignore-gitignore)` flag in the above command, it also includes the results in the directories such as `node_modules` by default. + Without `-G (--ignore-gitignore)` flag in the above command, it also includes the results in the directories such as `node_modules` by default. - Do not show paths which are just directories and not actual files. -```bash -$ ff -D user + ```bash + $ ff -D user -./app/models/user.rb -./app/models/user/address.rb -./specs/models/user_spec.rb -./specs/models/user/address_spec.rb -``` + ./app/models/user.rb + ./app/models/user/address.rb + ./specs/models/user_spec.rb + ./specs/models/user/address_spec.rb + ``` -Without `-D (--exclude-dir-paths)` flag in the above command, it also includes the paths of the matching directories in the results as follows. + Without `-D (--exclude-dir-paths)` flag in the above command, it also includes the paths of the matching directories in the results as follows. -```bash -$ ff user + ```bash + $ ff user -./app/models/user.rb -./app/models/user -./app/models/user/address.rb -./specs/models/user_spec.rb -./specs/models/user -./specs/models/user/address_spec.rb -``` + ./app/models/user.rb + ./app/models/user + ./app/models/user/address.rb + ./specs/models/user_spec.rb + ./specs/models/user + ./specs/models/user/address_spec.rb + ``` - Exclude (omit) files and directories which match the provided optional exclude RegExp pattern. -``` -ff rb$ app/controllers -x /(audit|admin|sso|api)/ -``` + ``` + ff rb$ app/controllers -x /(audit|admin|sso|api)/ + ``` + + Above command will show paths of all files whose name ends with `rb` inside the relative `app/controllers` directory excluding the paths which match `/(audit|admin|sso|api)/` pattern. + +- Limit searching beyond 3 levels deep in the given path. -Above command will show paths of all files whose name ends with `rb` inside the relative `app/controllers` directory excluding the paths which match `/(audit|admin|sso|api)/` pattern. + ``` + ff -L 3 .js$ + ``` diff --git a/src/app.rs b/src/app.rs index e62c2ed..badb806 100644 --- a/src/app.rs +++ b/src/app.rs @@ -54,6 +54,13 @@ pub fn app() -> ArgMatches<'static> { .short("s") .long("case-sensitive"), ) + .arg( + Arg::with_name("level") + .help("Recursively search only given level directories deep. By default no depth restriction is imposed. A value of 0 would always yield zero results. A value of 1 searches for the direct children in the given path.") + .short("L") + .takes_value(true) + .long("level"), + ) .arg( Arg::with_name("threads") .help("The approximate number of threads to use. A value of 0 (which is the default) results in thread count set to available CPU cores.") diff --git a/src/args.rs b/src/args.rs index 4e91e38..3448f2f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -16,6 +16,7 @@ pub struct Args { pub ignore_gitignore: bool, pub ignore_hidden: bool, pub case_sensitive: bool, + pub level: Option, pub threads: usize, pub exclude_reg_exp: Option, } @@ -76,6 +77,12 @@ impl ArgMatchesWrapper { } } + fn level(&self) -> Option { + let matches = &self.matches; + + clap::value_t!(matches.value_of("level"), usize).ok() + } + fn threads(&self) -> usize { let matches = &self.matches; let threads = clap::value_t!(matches.value_of("threads"), usize).unwrap_or(0); @@ -132,6 +139,7 @@ impl ArgMatchesWrapper { ignore_gitignore: self.should_ignore_gitignore_files(), case_sensitive: self.is_case_sensitive(), reg_exp: self.search_pattern(), + level: self.level(), threads: self.threads(), exclude_reg_exp: self.exclude_reg_exp(), } diff --git a/src/walker.rs b/src/walker.rs index 19723b4..392f8f2 100644 --- a/src/walker.rs +++ b/src/walker.rs @@ -22,6 +22,7 @@ impl<'a> Walker<'a> { let walker = WalkBuilder::new(&self.args.root_path) .hidden(self.args.ignore_hidden) .git_ignore(self.args.ignore_gitignore) + .max_depth(self.args.level) .threads(self.args.threads) .build_parallel();