1
0
mirror of https://github.com/sharkdp/bat.git synced 2026-02-08 00:32:08 +00:00

Strip overstriking to better support man pages

This commit is contained in:
Alex Kirk
2025-12-05 12:15:48 +01:00
parent eb2a8e29c7
commit 51bdaa5f88
2 changed files with 78 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::vec::Vec;
use nu_ansi_term::Color::{Fixed, Green, Red, Yellow};
@@ -29,8 +30,7 @@ use crate::error::*;
use crate::input::OpenedInput;
use crate::line_range::{MaxBufferedLineNumber, RangeCheckResult};
use crate::output::OutputHandle;
use crate::preprocessor::strip_ansi;
use crate::preprocessor::{expand_tabs, replace_nonprintable};
use crate::preprocessor::{expand_tabs, replace_nonprintable, strip_ansi, strip_overstrike};
use crate::style::StyleComponent;
use crate::terminal::{as_terminal_escaped, to_ansi_color};
use crate::vscreen::{AnsiStyle, EscapeSequence, EscapeSequenceIterator};
@@ -152,7 +152,7 @@ impl Printer for SimplePrinter<'_> {
self.config.nonprintable_notation,
);
write!(handle, "{line}")?;
} else {
} else if self.config.binary == BinaryBehavior::AsText {
match handle {
OutputHandle::IoWrite(handle) => handle.write_all(line_buffer)?,
OutputHandle::FmtWrite(handle) => {
@@ -166,6 +166,23 @@ impl Printer for SimplePrinter<'_> {
)?;
}
}
} else {
match handle {
OutputHandle::IoWrite(handle) => {
// Only strip overstrike for valid UTF-8, otherwise write raw bytes
if let Ok(line) = std::str::from_utf8(line_buffer) {
let line = strip_overstrike(line);
handle.write_all(line.as_bytes())?;
} else {
handle.write_all(line_buffer)?;
}
}
OutputHandle::FmtWrite(handle) => {
let line = String::from_utf8_lossy(line_buffer);
let line = strip_overstrike(&line);
write!(handle, "{line}")?;
}
}
};
}
Ok(())
@@ -622,6 +639,11 @@ impl Printer for InteractivePrinter<'_> {
}
};
// Strip overstrike sequences (used by man pages for bold/underline).
if line.contains('\x08') {
line = Cow::Owned(strip_overstrike(&line).into_owned());
}
// If ANSI escape sequences are supposed to be stripped, do it before syntax highlighting.
if self.strip_ansi {
line = strip_ansi(&line).into()