Skip to content
Myr

Add -q/--quiet/--has-match flag (#813)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Instead of printing to stdout, the program will return 1 as the exit code if there are no matches and 0 otherwise.

authored by Asha20 · committed by GitHub · parent 1c72f80 · Browse files · 8 files+53 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

8 files+53 added−2 removed

  • tests:+13 added−0 removed
  • other:+40 added−2 removed

8 files+53 added−2 removed

modified:doc/fd.1+7 added
91 unmodified lines
92.TP93.B \-194Limit the search to a single result and quit immediately. This is an alias for '--max-results=1'.95.TP96.B \-q, \-\-quiet97When the flag is present, the program does not print anything and will instead exit with a code of 0 if there is at least one search result.98Otherwise, the exit code will be 1.99This is mainly for usage in scripts and can be faster than checking for output because the search can be stopped early after the first match.100.B \-\-has\-results101can be used as an alias.102.TP103.B \-\-show-errors104Enable the display of filesystem errors for situations such as insufficient105permissions or dead symlinks.106.TP
285 unmodified lines
modified:src/app.rs+14 added
503 unmodified lines
504 .long_help("Limit the search to a single result and quit immediately. \505 This is an alias for '--max-results=1'.")506 )507 .arg(508 Arg::with_name("quiet")509 .long("quiet")510 .short("q")511 .alias("has-results")512 .hidden_short_help(true)513 .conflicts_with_all(&["exec", "exec-batch", "list-details", "max-results"])514 .long_help(515 "When the flag is present, the program does not print anything and will \516 return with an exit code of 0 if there is at least one match. Otherwise, the \517 exit code will be 1. \518 '--has-results' can be used as an alias."519 )520 )521 .arg(522 Arg::with_name("show-errors")523 .long("show-errors")524 .hidden_short_help(true)525 .overrides_with("show-errors")
104 unmodified lines
modified:src/exit_codes.rs+3 added-1 removed
1#[derive(Debug, Clone, Copy, PartialEq)]2pub enum ExitCode {3 Success,4 HasResults(bool),5 GeneralError,6 KilledBySigint,7}89impl From<ExitCode> for i32 {10 fn from(code: ExitCode) -> Self {11 match code {12 ExitCode::Success => 0,13 ExitCode::HasResults(has_results) => !has_results as i32,14 ExitCode::GeneralError => 1,15 ExitCode::KilledBySigint => 130,16 }17 }18}1920impl ExitCode {21 fn is_error(self) -> bool {20 self != ExitCode::Success22 i32::from(self) != 023 }24}2526pub fn merge_exitcodes(results: &[ExitCode]) -> ExitCode {
48 unmodified lines
modified:src/main.rs+1 added
344 unmodified lines
345 || matches.is_present("no-global-ignore-file")),346 follow_links: matches.is_present("follow"),347 one_file_system: matches.is_present("one-file-system"),348 null_separator: matches.is_present("null_separator"),349 quiet: matches.is_present("quiet"),350 max_depth: matches351 .value_of("max-depth")352 .or_else(|| matches.value_of("rg-depth"))353 .or_else(|| matches.value_of("exact-depth"))
141 unmodified lines
modified:src/options.rs+4 added
52 unmodified lines
5354 /// The number of threads to use.55 pub threads: usize,5657 /// If true, the program doesn't print anything and will instead return an exit code of 058 /// if there's at least one match. Otherwise, the exit code will be 1.59 pub quiet: bool,6061 /// Time to buffer results internally before streaming to the console. This is useful to62 /// provide a sorted output, in case the total execution time is shorter than63 /// `max_buffer_time`.64 pub max_buffer_time: Option<Duration>,
45 unmodified lines
modified:src/walk.rs+9 added-1 removed
231 unmodified lines
232233 for worker_result in rx {234 match worker_result {235 WorkerResult::Entry(value) => {236 if config.quiet {237 return ExitCode::HasResults(true);238 }239240 match mode {241 ReceiverMode::Buffering => {242 buffer.push(value);243
45 unmodified lines
289 output::print_entry(&mut stdout, &value, &config, &wants_to_quit);290 }291 }292293 if config.quiet {294 ExitCode::HasResults(false)295 } else {289 ExitCode::Success296 ExitCode::Success297 }298 }299 })300}301
202 unmodified lines
modified:tests/tests.rs+13 added
1423 unmodified lines
1424 "e1 e2.",1425 );1426}14271428/// Non-zero exit code (--quiet)1429#[test]1430fn test_quiet() {1431 let dirs = &[];1432 let files = &["a.foo", "b.foo"];1433 let te = TestEnv::new(dirs, files);14341435 te.assert_output(&["-q"], "");1436 te.assert_output(&["--quiet"], "");1437 te.assert_output(&["--has-results"], "");1438 te.assert_failure_with_error(&["--quiet", "c.foo"], "")1439}14401441/// Literal search (--fixed-strings)1442#[test]1443fn test_fixed_strings() {1444 let dirs = &["test1", "test2"];
373 unmodified lines
modified:CHANGELOG.md+2 added
1 unmodified line
23## Features4- Don't buffer command output from `--exec` when using a single thread. See #52256- Add new `-q, --quiet` flag, see #303 (@Asha20)78## Bugfixes910- Set default path separator to `/` in MSYS, see #537 and #730 (@aswild)11- fd cannot search files under a RAM disk, see #752
517 unmodified lines