diff options
author | sharkdp <davidpeter@web.de> | 2018-10-07 14:24:47 +0200 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2018-10-07 15:34:37 +0200 |
commit | ce96df00b6e77841f7c77fdc50e5b6e5b944e375 (patch) | |
tree | 382a70c4f44890092521535a2b9b2fb42c00566e /src | |
parent | f98fc5f06afd76a8eeb914ef7c2caf384834eade (diff) |
Do not display binary files in interactive mode
closes #248
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/printer.rs | 69 |
2 files changed, 54 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index 5b5941be..b0e88d15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/printer.rs b/src/printer.rs index 98011fc3..d4087c05 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -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 { + // Determine file content type + let content_type = content_inspector::inspect(&reader.first_line[..]); + + 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); - let highlighter = HighlightLines::new(syntax, theme); + // 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(()); |