on master help sign in
Merge pull request #1066 from gsomix/feature/1061-show-all-redirected
3a62e3dDavid Peter
CHANGELOG.md+3
@@ -5,6 +5,9 @@
5- Added support for the `NO_COLOR` environment variable, see #1021 and #1031 (@eth-p)
5- Added support for the `NO_COLOR` environment variable, see #1021 and #1031 (@eth-p)
6
6
7## Bugfixes
7## Bugfixes
8+
9+- Fixed non-printable characters display for redirected output, see #1061 (@gsomix)
10+
8## Other
11## Other
9## Syntaxes
12## Syntaxes
10
13
src/controller.rs+1−1
@@ -113,7 +113,7 @@ impl<'b> Controller<'b> {
113 };
113 };
114
114
115 let mut printer: Box<dyn Printer> = if self.config.loop_through {
115 let mut printer: Box<dyn Printer> = if self.config.loop_through {
116− Box::new(SimplePrinter::new())
116+ Box::new(SimplePrinter::new(&self.config))
117 } else {
117 } else {
118 Box::new(InteractivePrinter::new(
118 Box::new(InteractivePrinter::new(
119 &self.config,
119 &self.config,
src/printer.rs+16−6
@@ -52,15 +52,17 @@ pub(crate) trait Printer {
52 ) -> Result<()>;
52 ) -> Result<()>;
53}
53}
54
54
55−pub struct SimplePrinter;
55+pub struct SimplePrinter<'a> {
56+ config: &'a Config<'a>
57+}
56
58
57−impl SimplePrinter {
59+impl<'a> SimplePrinter<'a> {
58− pub fn new() -> Self {
60+ pub fn new(config: &'a Config) -> Self {
59− SimplePrinter {}
61+ SimplePrinter { config }
60 }
62 }
61}
63}
62
64
63−impl Printer for SimplePrinter {
65+impl<'a> Printer for SimplePrinter<'a> {
64 fn print_header(
66 fn print_header(
65 &mut self,
67 &mut self,
66 _handle: &mut dyn Write,
68 _handle: &mut dyn Write,
@@ -86,7 +88,15 @@ impl Printer for SimplePrinter {
86 line_buffer: &[u8],
88 line_buffer: &[u8],
87 ) -> Result<()> {
89 ) -> Result<()> {
88 if !out_of_range {
90 if !out_of_range {
91+ if self.config.show_nonprintable {
92+ let line = replace_nonprintable(line_buffer, self.config.tab_width);
93+ write!(handle, "{}", line)?;
94+ if line_buffer.last() == Some(&b'\n') {
95+ writeln!(handle)?;
96+ }
97+ } else {
89− handle.write_all(line_buffer)?;
98+ handle.write_all(line_buffer)?
99+ };
90 }
100 }
91 Ok(())
101 Ok(())
92 }
102 }
tests/examples/nonprintable.txtnew file

Binary file not shown.

tests/integration_tests.rs+10
@@ -716,3 +716,13 @@ fn do_not_detect_different_syntax_for_stdin_and_files() {
716 from_utf8(&cmd_for_stdin.get_output().stdout).expect("output is valid utf-8")
716 from_utf8(&cmd_for_stdin.get_output().stdout).expect("output is valid utf-8")
717 );
717 );
718}
718}
719+
720+#[test]
721+fn show_all_mode() {
722+ bat()
723+ .arg("--show-all")
724+ .arg("nonprintable.txt")
725+ .assert()
726+ .stdout("hello•world␊\n├──┤␍␀␇␈␛")
727+ .stderr("");
728+}