Skip to content

Commit c3205c8

Browse files
Merge pull request #422 from rwe/fix-start-with-ansi
fix: `starts_with_ansi` always returned 1 and had inverted logic
2 parents 1a1445c + 384ec32 commit c3205c8

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

diff-so-fancy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ my $ruler_width = git_config("diff-so-fancy.rulerWidth", undef);
2727
my $git_strip_prefix = git_config_boolean("diff.noprefix","false");
2828
my $has_stdin = has_stdin();
2929

30-
my $ansi_color_regex = qr/(\e\[([0-9]{1,3}(;[0-9]{1,3}){0,10})[mK])?/;
30+
my $ansi_regex = qr/\e\[([0-9]{1,3}(;[0-9]{1,3}){0,10})[mK]/;
31+
my $ansi_color_regex = qr/(${ansi_regex})?/;
3132
my $reset_color = color("reset");
3233
my $bold = color("bold");
3334
my $meta_color = "";
@@ -120,7 +121,7 @@ my $line_count = 0;
120121
while (my $line = <STDIN>) {
121122
# If the very first line of the diff doesn't start with ANSI color we're assuming
122123
# it's a raw patch file, and we have to color the added/removed lines ourself
123-
if (!$color_forced && $line_count == 0 && starts_with_ansi($line)) {
124+
if (!$color_forced && $line_count == 0 && !starts_with_ansi($line)) {
124125
$manually_color_lines = 1;
125126
}
126127

@@ -992,7 +993,8 @@ sub is_numeric {
992993
sub starts_with_ansi {
993994
my $str = shift();
994995

995-
if ($str =~ /^$ansi_color_regex/) {
996+
# NOTE: This is not `ansi_color_regex`, which includes "no ANSI sequences".
997+
if ($str =~ /^$ansi_regex/) {
996998
return 1;
997999
} else {
9981000
return 0;

0 commit comments

Comments
 (0)