5
6
# Highlight by reversing foreground and background. You could do
7
# other things like bold or underline if you prefer.
8
-my $HIGHLIGHT = "\x1b[7m";
9
-my $UNHIGHLIGHT = "\x1b[27m";
8
+my @OLD_HIGHLIGHT = (
9
+ color_config('color.diff-highlight.oldnormal'),
10
+ color_config('color.diff-highlight.oldhighlight', "\x1b[7m"),
11
+ color_config('color.diff-highlight.oldreset', "\x1b[27m")
12
+);
13
+my @NEW_HIGHLIGHT = (
14
+ color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
15
+ color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
16
+ color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
17
+);
18
+
19
+my $RESET = "\x1b[m";
20
my $COLOR = qr/\x1b\[[0-9;]*m/;
21
my $BORING = qr/$COLOR|\s/;
22
63
64
exit 0;
65
66
+# Ideally we would feed the default as a human-readable color to
67
+# git-config as the fallback value. But diff-highlight does
68
+# not otherwise depend on git at all, and there are reports
69
+# of it being used in other settings. Let's handle our own
70
+# fallback, which means we will work even if git can't be run.
71
+sub color_config {
72
+ my ($key, $default) = @_;
73
+ my $s = `git config --get-color $key 2>/dev/null`;
74
+ return length($s) ? $s : $default;
75
+}
76
+
77
sub show_hunk {
78
my ($a, $b) = @_;
79
149
}
150
151
if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
131
- return highlight_line(\@a, $pa, $sa),
132
- highlight_line(\@b, $pb, $sb);
152
+ return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
153
+ highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT);
154
}
155
else {
156
return join('', @a),
165
}
166
167
sub highlight_line {
147
- my ($line, $prefix, $suffix) = @_;
148
-
149
- return join('',
150
- @{$line}[0..($prefix-1)],
151
- $HIGHLIGHT,
152
- @{$line}[$prefix..$suffix],
153
- $UNHIGHLIGHT,
154
- @{$line}[($suffix+1)..$#$line]
155
- );
168
+ my ($line, $prefix, $suffix, $theme) = @_;
169
+
170
+ my $start = join('', @{$line}[0..($prefix-1)]);
171
+ my $mid = join('', @{$line}[$prefix..$suffix]);
172
+ my $end = join('', @{$line}[($suffix+1)..$#$line]);
173
+
174
+ # If we have a "normal" color specified, then take over the whole line.
175
+ # Otherwise, we try to just manipulate the highlighted bits.
176
+ if (defined $theme->[0]) {
177
+ s/$COLOR//g for ($start, $mid, $end);
178
+ chomp $end;
179
+ return join('',
180
+ $theme->[0], $start, $RESET,
181
+ $theme->[1], $mid, $RESET,
182
+ $theme->[0], $end, $RESET,
183
+ "\n"
184
+ );
185
+ } else {
186
+ return join('',
187
+ $start,
188
+ $theme->[1], $mid, $theme->[2],
189
+ $end
190
+ );
191
+ }
192
}
193
194
# Pairs are interesting to highlight only if we are going to end up