@@ -27,11 +27,37 @@ my $ruler_width = git_config("diff-so-fancy.rulerWidth", undef);
27
27
my $git_strip_prefix = git_config_boolean(" diff.noprefix" ," false" );
28
28
my $has_stdin = has_stdin();
29
29
30
+ my $box_horizontal ;
31
+ my $box_vertical ;
32
+ my $box_down ;
33
+ my $box_up ;
34
+ # BOX DRAWINGS LIGHT HORIZONTAL http://www.fileformat.info/info/unicode/char/2500/index.htm
35
+ # BOX DRAWINGS LIGHT VERTICAL https://www.fileformat.info/info/unicode/char/2502/index.htm
36
+ # BOX DRAWINGS LIGHT DOWN AND LEFT https://www.fileformat.info/info/unicode/char/2510/index.htm
37
+ # BOX DRAWINGS LIGHT UP AND LEFT https://www.fileformat.info/info/unicode/char/2518/index.htm
38
+ if ($use_unicode_dash_for_ruler && should_print_unicode()) {
39
+ # $box_horizontal = Encode::encode('UTF-8', "\x{2500}");
40
+ $box_horizontal = " \xE2\x94\x80 " ;
41
+ # $box_vertical = Encode::encode('UTF-8', "\x{2502}");
42
+ $box_vertical = " \xE2\x94\x82 " ;
43
+ # $box_down = Encode::encode('UTF-8', "\x{2510}");
44
+ $box_down = " \xE2\x94\x90 " ;
45
+ # $box_up = Encode::encode('UTF-8', "\x{2518}");
46
+ $box_up = " \xE2\x94\x98 " ;
47
+ } else {
48
+ $box_horizontal = " -" ;
49
+ $box_vertical = " |" ;
50
+ $box_down = " ." ;
51
+ $box_up = " '" ;
52
+ }
53
+
30
54
my $ansi_regex = qr /\e\[ ([0-9]{1,3}(;[0-9]{1,3}){0,10})[mK]/ ;
31
55
my $ansi_color_regex = qr / (${ansi_regex} )?/ ;
32
56
my $reset_color = color(" reset" );
33
57
my $bold = color(" bold" );
34
58
my $meta_color = " " ;
59
+ my $commit_color = " " ;
60
+ my $min_header_level = 2;
35
61
36
62
# Set the diff highlight colors from the config
37
63
init_diff_highlight_colors();
@@ -159,13 +185,17 @@ sub do_dsf_stuff {
159
185
160
186
# ######################################################################
161
187
162
- # ###################################################################
163
- # Look for git index and replace it horizontal line (header later) #
164
- # ###################################################################
165
- if ($line =~ / ^${ansi_color_regex} index / ) {
166
- # Print the line color and then the actual line
188
+ # #######################
189
+ # Look for commit line #
190
+ # #######################
191
+ if ($line =~ / ^${ansi_color_regex} (commit [0-9a-f]{40}.*)$ / ) {
192
+ $commit_color = $1 || get_config_color(" commit" );
193
+ print_header(1,$commit_color ,$4 );
194
+ # #####################
195
+ # Look for git index #
196
+ # #####################
197
+ } elsif ($line =~ / ^${ansi_color_regex} index / ) {
167
198
$meta_color = $1 || get_config_color(" meta" );
168
-
169
199
# Get the next line without incrementing counter while loop
170
200
my $next = $input -> [0] || " " ;
171
201
my ($file_1 ,$file_2 );
@@ -185,9 +215,7 @@ sub do_dsf_stuff {
185
215
}
186
216
187
217
if ($file_1 && $file_2 ) {
188
- print horizontal_rule($meta_color );
189
- print $meta_color . file_change_string($file_1 ,$file_2 ) . " \n " ;
190
- print horizontal_rule($meta_color );
218
+ print_header(2,$meta_color ,file_change_string($file_1 ,$file_2 ));
191
219
}
192
220
# ########################
193
221
# Look for the filename #
@@ -198,7 +226,6 @@ sub do_dsf_stuff {
198
226
# Mercurial looks like: diff -r 82e55d328c8c hello.c
199
227
if ($4 eq " -r" ) {
200
228
$is_mercurial = 1;
201
- $meta_color = get_config_color(" meta" );
202
229
# Git looks like: diff --git a/diff-so-fancy b/diff-so-fancy
203
230
} else {
204
231
$last_file_seen = $5 ;
@@ -214,8 +241,6 @@ sub do_dsf_stuff {
214
241
# Find the first file: --- a/README.md #
215
242
# #######################################
216
243
} elsif (!$in_hunk && $line =~ / ^$ansi_color_regex --- (\w\/ )?(.+?)(\e |\t |$) / ) {
217
- $meta_color = get_config_color(" meta" );
218
-
219
244
if ($git_strip_prefix ) {
220
245
my $file_dir = $4 || " " ;
221
246
$file_1 = $file_dir . $5 ;
@@ -240,20 +265,8 @@ sub do_dsf_stuff {
240
265
$last_file_seen = $file_2 ;
241
266
}
242
267
243
- # Print out the top horizontal line of the header
244
- print $reset_color ;
245
- print horizontal_rule($meta_color );
246
-
247
- # Mercurial coloring is slightly different so we need to hard reset colors
248
- if ($is_mercurial ) {
249
- print $reset_color ;
250
- }
251
-
252
- print $meta_color ;
253
- print file_change_string($file_1 ,$file_2 ) . " \n " ;
254
-
255
- # Print out the bottom horizontal line of the header
256
- print horizontal_rule($meta_color );
268
+ $meta_color ||= get_config_color(" meta" );
269
+ print_header(2,$meta_color ,file_change_string($file_1 ,$file_2 ));
257
270
# #######################################
258
271
# Check for "@@ -3,41 +3,63 @@" syntax #
259
272
# #######################################
@@ -323,10 +336,9 @@ sub do_dsf_stuff {
323
336
# Look for binary file changes #
324
337
# ###############################
325
338
} elsif ($line =~ / ^Binary files (\w\/ )?(.+?) and (\w\/ )?(.+?) differ/ ) {
326
- my $change = file_change_string($2 ,$4 );
327
- print horizontal_rule($meta_color );
328
- print " $meta_color$change (binary)\n " ;
329
- print horizontal_rule($meta_color );
339
+ my ($change ,$change_length ) = file_change_string($2 ,$4 );
340
+ $meta_color ||= get_config_color(" meta" );
341
+ print_header(2,$meta_color ,$change . " (binary)" , $change_length + 9);
330
342
# ####################################################
331
343
# Check if we're changing the permissions of a file #
332
344
# ####################################################
@@ -365,14 +377,9 @@ sub do_dsf_stuff {
365
377
my ($file2 ) = $next =~ / rename to (.+?)(\e |\t |$) / ;
366
378
367
379
if ($file1 && $file2 ) {
368
- # We may not have extracted this yet, so we pull from the config if not
369
- $meta_color = get_config_color( " meta " );
380
+ $meta_color ||= get_config_color( " meta " );
381
+ print_header(2, $meta_color ,file_change_string( $file1 , $file2 ) );
370
382
371
- my $change = file_change_string($file1 ,$file2 );
372
-
373
- print horizontal_rule($meta_color );
374
- print $meta_color . $change . " \n " ;
375
- print horizontal_rule($meta_color );
376
383
}
377
384
378
385
$i += 3; # We've consumed three lines
@@ -613,26 +620,25 @@ sub trim {
613
620
return $s ;
614
621
}
615
622
616
- # Print a line of em-dash or line-drawing chars the full width of the screen
617
- sub horizontal_rule {
618
- my $color = $_ [0] || " " ;
619
- my $width = get_terminal_width();
620
-
621
- # em-dash http://www.fileformat.info/info/unicode/char/2014/index.htm
622
- # my $dash = "\x{2014}";
623
- # BOX DRAWINGS LIGHT HORIZONTAL http://www.fileformat.info/info/unicode/char/2500/index.htm
624
- my $dash ;
625
- if ($use_unicode_dash_for_ruler && should_print_unicode()) {
626
- # $dash = Encode::encode('UTF-8', "\x{2500}");
627
- $dash = " \xE2\x94\x80 " ;
623
+ sub print_header {
624
+ my $level = shift ();
625
+ my $color = shift ();
626
+ my $line = shift ();
627
+ if ($level < $min_header_level ) {
628
+ $min_header_level = $level
629
+ }
630
+ if ($level > $min_header_level ) {
631
+ my $line_length = shift ();
632
+ my $ruler = $box_horizontal x ($line_length + 1);
633
+ print $color .$ruler .$box_down ." \n " ;
634
+ print $color .$line ." " .$color .$box_vertical ." \n " ;
635
+ print $color .$ruler .$box_up ." \n " ;
628
636
} else {
629
- $dash = " -" ;
637
+ my $ruler = $box_horizontal x get_terminal_width();
638
+ print $color .$ruler ." \n " ;
639
+ print $color .$line ." \n " ;
640
+ print $color .$ruler ." \n " ;
630
641
}
631
-
632
- # Draw the line
633
- my $ret = $color . ($dash x $width ) . " $reset_color \n " ;
634
-
635
- return $ret ;
636
642
}
637
643
638
644
sub file_change_string {
@@ -641,25 +647,26 @@ sub file_change_string {
641
647
642
648
# If they're the same it's a modify
643
649
if ($file_1 eq $file_2 ) {
644
- return " modified: $file_1 " ;
650
+ return ( " modified: $file_1 " , 10 + length ( $file_1 )) ;
645
651
# If the first is /dev/null it's a new file
646
652
} elsif ($file_1 eq " /dev/null" ) {
647
653
my $add_color = $DiffHighlight::NEW_HIGHLIGHT [1];
648
- return " added: $add_color$file_2$reset_color " ;
654
+ return ( " added: $add_color$file_2$reset_color " , 7 + length ( $file_2 )) ;
649
655
# If the second is /dev/null it's a deletion
650
656
} elsif ($file_2 eq " /dev/null" ) {
651
657
my $del_color = $DiffHighlight::OLD_HIGHLIGHT [1];
652
- return " deleted: $del_color$file_1$reset_color " ;
658
+ return ( " deleted: $del_color$file_1$reset_color " , 9 + length ( $file_1 )) ;
653
659
# If the files aren't the same it's a rename
654
660
} elsif ($file_1 ne $file_2 ) {
655
661
my ($old , $new ) = DiffHighlight::highlight_pair($file_1 ,$file_2 ,{only_diff => 1});
656
662
# highlight_pair already includes reset_color, but adds newline characters that need to be trimmed off
657
663
$old = trim($old );
658
664
$new = trim($new );
659
- return " renamed: $old$meta_color to $new "
665
+ $meta_color ||= get_config_color(" meta" );
666
+ return (" renamed: $old$meta_color to $new " , 13 + length ($file_1 ) + length ($file_2 ));
660
667
# Something we haven't thought of yet
661
668
} else {
662
- return " $file_1 -> $file_2 " ;
669
+ return ( " $file_1 -> $file_2 " , 4 + length ( $file_1 ) + length ( $file_2 )) ;
663
670
}
664
671
}
665
672
@@ -861,6 +868,9 @@ sub color {
861
868
if ($str eq " meta" ) {
862
869
# Default ANSI yellow
863
870
$ret = git_ansi_color(git_config(' color.diff.meta' )) || color(11);
871
+ } elsif ($str eq " commit" ) {
872
+ # Default ANSI yellow bold
873
+ $ret = git_ansi_color(git_config(' color.diff.commit' )) || color(' 11_bold' );
864
874
} elsif ($str eq " reset" ) {
865
875
$ret = color(" reset" );
866
876
} elsif ($str eq " add_line" ) {
0 commit comments