summaryrefslogtreecommitdiffstats
path: root/src/features/diff_highlight.rs
blob: 20554fde567efb1a0985bb5fa71aa82750e55159 (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
use crate::features::OptionValueFunction;

pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
    _make_feature(false)
}

pub fn _make_feature(bold: bool) -> Vec<(String, OptionValueFunction)> {
    builtin_feature!([
        (
            "commit-style",
            String,
            Some("color.diff.commit"),
            _opt => "none"
        ),
        (
            "commit-decoration-style",
            String,
            None,
            _opt => "none"
        ),
        (
            "file-style",
            String,
            Some("color.diff.meta"),
            _opt => "none"
        ),
        (
            "file-decoration-style",
            String,
            None,
            _opt => "none"
        ),
        (
            "hunk-header-style",
            String,
            Some("color.diff.frag"),
            _opt => "bold syntax"
        ),
        (
            "hunk-header-decoration-style",
            String,
            None,
            _opt => "magenta box"
        ),
        (
            "minus-style",
            String,
            Some("color.diff.old"),
            _opt => if bold { "bold red" } else { "red" }
        ),
        (
            "minus-non-emph-style",
            String,
            Some("color.diff-highlight.oldNormal"),
            opt => opt.minus_style.clone()
        ),
        (
            "minus-emph-style",
            String,
            Some("color.diff-highlight.oldHighlight"),
            opt => format!("{} reverse", opt.minus_style)
        ),
        (
            "zero-style",
            String,
            None,
            _opt => "normal"
        ),
        (
            "plus-style",
            String,
            Some("color.diff.new"),
            _opt => if bold { "bold green" } else { "green" }
        ),
        (
            "plus-non-emph-style",
            String,
            Some("color.diff-highlight.newNormal"),
            opt => opt.plus_style.clone()
        ),
        (
            "plus-emph-style",
            String,
            Some("color.diff-highlight.newHighlight"),
            opt => format!("{} reverse", opt.plus_style)
        )
    ])
}

#[cfg(test)]
mod test_utils {
    use std::fs::remove_file;

    use crate::features;

    #[test]
    fn test_diff_highlight_defaults() {
        let opt = features::tests::make_options(&["--features", "diff-highlight"], None, None);
        assert_eq!(opt.minus_style, "red");
        assert_eq!(opt.minus_non_emph_style, "red");
        assert_eq!(opt.minus_emph_style, "red reverse");
        assert_eq!(opt.zero_style, "normal");
        assert_eq!(opt.plus_style, "green");
        assert_eq!(opt.plus_non_emph_style, "green");
        assert_eq!(opt.plus_emph_style, "green reverse");
    }

    #[test]
    fn test_diff_highlight_respects_gitconfig() {
        let git_config_contents = b"
[color \"diff\"]
    old = red bold
    new = green bold

[color \"diff-highlight\"]
    oldNormal = ul red bold
    oldHighlight = red bold 52
    newNormal = ul green bold
    newHighlight = green bold 22
";
        let git_config_path = "delta__test_diff_highlight.gitconfig";

        let opt = features::tests::make_options(
            &["--features", "diff-highlight"],
            Some(git_config_contents),
            Some(git_config_path),
        );

        assert_eq!(opt.minus_style, "red bold");
        assert_eq!(opt.minus_non_emph_style, "ul red bold");
        assert_eq!(opt.minus_emph_style, "red bold 52");
        assert_eq!(opt.zero_style, "normal");
        assert_eq!(opt.plus_style, "green bold");
        assert_eq!(opt.plus_non_emph_style, "ul green bold");
        assert_eq!(opt.plus_emph_style, "green bold 22");

        remove_file(git_config_path).unwrap();
    }
}