Do not show style decorations when piped with decorations=auto

`--decorations=auto` is documented to enable decorations only when stdout is
an interactive terminal, but `--color=always` forces the printer path and the
requested style components were applied anyway. `bat --color=always file | cat`
therefore printed the header, grid, and line numbers.

Return an empty component set when stdout is not interactive unless
decorations are explicitly `always` or `--force-colorization` is used.
Explicit `-n` and `-b` flags are still honored, matching the existing `cat -n`
compatibility behavior, and `--diff` keeps its change markers.

Adds integration tests for piped color output with auto decorations, with
`--style=full`, and with `--decorations=always`.

Fixes #3710
This commit is contained in:
draarivpatel-ui committed 2026-11-25 18:38:10 -06:00
1 parent 59bc9df321
commit 6d35a98db5
2 files changed
+63

No files matched your search

+24
View File
@@ -599,6 +599,30 @@ impl App {
])));
}
// With `--decorations=auto` (the default), decorations may only be shown when
// stdout is an interactive terminal. `-n`/`-b` above and `--decorations=always`
// (including `--force-colorization`, which implies it) are explicit exceptions.
let decorations = self
.matches
.get_one::<String>("decorations")
.map(|s| s.as_str());
if !self.interactive_output
&& decorations != Some("always")
&& !self.matches.get_flag("force-colorization")
{
let mut components = HashSet::new();
// When --diff is active, preserve change markers and snip separators
// so that diff output remains visually useful.
if self.matches.try_contains_id("diff").unwrap_or_default()
&& self.matches.get_flag("diff")
{
#[cfg(feature = "git")]
components.insert(StyleComponent::Changes);
components.insert(StyleComponent::Snip);
}
return Some(StyleComponents(components));
}
// Plain if `--plain` is specified at least once.
if self.matches.get_count("plain") > 0 {
let mut components = HashSet::from([StyleComponent::Plain]);
+39
View File
@@ -731,6 +731,45 @@ fn piped_output_with_line_numbers_with_header_grid_style_flag() {
.stdout("hello\nworld\n");
}
#[test]
fn piped_output_with_color_always_and_auto_decorations_is_plain() {
// `--decorations=auto` must not enable decorations when stdout is not a terminal,
// even if `--color=always` forces colorized output.
bat()
.arg("multiline.txt")
.arg("--color=always")
.arg("--decorations=auto")
.assert()
.success()
.stdout(predicate::str::contains("\u{1b}["))
.stdout(predicate::str::contains("File:").not())
.stdout(predicate::str::contains("\u{2502}").not());
}
#[test]
fn piped_output_with_color_always_ignores_style_decorations_when_auto() {
bat()
.arg("multiline.txt")
.arg("--color=always")
.arg("--decorations=auto")
.arg("--style=full")
.assert()
.success()
.stdout(predicate::str::contains("File:").not())
.stdout(predicate::str::contains("\u{2502}").not());
}
#[test]
fn piped_output_with_color_always_and_always_decorations_keeps_decorations() {
bat()
.arg("multiline.txt")
.arg("--color=always")
.arg("--decorations=always")
.assert()
.success()
.stdout(predicate::str::contains("File:"));
}
#[test]
fn piped_output_with_auto_style() {
bat()