From: Nicolas Boisselier Date: Thu, 20 Apr 2023 21:20:43 +0000 (+0200) Subject: export color function X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=ef3486e72e04a80e190ba64a3377deba49e46516;p=nb.git export color function --- diff --git a/bin/color.pl b/bin/color.pl index aee49b3d..7370bdde 100755 --- a/bin/color.pl +++ b/bin/color.pl @@ -3,7 +3,7 @@ use strict qw(subs vars refs); ############################################################################### # V E R S I O N S ############################################################################### -my $VERSION = '0.0.2'; +my $VERSION = '0.0.3'; # english only - NB 07.02.14 # NB 23.11.01 # - creation ! @@ -11,48 +11,6 @@ my $VERSION = '0.0.2'; # G L O B A L E ############################################################################### my ($NAME) = $0 =~ m#([^/]+)$#; -my %BG = ( - 'black' => 40, - 'red' => 41, - 'green' => 42, - 'yellow' => 43, - 'blue' => 44, - 'magenta' => 45, - 'cyan' => 46, - 'white' => 47, - 'default' => 49, -); - -my %FG = ( - 'black' => 30, - 'red' => 31, - 'green' => 32, - 'yellow' => 33, - 'blue' => 34, - 'magenta' => 35, - 'cyan' => 36, - 'white' => 37, - 'default' => 39, -); - -my %EFFECT = ( - 'none' => 0, - 'bold' => 1, - 'pale' => 2, - 'evidence' => 3, - 'underline' => 4, - 'blink' => 5, - 'invert' => 7, - 'cache' => 8, - 'normal' => 22, - 'no_evidence' => 23, - 'no_souligne' => 24, - 'no_underline' => 24, - 'no_clignotant' => 25, - 'no_blink' => 25, - 'no_inverse' => 27, - 'no_invert' => 27, -); ############################################################################### # V A L E U R P A R D E F A U T ############################################################################### @@ -90,6 +48,7 @@ use Getopt::Long qw(:config default no_ignore_case); my %Opt; ############################################################################### # P R O G R A M M E P R I N C I P A L ############################################################################### +use NB::Color qw/color %BG %FG %EFFECT/; my $input = join('',@ARGV) || 'My Text'; if ($Opt{all}) { @@ -103,7 +62,7 @@ if ($Opt{all}) { printf "%21s", "$e ($e_num) | "; - print in_color($input,{ + print color($input,{ effect => $e, num => $PRINT_CODE, }),"\n"; @@ -115,7 +74,7 @@ if ($Opt{all}) { printf "%17s", "$fg ($fg_num) | "; - print in_color($input,{ + print color($input,{ fg => $fg, bg => $BG, effect => $EFFECT, @@ -129,7 +88,7 @@ if ($Opt{all}) { printf "%17s", "$bg ($bg_num) | "; - print in_color($input,{ + print color($input,{ fg => $FG, bg => $bg, effect => $EFFECT, @@ -140,7 +99,7 @@ if ($Opt{all}) { exit 0; } -print in_color($input,{ +print color($input,{ fg => $FG, bg => $BG, effect => $EFFECT, @@ -152,48 +111,6 @@ exit 0; ############################################################################### # F O N C T I O N S ############################################################################### -sub in_color { -#------------------------------------------------------------------------------ -# NB 18.11.01 -#------------------------------------------------------------------------------ -my ($txt,$hparam) = @_; - - my $new = ""; - $hparam->{bg} ||= 'default'; - $hparam->{fg} ||= 'default'; - $hparam->{effect} ||= 'normal'; - #die $hparam->{num}; - - if (!$hparam->{num} or $hparam->{num} == 2) { - - $new .= - "\e[" - . $BG{$hparam->{bg}} - . ";".$FG{$hparam->{fg}} - . ";".$EFFECT{$hparam->{effect}} - . "m".$txt - . "\e[0m" - ; - $new .= " | " if $hparam->{num} == 2; - - } - - if ($hparam->{num}) { - - $new .= - '\e[' - . $BG{$hparam->{bg}} - . ";".$FG{$hparam->{fg}} - . ";".$EFFECT{$hparam->{effect}} - . "m".$txt - . '\e[0m' - ; - - } - - return $new; -} - sub version { #------------------------------------------------------------------------------ # Affiche la version diff --git a/lib/perl/NB/Color.pm b/lib/perl/NB/Color.pm index 246fa528..9dd40d00 100644 --- a/lib/perl/NB/Color.pm +++ b/lib/perl/NB/Color.pm @@ -8,7 +8,7 @@ our @ISA = qw(Exporter); # inherit all of Exporter's methods our @EXPORT_OK = qw/ color /; -our @EXPORT = qw//; +our @EXPORT = qw/%BG %FG %EFFECT/; push(@EXPORT, @EXPORT_OK); our %EXPORT_TAGS; $EXPORT_TAGS{all} = [@EXPORT_OK];