Skip to content

Commit e0e5af4

Browse files
Add early implementation for parsing git config color strings
1 parent 9b9058d commit e0e5af4

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

diff-so-fancy

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,74 @@ sub color {
898898
}
899899
}
900900

901+
sub git_ansi_color {
902+
my $str = shift();
903+
my @parts = split(' ', $str);
904+
905+
if (!@parts) {
906+
return '';
907+
}
908+
909+
my $colors = {
910+
'black' => 0,
911+
'red' => 1,
912+
'green' => 2,
913+
'yellow' => 3,
914+
'blue' => 4,
915+
'magenta' => 5,
916+
'cyan' => 6,
917+
'white' => 7,
918+
};
919+
920+
my $fg = $parts[0] || "";
921+
my $mod = $parts[1] || "";
922+
my $bg = $parts[2] || "";
923+
924+
my @ansi_part = ();
925+
#############################################
926+
927+
if ($mod eq 'bold') {
928+
push(@ansi_part, "1");
929+
}
930+
931+
#############################################
932+
933+
# It's an RGB value
934+
if (is_numeric($fg)) {
935+
push(@ansi_part, "38;5;$fg");
936+
# It's a simple 16 color OG ansi
937+
} elsif ($fg) {
938+
push(@ansi_part, $colors->{$fg} + 30);
939+
}
940+
941+
#############################################
942+
943+
# It's an RGB value
944+
if (is_numeric($bg)) {
945+
push(@ansi_part, "48;5;$bg");
946+
# It's a simple 16 color OG ansi
947+
} elsif ($bg) {
948+
push(@ansi_part, $colors->{$fg} + 40);
949+
}
950+
951+
#############################################
952+
953+
my $ansi_str = join(";", @ansi_part);
954+
my $ret = "\e[" . $ansi_str . "m";
955+
956+
return $ret;
957+
}
958+
959+
sub is_numeric {
960+
my $s = shift();
961+
962+
if ($s =~ /^\d+$/) {
963+
return 1;
964+
}
965+
966+
return 0;
967+
}
968+
901969
sub starts_with_ansi {
902970
my $str = shift();
903971

0 commit comments

Comments
 (0)