mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-13 22:28:26 +00:00
Only print the header for empty files
Instead of printing ``` ───────┬───────────────────────────────────────────────────────── │ File: some-file ───────┼───────────────────────────────────────────────────────── ───────┴───────────────────────────────────────────────────────── ``` for empty files, bat will now print ``` ───────┬───────────────────────────────────────────────────────── │ File: some-file <EMPTY> ───────┴───────────────────────────────────────────────────────── ```
This commit is contained in:
parent
ae7db313a6
commit
9b1b3dda14
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user