diff --git a/src/assets.rs b/src/assets.rs index ce3a418d49..151e58102d 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -2,7 +2,7 @@ use std::collections::BTreeMap; use std::ffi::OsStr; use std::fs::{self, File}; use std::io::BufReader; -use std::path::Path; +use std::path::{Path, PathBuf}; use syntect::dumps::{dump_to_file, from_binary, from_reader}; use syntect::highlighting::{Theme, ThemeSet}; @@ -13,7 +13,7 @@ use path_abs::PathAbs; use crate::assets_metadata::AssetsMetadata; use crate::bat_warning; use crate::error::*; -use crate::input::{InputReader, OpenedInput, OpenedInputKind}; +use crate::input::{InputReader, OpenedInput}; use crate::syntax_mapping::{MappingTarget, SyntaxMapping}; #[derive(Debug)] @@ -225,19 +225,15 @@ impl HighlightingAssets { // Get the path of the file: // If this was set by the metadata, that will take priority. // If it wasn't, it will use the real file path (if available). - let path_str = - input - .metadata - .user_provided_name - .as_ref() - .or_else(|| match input.kind { - OpenedInputKind::OrdinaryFile(ref path) => Some(path), - _ => None, - }); - - if let Some(path_str) = path_str { + let path_str: Option = input + .metadata + .user_provided_name + .as_ref() + .cloned() + .or_else(|| input.original_name().map(Into::::into)); + + if let Some(ref path) = path_str { // If a path was provided, we try and detect the syntax based on extension mappings. - let path = Path::new(path_str); let absolute_path = PathAbs::new(path) .ok() .map(|p| p.as_path().to_path_buf()) diff --git a/src/controller.rs b/src/controller.rs index ad78233f56..3f4c7199b6 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -130,27 +130,26 @@ impl<'b> Controller<'b> { let line_changes = if self.config.visible_lines.diff_mode() || (!self.config.loop_through && self.config.style_components.changes()) { - match opened_input.kind { - crate::input::OpenedInputKind::OrdinaryFile(ref path) => { - let diff = get_git_diff(path); - - // 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 + let path = opened_input.original_name(); + if let Some(path) = path { + let diff = get_git_diff(path); + + // Skip files without Git modifications + if self.config.visible_lines.diff_mode() + && diff + .as_ref() + .map(|changes| changes.is_empty()) + .unwrap_or(false) + { continue; } - _ => None, + + diff + } else if self.config.visible_lines.diff_mode() { + // Skip non-file inputs in diff mode + continue; + } else { + None } } else { None diff --git a/src/input.rs b/src/input.rs index 73b541078f..877da2f5ef 100644 --- a/src/input.rs +++ b/src/input.rs @@ -116,6 +116,7 @@ impl<'a> InputKind<'a> { #[derive(Clone, Default)] pub(crate) struct InputMetadata { pub(crate) user_provided_name: Option, + pub(crate) original_name: Option, } pub struct Input<'a> { @@ -179,6 +180,17 @@ impl<'a> OpenedInput<'a> { pub fn close(mut self) -> std::result::Result<(), Vec> { self.close_handles() } + + pub(crate) fn original_name(&self) -> Option<&Path> { + if let Some(original_name) = &self.metadata.original_name { + Some(original_name.as_path()) + } else { + match &self.kind { + OpenedInputKind::OrdinaryFile(path) => Some(path), + _ => None, + } + } + } } impl<'a> Input<'a> { @@ -235,12 +247,14 @@ impl<'a> Input<'a> { pub(crate) fn preprocessed_from(mut self, input: &Input<'a>) -> Self { self.description.clone_from(&input.description); self.description.preprocessed = true; - self.metadata.user_provided_name = input + + self.metadata.user_provided_name = input.metadata.user_provided_name.clone(); + self.metadata.original_name = input .metadata - .user_provided_name + .original_name .as_ref() .cloned() - .or(input.path().map(ToOwned::to_owned)); + .or_else(|| input.path().map(ToOwned::to_owned)); self }