Skip to content

Commit e93972f

Browse files
Support a couple more git ansi color strings
1 parent 5367f9f commit e93972f

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

diff-so-fancy

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,14 +930,20 @@ sub git_ansi_color {
930930
@parts = grep { !/reverse/ } @parts; # Remove from array
931931
}
932932

933-
my $fg = $parts[0] || "";
934-
my $bg = $parts[1] || "";
933+
my $fg = $parts[0] // "";
934+
my $bg = $parts[1] // "";
935935

936936
#############################################
937937

938938
# It's an numeric value, so it's an 8 bit color
939939
if (is_numeric($fg)) {
940-
push(@ansi_part, "38;5;$fg");
940+
if ($fg < 8) {
941+
push(@ansi_part, $fg + 30);
942+
} elsif ($fg < 16) {
943+
push(@ansi_part, $fg + 82);
944+
} else {
945+
push(@ansi_part, "38;5;$fg");
946+
}
941947
# It's a simple 16 color OG ansi
942948
} elsif ($fg) {
943949
my $bright = $fg =~ s/bright//;
@@ -952,7 +958,13 @@ sub git_ansi_color {
952958

953959
# It's an numeric value, so it's an 8 bit color
954960
if (is_numeric($bg)) {
955-
push(@ansi_part, "48;5;$bg");
961+
if ($bg < 8) {
962+
push(@ansi_part, $bg + 40);
963+
} elsif ($bg < 16) {
964+
push(@ansi_part, $bg + 92);
965+
} else {
966+
push(@ansi_part, "48;5;$bg");
967+
}
956968
# It's a simple 16 color OG ansi
957969
} elsif ($bg) {
958970
my $bright = $bg =~ s/bright//;

test/git_ansi_color.pl

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
###############################################################################
88
###############################################################################
99

10-
compare_color("\e[1;91m" , git_ansi_color("brightred bold") , "brightred bold");
11-
compare_color("\e[1;31m" , git_ansi_color("red bold") , "red bold");
12-
compare_color("\e[1;32m" , git_ansi_color("green bold") , "green bold");
13-
compare_color("\e[33m" , git_ansi_color("yellow") , "yellow");
14-
compare_color("\e[1;7;32m" , git_ansi_color("green bold reverse"), "green bold reverse");
15-
compare_color("\e[1;35m" , git_ansi_color("magenta bold") , "magenta bold");
16-
compare_color("\e[1;38;5;146m" , git_ansi_color("146 bold") , "146 bold");
17-
compare_color("\e[1;38;5;146;48;5;22m", git_ansi_color("146 bold 22") , "146 bold 22");
18-
compare_color("\e[1;34;40m" , git_ansi_color("blue black bold") , "blue black gold");
19-
compare_color("\e[38;5;11m" , git_ansi_color("11") , "11");
20-
compare_color("\e[7;31m" , git_ansi_color("red reverse") , "red reverse");
21-
compare_color("\e[1;31;48;5;52m" , git_ansi_color("red bold 52") , "red bold 52");
22-
compare_color("\e[38;5;10;48;5;20m" , git_ansi_color("10 20") , "10 20");
10+
compare_color("\e[1;91m" , git_ansi_color("brightred bold") , "brightred bold");
11+
compare_color("\e[1;31m" , git_ansi_color("red bold") , "red bold");
12+
compare_color("\e[1;32m" , git_ansi_color("green bold") , "green bold");
13+
compare_color("\e[33m" , git_ansi_color("yellow") , "yellow");
14+
compare_color("\e[1;7;32m" , git_ansi_color("green bold reverse"), "green bold reverse");
15+
compare_color("\e[1;35m" , git_ansi_color("magenta bold") , "magenta bold");
16+
compare_color("\e[1;38;5;146m" , git_ansi_color("146 bold") , "146 bold");
17+
compare_color("\e[1;38;5;146;48;5;22m" , git_ansi_color("146 bold 22") , "146 bold 22");
18+
compare_color("\e[1;34;40m" , git_ansi_color("blue black bold") , "blue black gold");
19+
compare_color("\e[93m" , git_ansi_color("11") , "11");
20+
compare_color("\e[7;31m" , git_ansi_color("red reverse") , "red reverse");
21+
compare_color("\e[1;31;48;5;52m" , git_ansi_color("red bold 52") , "red bold 52");
22+
compare_color("\e[92;48;5;20m" , git_ansi_color("10 20") , "10 20");
23+
compare_color("\e[30;47m" , git_ansi_color("0 7") , "0 7");
24+
compare_color("\e[94;105m" , git_ansi_color("12 13") , "12 13");
25+
compare_color("\e[1;38;5;254;48;5;255m", git_ansi_color("254 bold 255") , "254 bold 255");
2326

2427
###############################################################################
2528
###############################################################################
@@ -124,14 +127,20 @@ sub git_ansi_color {
124127
@parts = grep { !/reverse/ } @parts; # Remove from array
125128
}
126129

127-
my $fg = $parts[0] || "";
128-
my $bg = $parts[1] || "";
130+
my $fg = $parts[0] // "";
131+
my $bg = $parts[1] // "";
129132

130133
#############################################
131134

132135
# It's an numeric value, so it's an 8 bit color
133136
if (is_numeric($fg)) {
134-
push(@ansi_part, "38;5;$fg");
137+
if ($fg < 8) {
138+
push(@ansi_part, $fg + 30);
139+
} elsif ($fg < 16) {
140+
push(@ansi_part, $fg + 82);
141+
} else {
142+
push(@ansi_part, "38;5;$fg");
143+
}
135144
# It's a simple 16 color OG ansi
136145
} elsif ($fg) {
137146
my $bright = $fg =~ s/bright//;
@@ -146,7 +155,13 @@ sub git_ansi_color {
146155

147156
# It's an numeric value, so it's an 8 bit color
148157
if (is_numeric($bg)) {
149-
push(@ansi_part, "48;5;$bg");
158+
if ($bg < 8) {
159+
push(@ansi_part, $bg + 40);
160+
} elsif ($bg < 16) {
161+
push(@ansi_part, $bg + 92);
162+
} else {
163+
push(@ansi_part, "48;5;$bg");
164+
}
150165
# It's a simple 16 color OG ansi
151166
} elsif ($bg) {
152167
my $bright = $bg =~ s/bright//;

0 commit comments

Comments
 (0)