Skip to content
Myr

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

7 files+89 added−2 removed

modified:doc/fd.1+6 added
108 unmodified lines
109Limit directory traversal to at most110.I d111levels of depth. By default, there is no limit on the search depth.112.TP113.BI "\-\-min\-depth " d114Only show search results starting at the given depth. See also: '--max-depth' and '--exact-depth'.115.TP116.BI "\-\-exact\-depth " d117Only show search results at the exact given depth. This is an alias for '--min-depth <depth> --max-depth <depth>'.118.TP119.BI "\-t, \-\-type " filetype120Filter search by type:121.RS122.IP "f, file"
164 unmodified lines
modified:src/app.rs+25 added-1 removed
166 unmodified lines
167 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 lines
186 .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 lines
modified:src/main.rs+6 added-1 removed
224 unmodified lines
225 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 lines
modified:src/options.rs+3 added
38 unmodified lines
39 /// 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>,4243 /// The minimum depth for reported entries, or `None`.44 pub min_depth: Option<usize>,4546 /// The number of threads to use.47 pub threads: usize,4849 /// Time to buffer results internally before streaming to the console. This is useful to
44 unmodified lines
modified:src/walk.rs+13 added
281 unmodified lines
282 DirEntry::Normal(e) => e.metadata().ok(),283 DirEntry::BrokenSymlink(_) => None,284 }285 }286287 pub fn depth(&self) -> Option<usize> {288 match self {289 DirEntry::Normal(e) => Some(e.depth()),290 DirEntry::BrokenSymlink(_) => None,291 }292 }293}294295fn spawn_senders(296 config: &Arc<Options>,
47 unmodified lines
344 return ignore::WalkState::Continue;345 }346 };347348 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 }353354 // Check the name first, since it doesn't require metadata355 let entry_path = entry.path();356357 let search_str: Cow<OsStr> = if config.search_full_path {
96 unmodified lines
modified:tests/tests.rs+34 added
677 unmodified lines
678 symlink",679 );680}681682/// Minimum depth (--min-depth)683#[test]684fn test_min_depth() {685 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);686687 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 );695696 te.assert_output(697 &["--min-depth", "4"],698 "one/two/three/d.foo699 one/two/three/directory_foo",700 );701}702703/// Exact depth (--exact-depth)704#[test]705fn test_exact_depth() {706 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);707708 te.assert_output(709 &["--exact-depth", "3"],710 "one/two/c.foo711 one/two/C.Foo2712 one/two/three",713 );714}715716/// Absolute paths (--absolute-path)717#[test]718fn test_absolute_path() {719 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);
841 unmodified lines
modified:CHANGELOG.md+2 added
10 unmodified lines
11- 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.1920## Bugfixes
414 unmodified lines