Skip to content

Commit

Permalink
Fix git changes not being displayed when using lessopen
Browse files Browse the repository at this point in the history
  • Loading branch information
eth-p committed Mar 24, 2021
1 parent e094c4b commit d4a55f8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
24 changes: 10 additions & 14 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)]
Expand Down Expand Up @@ -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<PathBuf> = input
.metadata
.user_provided_name
.as_ref()
.cloned()
.or_else(|| input.original_name().map(Into::<PathBuf>::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())
Expand Down
37 changes: 18 additions & 19 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl<'a> InputKind<'a> {
#[derive(Clone, Default)]
pub(crate) struct InputMetadata {
pub(crate) user_provided_name: Option<PathBuf>,
pub(crate) original_name: Option<PathBuf>,
}

pub struct Input<'a> {
Expand Down Expand Up @@ -179,6 +180,17 @@ impl<'a> OpenedInput<'a> {
pub fn close(mut self) -> std::result::Result<(), Vec<Error>> {
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> {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d4a55f8

Please sign in to comment.