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
Provenance
- Commit
334488c- Tree
50eb576- Author
Asha20 <vukasinstepanovic@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
1c72f80
8 files changed+53 added−2 removed
modified:doc/fd.1+7 added
91 unmodified lines92 .TP93 .B \-194 Limit the search to a single result and quit immediately. This is an alias for '--max-results=1'.95 .TP96+.B \-q, \-\-quiet97+When 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.98+Otherwise, the exit code will be 1.99+This 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\-results101+can be used as an alias.102+.TP103 .B \-\-show-errors104 Enable the display of filesystem errors for situations such as insufficient105 permissions or dead symlinks.106 .TP285 unmodified linesmodified:src/app.rs+14 added
503 unmodified lines504 .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 linesmodified:src/exit_codes.rs+3 added-1 removed
1 #[derive(Debug, Clone, Copy, PartialEq)]2 pub enum ExitCode {3 Success,4+ HasResults(bool),5 GeneralError,6 KilledBySigint,7 }8 9 impl 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 }19 20 impl ExitCode {21 fn is_error(self) -> bool {20- self != ExitCode::Success22+ i32::from(self) != 023 }24 }25 26 pub fn merge_exitcodes(results: &[ExitCode]) -> ExitCode {48 unmodified linesmodified:src/main.rs+1 added
344 unmodified lines345 || 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 linesmodified:src/options.rs+4 added
52 unmodified lines53 54 /// The number of threads to use.55 pub threads: usize,56 57+ /// 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,60+61 /// 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 linesmodified:src/walk.rs+9 added-1 removed
231 unmodified lines232 233 for worker_result in rx {234 match worker_result {235 WorkerResult::Entry(value) => {236+ if config.quiet {237+ return ExitCode::HasResults(true);238+ }239+240 match mode {241 ReceiverMode::Buffering => {242 buffer.push(value);243 45 unmodified lines289 output::print_entry(&mut stdout, &value, &config, &wants_to_quit);290 }291 }292 293+ if config.quiet {294+ ExitCode::HasResults(false)295+ } else {289- ExitCode::Success296+ ExitCode::Success297+ }298 }299 })300 }301 202 unmodified linesmodified:tests/tests.rs+13 added
1423 unmodified lines1424 "e1 e2.",1425 );1426 }1427 1428+/// Non-zero exit code (--quiet)1429+#[test]1430+fn test_quiet() {1431+ let dirs = &[];1432+ let files = &["a.foo", "b.foo"];1433+ let te = TestEnv::new(dirs, files);1434+1435+ 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+}1440+1441 /// Literal search (--fixed-strings)1442 #[test]1443 fn test_fixed_strings() {1444 let dirs = &["test1", "test2"];373 unmodified linesmodified:CHANGELOG.md+2 added
1 unmodified line2 3 ## Features4 - Don't buffer command output from `--exec` when using a single thread. See #5225 6+- Add new `-q, --quiet` flag, see #303 (@Asha20)7+8 ## Bugfixes9 10 - Set default path separator to `/` in MSYS, see #537 and #730 (@aswild)11 - fd cannot search files under a RAM disk, see #752517 unmodified lines