diff --git a/CHANGELOG.md b/CHANGELOG.md index abdfc79..ffd4327 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ 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 ` where `fd` can only exit when it finds the search results ` + 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. ## Bugfixes diff --git a/doc/fd.1 b/doc/fd.1 index bd1b5c2..d1334e3 100644 --- a/doc/fd.1 +++ b/doc/fd.1 @@ -89,6 +89,9 @@ Separate search results by the null character (instead of newlines). Useful for .B \-\-max\-results count Limit the number of search results to 'count' and quit immediately. .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 Enable the display of filesystem errors for situations such as insufficient permissions or dead symlinks. diff --git a/src/app.rs b/src/app.rs index 96d140a..47a71dd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -415,6 +415,15 @@ pub fn build_app() -> App<'static, 'static> { .hidden_short_help(true) .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::with_name("show-errors") .long("show-errors") diff --git a/src/main.rs b/src/main.rs index 69a61d4..5872778 100644 --- a/src/main.rs +++ b/src/main.rs @@ -295,7 +295,8 @@ fn run() -> Result { max_results: matches .value_of("max-results") .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) diff --git a/tests/tests.rs b/tests/tests.rs index 5a72566..1b8a244 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1458,11 +1458,15 @@ fn test_max_results() { ); // 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 stdout = String::from_utf8_lossy(&output.stdout); - let stdout = stdout.trim(); - let stdout = stdout.replace(&std::path::MAIN_SEPARATOR.to_string(), "/"); - assert!(stdout == "one/two/C.Foo2" || stdout == "one/two/c.foo"); + let assert_just_one_result_with_option = |option| { + let output = te.assert_success_and_get_output(".", &[option, "c.foo"]); + let stdout = String::from_utf8_lossy(&output.stdout) + .trim() + .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