Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply hyperlinks to diff stat file paths #1035

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/handlers/diff_stat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::borrow::Cow;

use crate::config::Config;
use crate::delta::{State, StateMachine};
use crate::features;
use crate::utils;

impl<'a> StateMachine<'a> {
#[inline]
Expand All @@ -17,11 +21,9 @@ impl<'a> StateMachine<'a> {
let mut handled_line = false;
if self.config.relative_paths {
if let Some(cwd) = self.config.cwd_relative_to_repo_root.as_deref() {
if let Some(replacement_line) = relativize_path_in_diff_stat_line(
&self.raw_line,
cwd,
self.config.diff_stat_align_width,
) {
if let Some(replacement_line) =
relativize_path_in_diff_stat_line(&self.raw_line, cwd, self.config)
{
self.painter.emit()?;
writeln!(self.painter.writer, "{}", replacement_line)?;
handled_line = true
Expand All @@ -44,18 +46,32 @@ lazy_static! {
pub fn relativize_path_in_diff_stat_line(
line: &str,
cwd_relative_to_repo_root: &str,
diff_stat_align_width: usize,
config: &Config,
) -> Option<String> {
let caps = DIFF_STAT_LINE_REGEX.captures(line)?;
let path_relative_to_repo_root = caps.get(1).unwrap().as_str();

let relative_path =
pathdiff::diff_paths(path_relative_to_repo_root, cwd_relative_to_repo_root)?;
let relative_path = relative_path.to_str()?;
let formatted_path = match (
config.hyperlinks,
utils::path::absolute_path(path_relative_to_repo_root, config),
) {
(true, Some(absolute_path)) => features::hyperlinks::format_osc8_file_hyperlink(
absolute_path,
None,
relative_path,
config,
),
_ => Cow::from(relative_path),
};
let suffix = caps.get(2).unwrap().as_str();
let pad_width = diff_stat_align_width.saturating_sub(relative_path.len());
let pad_width = config
.diff_stat_align_width
.saturating_sub(relative_path.len());
let padding = " ".repeat(pad_width);
Some(format!(" {}{}{}", relative_path, padding, suffix))
Some(format!(" {}{}{}", formatted_path, padding, suffix))
}

#[cfg(test)]
Expand Down