diff options
author | Dan Davison <dandavison7@gmail.com> | 2021-04-24 08:43:29 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2021-04-24 16:43:24 -0400 |
commit | dc8a6fc3d48c16f82402d46cd83106cf94ed83fe (patch) | |
tree | cf7e9e2198604801c76a2fcb91579fdda1c13ed2 /src | |
parent | 2cc9b5d296f902704ab19eed308b3c145f9a89e6 (diff) |
Add relative-paths option
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 5 | ||||
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | src/delta.rs | 32 | ||||
-rw-r--r-- | src/options/set.rs | 1 | ||||
-rw-r--r-- | src/parse.rs | 6 |
5 files changed, 32 insertions, 14 deletions
@@ -231,6 +231,11 @@ pub struct Opt { /// --file-renamed-label. pub navigate: bool, + #[structopt(long = "relative-paths")] + /// Output all file paths relative to the current directory so that they + /// resolve correctly when clicked on or used in shell commands. + pub relative_paths: bool, + #[structopt(long = "hyperlinks")] /// Render commit hashes, file names, and line numbers as hyperlinks, according to the /// hyperlink spec for terminal emulators: diff --git a/src/config.rs b/src/config.rs index 6d949dff..fbad2e41 100644 --- a/src/config.rs +++ b/src/config.rs @@ -72,6 +72,7 @@ pub struct Config { pub plus_style: Style, pub git_minus_style: Style, pub git_plus_style: Style, + pub relative_paths: bool, pub show_themes: bool, pub side_by_side: bool, pub side_by_side_data: side_by_side::SideBySideData, @@ -239,6 +240,7 @@ impl From<cli::Opt> for Config { plus_style, git_minus_style, git_plus_style, + relative_paths: opt.relative_paths, show_themes: opt.show_themes, side_by_side: opt.side_by_side, side_by_side_data, diff --git a/src/delta.rs b/src/delta.rs index ac820de7..26dbc687 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -230,15 +230,17 @@ impl<'a> StateMachine<'a> { fn handle_diff_stat_line(&mut self) -> std::io::Result<bool> { let mut handled_line = false; - if let Some(cwd) = self.config.cwd_relative_to_repo_root.as_deref() { - if let Some(replacement_line) = parse::relativize_path_in_diff_stat_line( - &self.raw_line, - cwd, - self.config.diff_stat_align_width, - ) { - self.painter.emit()?; - writeln!(self.painter.writer, "{}", replacement_line)?; - handled_line = true + if self.config.relative_paths { + if let Some(cwd) = self.config.cwd_relative_to_repo_root.as_deref() { + if let Some(replacement_line) = parse::relativize_path_in_diff_stat_line( + &self.raw_line, + cwd, + self.config.diff_stat_align_width, + ) { + self.painter.emit()?; + writeln!(self.painter.writer, "{}", replacement_line)?; + handled_line = true + } } } Ok(handled_line) @@ -258,7 +260,11 @@ impl<'a> StateMachine<'a> { let parsed_file_meta_line = parse::parse_file_meta_line( &self.line, self.source == Source::GitDiff, - self.config.cwd_relative_to_repo_root.as_deref(), + if self.config.relative_paths { + self.config.cwd_relative_to_repo_root.as_deref() + } else { + None + }, ); self.minus_file = parsed_file_meta_line.0; self.file_event = parsed_file_meta_line.1; @@ -295,7 +301,11 @@ impl<'a> StateMachine<'a> { let parsed_file_meta_line = parse::parse_file_meta_line( &self.line, self.source == Source::GitDiff, - self.config.cwd_relative_to_repo_root.as_deref(), + if self.config.relative_paths { + self.config.cwd_relative_to_repo_root.as_deref() + } else { + None + }, ); self.plus_file = parsed_file_meta_line.0; self.painter diff --git a/src/options/set.rs b/src/options/set.rs index 8373e86b..6a1a7ad0 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -171,6 +171,7 @@ pub fn set_options( plus_empty_line_marker_style, plus_non_emph_style, raw, + relative_paths, show_themes, side_by_side, tab_width, diff --git a/src/parse.rs b/src/parse.rs index 167ae272..d811fb8a 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -31,7 +31,7 @@ pub enum FileEvent { pub fn parse_file_meta_line( line: &str, git_diff_name: bool, - cwd_relative_to_repo_root: Option<&str>, + relative_path_base: Option<&str>, ) -> (String, FileEvent) { let (mut path, file_event) = match line { line if line.starts_with("--- ") || line.starts_with("+++ ") => { @@ -62,8 +62,8 @@ pub fn parse_file_meta_line( _ => ("".to_string(), FileEvent::NoEvent), }; - if let Some(cwd) = cwd_relative_to_repo_root { - if let Some(relative_path) = pathdiff::diff_paths(&path, cwd) { + if let Some(base) = relative_path_base { + if let Some(relative_path) = pathdiff::diff_paths(&path, base) { if let Some(relative_path) = relative_path.to_str() { path = relative_path.to_owned(); } |