summaryrefslogtreecommitdiffstats
path: root/src/features/hyperlinks.rs
blob: 46e0984df45ac216712b0541f2dd4e0d6e7e9553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::borrow::Cow;

use lazy_static::lazy_static;
use regex::{Captures, Regex};

use crate::config::Config;
use crate::features::OptionValueFunction;
use crate::git_config_entry::{GitConfigEntry, GitRemoteRepo};

#[cfg(not(tarpaulin_include))]
pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
    builtin_feature!([
        (
            "hyperlinks",
            bool,
            None,
            _opt => true
        )
    ])
}

pub fn format_commit_line_with_osc8_commit_hyperlink<'a>(
    line: &'a str,
    config: &Config,
) -> Cow<'a, str> {
    if let Some(GitConfigEntry::GitRemote(GitRemoteRepo::GitHubRepo(repo))) =
        config.git_config_entries.get("remote.origin.url")
    {
        COMMIT_LINE_REGEX.replace(line, |captures: &Captures| {
            format_commit_line_captures_with_osc8_commit_hyperlink(captures, repo)
        })
    } else {
        Cow::from(line)
    }
}

/// Create a file hyperlink to `path`, displaying `text`.
pub fn format_osc8_file_hyperlink<'a>(
    relative_path: &'a str,
    line_number: Option<usize>,
    text: &str,
    config: &Config,
) -> Cow<'a, str> {
    if let Some(GitConfigEntry::Path(workdir)) = config.git_config_entries.get("delta.__workdir__")
    {
        let absolute_path = workdir.join(relative_path);
        let mut url = config
            .hyperlinks_file_link_format
            .replace("{path}", &absolute_path.to_string_lossy());
        if let Some(n) = line_number {
            url = url.replace("{line}", &format!("{}", n))
        } else {
            url = url.replace("{line}", "")
        };
        Cow::from(format!(
            "{osc}8;;{url}{st}{text}{osc}8;;{st}",
            url = url,
            text = text,
            osc = "\x1b]",
            st = "\x1b\\"
        ))
    } else {
        Cow::from(relative_path)
    }
}

lazy_static! {
    static ref COMMIT_LINE_REGEX: Regex = Regex::new("(.* )([0-9a-f]{40})(.*)").unwrap();
}

fn format_commit_line_captures_with_osc8_commit_hyperlink(
    captures: &Captures,
    github_repo: &str,
) -> String {
    let commit = captures.get(2).unwrap().as_str();
    format!(
        "{prefix}{osc}8;;{url}{st}{commit}{osc}8;;{st}{suffix}",
        url = format_github_commit_url(commit, github_repo),
        commit = commit,
        prefix = captures.get(1).unwrap().as_str(),
        suffix = captures.get(3).unwrap().as_str(),
        osc = "\x1b]",
        st = "\x1b\\"
    )
}

fn format_github_commit_url(commit: &str, github_repo: &str) -> String {
    format!("https://github.com/{}/commit/{}", github_repo, commit)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::integration_test_utils::integration_test_utils::make_config_from_args_and_git_config;
    use std::fs::remove_file;
    use std::path::{Path, PathBuf};

    #[test]
    fn test_format_commit_line_with_osc8_commit_hyperlink() {
        let git_config_contents = b"
[remote \"origin\"]
    url = git@github.com:dandavison/delta.git
";
        let git_config_path = "delta__format_commit_line_with_osc8_commit_hyperlink.gitconfig";
        let config = make_config_from_args_and_git_config(
            &[],
            Some(git_config_contents),
            Some(git_config_path),
        );
        assert_eq!(
            format_commit_line_with_osc8_commit_hyperlink(
                "commit e198c0d841d9fb660e59e0329235a8601b407c69 (HEAD -> master, origin/master)",
                &config
            ),
            "commit \u{1b}]8;;https://github.com/dandavison/delta/commit/e198c0d841d9fb660e59e0329235a8601b407c69\u{1b}\\e198c0d841d9fb660e59e0329235a8601b407c69\u{1b}]8;;\u{1b}\\ (HEAD -> master, origin/master)"
        );
        remove_file(git_config_path).unwrap();
    }

    #[test]
    fn test_format_osc8_file_hyperlink() {
        let mut config = make_config_from_args_and_git_config(&[], None, None);
        config.git_config_entries.insert(
            "delta.__workdir__".to_string(),
            GitConfigEntry::Path(PathBuf::from("/working/directory")),
        );
        println!(
            "{}",
            format!(
                "\x1b]8;;file://{}\x1b\\link-text\x1b]8;;\x1b\\",
                Path::new("/working/directory/relative/path/file.rs").to_string_lossy()
            )
        );
        println!(
            "{}",
            format_osc8_file_hyperlink("relative/path/file.rs", None, "link-text", &config)
        );
        dbg!(format!(
            "\x1b]8;;file://{}\x1b\\link-text\x1b]8;;\x1b\\",
            Path::new("/working/directory/relative/path/file.rs").to_string_lossy()
        ));
        dbg!(format_osc8_file_hyperlink(
            "relative/path/file.rs",
            None,
            "link-text",
            &config
        ));
        assert_eq!(
            format!(
                "\x1b]8;;file://{}\x1b\\link-text\x1b]8;;\x1b\\",
                Path::new("/working/directory/relative/path/file.rs").to_string_lossy()
            ),
            format_osc8_file_hyperlink("relative/path/file.rs", None, "link-text", &config)
        )
    }

    #[test]
    fn test_format_osc8_file_hyperlink_with_line_number() {
        let mut config = make_config_from_args_and_git_config(
            &["--hyperlinks-file-link-format", "file-line://{path}:{line}"],
            None,
            None,
        );
        config.git_config_entries.insert(
            "delta.__workdir__".to_string(),
            GitConfigEntry::Path(PathBuf::from("/working/directory")),
        );
        assert_eq!(
            format!(
                "\x1b]8;;file-line://{}:7\x1b\\link-text\x1b]8;;\x1b\\",
                Path::new("/working/directory/relative/path/file.rs").to_string_lossy()
            ),
            format_osc8_file_hyperlink("relative/path/file.rs", Some(7), "link-text", &config)
        )
    }

    #[test]
    fn test_format_github_commit_url() {
        assert_eq!(
            format_github_commit_url(
                "b9a76d4523949c09013f24ff555180da1d39e9e4",
                "dandavison/delta"
            ),
            "https://github.com/dandavison/delta/commit/b9a76d4523949c09013f24ff555180da1d39e9e4"
        )
    }
}