Skip to content

Commit 59f5d97

Browse files
Add the new color() with a text option
1 parent 9318c4a commit 59f5d97

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

diff-so-fancy

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,18 +823,21 @@ sub set_defaults {
823823
# Borrowed from: https://www.perturb.org/display/1167_Perl_ANSI_colors.html
824824
# String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
825825
sub color {
826-
my $str = shift();
826+
my ($str, $txt) = @_;
827+
828+
# If we're NOT connected to a an interactive terminal don't do color
829+
#if (-t STDOUT == 0) { return ''; }
827830

828831
# No string sent in, so we just reset
829832
if (!length($str) || $str eq 'reset') { return "\e[0m"; }
830833

831834
# Some predefined colors
832-
my %color_map = qw(red 160 blue 21 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
835+
my %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0);
833836
$str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg;
834837

835838
# Get foreground/background and any commands
836-
my ($fc,$cmd) = $str =~ /(\d+)?_?(\w+)?/g;
837-
my ($bc) = $str =~ /on_?(\d+)/g;
839+
my ($fc,$cmd) = $str =~ /^(\d{1,3})?_?(\w+)?$/g;
840+
my ($bc) = $str =~ /on_(\d{1,3})$/g;
838841

839842
# Some predefined commands
840843
my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7);
@@ -844,6 +847,7 @@ sub color {
844847
if ($cmd_num) { $ret .= "\e[${cmd_num}m"; }
845848
if (defined($fc)) { $ret .= "\e[38;5;${fc}m"; }
846849
if (defined($bc)) { $ret .= "\e[48;5;${bc}m"; }
850+
if ($txt) { $ret .= $txt . "\e[0m"; }
847851

848852
return $ret;
849853
}

0 commit comments

Comments
 (0)