1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-04-15 15:20:33 +01:00

Display binary file content for bat -A

This commit is contained in:
pjsier 2019-08-26 22:05:47 -05:00 committed by David Peter
parent c64ab29739
commit b48b9fcf3b

View File

@ -1,3 +1,4 @@
use std::ascii;
use std::io::Write; use std::io::Write;
use std::vec::Vec; use std::vec::Vec;
@ -141,7 +142,10 @@ impl<'a> InteractivePrinter<'a> {
let mut line_changes = None; let mut line_changes = None;
let highlighter = if reader.content_type.map_or(false, |c| c.is_binary()) { let highlighter = if reader
.content_type
.map_or(false, |c| c.is_binary() && !config.show_nonprintable)
{
None None
} else { } else {
// Get the Git modifications // Get the Git modifications
@ -328,9 +332,18 @@ impl<'a> Printer for InteractivePrinter<'a> {
line_buffer: &[u8], line_buffer: &[u8],
) -> Result<()> { ) -> Result<()> {
let mut line = match self.content_type { let mut line = match self.content_type {
Some(ContentType::BINARY) | None => { None => {
return Ok(()); return Ok(());
} }
Some(ContentType::BINARY) => String::from_utf8(
line_buffer
.as_ref()
.iter()
.map(|b| ascii::escape_default(*b))
.flatten()
.collect(),
)
.unwrap(),
Some(ContentType::UTF_16LE) => UTF_16LE Some(ContentType::UTF_16LE) => UTF_16LE
.decode(&line_buffer, DecoderTrap::Replace) .decode(&line_buffer, DecoderTrap::Replace)
.map_err(|_| "Invalid UTF-16LE")?, .map_err(|_| "Invalid UTF-16LE")?,