Add --min-depth and --exact-depth
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add new `--min-depth <depth>` and `--exact-depth <depth>` options in addition to the existing option to limit the maximum depth. closes #404
authored by sharkdp · committed by David Peter · parent 47974b6 · Browse files · 7 files·+89 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+89 added−2 removed
- tests:+34 added−0 removed
- other:+55 added−2 removed
Provenance
- Commit
d63c63b- Tree
c6db4d6- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
47974b6
7 files changed+89 added−2 removed
modified:doc/fd.1+6 added
108 unmodified lines109 Limit directory traversal to at most110 .I d111 levels of depth. By default, there is no limit on the search depth.112 .TP113+.BI "\-\-min\-depth " d114+Only show search results starting at the given depth. See also: '--max-depth' and '--exact-depth'.115+.TP116+.BI "\-\-exact\-depth " d117+Only show search results at the exact given depth. This is an alias for '--min-depth <depth> --max-depth <depth>'.118+.TP119 .BI "\-t, \-\-type " filetype120 Filter search by type:121 .RS122 .IP "f, file"164 unmodified linesmodified:src/app.rs+25 added-1 removed
166 unmodified lines167 Useful for piping results to 'xargs'.",168 ),169 )170 .arg(171- Arg::with_name("depth")171+ Arg::with_name("max-depth")172 .long("max-depth")173 .short("d")174 .takes_value(true)175+ .value_name("depth")176 .help("Set maximum search depth (default: none)")177 .long_help(178 "Limit the directory traversal to a given depth. By default, there is no \179 limit on the search depth.",6 unmodified lines186 .hidden(true)187 .takes_value(true)188 )189 .arg(190+ Arg::with_name("min-depth")191+ .long("min-depth")192+ .takes_value(true)193+ .value_name("depth")194+ .hidden_short_help(true)195+ .long_help(196+ "Only show search results starting at the given depth. \197+ See also: '--max-depth' and '--exact-depth'",198+ ),199+ )200+ .arg(201+ Arg::with_name("exact-depth")202+ .long("exact-depth")203+ .takes_value(true)204+ .value_name("depth")205+ .hidden_short_help(true)206+ .conflicts_with_all(&["max-depth", "min-depth"])207+ .long_help(208+ "Only show search results at the exact given depth. This is an alias for \209+ '--min-depth <depth> --max-depth <depth>'.",210+ ),211+ )212+ .arg(213 Arg::with_name("file-type")214 .long("type")215 .short("t")216 .multiple(true)319 unmodified linesmodified:src/main.rs+6 added-1 removed
224 unmodified lines225 follow_links: matches.is_present("follow"),226 one_file_system: matches.is_present("one-file-system"),227 null_separator: matches.is_present("null_separator"),228 max_depth: matches229- .value_of("depth")229+ .value_of("max-depth")230 .or_else(|| matches.value_of("rg-depth"))231+ .or_else(|| matches.value_of("exact-depth"))232 .and_then(|n| usize::from_str_radix(n, 10).ok()),233+ min_depth: matches234+ .value_of("min-depth")235+ .or_else(|| matches.value_of("exact-depth"))236+ .and_then(|n| usize::from_str_radix(n, 10).ok()),237 threads: std::cmp::max(238 matches239 .value_of("threads")240 .and_then(|n| usize::from_str_radix(n, 10).ok())100 unmodified linesmodified:src/options.rs+3 added
38 unmodified lines39 /// A depth of `1` includes all files under the current directory, a depth of `2` also includes40 /// all files under subdirectories of the current directory, etc.41 pub max_depth: Option<usize>,42 43+ /// The minimum depth for reported entries, or `None`.44+ pub min_depth: Option<usize>,45+46 /// The number of threads to use.47 pub threads: usize,48 49 /// Time to buffer results internally before streaming to the console. This is useful to44 unmodified linesmodified:src/walk.rs+13 added
281 unmodified lines282 DirEntry::Normal(e) => e.metadata().ok(),283 DirEntry::BrokenSymlink(_) => None,284 }285 }286+287+ pub fn depth(&self) -> Option<usize> {288+ match self {289+ DirEntry::Normal(e) => Some(e.depth()),290+ DirEntry::BrokenSymlink(_) => None,291+ }292+ }293 }294 295 fn spawn_senders(296 config: &Arc<Options>,47 unmodified lines344 return ignore::WalkState::Continue;345 }346 };347 348+ if let Some(min_depth) = config.min_depth {349+ if entry.depth().map_or(true, |d| d < min_depth) {350+ return ignore::WalkState::Continue;351+ }352+ }353+354 // Check the name first, since it doesn't require metadata355 let entry_path = entry.path();356 357 let search_str: Cow<OsStr> = if config.search_full_path {96 unmodified linesmodified:tests/tests.rs+34 added
677 unmodified lines678 symlink",679 );680 }681 682+/// Minimum depth (--min-depth)683+#[test]684+fn test_min_depth() {685+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);686+687+ te.assert_output(688+ &["--min-depth", "3"],689+ "one/two/c.foo690+ one/two/C.Foo2691+ one/two/three692+ one/two/three/d.foo693+ one/two/three/directory_foo",694+ );695+696+ te.assert_output(697+ &["--min-depth", "4"],698+ "one/two/three/d.foo699+ one/two/three/directory_foo",700+ );701+}702+703+/// Exact depth (--exact-depth)704+#[test]705+fn test_exact_depth() {706+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);707+708+ te.assert_output(709+ &["--exact-depth", "3"],710+ "one/two/c.foo711+ one/two/C.Foo2712+ one/two/three",713+ );714+}715+716 /// Absolute paths (--absolute-path)717 #[test]718 fn test_absolute_path() {719 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);841 unmodified linesmodified:CHANGELOG.md+2 added
10 unmodified lines11 - Add a new `--max-results=<count>` option to limit the number of search results, see #472 and #47612 This can be useful to speed up searches in cases where you know that there are only N results.13 Using this option is also (slightly) faster than piping to `head -n <count>` where `fd` can only14 exit when it finds the search results `<count> + 1`.15+- Add new `--min-depth <depth>` and `--exact-depth <depth>` options in addition to the existing option16+ to limit the maximum depth. See #404.17 - Add the alias `-1` for `--max-results=1`, see #561. (@SimplyDanny).18 - Support additional ANSI font styles in `LS_COLORS`: faint, slow blink, rapid blink, dimmed, hidden and strikethrough.19 20 ## Bugfixes414 unmodified lines