summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2019-05-15 21:53:22 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2019-05-15 22:37:03 +0200
commit9b1b3dda14e059180de55629fec452862a19cf37 (patch)
tree9137220070b303b241afe21df03b7d21434be60e
parentae7db313a6683abf81e09e7b100b60a1d6df1c0f (diff)
Only print the header for empty files
Instead of printing ``` ───────┬───────────────────────────────────────────────────────── │ File: some-file ───────┼───────────────────────────────────────────────────────── ───────┴───────────────────────────────────────────────────────── ``` for empty files, bat will now print ``` ───────┬───────────────────────────────────────────────────────── │ File: some-file <EMPTY> ───────┴───────────────────────────────────────────────────────── ```
-rw-r--r--src/inputfile.rs12
-rw-r--r--src/printer.rs24
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<dyn BufRead + 'a>,
pub first_line: Vec<u8>,
- pub content_type: ContentType,
+ pub content_type: Option<ContentType>,
}
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<Box<dyn Decoration>>,
panel_width: usize,
ansi_prefix_sgr: String,
- content_type: ContentType,
+ content_type: Option<ContentType>,
pub line_changes: Option<LineChanges>,
highlighter: Option<HighlightLines<'a>>,
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 => " <BINARY>",
- ContentType::UTF_16LE => " <UTF-16LE>",
- ContentType::UTF_16BE => " <UTF-16BE>",
+ Some(ContentType::BINARY) => " <BINARY>",
+ Some(ContentType::UTF_16LE) => " <UTF-16LE>",
+ Some(ContentType::UTF_16BE) => " <UTF-16BE>",
+ None => " <EMPTY>",
_ => "",
};
@@ -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(),