Skip to content

Commit

Permalink
Update the fatpacked version
Browse files Browse the repository at this point in the history
  • Loading branch information
scottchiefbaker committed Jun 28, 2019
1 parent 0e792f9 commit 009d0cc
Showing 1 changed file with 79 additions and 36 deletions.
115 changes: 79 additions & 36 deletions third_party/build_fatpack/diff-so-fancy
Expand Up @@ -11,7 +11,11 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
use 5.008;
use warnings FATAL => 'all';
use strict;
use Encode;
# Use the correct value for both UNIX and Windows (/dev/null vs nul)
use File::Spec;
my $NULL = File::Spec->devnull();
# Highlight by reversing foreground and background. You could do
# other things like bold or underline if you prefer.
Expand All @@ -26,41 +30,88 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
$OLD_HIGHLIGHT[2],
);
my $RESET = "\x1b[m";
my $COLOR = qr/\x1b\[[0-9;]*m/;
my $BORING = qr/$COLOR|\s/;
# The patch portion of git log -p --graph should only ever have preceding | and
# not / or \ as merge history only shows up on the commit line.
my $GRAPH = qr/$COLOR?\|$COLOR?\s+/;
my @removed;
my @added;
my $in_hunk;
my $graph_indent = 0;
our $line_cb = sub { print @_ };
our $flush_cb = sub { local $| = 1 };
sub handle_line {
# Count the visible width of a string, excluding any terminal color sequences.
sub visible_width {
local $_ = shift;
my $ret = 0;
while (length) {
if (s/^$COLOR//) {
# skip colors
} elsif (s/^.//) {
$ret++;
}
}
return $ret;
}
# Return a substring of $str, omitting $len visible characters from the
# beginning, where terminal color sequences do not count as visible.
sub visible_substr {
my ($str, $len) = @_;
while ($len > 0) {
if ($str =~ s/^$COLOR//) {
next
}
$str =~ s/^.//;
$len--;
}
return $str;
}
sub handle_line {
my $orig = shift;
local $_ = $orig;
# match a graph line that begins a commit
if (/^(?:$COLOR?\|$COLOR?[ ])* # zero or more leading "|" with space
$COLOR?\*$COLOR?[ ] # a "*" with its trailing space
(?:$COLOR?\|$COLOR?[ ])* # zero or more trailing "|"
[ ]* # trailing whitespace for merges
/x) {
my $graph_prefix = $&;
# We must flush before setting graph indent, since the
# new commit may be indented differently from what we
# queued.
flush();
$graph_indent = visible_width($graph_prefix);
} elsif ($graph_indent) {
if (length($_) < $graph_indent) {
$graph_indent = 0;
} else {
$_ = visible_substr($_, $graph_indent);
}
}
if (!$in_hunk) {
$line_cb->($_);
$in_hunk = /^$GRAPH*$COLOR*\@\@ /;
$line_cb->($orig);
$in_hunk = /^$COLOR*\@\@ /;
}
elsif (/^$GRAPH*$COLOR*-/) {
push @removed, $_;
elsif (/^$COLOR*-/) {
push @removed, $orig;
}
elsif (/^$GRAPH*$COLOR*\+/) {
push @added, $_;
elsif (/^$COLOR*\+/) {
push @added, $orig;
}
else {
show_hunk(\@removed, \@added);
@removed = ();
@added = ();
$line_cb->($_);
$in_hunk = /^$GRAPH*$COLOR*[\@ ]/;
flush();
$line_cb->($orig);
$in_hunk = /^$COLOR*[\@ ]/;
}
# Most of the time there is enough output to keep things streaming,
Expand All @@ -80,6 +131,8 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
# Flush any queued hunk (this can happen when there is no trailing
# context in the final diff of the input).
show_hunk(\@removed, \@added);
@removed = ();
@added = ();
}
sub highlight_stdin {
Expand All @@ -96,7 +149,7 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
# fallback, which means we will work even if git can't be run.
sub color_config {
my ($key, $default) = @_;
my $s = `git config --get-color $key 2>/dev/null`;
my $s = `git config --get-color $key 2>$NULL`;
return length($s) ? $s : $default;
}
Expand Down Expand Up @@ -130,7 +183,6 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
sub highlight_pair {
my @a = split_line(shift);
my @b = split_line(shift);
my $opts = shift();
# Find common prefix, taking care to skip any ansi
# color codes.
Expand Down Expand Up @@ -175,18 +227,9 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
}
}
my @OLD_COLOR_SPEC = @OLD_HIGHLIGHT;
my @NEW_COLOR_SPEC = @NEW_HIGHLIGHT;
# If we're only highlight the differences temp disable the old/new normal colors
if ($opts->{'only_diff'}) {
$OLD_COLOR_SPEC[0] = '';
$NEW_COLOR_SPEC[0] = '';
}
if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
return highlight_line(\@a, $pa, $sa, \@OLD_COLOR_SPEC),
highlight_line(\@b, $pb, $sb, \@NEW_COLOR_SPEC);
return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT);
}
else {
return join('', @a),
Expand All @@ -199,8 +242,8 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
# or "+"
sub split_line {
local $_ = shift;
return eval { $_ = Encode::decode('UTF-8', $_, 1); 1 } ?
map { Encode::encode('UTF-8', $_) }
return utf8::decode($_) ?
map { utf8::encode($_); $_ }
map { /$COLOR/ ? $_ : (split //) }
split /($COLOR+)/ :
map { /$COLOR/ ? $_ : (split //) }
Expand Down Expand Up @@ -245,8 +288,8 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
my $suffix_a = join('', @$a[($sa+1)..$#$a]);
my $suffix_b = join('', @$b[($sb+1)..$#$b]);
return $prefix_a !~ /^$GRAPH*$COLOR*-$BORING*$/ ||
$prefix_b !~ /^$GRAPH*$COLOR*\+$BORING*$/ ||
return visible_substr($prefix_a, $graph_indent) !~ /^$COLOR*-$BORING*$/ ||
visible_substr($prefix_b, $graph_indent) !~ /^$COLOR*\+$BORING*$/ ||
$suffix_a !~ /^$BORING*$/ ||
$suffix_b !~ /^$BORING*$/;
}
Expand Down Expand Up @@ -289,7 +332,7 @@ unshift @INC, bless \%fatpacked, $class;
} # END OF FATPACK CODE


my $VERSION = "1.2.5";
my $VERSION = "1.2.6";

#################################################################################

Expand Down

0 comments on commit 009d0cc

Please sign in to comment.