mirror of https://github.com/sharkdp/fd.git
Add -q/--quiet/--has-match flag (#813)
Instead of printing to stdout, the program will return 1 as the exit code if there are no matches and 0 otherwise.
This commit is contained in:
parent
1c72f80ff5
commit
334488cab7
|
@ -3,6 +3,8 @@
|
|||
## Features
|
||||
- Don't buffer command output from `--exec` when using a single thread. See #522
|
||||
|
||||
- Add new `-q, --quiet` flag, see #303 (@Asha20)
|
||||
|
||||
## Bugfixes
|
||||
|
||||
- Set default path separator to `/` in MSYS, see #537 and #730 (@aswild)
|
||||
|
|
|
@ -93,6 +93,13 @@ Limit the number of search results to 'count' and quit immediately.
|
|||
.B \-1
|
||||
Limit the search to a single result and quit immediately. This is an alias for '--max-results=1'.
|
||||
.TP
|
||||
.B \-q, \-\-quiet
|
||||
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.
|
||||
Otherwise, the exit code will be 1.
|
||||
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.
|
||||
.B \-\-has\-results
|
||||
can be used as an alias.
|
||||
.TP
|
||||
.B \-\-show-errors
|
||||
Enable the display of filesystem errors for situations such as insufficient
|
||||
permissions or dead symlinks.
|
||||
|
|
14
src/app.rs
14
src/app.rs
|
@ -504,6 +504,20 @@ pub fn build_app() -> App<'static, 'static> {
|
|||
.long_help("Limit the search to a single result and quit immediately. \
|
||||
This is an alias for '--max-results=1'.")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("quiet")
|
||||
.long("quiet")
|
||||
.short("q")
|
||||
.alias("has-results")
|
||||
.hidden_short_help(true)
|
||||
.conflicts_with_all(&["exec", "exec-batch", "list-details", "max-results"])
|
||||
.long_help(
|
||||
"When the flag is present, the program does not print anything and will \
|
||||
return with an exit code of 0 if there is at least one match. Otherwise, the \
|
||||
exit code will be 1. \
|
||||
'--has-results' can be used as an alias."
|
||||
)
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("show-errors")
|
||||
.long("show-errors")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum ExitCode {
|
||||
Success,
|
||||
HasResults(bool),
|
||||
GeneralError,
|
||||
KilledBySigint,
|
||||
}
|
||||
|
@ -9,6 +10,7 @@ impl From<ExitCode> for i32 {
|
|||
fn from(code: ExitCode) -> Self {
|
||||
match code {
|
||||
ExitCode::Success => 0,
|
||||
ExitCode::HasResults(has_results) => !has_results as i32,
|
||||
ExitCode::GeneralError => 1,
|
||||
ExitCode::KilledBySigint => 130,
|
||||
}
|
||||
|
@ -17,7 +19,7 @@ impl From<ExitCode> for i32 {
|
|||
|
||||
impl ExitCode {
|
||||
fn is_error(self) -> bool {
|
||||
self != ExitCode::Success
|
||||
i32::from(self) != 0
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -346,6 +346,7 @@ fn run() -> Result<ExitCode> {
|
|||
follow_links: matches.is_present("follow"),
|
||||
one_file_system: matches.is_present("one-file-system"),
|
||||
null_separator: matches.is_present("null_separator"),
|
||||
quiet: matches.is_present("quiet"),
|
||||
max_depth: matches
|
||||
.value_of("max-depth")
|
||||
.or_else(|| matches.value_of("rg-depth"))
|
||||
|
|
|
@ -54,6 +54,10 @@ pub struct Options {
|
|||
/// The number of threads to use.
|
||||
pub threads: usize,
|
||||
|
||||
/// If true, the program doesn't print anything and will instead return an exit code of 0
|
||||
/// if there's at least one match. Otherwise, the exit code will be 1.
|
||||
pub quiet: bool,
|
||||
|
||||
/// Time to buffer results internally before streaming to the console. This is useful to
|
||||
/// provide a sorted output, in case the total execution time is shorter than
|
||||
/// `max_buffer_time`.
|
||||
|
|
10
src/walk.rs
10
src/walk.rs
|
@ -233,6 +233,10 @@ fn spawn_receiver(
|
|||
for worker_result in rx {
|
||||
match worker_result {
|
||||
WorkerResult::Entry(value) => {
|
||||
if config.quiet {
|
||||
return ExitCode::HasResults(true);
|
||||
}
|
||||
|
||||
match mode {
|
||||
ReceiverMode::Buffering => {
|
||||
buffer.push(value);
|
||||
|
@ -286,7 +290,11 @@ fn spawn_receiver(
|
|||
}
|
||||
}
|
||||
|
||||
ExitCode::Success
|
||||
if config.quiet {
|
||||
ExitCode::HasResults(false)
|
||||
} else {
|
||||
ExitCode::Success
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1425,6 +1425,19 @@ fn test_exec_with_separator() {
|
|||
);
|
||||
}
|
||||
|
||||
/// Non-zero exit code (--quiet)
|
||||
#[test]
|
||||
fn test_quiet() {
|
||||
let dirs = &[];
|
||||
let files = &["a.foo", "b.foo"];
|
||||
let te = TestEnv::new(dirs, files);
|
||||
|
||||
te.assert_output(&["-q"], "");
|
||||
te.assert_output(&["--quiet"], "");
|
||||
te.assert_output(&["--has-results"], "");
|
||||
te.assert_failure_with_error(&["--quiet", "c.foo"], "")
|
||||
}
|
||||
|
||||
/// Literal search (--fixed-strings)
|
||||
#[test]
|
||||
fn test_fixed_strings() {
|
||||
|
|
Loading…
Reference in New Issue