@@ -11,7 +11,11 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
11
11
use 5.008;
12
12
use warnings FATAL => 'all';
13
13
use strict;
14
- use Encode;
14
+
15
+ # Use the correct value for both UNIX and Windows (/dev/null vs nul)
16
+ use File::Spec;
17
+
18
+ my $NULL = File::Spec->devnull();
15
19
16
20
# Highlight by reversing foreground and background. You could do
17
21
# other things like bold or underline if you prefer.
@@ -26,41 +30,88 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
26
30
$OLD_HIGHLIGHT[2],
27
31
);
28
32
33
+
34
+
29
35
my $RESET = "\x1b[m";
30
36
my $COLOR = qr/\x1b\[[0-9;]*m/;
31
37
my $BORING = qr/$COLOR|\s/;
32
38
33
- # The patch portion of git log -p --graph should only ever have preceding | and
34
- # not / or \ as merge history only shows up on the commit line.
35
- my $GRAPH = qr/$COLOR?\|$COLOR?\s+/;
36
-
37
39
my @removed;
38
40
my @added;
39
41
my $in_hunk;
42
+ my $graph_indent = 0;
40
43
41
44
our $line_cb = sub { print @_ };
42
45
our $flush_cb = sub { local $| = 1 };
43
46
44
- sub handle_line {
47
+ # Count the visible width of a string, excluding any terminal color sequences.
48
+ sub visible_width {
45
49
local $_ = shift;
50
+ my $ret = 0;
51
+ while (length) {
52
+ if (s/^$COLOR//) {
53
+ # skip colors
54
+ } elsif (s/^.//) {
55
+ $ret++;
56
+ }
57
+ }
58
+ return $ret;
59
+ }
60
+
61
+ # Return a substring of $str, omitting $len visible characters from the
62
+ # beginning, where terminal color sequences do not count as visible.
63
+ sub visible_substr {
64
+ my ($str, $len) = @_;
65
+ while ($len > 0) {
66
+ if ($str =~ s/^$COLOR//) {
67
+ next
68
+ }
69
+ $str =~ s/^.//;
70
+ $len--;
71
+ }
72
+ return $str;
73
+ }
74
+
75
+ sub handle_line {
76
+ my $orig = shift;
77
+ local $_ = $orig;
78
+
79
+ # match a graph line that begins a commit
80
+ if (/^(?:$COLOR?\|$COLOR?[ ])* # zero or more leading "|" with space
81
+ $COLOR?\*$COLOR?[ ] # a "*" with its trailing space
82
+ (?:$COLOR?\|$COLOR?[ ])* # zero or more trailing "|"
83
+ [ ]* # trailing whitespace for merges
84
+ /x) {
85
+ my $graph_prefix = $&;
86
+
87
+ # We must flush before setting graph indent, since the
88
+ # new commit may be indented differently from what we
89
+ # queued.
90
+ flush();
91
+ $graph_indent = visible_width($graph_prefix);
92
+
93
+ } elsif ($graph_indent) {
94
+ if (length($_) < $graph_indent) {
95
+ $graph_indent = 0;
96
+ } else {
97
+ $_ = visible_substr($_, $graph_indent);
98
+ }
99
+ }
46
100
47
101
if (!$in_hunk) {
48
- $line_cb->($_ );
49
- $in_hunk = /^$GRAPH*$ COLOR*\@\@ /;
102
+ $line_cb->($orig );
103
+ $in_hunk = /^$COLOR*\@\@ /;
50
104
}
51
- elsif (/^$GRAPH*$ COLOR*-/) {
52
- push @removed, $_ ;
105
+ elsif (/^$COLOR*-/) {
106
+ push @removed, $orig ;
53
107
}
54
- elsif (/^$GRAPH*$ COLOR*\+/) {
55
- push @added, $_ ;
108
+ elsif (/^$COLOR*\+/) {
109
+ push @added, $orig ;
56
110
}
57
111
else {
58
- show_hunk(\@removed, \@added);
59
- @removed = ();
60
- @added = ();
61
-
62
- $line_cb->($_);
63
- $in_hunk = /^$GRAPH*$COLOR*[\@ ]/;
112
+ flush();
113
+ $line_cb->($orig);
114
+ $in_hunk = /^$COLOR*[\@ ]/;
64
115
}
65
116
66
117
# Most of the time there is enough output to keep things streaming,
@@ -80,6 +131,8 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
80
131
# Flush any queued hunk (this can happen when there is no trailing
81
132
# context in the final diff of the input).
82
133
show_hunk(\@removed, \@added);
134
+ @removed = ();
135
+ @added = ();
83
136
}
84
137
85
138
sub highlight_stdin {
@@ -96,7 +149,7 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
96
149
# fallback, which means we will work even if git can't be run.
97
150
sub color_config {
98
151
my ($key, $default) = @_;
99
- my $s = `git config --get-color $key 2>/dev/null `;
152
+ my $s = `git config --get-color $key 2>$NULL `;
100
153
return length($s) ? $s : $default;
101
154
}
102
155
@@ -130,7 +183,6 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
130
183
sub highlight_pair {
131
184
my @a = split_line(shift);
132
185
my @b = split_line(shift);
133
- my $opts = shift();
134
186
135
187
# Find common prefix, taking care to skip any ansi
136
188
# color codes.
@@ -175,18 +227,9 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
175
227
}
176
228
}
177
229
178
- my @OLD_COLOR_SPEC = @OLD_HIGHLIGHT;
179
- my @NEW_COLOR_SPEC = @NEW_HIGHLIGHT;
180
-
181
- # If we're only highlight the differences temp disable the old/new normal colors
182
- if ($opts->{'only_diff'}) {
183
- $OLD_COLOR_SPEC[0] = '';
184
- $NEW_COLOR_SPEC[0] = '';
185
- }
186
-
187
230
if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
188
- return highlight_line(\@a, $pa, $sa, \@OLD_COLOR_SPEC ),
189
- highlight_line(\@b, $pb, $sb, \@NEW_COLOR_SPEC );
231
+ return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT ),
232
+ highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT );
190
233
}
191
234
else {
192
235
return join('', @a),
@@ -199,8 +242,8 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
199
242
# or "+"
200
243
sub split_line {
201
244
local $_ = shift;
202
- return eval { $_ = Encode ::decode('UTF-8', $_, 1); 1 } ?
203
- map { Encode ::encode('UTF-8', $_) }
245
+ return utf8 ::decode($_) ?
246
+ map { utf8 ::encode($_); $_ }
204
247
map { /$COLOR/ ? $_ : (split //) }
205
248
split /($COLOR+)/ :
206
249
map { /$COLOR/ ? $_ : (split //) }
@@ -245,8 +288,8 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
245
288
my $suffix_a = join('', @$a[($sa+1)..$#$a]);
246
289
my $suffix_b = join('', @$b[($sb+1)..$#$b]);
247
290
248
- return $prefix_a !~ /^$GRAPH* $COLOR*-$BORING*$/ ||
249
- $prefix_b !~ /^$GRAPH* $COLOR*\+$BORING*$/ ||
291
+ return visible_substr( $prefix_a, $graph_indent) !~ /^$COLOR*-$BORING*$/ ||
292
+ visible_substr( $prefix_b, $graph_indent) !~ /^$COLOR*\+$BORING*$/ ||
250
293
$suffix_a !~ /^$BORING*$/ ||
251
294
$suffix_b !~ /^$BORING*$/;
252
295
}
@@ -289,7 +332,7 @@ unshift @INC, bless \%fatpacked, $class;
289
332
} # END OF FATPACK CODE
290
333
291
334
292
- my $VERSION = " 1.2.5 " ;
335
+ my $VERSION = " 1.2.6 " ;
293
336
294
337
# ################################################################################
295
338
0 commit comments