From 6d35a98db5485214e00a3cb533655ccd405014ff Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:38:10 -0600 Subject: [PATCH] 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 --- src/bin/bat/app.rs | 24 +++++++++++++++++++++++ tests/integration_tests.rs | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 66f99541..351a29b8 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -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::("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]); diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 9fad1207..ca36da3e 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -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()