on master help sign in
Do not display binary files in interactive mode

closes #248

commitce96df00b6e77841f7c77fdc50e5b6e5b944e375 authorsharkdp <davidpeter@web.de> dateSun 7 Oct 2018, 14:24 (8 years ago) parentf98fc5f
4 files changed+65−16
Cargo.lock+10 Cargo.toml+1 src/main.rs+1 src/printer.rs+53−16
Cargo.lock+10
@@ -50,6 +50,7 @@ dependencies = [
5050 "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
5151 "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
5252 "console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
53+ "content_inspector 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
5354 "directories 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
5455 "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
5556 "git2 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -148,6 +149,14 @@ dependencies = [
148149 "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
149150]
150151
152+[[package]]
153+name = "content_inspector"
154+version = "0.2.2"
155+source = "registry+https://github.com/rust-lang/crates.io-index"
156+dependencies = [
157+ "memchr 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
158+]
159+
151160[[package]]
152161name = "directories"
153162version = "1.0.1"
@@ -830,6 +839,7 @@ dependencies = [
830839"checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"
831840"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
832841"checksum console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd48adf136733979b49e15bc3b4c43cc0d3c85ece7bd08e6daa414c6fcb13e6"
842+"checksum content_inspector 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d902e17eb0038a23c88baa0d78c75fac7968132e73f4fdb9ea77b03d2641b669"
833843"checksum directories 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b106a38a9bf6c763c6c2e2c3332ab7635da453a68a6babca776386b3b287d338"
834844"checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02"
835845"checksum flate2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37847f133aae7acf82bb9577ccd8bda241df836787642654286e79679826a54b"
Cargo.toml+1
@@ -22,6 +22,7 @@ console = "0.6"
2222directories = "1.0"
2323lazy_static = "1.0"
2424wild = "2.0"
25+content_inspector = "0.2.2"
2526
2627[dependencies.git2]
2728version = "0.7"
src/main.rs+1
@@ -13,6 +13,7 @@ extern crate lazy_static;
1313extern crate ansi_term;
1414extern crate atty;
1515extern crate console;
16+extern crate content_inspector;
1617extern crate directories;
1718extern crate git2;
1819extern crate syntect;
src/printer.rs+53−16
@@ -9,6 +9,8 @@ use console::AnsiCodeIterator;
99use syntect::easy::HighlightLines;
1010use syntect::highlighting::Theme;
1111
12+use content_inspector::{self, ContentType};
13+
1214use app::Config;
1315use assets::HighlightingAssets;
1416use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
@@ -69,8 +71,9 @@ pub struct InteractivePrinter<'a> {
6971 decorations: Vec<Box<dyn Decoration>>,
7072 panel_width: usize,
7173 ansi_prefix_sgr: String,
74+ content_type: ContentType,
7275 pub line_changes: Option<LineChanges>,
73− highlighter: HighlightLines<'a>,
76+ highlighter: Option<HighlightLines<'a>>,
7477}
7578
7679impl<'a> InteractivePrinter<'a> {
@@ -118,19 +121,28 @@ impl<'a> InteractivePrinter<'a> {
118121 panel_width = 0;
119122 }
120123
121− // Get the Git modifications
122− let line_changes = if config.output_components.changes() {
123− match file {
124− InputFile::Ordinary(filename) => get_git_diff(filename),
125− _ => None,
126− }
127− } else {
124+ // Determine file content type
125+ let content_type = content_inspector::inspect(&reader.first_line[..]);
126+
127+ let mut line_changes = None;
128+
129+ let highlighter = if content_type.is_binary() {
128130 None
129− };
131+ } else {
132+ // Get the Git modifications
133+ line_changes = if config.output_components.changes() {
134+ match file {
135+ InputFile::Ordinary(filename) => get_git_diff(filename),
136+ _ => None,
137+ }
138+ } else {
139+ None
140+ };
130141
131− // Determine the type of syntax for highlighting
132− let syntax = assets.get_syntax(config.language, file, reader);
133− let highlighter = HighlightLines::new(syntax, theme);
142+ // Determine the type of syntax for highlighting
143+ let syntax = assets.get_syntax(config.language, file, reader);
144+ Some(HighlightLines::new(syntax, theme))
145+ };
134146
135147 InteractivePrinter {
136148 panel_width,
@@ -138,6 +150,7 @@ impl<'a> InteractivePrinter<'a> {
138150 config,
139151 decorations,
140152 ansi_prefix_sgr: String::new(),
153+ content_type,
141154 line_changes,
142155 highlighter,
143156 }
@@ -194,17 +207,33 @@ impl<'a> Printer for InteractivePrinter<'a> {
194207 _ => ("", "STDIN"),
195208 };
196209
197− writeln!(handle, "{}{}", prefix, self.colors.filename.paint(name))?;
210+ let mode = if self.content_type.is_binary() {
211+ " <BINARY>"
212+ } else {
213+ ""
214+ };
215+
216+ writeln!(
217+ handle,
218+ "{}{}{}",
219+ prefix,
220+ self.colors.filename.paint(name),
221+ mode
222+ )?;
198223
199224 if self.config.output_components.grid() {
200− self.print_horizontal_line(handle, '┼')?;
225+ if self.content_type.is_text() {
226+ self.print_horizontal_line(handle, '┼')?;
227+ } else {
228+ self.print_horizontal_line(handle, '┴')?;
229+ }
201230 }
202231
203232 Ok(())
204233 }
205234
206235 fn print_footer(&mut self, handle: &mut Write) -> Result<()> {
207− if self.config.output_components.grid() {
236+ if self.config.output_components.grid() && self.content_type.is_text() {
208237 self.print_horizontal_line(handle, '┴')
209238 } else {
210239 Ok(())
@@ -219,7 +248,15 @@ impl<'a> Printer for InteractivePrinter<'a> {
219248 line_buffer: &[u8],
220249 ) -> Result<()> {
221250 let line = String::from_utf8_lossy(&line_buffer).to_string();
222− let regions = self.highlighter.highlight(line.as_ref());
251+ let regions = {
252+ let highlighter = match self.highlighter {
253+ Some(ref mut highlighter) => highlighter,
254+ _ => {
255+ return Ok(());
256+ }
257+ };
258+ highlighter.highlight(line.as_ref())
259+ };
223260
224261 if out_of_range {
225262 return Ok(());