From de414ed631e96c3cff6d2dea19c04aaf40d9acb2 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Thu, 11 Dec 2025 05:45:44 +0100 Subject: [PATCH] Limit overstrike stripping to man pages and help --- src/printer.rs | 21 +++++++++++++-------- tests/integration_tests.rs | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 8cd0e1b0..499106ec 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -270,14 +270,18 @@ impl<'a> InteractivePrinter<'a> { || matches!(config.binary, BinaryBehavior::AsText)) && (config.colored_output || config.strip_ansi == StripAnsiMode::Auto); - let (is_plain_text, highlighter_from_set) = if needs_to_match_syntax { + let (is_plain_text, highlighter_from_set, syntax_name) = if needs_to_match_syntax { // Determine the type of syntax for highlighting const PLAIN_TEXT_SYNTAX: &str = "Plain Text"; match assets.get_syntax(config.language, input, &config.syntax_mapping) { - Ok(syntax_in_set) => ( - syntax_in_set.syntax.name == PLAIN_TEXT_SYNTAX, - Some(HighlighterFromSet::new(syntax_in_set, theme)), - ), + Ok(syntax_in_set) => { + let syntax_name = syntax_in_set.syntax.name.as_str(); + ( + syntax_name == PLAIN_TEXT_SYNTAX, + Some(HighlighterFromSet::new(syntax_in_set, theme)), + syntax_name, + ) + } Err(Error::UndetectedSyntax(_)) => ( true, @@ -287,12 +291,13 @@ impl<'a> InteractivePrinter<'a> { .map(|s| HighlighterFromSet::new(s, theme)) .expect("A plain text syntax is available"), ), + PLAIN_TEXT_SYNTAX, ), Err(e) => return Err(e), } } else { - (false, None) + (false, None, "") }; // Determine when to strip ANSI sequences @@ -304,8 +309,8 @@ impl<'a> InteractivePrinter<'a> { _ => false, }; - // Strip overstrike only when we have syntax highlighting (not plain text). - let strip_overstrike = !is_plain_text; + // Strip overstrike for man pages and help messages. + let strip_overstrike = matches!(syntax_name, "Manpage" | "Command Help"); Ok(InteractivePrinter { panel_width, diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 078daaf8..8896d9fa 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -2556,7 +2556,7 @@ fn no_strip_overstrike_for_plain_text() { #[test] fn strip_overstrike_with_syntax_highlighting() { - // Overstrike is stripped when syntax highlighting is applied (e.g., for help) + // Overstrike is stripped for certain syntax highlighting like command help. bat() .arg("--force-colorization") .arg("--language=help") @@ -2581,6 +2581,19 @@ fn strip_overstrike_for_manpage_syntax() { .stderr(""); } +#[test] +fn no_strip_overstrike_for_other_syntax() { + // Overstrike is NOT stripped for other syntaxes (e.g., Rust) + bat() + .arg("--force-colorization") + .arg("--language=rust") + .arg("overstrike.txt") + .assert() + .success() + .stdout(predicate::str::contains("\x08")) + .stderr(""); +} + #[test] fn show_all_shows_backspace_with_caret_notation() { // --show-all should display backspace characters (not strip them)