Skip to content

Commit 4f5a0e2

Browse files
committed
Fix inconsistently placed color reset codes
Sometimes the reset code ended up on a line by itself, and caused issues with total number of lines (important for `git add --patch`).
1 parent 11249af commit 4f5a0e2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

diff-so-fancy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,16 +543,23 @@ sub strip_leading_indicators {
543543
if ($manually_color_lines) {
544544
if (defined($5) && $5 eq "+") {
545545
my $add_line_color = get_config_color("add_line");
546-
$line = $add_line_color . $line . $reset_color;
546+
$line = $add_line_color . insert_reset_at_line_end($line);
547547
} elsif (defined($5) && $5 eq "-") {
548548
my $remove_line_color = get_config_color("remove_line");
549-
$line = $remove_line_color . $line . $reset_color;
549+
$line = $remove_line_color . insert_reset_at_line_end($line);
550550
}
551551
}
552552

553553
return $line;
554554
}
555555

556+
# Insert the color reset code at end of line, but before any newlines
557+
sub insert_reset_at_line_end {
558+
my $line = shift();
559+
$line =~ s/^(.*)([\n\r]+)?$/${1}${reset_color}${2}/;
560+
return $line;
561+
}
562+
556563
# Count the number of a given char in a string
557564
sub char_count {
558565
my ($needle,$str) = @_;

0 commit comments

Comments
 (0)