Do not display binary files in interactive mode

closes #248
This commit is contained in:
sharkdp 2018-10-07 14:24:47 +02:00 committed by David Peter
parent f98fc5f06a
commit ce96df00b6
4 changed files with 66 additions and 17 deletions

10
Cargo.lock generated
View File

@ -50,6 +50,7 @@ dependencies = [
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"content_inspector 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"directories 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"git2 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -148,6 +149,14 @@ dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "content_inspector"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "directories"
version = "1.0.1"
@ -830,6 +839,7 @@ dependencies = [
"checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd48adf136733979b49e15bc3b4c43cc0d3c85ece7bd08e6daa414c6fcb13e6"
"checksum content_inspector 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d902e17eb0038a23c88baa0d78c75fac7968132e73f4fdb9ea77b03d2641b669"
"checksum directories 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b106a38a9bf6c763c6c2e2c3332ab7635da453a68a6babca776386b3b287d338"
"checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02"
"checksum flate2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37847f133aae7acf82bb9577ccd8bda241df836787642654286e79679826a54b"

View File

@ -22,6 +22,7 @@ console = "0.6"
directories = "1.0"
lazy_static = "1.0"
wild = "2.0"
content_inspector = "0.2.2"
[dependencies.git2]
version = "0.7"

View File

@ -13,6 +13,7 @@ extern crate lazy_static;
extern crate ansi_term;
extern crate atty;
extern crate console;
extern crate content_inspector;
extern crate directories;
extern crate git2;
extern crate syntect;

View File

@ -9,6 +9,8 @@ use console::AnsiCodeIterator;
use syntect::easy::HighlightLines;
use syntect::highlighting::Theme;
use content_inspector::{self, ContentType};
use app::Config;
use assets::HighlightingAssets;
use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
@ -69,8 +71,9 @@ pub struct InteractivePrinter<'a> {
decorations: Vec<Box<dyn Decoration>>,
panel_width: usize,
ansi_prefix_sgr: String,
content_type: ContentType,
pub line_changes: Option<LineChanges>,
highlighter: HighlightLines<'a>,
highlighter: Option<HighlightLines<'a>>,
}
impl<'a> InteractivePrinter<'a> {
@ -118,19 +121,28 @@ impl<'a> InteractivePrinter<'a> {
panel_width = 0;
}
// Get the Git modifications
let line_changes = if config.output_components.changes() {
match file {
InputFile::Ordinary(filename) => get_git_diff(filename),
_ => None,
}
} else {
None
};
// Determine file content type
let content_type = content_inspector::inspect(&reader.first_line[..]);
// Determine the type of syntax for highlighting
let syntax = assets.get_syntax(config.language, file, reader);
let highlighter = HighlightLines::new(syntax, theme);
let mut line_changes = None;
let highlighter = if content_type.is_binary() {
None
} else {
// Get the Git modifications
line_changes = if config.output_components.changes() {
match file {
InputFile::Ordinary(filename) => get_git_diff(filename),
_ => None,
}
} else {
None
};
// Determine the type of syntax for highlighting
let syntax = assets.get_syntax(config.language, file, reader);
Some(HighlightLines::new(syntax, theme))
};
InteractivePrinter {
panel_width,
@ -138,6 +150,7 @@ impl<'a> InteractivePrinter<'a> {
config,
decorations,
ansi_prefix_sgr: String::new(),
content_type,
line_changes,
highlighter,
}
@ -194,17 +207,33 @@ impl<'a> Printer for InteractivePrinter<'a> {
_ => ("", "STDIN"),
};
writeln!(handle, "{}{}", prefix, self.colors.filename.paint(name))?;
let mode = if self.content_type.is_binary() {
" <BINARY>"
} else {
""
};
writeln!(
handle,
"{}{}{}",
prefix,
self.colors.filename.paint(name),
mode
)?;
if self.config.output_components.grid() {
self.print_horizontal_line(handle, '┼')?;
if self.content_type.is_text() {
self.print_horizontal_line(handle, '┼')?;
} else {
self.print_horizontal_line(handle, '┴')?;
}
}
Ok(())
}
fn print_footer(&mut self, handle: &mut Write) -> Result<()> {
if self.config.output_components.grid() {
if self.config.output_components.grid() && self.content_type.is_text() {
self.print_horizontal_line(handle, '┴')
} else {
Ok(())
@ -219,7 +248,15 @@ impl<'a> Printer for InteractivePrinter<'a> {
line_buffer: &[u8],
) -> Result<()> {
let line = String::from_utf8_lossy(&line_buffer).to_string();
let regions = self.highlighter.highlight(line.as_ref());
let regions = {
let highlighter = match self.highlighter {
Some(ref mut highlighter) => highlighter,
_ => {
return Ok(());
}
};
highlighter.highlight(line.as_ref())
};
if out_of_range {
return Ok(());