Skip to content

Commit

Permalink
Add the new color() with a text option
Browse files Browse the repository at this point in the history
  • Loading branch information
scottchiefbaker committed Apr 14, 2024
1 parent 9318c4a commit 59f5d97
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions diff-so-fancy
Original file line number Diff line number Diff line change
Expand Up @@ -823,18 +823,21 @@ sub set_defaults {
# Borrowed from: https://www.perturb.org/display/1167_Perl_ANSI_colors.html
# String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue'
sub color {
my $str = shift();
my ($str, $txt) = @_;

# If we're NOT connected to a an interactive terminal don't do color
#if (-t STDOUT == 0) { return ''; }

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

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

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

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

return $ret;
}
Expand Down

0 comments on commit 59f5d97

Please sign in to comment.