1
0
mirror of https://github.com/sharkdp/bat.git synced 2026-02-08 00:32:08 +00:00

Merge pull request #3563 from NORMAL-EX/feat/quiet-empty

feat: add --quiet-empty flag to suppress output on empty input
This commit is contained in:
Keith Hall
2026-02-01 11:15:45 +02:00
committed by GitHub
13 changed files with 73 additions and 1 deletions

View File

@@ -4,6 +4,7 @@
## Features
- Add `--quiet-empty` (`-E`) flag to suppress output when input is empty. Closes #1936, see #3563 (@NORMAL-EX)
- Improve native man pages and command help syntax highlighting by stripping overstriking, see #3517 (@akirk)
## Bugfixes

View File

@@ -186,6 +186,7 @@ Register-ArgumentCompleter -Native -CommandName '{{PROJECT_EXECUTABLE}}' -Script
[CompletionResult]::new('--acknowledgements' , 'acknowledgements' , [CompletionResultType]::ParameterName, 'Show acknowledgements.')
[CompletionResult]::new('--set-terminal-title' , 'set-terminal-title' , [CompletionResultType]::ParameterName, 'Sets terminal title to filenames when using a pager.')
[CompletionResult]::new('--diagnostic' , 'diagnostic' , [CompletionResultType]::ParameterName, 'Show diagnostic information for bug reports.')
[CompletionResult]::new('--quiet-empty' , 'quiet-empty' , [CompletionResultType]::ParameterName, 'Do not produce any output when the input is empty.')
# [CompletionResult]::new('-h' , 'h' , [CompletionResultType]::ParameterName, 'Print this help message.')
[CompletionResult]::new('--help' , 'help' , [CompletionResultType]::ParameterName, 'Print this help message.')
# [CompletionResult]::new('-V' , 'V' , [CompletionResultType]::ParameterName, 'Show version information.')

View File

@@ -100,6 +100,7 @@ _bat() {
--lessopen | \
--no-paging | \
--diagnostic | \
--quiet-empty | \
--acknowledgements | \
-h | --help | \
-V | --version | \
@@ -227,6 +228,7 @@ _bat() {
--lessopen
--completion
--diagnostic
--quiet-empty
--acknowledgements
--set-terminal-title
--help

View File

@@ -61,7 +61,7 @@ function __bat_no_excl_args
-s V -l version \
-l acknowledgements \
-l config-dir -l config-file \
-l diagnostic \
-l diagnostic -l quiet-empty \
-l list-languages -l list-themes
end
@@ -159,6 +159,7 @@ complete -c $bat -l config-file -f -d "Display location of configuration file" -
complete -c $bat -l decorations -x -a "$decorations_opts" -d "When to use --style decorations" -n __bat_no_excl_args
complete -c $bat -l diagnostic -d "Print diagnostic info for bug reports" -n __fish_is_first_arg
complete -c $bat -l quiet-empty -d "Do not produce any output when the input is empty" -n __fish_is_first_arg
complete -c $bat -s d -l diff -d "Only show lines with Git changes" -n __bat_no_excl_args

View File

@@ -62,6 +62,7 @@ _{{PROJECT_EXECUTABLE}}_main() {
--completion='[show shell completion for a certain shell]:shell:(bash fish zsh ps1)'
--set-terminal-title'[sets terminal title to filenames when using a pager]'
--diagnostic'[show diagnostic information for bug reports]'
--quiet-empty'[do not produce any output when the input is empty]'
-P'[disable paging]'
"--no-config[don't use the configuration file]"
"--no-custom-assets[don't load custom assets]"

View File

@@ -282,6 +282,10 @@ Show bat's cache directory.
.IP
Show diagnostic information for bug reports.
.HP
\fB\-\-quiet\-empty\fR
.IP
Do not produce any output when the input is empty (e.g. an empty file or empty stdin). This is useful in scripts where silent behavior is preferred for empty input.
.HP
\fB\-\-acknowledgements\fR
.IP
Show acknowledgements.

View File

@@ -212,6 +212,10 @@ Options:
--diagnostic
Show diagnostic information for bug reports.
-E, --quiet-empty
When this flag is set, bat will produce no output at all when the input is empty. This is
useful when piping commands that may produce empty output, like 'git diff'.
--acknowledgements
Show acknowledgements.

View File

@@ -60,6 +60,8 @@ Options:
Display all supported languages.
--completion <SHELL>
Show shell completion for a certain shell. [possible values: bash, fish, zsh, ps1]
-E, --quiet-empty
Produce no output when the input is empty.
-h, --help
Print help (see more with '--help')
-V, --version

View File

@@ -461,6 +461,7 @@ impl App {
Some("auto") => StripAnsiMode::Auto,
_ => unreachable!("other values for --strip-ansi are not allowed"),
},
quiet_empty: self.matches.get_flag("quiet-empty"),
theme: theme(self.theme_options()).to_string(),
visible_lines: match self.matches.try_contains_id("diff").unwrap_or_default()
&& self.matches.get_flag("diff")

View File

@@ -643,6 +643,18 @@ pub fn build_app(interactive_output: bool) -> Command {
.hide_short_help(true)
.help("Show diagnostic information for bug reports."),
)
.arg(
Arg::new("quiet-empty")
.long("quiet-empty")
.short('E')
.action(ArgAction::SetTrue)
.help("Produce no output when the input is empty.")
.long_help(
"When this flag is set, bat will produce no output at all when \
the input is empty. This is useful when piping commands that may \
produce empty output, like 'git diff'.",
),
)
.arg(
Arg::new("acknowledgements")
.long("acknowledgements")

View File

@@ -107,6 +107,9 @@ pub struct Config<'a> {
// Whether or not to strip ANSI escape codes from the input
pub strip_ansi: StripAnsiMode,
/// Whether or not to produce no output when input is empty
pub quiet_empty: bool,
}
#[cfg(all(feature = "minimal-application", feature = "paging"))]

View File

@@ -454,6 +454,11 @@ impl Printer for InteractivePrinter<'_> {
input: &OpenedInput,
add_header_padding: bool,
) -> Result<()> {
// If input is empty and quiet_empty is enabled, skip all output
if self.content_type.is_none() && self.config.quiet_empty {
return Ok(());
}
if add_header_padding && self.config.style_components.rule() {
self.print_horizontal_line_term(handle, self.colors.rule)?;
}
@@ -556,6 +561,11 @@ impl Printer for InteractivePrinter<'_> {
}
fn print_footer(&mut self, handle: &mut OutputHandle, _input: &OpenedInput) -> Result<()> {
// If input is empty and quiet_empty is enabled, skip footer
if self.content_type.is_none() && self.config.quiet_empty {
return Ok(());
}
if self.config.style_components.grid()
&& (self.content_type.is_some_and(|c| c.is_text())
|| self.config.show_nonprintable

View File

@@ -3654,3 +3654,33 @@ fn plain_with_sized_terminal_width() {
.stdout("hello \nworld\n")
.stderr("");
}
#[test]
fn quiet_empty_suppresses_output_on_empty_stdin() {
bat()
.arg("--quiet-empty")
.write_stdin("")
.assert()
.success()
.stdout("");
}
#[test]
fn quiet_empty_does_not_affect_non_empty_input() {
bat()
.arg("--quiet-empty")
.write_stdin("hello\n")
.assert()
.success()
.stdout("hello\n");
}
#[test]
fn quiet_empty_suppresses_output_on_empty_file() {
bat()
.arg("--quiet-empty")
.arg("empty.txt")
.assert()
.success()
.stdout("");
}