@@ -28,10 +28,31 @@ my $ruler_width = git_config("diff-so-fancy.rulerWidth", undef);
28
28
my $git_strip_prefix = git_config_boolean(" diff.noprefix" ," false" );
29
29
my $has_stdin = has_stdin();
30
30
31
+ my $box_horizontal ;
32
+ my $box_vertical ;
33
+ my $box_down ;
34
+ my $box_up ;
35
+ # BOX DRAWINGS LIGHT HORIZONTAL http://www.fileformat.info/info/unicode/char/2500/index.htm
36
+ # BOX DRAWINGS LIGHT VERTICAL https://www.fileformat.info/info/unicode/char/2502/index.htm
37
+ # BOX DRAWINGS LIGHT DOWN AND LEFT https://www.fileformat.info/info/unicode/char/2510/index.htm
38
+ # BOX DRAWINGS LIGHT UP AND LEFT https://www.fileformat.info/info/unicode/char/2518/index.htm
39
+ if ($use_unicode_dash_for_ruler && should_print_unicode()) {
40
+ $box_horizontal = Encode::encode(' UTF-8' , " \x{2500} " );
41
+ $box_vertical = Encode::encode(' UTF-8' , " \x{2502} " );
42
+ $box_down = Encode::encode(' UTF-8' , " \x{2510} " );
43
+ $box_up = Encode::encode(' UTF-8' , " \x{2518} " );
44
+ } else {
45
+ $box_horizontal = " -" ;
46
+ $box_vertical = " |" ;
47
+ $box_down = " ." ;
48
+ $box_up = " '" ;
49
+ }
50
+
31
51
my $ansi_color_regex = qr / (\e\[ ([0-9]{1,3}(;[0-9]{1,3}){0,10})[mK])?/ ;
32
52
my $reset_color = color(" reset" );
33
53
my $bold = color(" bold" );
34
54
my $meta_color = " " ;
55
+ my $commit_color = " " ;
35
56
36
57
my ($file_1 ,$file_2 );
37
58
my $args = argv(); # Hashref of all the ARGV stuff
@@ -145,13 +166,17 @@ sub do_dsf_stuff {
145
166
146
167
# ######################################################################
147
168
169
+ # #######################
170
+ # Look for commit line #
171
+ # #######################
172
+ if ($line =~ / ^${ansi_color_regex} commit [0-9a-f]{40}/ ) {
173
+ $commit_color = $1 || get_config_color(" commit" );
174
+ print_commit_box($line );
148
175
# ###################################################################
149
176
# Look for git index and replace it horizontal line (header later) #
150
177
# ###################################################################
151
- if ($line =~ / ^${ansi_color_regex} index / ) {
152
- # Print the line color and then the actual line
178
+ } elsif ($line =~ / ^${ansi_color_regex} index / ) {
153
179
$meta_color = $1 || get_config_color(" meta" );
154
-
155
180
# Get the next line without incrementing counter while loop
156
181
my $next = $input -> [0] || " " ;
157
182
my ($file_1 ,$file_2 );
@@ -171,9 +196,7 @@ sub do_dsf_stuff {
171
196
}
172
197
173
198
if ($file_1 && $file_2 ) {
174
- print horizontal_rule($meta_color );
175
- print $meta_color . file_change_string($file_1 ,$file_2 ) . " \n " ;
176
- print horizontal_rule($meta_color );
199
+ print_file_change_box(file_change_string($file_1 ,$file_2 ));
177
200
}
178
201
# ########################
179
202
# Look for the filename #
@@ -184,7 +207,6 @@ sub do_dsf_stuff {
184
207
# Mercurial looks like: diff -r 82e55d328c8c hello.c
185
208
if ($4 eq " -r" ) {
186
209
$is_mercurial = 1;
187
- $meta_color ||= get_config_color(" meta" );
188
210
# Git looks like: diff --git a/diff-so-fancy b/diff-so-fancy
189
211
} else {
190
212
$last_file_seen = $5 ;
@@ -200,8 +222,6 @@ sub do_dsf_stuff {
200
222
# Find the first file: --- a/README.md #
201
223
# #######################################
202
224
} elsif (!$in_hunk && $line =~ / ^$ansi_color_regex --- (\w\/ )?(.+?)(\e |\t |$) / ) {
203
- $meta_color ||= get_config_color(" meta" );
204
-
205
225
if ($git_strip_prefix ) {
206
226
my $file_dir = $4 || " " ;
207
227
$file_1 = $file_dir . $5 ;
@@ -226,20 +246,7 @@ sub do_dsf_stuff {
226
246
$last_file_seen = $file_2 ;
227
247
}
228
248
229
- # Print out the top horizontal line of the header
230
- print $reset_color ;
231
- print horizontal_rule($meta_color );
232
-
233
- # Mercurial coloring is slightly different so we need to hard reset colors
234
- if ($is_mercurial ) {
235
- print $reset_color ;
236
- }
237
-
238
- print $meta_color ;
239
- print file_change_string($file_1 ,$file_2 ) . " \n " ;
240
-
241
- # Print out the bottom horizontal line of the header
242
- print horizontal_rule($meta_color );
249
+ print_file_change_box(file_change_string($file_1 ,$file_2 ));
243
250
# #######################################
244
251
# Check for "@@ -3,41 +3,63 @@" syntax #
245
252
# #######################################
@@ -309,10 +316,8 @@ sub do_dsf_stuff {
309
316
# Look for binary file changes #
310
317
# ###############################
311
318
} elsif ($line =~ / ^Binary files (\w\/ )?(.+?) and (\w\/ )?(.+?) differ/ ) {
312
- my $change = file_change_string($2 ,$4 );
313
- print horizontal_rule($meta_color );
314
- print " $meta_color$change (binary)\n " ;
315
- print horizontal_rule($meta_color );
319
+ my ($change ,$change_length ) = file_change_string($2 ,$4 );
320
+ print_file_change_box($change . " (binary)" , $change_length + 9);
316
321
# ####################################################
317
322
# Check if we're changing the permissions of a file #
318
323
# ####################################################
@@ -351,14 +356,7 @@ sub do_dsf_stuff {
351
356
my ($file2 ) = $next =~ / rename to (.+?)(\e |\t |$) / ;
352
357
353
358
if ($file1 && $file2 ) {
354
- # We may not have extracted this yet, so we pull from the config if not
355
- $meta_color ||= get_config_color(" meta" );
356
-
357
- my $change = file_change_string($file1 ,$file2 );
358
-
359
- print horizontal_rule($meta_color );
360
- print $meta_color . $change . " \n " ;
361
- print horizontal_rule($meta_color );
359
+ print_file_change_box(file_change_string($file1 ,$file2 ));
362
360
}
363
361
364
362
$i += 3; # We've consumed three lines
@@ -624,25 +622,23 @@ sub trim {
624
622
return $s ;
625
623
}
626
624
627
- # Print a line of em-dash or line-drawing chars the full width of the screen
628
- sub horizontal_rule {
629
- my $color = $_ [0] || " " ;
630
- my $width = get_terminal_width();
631
-
632
- # em-dash http://www.fileformat.info/info/unicode/char/2014/index.htm
633
- # my $dash = "\x{2014}";
634
- # BOX DRAWINGS LIGHT HORIZONTAL http://www.fileformat.info/info/unicode/char/2500/index.htm
635
- my $dash ;
636
- if ($use_unicode_dash_for_ruler && should_print_unicode()) {
637
- $dash = Encode::encode(' UTF-8' , " \x{2500} " );
638
- } else {
639
- $dash = " -" ;
640
- }
641
-
642
- # Draw the line
643
- my $ret = $color . ($dash x $width ) . " $reset_color \n " ;
625
+ sub print_commit_box {
626
+ my $line = shift ();
627
+ my $ruler = $box_horizontal x get_terminal_width();
628
+ $commit_color ||= get_config_color(" commit" );
629
+ print $commit_color ,$ruler ,$reset_color ," \n " ;
630
+ print $line ;
631
+ print $commit_color ,$ruler ,$reset_color ," \n " ;
632
+ }
644
633
645
- return $ret ;
634
+ sub print_file_change_box {
635
+ my $change = shift ();
636
+ my $change_length = shift ();
637
+ my $ruler = $box_horizontal x ($change_length + 1);
638
+ $meta_color ||= get_config_color(" meta" );
639
+ print $meta_color ,$ruler ,$box_down ,$reset_color ," \n " ;
640
+ print $meta_color ,$change ," " ,$meta_color ,$box_vertical ,$reset_color ," \n " ;
641
+ print $meta_color ,$ruler ,$box_up ,$reset_color ," \n " ;
646
642
}
647
643
648
644
sub file_change_string {
@@ -651,25 +647,26 @@ sub file_change_string {
651
647
652
648
# If they're the same it's a modify
653
649
if ($file_1 eq $file_2 ) {
654
- return " modified: $file_1 " ;
650
+ return ( " modified: $file_1 " , 10 + length ( $file_1 )) ;
655
651
# If the first is /dev/null it's a new file
656
652
} elsif ($file_1 eq " /dev/null" ) {
657
653
my $add_color = $DiffHighlight::NEW_HIGHLIGHT [1];
658
- return " added: $add_color$file_2$reset_color " ;
654
+ return ( " added: $add_color$file_2$reset_color " , 7 + length ( $file_2 )) ;
659
655
# If the second is /dev/null it's a deletion
660
656
} elsif ($file_2 eq " /dev/null" ) {
661
657
my $del_color = $DiffHighlight::OLD_HIGHLIGHT [1];
662
- return " deleted: $del_color$file_1$reset_color " ;
658
+ return ( " deleted: $del_color$file_1$reset_color " , 9 + length ( $file_1 )) ;
663
659
# If the files aren't the same it's a rename
664
660
} elsif ($file_1 ne $file_2 ) {
665
661
my ($old , $new ) = DiffHighlight::highlight_pair($file_1 ,$file_2 ,{only_diff => 1});
666
662
# highlight_pair already includes reset_color, but adds newline characters that need to be trimmed off
667
663
$old = trim($old );
668
664
$new = trim($new );
669
- 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 ));
670
667
# Something we haven't thought of yet
671
668
} else {
672
- return " $file_1 -> $file_2 " ;
669
+ return ( " $file_1 -> $file_2 " , 4 + length ( $file_1 ) + length ( $file_2 )) ;
673
670
}
674
671
}
675
672
@@ -871,6 +868,9 @@ sub color {
871
868
if ($str eq " meta" ) {
872
869
# Default ANSI yellow
873
870
$ret = DiffHighlight::color_config(' color.diff.meta' , color(11));
871
+ } elsif ($str eq " commit" ) {
872
+ # Default ANSI yellow bold
873
+ $ret = DiffHighlight::color_config(' color.diff.commit' , color(' 11_bold' ));
874
874
} elsif ($str eq " reset" ) {
875
875
$ret = color(" reset" );
876
876
} elsif ($str eq " add_line" ) {
0 commit comments