mirror of
https://github.com/sharkdp/bat
synced 2026-06-09 10:03:18 +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:
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
## Features
|
## 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)
|
- Improve native man pages and command help syntax highlighting by stripping overstriking, see #3517 (@akirk)
|
||||||
|
|
||||||
## Bugfixes
|
## Bugfixes
|
||||||
|
|||||||
Vendored
+1
@@ -186,6 +186,7 @@ Register-ArgumentCompleter -Native -CommandName '{{PROJECT_EXECUTABLE}}' -Script
|
|||||||
[CompletionResult]::new('--acknowledgements' , 'acknowledgements' , [CompletionResultType]::ParameterName, 'Show acknowledgements.')
|
[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('--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('--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('-h' , 'h' , [CompletionResultType]::ParameterName, 'Print this help message.')
|
||||||
[CompletionResult]::new('--help' , 'help' , [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.')
|
# [CompletionResult]::new('-V' , 'V' , [CompletionResultType]::ParameterName, 'Show version information.')
|
||||||
|
|||||||
Vendored
+2
@@ -100,6 +100,7 @@ _bat() {
|
|||||||
--lessopen | \
|
--lessopen | \
|
||||||
--no-paging | \
|
--no-paging | \
|
||||||
--diagnostic | \
|
--diagnostic | \
|
||||||
|
--quiet-empty | \
|
||||||
--acknowledgements | \
|
--acknowledgements | \
|
||||||
-h | --help | \
|
-h | --help | \
|
||||||
-V | --version | \
|
-V | --version | \
|
||||||
@@ -227,6 +228,7 @@ _bat() {
|
|||||||
--lessopen
|
--lessopen
|
||||||
--completion
|
--completion
|
||||||
--diagnostic
|
--diagnostic
|
||||||
|
--quiet-empty
|
||||||
--acknowledgements
|
--acknowledgements
|
||||||
--set-terminal-title
|
--set-terminal-title
|
||||||
--help
|
--help
|
||||||
|
|||||||
Vendored
+2
-1
@@ -61,7 +61,7 @@ function __bat_no_excl_args
|
|||||||
-s V -l version \
|
-s V -l version \
|
||||||
-l acknowledgements \
|
-l acknowledgements \
|
||||||
-l config-dir -l config-file \
|
-l config-dir -l config-file \
|
||||||
-l diagnostic \
|
-l diagnostic -l quiet-empty \
|
||||||
-l list-languages -l list-themes
|
-l list-languages -l list-themes
|
||||||
end
|
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 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 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
|
complete -c $bat -s d -l diff -d "Only show lines with Git changes" -n __bat_no_excl_args
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
@@ -62,6 +62,7 @@ _{{PROJECT_EXECUTABLE}}_main() {
|
|||||||
--completion='[show shell completion for a certain shell]:shell:(bash fish zsh ps1)'
|
--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]'
|
--set-terminal-title'[sets terminal title to filenames when using a pager]'
|
||||||
--diagnostic'[show diagnostic information for bug reports]'
|
--diagnostic'[show diagnostic information for bug reports]'
|
||||||
|
--quiet-empty'[do not produce any output when the input is empty]'
|
||||||
-P'[disable paging]'
|
-P'[disable paging]'
|
||||||
"--no-config[don't use the configuration file]"
|
"--no-config[don't use the configuration file]"
|
||||||
"--no-custom-assets[don't load custom assets]"
|
"--no-custom-assets[don't load custom assets]"
|
||||||
|
|||||||
Vendored
+4
@@ -282,6 +282,10 @@ Show bat's cache directory.
|
|||||||
.IP
|
.IP
|
||||||
Show diagnostic information for bug reports.
|
Show diagnostic information for bug reports.
|
||||||
.HP
|
.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
|
\fB\-\-acknowledgements\fR
|
||||||
.IP
|
.IP
|
||||||
Show acknowledgements.
|
Show acknowledgements.
|
||||||
|
|||||||
@@ -212,6 +212,10 @@ Options:
|
|||||||
--diagnostic
|
--diagnostic
|
||||||
Show diagnostic information for bug reports.
|
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
|
--acknowledgements
|
||||||
Show acknowledgements.
|
Show acknowledgements.
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ Options:
|
|||||||
Display all supported languages.
|
Display all supported languages.
|
||||||
--completion <SHELL>
|
--completion <SHELL>
|
||||||
Show shell completion for a certain shell. [possible values: bash, fish, zsh, ps1]
|
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
|
-h, --help
|
||||||
Print help (see more with '--help')
|
Print help (see more with '--help')
|
||||||
-V, --version
|
-V, --version
|
||||||
|
|||||||
@@ -461,6 +461,7 @@ impl App {
|
|||||||
Some("auto") => StripAnsiMode::Auto,
|
Some("auto") => StripAnsiMode::Auto,
|
||||||
_ => unreachable!("other values for --strip-ansi are not allowed"),
|
_ => unreachable!("other values for --strip-ansi are not allowed"),
|
||||||
},
|
},
|
||||||
|
quiet_empty: self.matches.get_flag("quiet-empty"),
|
||||||
theme: theme(self.theme_options()).to_string(),
|
theme: theme(self.theme_options()).to_string(),
|
||||||
visible_lines: match self.matches.try_contains_id("diff").unwrap_or_default()
|
visible_lines: match self.matches.try_contains_id("diff").unwrap_or_default()
|
||||||
&& self.matches.get_flag("diff")
|
&& self.matches.get_flag("diff")
|
||||||
|
|||||||
@@ -643,6 +643,18 @@ pub fn build_app(interactive_output: bool) -> Command {
|
|||||||
.hide_short_help(true)
|
.hide_short_help(true)
|
||||||
.help("Show diagnostic information for bug reports."),
|
.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(
|
||||||
Arg::new("acknowledgements")
|
Arg::new("acknowledgements")
|
||||||
.long("acknowledgements")
|
.long("acknowledgements")
|
||||||
|
|||||||
@@ -107,6 +107,9 @@ pub struct Config<'a> {
|
|||||||
|
|
||||||
// Whether or not to strip ANSI escape codes from the input
|
// Whether or not to strip ANSI escape codes from the input
|
||||||
pub strip_ansi: StripAnsiMode,
|
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"))]
|
#[cfg(all(feature = "minimal-application", feature = "paging"))]
|
||||||
|
|||||||
@@ -454,6 +454,11 @@ impl Printer for InteractivePrinter<'_> {
|
|||||||
input: &OpenedInput,
|
input: &OpenedInput,
|
||||||
add_header_padding: bool,
|
add_header_padding: bool,
|
||||||
) -> Result<()> {
|
) -> 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() {
|
if add_header_padding && self.config.style_components.rule() {
|
||||||
self.print_horizontal_line_term(handle, self.colors.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<()> {
|
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()
|
if self.config.style_components.grid()
|
||||||
&& (self.content_type.is_some_and(|c| c.is_text())
|
&& (self.content_type.is_some_and(|c| c.is_text())
|
||||||
|| self.config.show_nonprintable
|
|| self.config.show_nonprintable
|
||||||
|
|||||||
@@ -3654,3 +3654,33 @@ fn plain_with_sized_terminal_width() {
|
|||||||
.stdout("hello \nworld\n")
|
.stdout("hello \nworld\n")
|
||||||
.stderr("");
|
.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("");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user