diff --git a/CHANGELOG.md b/CHANGELOG.md index bfd57e69..3e07aa01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - Added support for the `NO_COLOR` environment variable, see #1021 and #1031 (@eth-p) ## Bugfixes + +- Fixed non-printable characters display for redirected output, see #1061 (@gsomix) + ## Other ## Syntaxes diff --git a/src/controller.rs b/src/controller.rs index e11080f5..3fe44a72 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -113,7 +113,7 @@ impl<'b> Controller<'b> { }; let mut printer: Box = if self.config.loop_through { - Box::new(SimplePrinter::new()) + Box::new(SimplePrinter::new(&self.config)) } else { Box::new(InteractivePrinter::new( &self.config, diff --git a/src/printer.rs b/src/printer.rs index addc7972..7e369a6e 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -52,15 +52,17 @@ pub(crate) trait Printer { ) -> Result<()>; } -pub struct SimplePrinter; +pub struct SimplePrinter<'a> { + config: &'a Config<'a> +} -impl SimplePrinter { - pub fn new() -> Self { - SimplePrinter {} +impl<'a> SimplePrinter<'a> { + pub fn new(config: &'a Config) -> Self { + SimplePrinter { config } } } -impl Printer for SimplePrinter { +impl<'a> Printer for SimplePrinter<'a> { fn print_header( &mut self, _handle: &mut dyn Write, @@ -86,7 +88,15 @@ impl Printer for SimplePrinter { line_buffer: &[u8], ) -> Result<()> { if !out_of_range { - handle.write_all(line_buffer)?; + if self.config.show_nonprintable { + let line = replace_nonprintable(line_buffer, self.config.tab_width); + write!(handle, "{}", line)?; + if line_buffer.last() == Some(&b'\n') { + writeln!(handle)?; + } + } else { + handle.write_all(line_buffer)? + }; } Ok(()) } diff --git a/tests/examples/nonprintable.txt b/tests/examples/nonprintable.txt new file mode 100644 index 00000000..00f60e33 Binary files /dev/null and b/tests/examples/nonprintable.txt differ diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index c1fce864..d78f068e 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -716,3 +716,13 @@ fn do_not_detect_different_syntax_for_stdin_and_files() { from_utf8(&cmd_for_stdin.get_output().stdout).expect("output is valid utf-8") ); } + +#[test] +fn show_all_mode() { + bat() + .arg("--show-all") + .arg("nonprintable.txt") + .assert() + .stdout("hello•world␊\n├──┤␍␀␇␈␛") + .stderr(""); +}