Skip to content

Commit 4bd14c3

Browse files
Newer version of color()
1 parent 4172462 commit 4bd14c3

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

diff-so-fancy

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,8 @@ sub set_defaults {
755755
return 1;
756756
}
757757

758-
# String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red_bold', 'red_on_blue', 'blink', 'italic'
758+
# Borrowed from: https://www.perturb.org/display/1167_Perl_ANSI_colors.html
759+
# String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
759760
sub color {
760761
my $str = shift();
761762

@@ -764,15 +765,15 @@ sub color {
764765

765766
# Some predefined colors
766767
my %color_map = qw(red 160 blue 21 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
767-
$str =~ s/$_/$color_map{$_}/g for keys %color_map;
768+
$str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg;
768769

769770
# Get foreground/background and any commands
770-
my ($fc,$cmd) = $str =~ /^(\d+)?_?(\w+)?/g;
771-
my ($bc) = $str =~ /on_?(\d+)$/g;
771+
my ($fc,$cmd) = $str =~ /(\d+)?_?(\w+)?/g;
772+
my ($bc) = $str =~ /on_?(\d+)/g;
772773

773774
# Some predefined commands
774775
my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7);
775-
my $cmd_num = $cmd_map{$cmd || 0};
776+
my $cmd_num = $cmd_map{$cmd // 0};
776777

777778
my $ret = '';
778779
if ($cmd_num) { $ret .= "\e[${cmd_num}m"; }

0 commit comments

Comments
 (0)