Add alias -1 for --max-results=1

This commit is contained in:
Danny Mösch 2020-04-09 17:21:40 +02:00 committed by David Peter
parent 95eae00126
commit 2bab4a2249
5 changed files with 24 additions and 6 deletions

View file

@ -12,6 +12,7 @@
This can be useful to speed up searches in cases where you know that there are only N results. This can be useful to speed up searches in cases where you know that there are only N results.
Using this option is also (slightly) faster than piping to `head -n <count>` where `fd` can only Using this option is also (slightly) faster than piping to `head -n <count>` where `fd` can only
exit when it finds the search results `<count> + 1`. exit when it finds the search results `<count> + 1`.
- Add the alias `-1` for `--max-results=1`, see #561. (@SimplyDanny).
- Support additional ANSI font styles in `LS_COLORS`: faint, slow blink, rapid blink, dimmed, hidden and strikethrough. - Support additional ANSI font styles in `LS_COLORS`: faint, slow blink, rapid blink, dimmed, hidden and strikethrough.
## Bugfixes ## Bugfixes

3
doc/fd.1 vendored
View file

@ -89,6 +89,9 @@ Separate search results by the null character (instead of newlines). Useful for
.B \-\-max\-results count .B \-\-max\-results count
Limit the number of search results to 'count' and quit immediately. Limit the number of search results to 'count' and quit immediately.
.TP .TP
.B \-1
Limit the search to a single result and quit immediately. This is an alias for '--max-results=1'.
.TP
.B \-\-show-errors .B \-\-show-errors
Enable the display of filesystem errors for situations such as insufficient Enable the display of filesystem errors for situations such as insufficient
permissions or dead symlinks. permissions or dead symlinks.

View file

@ -415,6 +415,15 @@ pub fn build_app() -> App<'static, 'static> {
.hidden_short_help(true) .hidden_short_help(true)
.long_help("Limit the number of search results to 'count' and quit immediately."), .long_help("Limit the number of search results to 'count' and quit immediately."),
) )
.arg(
Arg::with_name("max-one-result")
.short("1")
.hidden_short_help(true)
.overrides_with("max-results")
.conflicts_with_all(&["exec", "exec-batch"])
.long_help("Limit the search to a single result and quit immediately. \
This is an alias for '--max-results=1'.")
)
.arg( .arg(
Arg::with_name("show-errors") Arg::with_name("show-errors")
.long("show-errors") .long("show-errors")

View file

@ -295,7 +295,8 @@ fn run() -> Result<ExitCode> {
max_results: matches max_results: matches
.value_of("max-results") .value_of("max-results")
.and_then(|n| usize::from_str_radix(n, 10).ok()) .and_then(|n| usize::from_str_radix(n, 10).ok())
.filter(|&n| n != 0), .filter(|&n| n != 0)
.or_else(|| if matches.is_present("max-one-result") { Some(1) } else { None }),
}; };
let re = RegexBuilder::new(&pattern_regex) let re = RegexBuilder::new(&pattern_regex)

View file

@ -1458,11 +1458,15 @@ fn test_max_results() {
); );
// Limited to one result. We could find either C.Foo2 or c.foo // Limited to one result. We could find either C.Foo2 or c.foo
let output = te.assert_success_and_get_output(".", &["--max-results=1", "c.foo"]); let assert_just_one_result_with_option = |option| {
let stdout = String::from_utf8_lossy(&output.stdout); let output = te.assert_success_and_get_output(".", &[option, "c.foo"]);
let stdout = stdout.trim(); let stdout = String::from_utf8_lossy(&output.stdout)
let stdout = stdout.replace(&std::path::MAIN_SEPARATOR.to_string(), "/"); .trim()
assert!(stdout == "one/two/C.Foo2" || stdout == "one/two/c.foo"); .replace(&std::path::MAIN_SEPARATOR.to_string(), "/");
assert!(stdout == "one/two/C.Foo2" || stdout == "one/two/c.foo");
};
assert_just_one_result_with_option("--max-results=1");
assert_just_one_result_with_option("-1");
} }
/// Filenames with non-utf8 paths are passed to the executed program unchanged /// Filenames with non-utf8 paths are passed to the executed program unchanged