Getopt::EX::Colormap - ANSI terminal color and option support
GetOptions('colormap|cm:s' => @opt_colormap); require Getopt::EX::Colormap; my $cm = new Getopt::EX::Colormap; $cm->load_params(@opt_colormap); print $cm->color('FILE', 'FILE labeled text'); print $cm->index_color($index, 'TEXT'); or use Getopt::EX::Colormap qw(colorize); $text = colorize(SPEC, TEXT); $text = colorize(SPEC_1, TEXT_1, SPEC_2, TEXT_2, ...);
Coloring text capability is not strongly bound to option processing, but it may be useful to give simple uniform way to specify complicated color setting from command line.
This module assumes the color information is given in two ways: one in labeled list, and one in indexed list.
This is an example of labeled list:
--cm 'COMMAND=SE,OMARK=CS,NMARK=MS' \ --cm 'OTEXT=C,NTEXT=M,*CHANGE=BD/445,DELETE=APPEND=RD/544' \ --cm 'CMARK=GS,MMARK=YS,CTEXT=G,MTEXT=Y'
Each color definitions are separated by comma (,) and label is specified by LABEL= style precedence. Multiple labels can be set for same value by connecting them together. Label name can be specified with * and ? wild characters.
,
*
?
Indexed list example is like this:
--cm 555/100,555/010,555/001 \ --cm 555/011,555/101,555/110 \ --cm 555/021,555/201,555/210 \ --cm 555/012,555/102,555/120
This is the example of RGB 6x6x6 216 colors specification. Left side of slash is foreground color, and right side is for background. This color list is accessed by index.
Handler maintains hash and list objects, and labeled colors are stored in hash, non-label colors are in list automatically. User can mix both specifications.
Besides producing ANSI colored text, this module supports calling arbitrary function to handle a string. See "FUNCTION SPEC" section for more detail.
Color specification is a combination of single uppercase character representing 8 colors :
R Red G Green B Blue C Cyan M Magenta Y Yellow K Black W White
and alternative (usually brighter) colors in lowercase:
r, g, b, c, m, y, k, w
or RGB values and 24 grey levels if using ANSI 256 or full color terminal :
000000 .. FFFFFF : 24bit RGB colors 000 .. 555 : 6x6x6 RGB 216 colors L00 .. L23 : 24 grey levels
Note that, when values are all same in 24bit RGB, it is converted to 24 grey level, otherwise 6x6x6 216 color.
with other special effects :
Z 0 Zero (reset) D 1 Double-struck (boldface) P 2 Pale (dark) I 3 Italic U 4 Underline F 5 Flash (blink: slow) Q 6 Quick (blink: rapid) S 7 Stand-out (reverse video) V 8 Vanish (concealed) J 9 Junk (crossed out) E Erase Line ; No effect X No effect
If the spec includes /, left side is considered as foreground color and right side as background. If multiple colors are given in same spec, all indicators are produced in the order of their presence. Consequently, the last one takes effect.
/
Effect characters are case insensitive, and can be found anywhere and in any order in color spec string. Because X and ; takes no effect, you can use them to improve readability, like SxD;K/544.
X
;
SxD;K/544
Samples:
RGB 6x6x6 24bit color === ======= ============= ================== B 005 0000FF : blue foreground /M /505 /FF00FF : magenta background K/W 000/555 000000/FFFFFF : black on white R/G 500/050 FF0000/00FF00 : red on green W/w L03/L20 303030/c6c6c6 : grey on grey
24-bit RGB color sequence is supported but disabled by default. Set $COLOR_RGB24 module variable to enable it.
$COLOR_RGB24
Character "E" is abbreviation for "{EL}", and it clears the line from cursor to the end of the line. At this time, background color is set to the area. When this code is found at the end of start sequence, it is copied to just before ending reset sequence, to kepp the effect even when the text is wrapped to multiple lines.
Other ANSI CSI sequences are also available in the form of "{NAME}", despite there are few reasons to use them.
CUU n Cursor up CUD n Cursor Down CUF n Cursor Forward CUB n Cursor Back CNL n Cursor Next Line CPL n Cursor Previous line CHA n Cursor Horizontal Absolute CUP n,m Cursor Position ED n Erase in Display (0 after, 1 before, 2 entire, 3 w/buffer) EL n Erase in Line (0 after, 1 before, 2 entire) SU n Scroll Up SD n Scroll Down HVP n,m Horizontal Vertical Position SGR n* Select Graphic Rendition SCP Save Cursor Position RCP Restore Cursor Position
These name accept following optional numerical parameters, using comma (',') or semicolon (';') to separate multiple ones, with optional braces. For example, color spec DK/544 can be described as {SGR1;30;48;5;224} or more readable {SGR(1,30,48,5,224)}.
DK/544
{SGR1;30;48;5;224}
{SGR(1,30,48,5,224)}
It is also possible to set arbitrary function which is called to handle string in place of color, and that is not necessarily concerned with color. This scheme is quite powerful and the module name itself may be somewhat misleading. Spec string which start with sub{ is considered as a function definition. So
sub{
% example --cm 'sub{uc}'
set the function object in the color entry. And when color method is called with that object, specified function is called instead of producing ANSI color sequence. Function is supposed to get the target text as a global variable $_, and return the result as a string. Function sub{uc} in the above example returns uppercase version of $_.
color
$_
sub{uc}
If your script prints file name according to the color spec labeled by FILE, then
% example --cm FILE=R
prints the file name in red, but
% example --cm FILE=sub{uc}
will print the name in uppercases.
Spec start with & is considered as a function name. If the function double is defined like:
&
double
sub double { $_ . $_ }
then, command
% example --cm '&double'
produces doubled text by color method. Function can also take parameters, so the next example
sub repeat { my %opt = @_; $_ x $opt{count} // 1; } % example --cm '&repeat(count=3)'
produces tripled text.
Function object is created by <Getopt::EX::Func> module. Take a look at the module for detail.
#!/usr/bin/perl use strict; use warnings; my @opt_colormap; use Getopt::EX::Long; GetOptions("colormap|cm=s" => \@opt_colormap); my %colormap = ( # default color map FILE => 'R', LINE => 'G', TEXT => 'B', ); my @colors; require Getopt::EX::Colormap; my $handler = new Getopt::EX::Colormap HASH => \%colormap, LIST => \@colors; $handler->load_params(@opt_colormap); for (0 .. $#colors) { print $handler->index_color($_, "COLOR $_"), "\n"; } for (sort keys %colormap) { print $handler->color($_, $_), "\n"; }
This sample program is complete to work. If you save this script as a file example, try to put following contents in ~/.examplerc and see what happens.
option default \ --cm 555/100,555/010,555/001 \ --cm 555/011,555/101,555/110 \ --cm 555/021,555/201,555/210 \ --cm 555/012,555/102,555/120
Return colored text indicated by label or color spec string.
Return colored text indicated by index. If the index is bigger than color list, it rounds up.
See super class Getopt::EX::LabeledParam.
Getopt::EX, Getopt::EX::LabeledParam
https://en.wikipedia.org/wiki/ANSI_escape_code
To install Getopt::EX, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Getopt::EX
CPAN shell
perl -MCPAN -e shell install Getopt::EX
For more information on module installation, please visit the detailed CPAN module installation guide.