From 23afc8e90c4c98c405a07c1d0bfc54a8d719852c Mon Sep 17 00:00:00 2001 From: sharkdp Date: Fri, 24 Apr 2020 16:06:04 +0200 Subject: [PATCH] Skip non-file inputs when using --diff --- src/config.rs | 2 +- src/controller.rs | 36 ++++++++++++++++++++---------------- src/printer.rs | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3c24b77f..58d30a3b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,7 +16,7 @@ pub enum VisibleLines { } impl VisibleLines { - pub fn diff_context(&self) -> bool { + pub fn diff_mode(&self) -> bool { match self { Self::Ranges(_) => false, #[cfg(feature = "git")] diff --git a/src/controller.rs b/src/controller.rs index 45156a26..7d760a2f 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -73,26 +73,30 @@ impl<'b> Controller<'b> { } Ok(mut opened_input) => { #[cfg(feature = "git")] - let line_changes = if self.config.visible_lines.diff_context() + let line_changes = if self.config.visible_lines.diff_mode() || (!self.config.loop_through && self.config.style_components.changes()) { - if let crate::input::OpenedInputKind::OrdinaryFile(ref path) = - opened_input.kind - { - let diff = get_git_diff(path); + match opened_input.kind { + crate::input::OpenedInputKind::OrdinaryFile(ref path) => { + let diff = get_git_diff(path); - if self.config.visible_lines.diff_context() - && diff - .as_ref() - .map(|changes| changes.is_empty()) - .unwrap_or(false) - { + // Skip files without Git modifications + if self.config.visible_lines.diff_mode() + && diff + .as_ref() + .map(|changes| changes.is_empty()) + .unwrap_or(false) + { + continue; + } + + diff + } + _ if self.config.visible_lines.diff_mode() => { + // Skip non-file inputs in diff mode continue; } - - diff - } else { - None + _ => None, } } else { None @@ -134,7 +138,7 @@ impl<'b> Controller<'b> { printer: &mut dyn Printer, writer: &mut dyn Write, input: &mut OpenedInput, - #[cfg(feature = "git")] line_changes: &Option, + line_changes: &Option, ) -> Result<()> { if !input.reader.first_line.is_empty() || self.config.style_components.header() { printer.print_header(writer, input)?; diff --git a/src/printer.rs b/src/printer.rs index 09bf4ecf..d022dbcf 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -101,7 +101,7 @@ impl<'a> InteractivePrinter<'a> { config: &'a Config, assets: &'a HighlightingAssets, input: &mut OpenedInput, - #[cfg(feature = "git")] line_changes: &'a Option, + line_changes: &'a Option, ) -> Self { let theme = assets.get_theme(&config.theme);