Skip to content

Commit

Permalink
Do not display binary files in interactive mode
Browse files Browse the repository at this point in the history
closes #248
  • Loading branch information
sharkdp committed Oct 7, 2018
1 parent b9853b3 commit 34ba077
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
69 changes: 53 additions & 16 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -118,26 +121,36 @@ 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,
colors,
config,
decorations,
ansi_prefix_sgr: String::new(),
content_type,
line_changes,
highlighter,
}
Expand Down Expand Up @@ -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(())
Expand All @@ -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(());