on master help sign in
Merge pull request #1066 from gsomix/feature/1061-show-all-redirected

Enable non-printable characters for redirected output

commit3a62e3d18835dce57294e5cec48e9d878351629b authorDavid Peter <sharkdp@users.noreply.github.com> dateThu 2 Jul 2020, 08:21 (6 years ago) parent963d9ee3c5ce9f
5 files changed+30−7
CHANGELOG.md+3 src/controller.rs+1−1 src/printer.rs+16−6 tests/examples/nonprintable.txt tests/integration_tests.rs+10
CHANGELOG.md+3
@@ -5,6 +5,9 @@
55- Added support for the `NO_COLOR` environment variable, see #1021 and #1031 (@eth-p)
66
77## Bugfixes
8+
9+- Fixed non-printable characters display for redirected output, see #1061 (@gsomix)
10+
811## Other
912## Syntaxes
1013
src/controller.rs+1−1
@@ -113,7 +113,7 @@ impl<'b> Controller<'b> {
113113 };
114114
115115 let mut printer: Box<dyn Printer> = if self.config.loop_through {
116− Box::new(SimplePrinter::new())
116+ Box::new(SimplePrinter::new(&self.config))
117117 } else {
118118 Box::new(InteractivePrinter::new(
119119 &self.config,
src/printer.rs+16−6
@@ -52,15 +52,17 @@ pub(crate) trait Printer {
5252 ) -> Result<()>;
5353}
5454
55−pub struct SimplePrinter;
55+pub struct SimplePrinter<'a> {
56+ config: &'a Config<'a>
57+}
5658
57−impl SimplePrinter {
58− pub fn new() -> Self {
59− SimplePrinter {}
59+impl<'a> SimplePrinter<'a> {
60+ pub fn new(config: &'a Config) -> Self {
61+ SimplePrinter { config }
6062 }
6163}
6264
63−impl Printer for SimplePrinter {
65+impl<'a> Printer for SimplePrinter<'a> {
6466 fn print_header(
6567 &mut self,
6668 _handle: &mut dyn Write,
@@ -86,7 +88,15 @@ impl Printer for SimplePrinter {
8688 line_buffer: &[u8],
8789 ) -> Result<()> {
8890 if !out_of_range {
89− handle.write_all(line_buffer)?;
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 {
98+ handle.write_all(line_buffer)?
99+ };
90100 }
91101 Ok(())
92102 }
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() {
716716 from_utf8(&cmd_for_stdin.get_output().stdout).expect("output is valid utf-8")
717717 );
718718}
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+}