@@ -20,13 +20,13 @@ $fatpacked{"DiffHighlight.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'D
20
20
# Highlight by reversing foreground and background. You could do
21
21
# other things like bold or underline if you prefer.
22
22
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",
26
26
);
27
27
our @NEW_HIGHLIGHT = (
28
- "\e[1;32m" ,
29
- "\e[1;32;48;5;22m" ,
28
+ $OLD_HIGHLIGHT[0] ,
29
+ $OLD_HIGHLIGHT[1] ,
30
30
$OLD_HIGHLIGHT[2],
31
31
);
32
32
@@ -336,7 +336,7 @@ unshift @INC, bless \%fatpacked, $class;
336
336
} # END OF FATPACK CODE
337
337
338
338
339
- my $VERSION = " 1.4.1 " ;
339
+ my $VERSION = " 1.4.2 " ;
340
340
341
341
# ################################################################################
342
342
@@ -409,6 +409,7 @@ if (!$has_stdin) {
409
409
die (version());
410
410
} elsif ($args -> {' set-defaults' }) {
411
411
my $ok = set_defaults();
412
+ exit ;
412
413
} elsif ($args -> {colors }) {
413
414
# We print this to STDOUT so we can redirect to bash to auto-set the colors
414
415
print get_default_colors();
@@ -532,7 +533,7 @@ sub do_dsf_stuff {
532
533
# Mercurial looks like: diff -r 82e55d328c8c hello.c
533
534
if ($4 eq " -r" ) {
534
535
$is_mercurial = 1;
535
- $meta_color || = get_config_color(" meta" );
536
+ $meta_color = get_config_color(" meta" );
536
537
# Git looks like: diff --git a/diff-so-fancy b/diff-so-fancy
537
538
} else {
538
539
$last_file_seen = $5 ;
@@ -548,7 +549,7 @@ sub do_dsf_stuff {
548
549
# Find the first file: --- a/README.md #
549
550
# #######################################
550
551
} elsif (!$in_hunk && $line =~ / ^$ansi_color_regex --- (\w\/ )?(.+?)(\e |\t |$) / ) {
551
- $meta_color || = get_config_color(" meta" );
552
+ $meta_color = get_config_color(" meta" );
552
553
553
554
if ($git_strip_prefix ) {
554
555
my $file_dir = $4 || " " ;
@@ -700,7 +701,7 @@ sub do_dsf_stuff {
700
701
701
702
if ($file1 && $file2 ) {
702
703
# 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" );
704
705
705
706
my $change = file_change_string($file1 ,$file2 );
706
707
@@ -793,11 +794,6 @@ sub git_config {
793
794
my $search_key = lc ($_ [0] || " " );
794
795
my $default_value = lc ($_ [1] || " " );
795
796
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
-
801
797
state $raw = {};
802
798
if (%$raw && $search_key ) {
803
799
return $raw -> {$search_key } || $default_value ;
@@ -938,18 +934,13 @@ sub insert_reset_at_line_end {
938
934
}
939
935
940
936
# Count the number of a given char in a string
937
+ # https://www.perturb.org/display/1010_Perl_Count_occurrences_of_substring.html
941
938
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 ) = @_ ;
948
940
949
- if ($needle eq $found ) { $ret ++; }
950
- }
941
+ my $count = () = ($haystack =~ / $needle /g );
951
942
952
- return $ret ;
943
+ return $count ;
953
944
}
954
945
955
946
# Remove all ANSI codes from a string
@@ -964,7 +955,9 @@ sub bleach_text {
964
955
sub trim {
965
956
my $s = shift ();
966
957
if (!$s ) { return " " ; }
967
- $s =~ s / ^\s *|\s *$// g ;
958
+
959
+ $s =~ s / ^\s *// u ;
960
+ $s =~ s /\s *$// u ;
968
961
969
962
return $s ;
970
963
}
@@ -1238,14 +1231,14 @@ sub color {
1238
1231
}
1239
1232
}
1240
1233
1234
+ # https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_colors_in_git
1241
1235
sub git_ansi_color {
1242
1236
my $str = shift ();
1243
1237
my @parts = split (' ' , $str );
1244
1238
1245
1239
if (!@parts ) {
1246
1240
return ' ' ;
1247
1241
}
1248
-
1249
1242
my $colors = {
1250
1243
' black' => 0,
1251
1244
' red' => 1,
@@ -1257,35 +1250,61 @@ sub git_ansi_color {
1257
1250
' white' => 7,
1258
1251
};
1259
1252
1260
- my $fg = $parts [0] || " " ;
1261
- my $mod = $parts [1] || " " ;
1262
- my $bg = $parts [2] || " " ;
1263
-
1264
1253
my @ansi_part = ();
1265
- # ############################################
1266
1254
1267
- if ($mod eq ' bold' ) {
1255
+ if (grep { / bold/ } @parts ) {
1268
1256
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
1269
1263
}
1270
1264
1265
+ my $fg = $parts [0] // " " ;
1266
+ my $bg = $parts [1] // " " ;
1267
+
1271
1268
# ############################################
1272
1269
1273
- # It's an RGB value
1270
+ # It's an numeric value, so it's an 8 bit color
1274
1271
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
+ }
1276
1279
# It's a simple 16 color OG ansi
1277
1280
} 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 );
1279
1287
}
1280
1288
1281
1289
# ############################################
1282
1290
1283
- # It's an RGB value
1291
+ # It's an numeric value, so it's an 8 bit color
1284
1292
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
+ }
1286
1300
# It's a simple 16 color OG ansi
1287
1301
} 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 );
1289
1308
}
1290
1309
1291
1310
# ############################################
@@ -1379,12 +1398,45 @@ sub yes_no {
1379
1398
}
1380
1399
}
1381
1400
1401
+ # If there are colors set in the gitconfig use those, otherwise leave the defaults
1382
1402
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
+ }
1385
1424
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
+ }
1388
1440
}
1389
1441
1390
1442
# vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4
0 commit comments