Skip to content

Commit 02c8c00

Browse files
Bump the fatpacked version
1 parent 8b5e270 commit 02c8c00

File tree

1 file changed

+93
-41
lines changed

1 file changed

+93
-41
lines changed

third_party/build_fatpack/diff-so-fancy

+93-41
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
2020
# Highlight by reversing foreground and background. You could do
2121
# other things like bold or underline if you prefer.
2222
our @OLD_HIGHLIGHT = (
23-
"\e[1;31m",
24-
"\e[1;31;48;5;52m",
25-
"\x1b[27m",
23+
undef,
24+
"\e[7m",
25+
"\e[27m",
2626
);
2727
our @NEW_HIGHLIGHT = (
28-
"\e[1;32m",
29-
"\e[1;32;48;5;22m",
28+
$OLD_HIGHLIGHT[0],
29+
$OLD_HIGHLIGHT[1],
3030
$OLD_HIGHLIGHT[2],
3131
);
3232
@@ -336,7 +336,7 @@ unshift @INC, bless \%fatpacked, $class;
336336
} # END OF FATPACK CODE
337337

338338

339-
my $VERSION = "1.4.1";
339+
my $VERSION = "1.4.2";
340340

341341
#################################################################################
342342

@@ -409,6 +409,7 @@ if (!$has_stdin) {
409409
die(version());
410410
} elsif ($args->{'set-defaults'}) {
411411
my $ok = set_defaults();
412+
exit;
412413
} elsif ($args->{colors}) {
413414
# We print this to STDOUT so we can redirect to bash to auto-set the colors
414415
print get_default_colors();
@@ -532,7 +533,7 @@ sub do_dsf_stuff {
532533
# Mercurial looks like: diff -r 82e55d328c8c hello.c
533534
if ($4 eq "-r") {
534535
$is_mercurial = 1;
535-
$meta_color ||= get_config_color("meta");
536+
$meta_color = get_config_color("meta");
536537
# Git looks like: diff --git a/diff-so-fancy b/diff-so-fancy
537538
} else {
538539
$last_file_seen = $5;
@@ -548,7 +549,7 @@ sub do_dsf_stuff {
548549
# Find the first file: --- a/README.md #
549550
########################################
550551
} elsif (!$in_hunk && $line =~ /^$ansi_color_regex--- (\w\/)?(.+?)(\e|\t|$)/) {
551-
$meta_color ||= get_config_color("meta");
552+
$meta_color = get_config_color("meta");
552553

553554
if ($git_strip_prefix) {
554555
my $file_dir = $4 || "";
@@ -700,7 +701,7 @@ sub do_dsf_stuff {
700701

701702
if ($file1 && $file2) {
702703
# We may not have extracted this yet, so we pull from the config if not
703-
$meta_color ||= get_config_color("meta");
704+
$meta_color = get_config_color("meta");
704705

705706
my $change = file_change_string($file1,$file2);
706707

@@ -793,11 +794,6 @@ sub git_config {
793794
my $search_key = lc($_[0] || "");
794795
my $default_value = lc($_[1] || "");
795796

796-
# If we're in a unit test, use the default (don't read the users config)
797-
if (in_unit_test()) {
798-
return $default_value;
799-
}
800-
801797
state $raw = {};
802798
if (%$raw && $search_key) {
803799
return $raw->{$search_key} || $default_value;
@@ -938,18 +934,13 @@ sub insert_reset_at_line_end {
938934
}
939935

940936
# Count the number of a given char in a string
937+
# https://www.perturb.org/display/1010_Perl_Count_occurrences_of_substring.html
941938
sub char_count {
942-
my ($needle,$str) = @_;
943-
my $len = length($str);
944-
my $ret = 0;
945-
946-
for (my $i = 0; $i < $len; $i++) {
947-
my $found = substr($str,$i,1);
939+
my ($needle, $haystack) = @_;
948940

949-
if ($needle eq $found) { $ret++; }
950-
}
941+
my $count = () = ($haystack =~ /$needle/g);
951942

952-
return $ret;
943+
return $count;
953944
}
954945

955946
# Remove all ANSI codes from a string
@@ -964,7 +955,9 @@ sub bleach_text {
964955
sub trim {
965956
my $s = shift();
966957
if (!$s) { return ""; }
967-
$s =~ s/^\s*|\s*$//g;
958+
959+
$s =~ s/^\s*//u;
960+
$s =~ s/\s*$//u;
968961

969962
return $s;
970963
}
@@ -1238,14 +1231,14 @@ sub color {
12381231
}
12391232
}
12401233

1234+
# https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_colors_in_git
12411235
sub git_ansi_color {
12421236
my $str = shift();
12431237
my @parts = split(' ', $str);
12441238

12451239
if (!@parts) {
12461240
return '';
12471241
}
1248-
12491242
my $colors = {
12501243
'black' => 0,
12511244
'red' => 1,
@@ -1257,35 +1250,61 @@ sub git_ansi_color {
12571250
'white' => 7,
12581251
};
12591252

1260-
my $fg = $parts[0] || "";
1261-
my $mod = $parts[1] || "";
1262-
my $bg = $parts[2] || "";
1263-
12641253
my @ansi_part = ();
1265-
#############################################
12661254

1267-
if ($mod eq 'bold') {
1255+
if (grep { /bold/ } @parts) {
12681256
push(@ansi_part, "1");
1257+
@parts = grep { !/bold/ } @parts; # Remove from array
1258+
}
1259+
1260+
if (grep { /reverse/ } @parts) {
1261+
push(@ansi_part, "7");
1262+
@parts = grep { !/reverse/ } @parts; # Remove from array
12691263
}
12701264

1265+
my $fg = $parts[0] // "";
1266+
my $bg = $parts[1] // "";
1267+
12711268
#############################################
12721269

1273-
# It's an RGB value
1270+
# It's an numeric value, so it's an 8 bit color
12741271
if (is_numeric($fg)) {
1275-
push(@ansi_part, "38;5;$fg");
1272+
if ($fg < 8) {
1273+
push(@ansi_part, $fg + 30);
1274+
} elsif ($fg < 16) {
1275+
push(@ansi_part, $fg + 82);
1276+
} else {
1277+
push(@ansi_part, "38;5;$fg");
1278+
}
12761279
# It's a simple 16 color OG ansi
12771280
} elsif ($fg) {
1278-
push(@ansi_part, $colors->{$fg} + 30);
1281+
my $bright = $fg =~ s/bright//;
1282+
my $color_num = $colors->{$fg} + 30;
1283+
1284+
if ($bright) { $color_num += 60; } # Set bold
1285+
1286+
push(@ansi_part, $color_num);
12791287
}
12801288

12811289
#############################################
12821290

1283-
# It's an RGB value
1291+
# It's an numeric value, so it's an 8 bit color
12841292
if (is_numeric($bg)) {
1285-
push(@ansi_part, "48;5;$bg");
1293+
if ($bg < 8) {
1294+
push(@ansi_part, $bg + 40);
1295+
} elsif ($bg < 16) {
1296+
push(@ansi_part, $bg + 92);
1297+
} else {
1298+
push(@ansi_part, "48;5;$bg");
1299+
}
12861300
# It's a simple 16 color OG ansi
12871301
} elsif ($bg) {
1288-
push(@ansi_part, $colors->{$fg} + 40);
1302+
my $bright = $bg =~ s/bright//;
1303+
my $color_num = $colors->{$bg} + 40;
1304+
1305+
if ($bright) { $color_num += 60; } # Set bold
1306+
1307+
push(@ansi_part, $color_num);
12891308
}
12901309

12911310
#############################################
@@ -1379,12 +1398,45 @@ sub yes_no {
13791398
}
13801399
}
13811400

1401+
# If there are colors set in the gitconfig use those, otherwise leave the defaults
13821402
sub init_diff_highlight_colors {
1383-
$DiffHighlight::NEW_HIGHLIGHT[0] = git_ansi_color(git_config('color.diff-highlight.newnormal')) || color("2_bold");
1384-
$DiffHighlight::NEW_HIGHLIGHT[1] = git_ansi_color(git_config('color.diff-highlight.newhighlight')) || color("2_bold") . color("on_22");
1403+
$DiffHighlight::NEW_HIGHLIGHT[0] = git_ansi_color(git_config('color.diff-highlight.newnormal')) || $DiffHighlight::NEW_HIGHLIGHT[0];
1404+
$DiffHighlight::NEW_HIGHLIGHT[1] = git_ansi_color(git_config('color.diff-highlight.newhighlight')) || $DiffHighlight::NEW_HIGHLIGHT[1];
1405+
1406+
$DiffHighlight::OLD_HIGHLIGHT[0] = git_ansi_color(git_config('color.diff-highlight.oldnormal')) || $DiffHighlight::OLD_HIGHLIGHT[0];
1407+
$DiffHighlight::OLD_HIGHLIGHT[1] = git_ansi_color(git_config('color.diff-highlight.oldhighlight')) || $DiffHighlight::OLD_HIGHLIGHT[1];
1408+
}
1409+
1410+
sub debug_log {
1411+
my $log_line = shift();
1412+
my $file = "/tmp/diff-so-fancy.debug.log";
1413+
1414+
state $fh;
1415+
if (!$fh) {
1416+
printf("%sDebug log enabled:%s $file\n", color('orange'), color());
1417+
open ($fh, ">", $file) or die("Cannot write to $file");
1418+
}
1419+
1420+
print $fh trim($log_line) . "\n";
1421+
1422+
return 1;
1423+
}
13851424

1386-
$DiffHighlight::OLD_HIGHLIGHT[0] = git_ansi_color(git_config('color.diff-highlight.oldnormal')) || color("1_bold");
1387-
$DiffHighlight::OLD_HIGHLIGHT[1] = git_ansi_color(git_config('color.diff-highlight.oldhighlight')) || color("1_bold") . color("on_52");
1425+
# Enable k() and kd() if there is a DSF_DEBUG environment variable
1426+
BEGIN {
1427+
if ($ENV{"DSF_DEBUG"}) {
1428+
require Data::Dump::Color;
1429+
*k = sub { Data::Dump::Color::dd(@_) };
1430+
*kd = sub {
1431+
k(@_);
1432+
1433+
printf("Died at %2\$s line #%3\$s\n",caller());
1434+
exit(15);
1435+
}
1436+
} else {
1437+
*k = sub {};
1438+
*kd = sub {};
1439+
}
13881440
}
13891441

13901442
# vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4

0 commit comments

Comments
 (0)