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()