From 9b1b3dda14e059180de55629fec452862a19cf37 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 15 May 2019 21:53:22 +0200 Subject: [PATCH] Only print the header for empty files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of printing ``` ───────┬───────────────────────────────────────────────────────── │ File: some-file ───────┼───────────────────────────────────────────────────────── ───────┴───────────────────────────────────────────────────────── ``` for empty files, bat will now print ``` ───────┬───────────────────────────────────────────────────────── │ File: some-file ───────┴───────────────────────────────────────────────────────── ``` --- src/inputfile.rs | 12 ++++++++---- src/printer.rs | 24 +++++++++++++----------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/inputfile.rs b/src/inputfile.rs index eac9a1bc..6fda8aea 100644 --- a/src/inputfile.rs +++ b/src/inputfile.rs @@ -10,7 +10,7 @@ const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs"); pub struct InputFileReader<'a> { inner: Box, pub first_line: Vec, - pub content_type: ContentType, + pub content_type: Option, } impl<'a> InputFileReader<'a> { @@ -18,9 +18,13 @@ impl<'a> InputFileReader<'a> { let mut first_line = vec![]; reader.read_until(b'\n', &mut first_line).ok(); - let content_type = content_inspector::inspect(&first_line[..]); + let content_type = if first_line.is_empty() { + None + } else { + Some(content_inspector::inspect(&first_line[..])) + }; - if content_type == ContentType::UTF_16LE { + if content_type == Some(ContentType::UTF_16LE) { reader.read_until(0x00, &mut first_line).ok(); } @@ -35,7 +39,7 @@ impl<'a> InputFileReader<'a> { if self.first_line.is_empty() { let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?; - if self.content_type == ContentType::UTF_16LE { + if self.content_type == Some(ContentType::UTF_16LE) { self.inner.read_until(0x00, buf).ok(); } diff --git a/src/printer.rs b/src/printer.rs index 0e68b4d1..b9a157c3 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -78,7 +78,7 @@ pub struct InteractivePrinter<'a> { decorations: Vec>, panel_width: usize, ansi_prefix_sgr: String, - content_type: ContentType, + content_type: Option, pub line_changes: Option, highlighter: Option>, syntax_set: &'a SyntaxSet, @@ -134,7 +134,7 @@ impl<'a> InteractivePrinter<'a> { let mut line_changes = None; - let highlighter = if reader.content_type.is_binary() { + let highlighter = if reader.content_type.map_or(false, |c| c.is_binary()) { None } else { // Get the Git modifications @@ -194,7 +194,7 @@ impl<'a> InteractivePrinter<'a> { impl<'a> Printer for InteractivePrinter<'a> { fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()> { if !self.config.output_components.header() { - if ContentType::BINARY == self.content_type { + if Some(ContentType::BINARY) == self.content_type { let input = match file { InputFile::Ordinary(filename) => format!("file '{}'", filename), _ => "STDIN".into(), @@ -232,9 +232,10 @@ impl<'a> Printer for InteractivePrinter<'a> { }; let mode = match self.content_type { - ContentType::BINARY => " ", - ContentType::UTF_16LE => " ", - ContentType::UTF_16BE => " ", + Some(ContentType::BINARY) => " ", + Some(ContentType::UTF_16LE) => " ", + Some(ContentType::UTF_16BE) => " ", + None => " ", _ => "", }; @@ -247,7 +248,7 @@ impl<'a> Printer for InteractivePrinter<'a> { )?; if self.config.output_components.grid() { - if self.content_type.is_text() { + if self.content_type.map_or(false, |c| c.is_text()) { self.print_horizontal_line(handle, '┼')?; } else { self.print_horizontal_line(handle, '┴')?; @@ -258,7 +259,8 @@ impl<'a> Printer for InteractivePrinter<'a> { } fn print_footer(&mut self, handle: &mut Write) -> Result<()> { - if self.config.output_components.grid() && self.content_type.is_text() { + if self.config.output_components.grid() && self.content_type.map_or(false, |c| c.is_text()) + { self.print_horizontal_line(handle, '┴') } else { Ok(()) @@ -273,13 +275,13 @@ impl<'a> Printer for InteractivePrinter<'a> { line_buffer: &[u8], ) -> Result<()> { let mut line = match self.content_type { - ContentType::BINARY => { + Some(ContentType::BINARY) | None => { return Ok(()); } - ContentType::UTF_16LE => UTF_16LE + Some(ContentType::UTF_16LE) => UTF_16LE .decode(&line_buffer, DecoderTrap::Replace) .map_err(|_| "Invalid UTF-16LE")?, - ContentType::UTF_16BE => UTF_16BE + Some(ContentType::UTF_16BE) => UTF_16BE .decode(&line_buffer, DecoderTrap::Replace) .map_err(|_| "Invalid UTF-16BE")?, _ => String::from_utf8_lossy(&line_buffer).to_string(),