Debian Codegraph @

src/handlers/diff_stat.rs

raw symbols SCIP
1 use lazy_static::lazy_static;
2 use regex::Regex;
3 use std::borrow::Cow;
4
5 use crate::config::Config;
6 use crate::delta::{State, StateMachine};
7 use crate::features;
8 use crate::utils;
9
10 impl StateMachine<'_> {
11 #[inline]
12 fn test_diff_stat_line(&self) -> bool {
13 (self.state == State::CommitMeta || self.state == State::Unknown)
14 && self.line.starts_with(' ')
15 }
16
17 pub fn handle_diff_stat_line(&mut self) -> std::io::Result<bool> {
18 if !self.test_diff_stat_line() {
19 return Ok(false);
20 }
21 let mut handled_line = false;
22 if self.config.relative_paths {
23 if let Some(cwd) = self.config.cwd_relative_to_repo_root.as_deref() {
24 if let Some(replacement_line) =
25 relativize_path_in_diff_stat_line(&self.raw_line, cwd, self.config)
26 {
27 self.painter.emit()?;
28 writeln!(self.painter.writer, "{replacement_line}")?;
29 handled_line = true
30 }
31 }
32 }
33 Ok(handled_line)
34 }
35 }
36
37 // A regex to capture the path, and the content from the pipe onwards, in lines
38 // like these:
39 // " src/delta.rs | 14 ++++++++++----"
40 // " src/config.rs | 2 ++"
41 lazy_static! {
42 static ref DIFF_STAT_LINE_REGEX: Regex =
43 Regex::new(r" ([^\| ][^\|]+[^\| ]) +(\| +[0-9]+ .+)").unwrap();
44 }
45
46 pub fn relativize_path_in_diff_stat_line(
47 line: &str,
48 cwd_relative_to_repo_root: &str,
49 config: &Config,
50 ) -> Option<String> {
51 let caps = DIFF_STAT_LINE_REGEX.captures(line)?;
52 let path_relative_to_repo_root = caps.get(1).unwrap().as_str();
53
54 let relative_path =
55 pathdiff::diff_paths(path_relative_to_repo_root, cwd_relative_to_repo_root)?;
56 let relative_path = relative_path.to_str()?;
57 let formatted_path = match (
58 config.hyperlinks,
59 utils::path::absolute_path(path_relative_to_repo_root, config),
60 ) {
61 (true, Some(absolute_path)) => features::hyperlinks::format_osc8_file_hyperlink(
62 absolute_path,
63 None,
64 relative_path,
65 config,
66 ),
67 _ => Cow::from(relative_path),
68 };
69 let suffix = caps.get(2).unwrap().as_str();
70 let pad_width = config
71 .diff_stat_align_width
72 .saturating_sub(relative_path.len());
73 let padding = " ".repeat(pad_width);
74 Some(format!(" {formatted_path}{padding}{suffix}"))
75 }
76
77 #[cfg(test)]
78 mod tests {
79 use super::*;
80
81 #[test]
82 fn test_diff_stat_line_regex_1() {
83 let caps = DIFF_STAT_LINE_REGEX.captures(" src/delta.rs | 14 ++++++++++----");
84 assert!(caps.is_some());
85 let caps = caps.unwrap();
86 assert_eq!(caps.get(1).unwrap().as_str(), "src/delta.rs");
87 assert_eq!(caps.get(2).unwrap().as_str(), "| 14 ++++++++++----");
88 }
89
90 #[test]
91 fn test_diff_stat_line_regex_2() {
92 let caps = DIFF_STAT_LINE_REGEX.captures(" src/config.rs | 2 ++");
93 assert!(caps.is_some());
94 let caps = caps.unwrap();
95 assert_eq!(caps.get(1).unwrap().as_str(), "src/config.rs");
96 assert_eq!(caps.get(2).unwrap().as_str(), "| 2 ++");
97 }
98
99 #[test]
100 fn test_relative_path() {
101 for (path, cwd_relative_to_repo_root, expected) in &[
102 ("file.rs", "", "file.rs"),
103 ("file.rs", "a/", "../file.rs"),
104 ("a/file.rs", "a/", "file.rs"),
105 ("a/b/file.rs", "a", "b/file.rs"),
106 ("c/d/file.rs", "a/b/", "../../c/d/file.rs"),
107 ] {
108 assert_eq!(
109 pathdiff::diff_paths(path, cwd_relative_to_repo_root),
110 Some(expected.into())
111 )
112 }
113 }
114 }