summaryrefslogtreecommitdiffstats
path: root/src/features/diff_highlight.rs
blob: 35b71c4b7dc6f1082bfb1900bbf067955b15288c (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
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!([
        (
            "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 config = features::tests::make_config(&["--features", "diff-highlight"], None, None);

        assert_eq!(config.minus_style, features::tests::make_style("red"));
        assert_eq!(
            config.minus_non_emph_style,
            features::tests::make_style("red")
        );
        assert_eq!(
            config.minus_emph_style,
            features::tests::make_emph_style("red reverse")
        );
        assert_eq!(config.zero_style, features::tests::make_style(""));
        assert_eq!(config.plus_style, features::tests::make_style("green"));
        assert_eq!(
            config.plus_non_emph_style,
            features::tests::make_style("green")
        );
        assert_eq!(
            config.plus_emph_style,
            features::tests::make_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 config = features::tests::make_config(
            &["--features", "diff-highlight"],
            Some(git_config_contents),
            Some(git_config_path),
        );

        assert_eq!(config.minus_style, features::tests::make_style("red bold"));
        assert_eq!(
            config.minus_non_emph_style,
            features::tests::make_style("ul red bold")
        );
        assert_eq!(
            config.minus_emph_style,
            features::tests::make_emph_style("red bold 52")
        );
        assert_eq!(config.zero_style, features::tests::make_style(""));
        assert_eq!(config.plus_style, features::tests::make_style("green bold"));
        assert_eq!(
            config.plus_non_emph_style,
            features::tests::make_style("ul green bold")
        );
        assert_eq!(
            config.plus_emph_style,
            features::tests::make_emph_style("green bold 22")
        );

        remove_file(git_config_path).unwrap();
    }
}