blib/lib/Term/ANSIColor.pm

filter: Statementstmt 100% 261 / 261 Branchbran 100% 154 / 154 Conditioncond 100% 111 / 111 MC/DCmcdc 100% 71 / 71 Subroutinesub 100% 30 / 30 Podpod 25.0% 6 / 24 totaltotal 97.2% 633 / 651 SCAR 50.0
CC
149
Cov
100%
color
11.0
uncolor
10.0
coloralias
8.0
CRAP 149.0
↑ Summary
1 # Color screen output using ANSI escape sequences.
2 #
3 # This module provides utility functions (in two different forms) for coloring
4 # output with ANSI escape sequences.
5 #
6 # This module is sometimes used in low-memory environments, so avoid use of
7 # \d, \w, [:upper:], and similar constructs in the most important functions
8 # (color, colored, AUTOLOAD, and the generated constant functions) since
9 # loading the Unicode attribute files consumes a lot of memory.
10 #
11 # Ah, September, when the sysadmins turn colors and fall off the trees....
12 # -- Dave Van Domelen
13 #
14 # SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
15  
16 ##############################################################################
17 # Modules and declarations
18 ##############################################################################
19  
20 package Term::ANSIColor;
21  
22 9 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Also uses Carp but loads it on demand to reduce memory usage.
27  
28 9 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # use Exporter plus @ISA instead of use base to reduce memory usage.
31 ## no critic (ClassHierarchies::ProhibitExplicitISA)
32  
33 # Declare variables that should be set in BEGIN for robustness.
34 ## no critic (Modules::ProhibitAutomaticExportation)
35 9 our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS, @ISA, $VERSION);
36  
37 # We use autoloading, which sets this variable to the name of the called sub.
38 9 our $AUTOLOAD;
39  
40 # Set $VERSION and everything export-related in a BEGIN block for robustness
41 # against circular module loading (not that we load any modules, but
42 # consistency is good).
43 BEGIN {
44 9 ▶ $VERSION = '5.00';
Subroutine BEGIN: called CC=3 SCAR=11.0
45  
46 # All of the basic supported constants, used in %EXPORT_TAGS.
47 9 my @colorlist = qw(
48 CLEAR RESET BOLD DARK
49 FAINT ITALIC UNDERLINE UNDERSCORE
50 BLINK REVERSE CONCEALED
51  
52 BLACK RED GREEN YELLOW
53 BLUE MAGENTA CYAN WHITE
54 ON_BLACK ON_RED ON_GREEN ON_YELLOW
55 ON_BLUE ON_MAGENTA ON_CYAN ON_WHITE
56  
57 BRIGHT_BLACK BRIGHT_RED BRIGHT_GREEN BRIGHT_YELLOW
58 BRIGHT_BLUE BRIGHT_MAGENTA BRIGHT_CYAN BRIGHT_WHITE
59 ON_BRIGHT_BLACK ON_BRIGHT_RED ON_BRIGHT_GREEN ON_BRIGHT_YELLOW
60 ON_BRIGHT_BLUE ON_BRIGHT_MAGENTA ON_BRIGHT_CYAN ON_BRIGHT_WHITE
61 );
62  
63 # 256-color constants, used in %EXPORT_TAGS.
64 my @colorlist256 = (
65 2304 (map { ("ANSI$_", "ON_ANSI$_") } 0 .. 255),
66 9 (map { ("GREY$_", "ON_GREY$_") } 0 .. 23),
67 );
68 9 for my $r (0 .. 5) {
69 54 for my $g (0 .. 5) {
70 324 push(@colorlist256, map { ("RGB$r$g$_", "ON_RGB$r$g$_") } 0 .. 5);
71 }
72 }
73  
74 # Exported symbol configuration.
75 9 @ISA = qw(Exporter);
76 9 @EXPORT = qw(color colored);
77 9 @EXPORT_OK = qw(uncolor colorstrip colorvalid coloralias);
78 9 %EXPORT_TAGS = (
79 constants => \@colorlist,
80 constants256 => \@colorlist256,
81 pushpop => [@colorlist, qw(PUSHCOLOR POPCOLOR LOCALCOLOR)],
82 );
83 9 Exporter::export_ok_tags('pushpop', 'constants256');
84 }
85  
86 ##############################################################################
87 # Package variables
88 ##############################################################################
89  
90 # If this is set, any color changes will implicitly push the current color
91 # onto the stack and then pop it at the end of the constant sequence, just as
92 # if LOCALCOLOR were used.
93 9 our $AUTOLOCAL;
94  
95 # Caller sets this to force a reset at the end of each constant sequence.
96 9 our $AUTORESET;
97  
98 # Caller sets this to force colors to be reset at the end of each line.
99 9 our $EACHLINE;
100  
101 ##############################################################################
102 # Internal data structures
103 ##############################################################################
104  
105 # This module does quite a bit of initialization at the time it is first
106 # loaded, primarily to set up the package-global %ATTRIBUTES hash. The
107 # entries for 256-color names are easier to handle programmatically, and
108 # custom colors are also imported from the environment if any are set.
109  
110 # All basic supported attributes, including aliases.
111 #<<<
112 9 our %ATTRIBUTES = (
113 'clear' => 0,
114 'reset' => 0,
115 'bold' => 1,
116 'dark' => 2,
117 'faint' => 2,
118 'italic' => 3,
119 'underline' => 4,
120 'underscore' => 4,
121 'blink' => 5,
122 'reverse' => 7,
123 'concealed' => 8,
124  
125 'black' => 30, 'on_black' => 40,
126 'red' => 31, 'on_red' => 41,
127 'green' => 32, 'on_green' => 42,
128 'yellow' => 33, 'on_yellow' => 43,
129 'blue' => 34, 'on_blue' => 44,
130 'magenta' => 35, 'on_magenta' => 45,
131 'cyan' => 36, 'on_cyan' => 46,
132 'white' => 37, 'on_white' => 47,
133  
134 'bright_black' => 90, 'on_bright_black' => 100,
135 'bright_red' => 91, 'on_bright_red' => 101,
136 'bright_green' => 92, 'on_bright_green' => 102,
137 'bright_yellow' => 93, 'on_bright_yellow' => 103,
138 'bright_blue' => 94, 'on_bright_blue' => 104,
139 'bright_magenta' => 95, 'on_bright_magenta' => 105,
140 'bright_cyan' => 96, 'on_bright_cyan' => 106,
141 'bright_white' => 97, 'on_bright_white' => 107,
142 );
143 #>>>
144  
145 # Generating the 256-color codes involves a lot of codes and offsets that are
146 # not helped by turning them into constants.
147  
148 # The first 16 256-color codes are duplicates of the 16 ANSI colors. The rest
149 # are RBG and greyscale values.
150 9 for my $code (0 .. 15) {
151 144 $ATTRIBUTES{"ansi$code"} = "38;5;$code";
152 144 $ATTRIBUTES{"on_ansi$code"} = "48;5;$code";
153 }
154  
155 # 256-color RGB colors. Red, green, and blue can each be values 0 through 5,
156 # and the resulting 216 colors start with color 16.
157 9 for my $r (0 .. 5) {
158 54 for my $g (0 .. 5) {
159 324 for my $b (0 .. 5) {
160 1944 my $code = 16 + (6 * 6 * $r) + (6 * $g) + $b;
161 1944 $ATTRIBUTES{"rgb$r$g$b"} = "38;5;$code";
162 1944 $ATTRIBUTES{"on_rgb$r$g$b"} = "48;5;$code";
163 }
164 }
165 }
166  
167 # The last 256-color codes are 24 shades of grey.
168 9 for my $n (0 .. 23) {
169 216 my $code = $n + 232;
170 216 $ATTRIBUTES{"grey$n"} = "38;5;$code";
171 216 $ATTRIBUTES{"on_grey$n"} = "48;5;$code";
172 }
173  
174 # Reverse lookup. Alphabetically first name for a sequence is preferred.
175 9 our %ATTRIBUTES_R;
176 9 for my $attr (reverse(sort(keys(%ATTRIBUTES)))) {
177 4995 $ATTRIBUTES_R{ $ATTRIBUTES{$attr} } = $attr;
178 }
179  
180 # Provide ansiN names for all 256 characters to provide a convenient flat
181 # namespace if one doesn't want to mess with the RGB and greyscale naming. Do
182 # this after creating %ATTRIBUTES_R since we want to use the canonical names
183 # when reversing a color.
184 9 for my $code (16 .. 255) {
185 2160 $ATTRIBUTES{"ansi$code"} = "38;5;$code";
186 2160 $ATTRIBUTES{"on_ansi$code"} = "48;5;$code";
187 }
188  
189 # Import any custom colors set in the environment.
190 9 our %ALIASES;
191 9 ▶if (exists($ENV{ANSI_COLORS_ALIASES})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"ANSI_COLORS_ALIASES"}) 1 8 9
192 1 my $spec = $ENV{ANSI_COLORS_ALIASES};
193 1 $spec =~ s{ \A \s+ }{}xms;
194 1 $spec =~ s{ \s+ \z }{}xms;
195  
196 # Error reporting here is an interesting question. Use warn rather than
197 # carp because carp would report the line of the use or require, which
198 # doesn't help anyone understand what's going on, whereas seeing this code
199 # will be more helpful.
200 ## no critic (ErrorHandling::RequireCarping)
201 1 for my $definition (split(m{\s*,\s*}xms, $spec)) {
202 12 my ($new, $old) = split(m{\s*=\s*}xms, $definition, 2);
203 12 ▶ if (!$new || !$old) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $new && $old) { } 3 9 12
Condition100%3/3
$new && $old
!ll&&!rl&&r
129
MC/DC100%2/2
$new && $old
$new $old
Truth table100%3/3
A && B
A= $newB= $old
ABresult
0X0
100
111
204 3 warn qq{Bad color mapping "$definition"};
205 } else {
206 9 my $result = eval { coloralias($new, $old) };
207 9 ▶ if (!$result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 4 5 9
208 4 my $error = $@;
209 4 $error =~ s{ [ ] at [ ] .* }{}xms;
210 4 warn qq{$error in "$definition"};
211 }
212 }
213 }
214 }
215  
216 # Stores the current color stack maintained by PUSHCOLOR and POPCOLOR. This
217 # is global and therefore not threadsafe.
218 9 our @COLORSTACK;
219  
220 ##############################################################################
221 # Helper functions
222 ##############################################################################
223  
224 # Stub to load the Carp module on demand.
225 sub croak {
226 55 ▶ my (@args) = @_;
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
227 55 require Carp;
228 55 Carp::croak(@args);
229 }
230  
231 ##############################################################################
232 # Implementation (constant form)
233 ##############################################################################
234  
235 # Time to have fun! We now want to define the constant subs, which are named
236 # the same as the attributes above but in all caps. Each constant sub needs
237 # to act differently depending on whether $AUTORESET is set. Without
238 # autoreset:
239 #
240 # BLUE "text\n" ==> "\e[34mtext\n"
241 #
242 # If $AUTORESET is set, we should instead get:
243 #
244 # BLUE "text\n" ==> "\e[34mtext\n\e[0m"
245 #
246 # The sub also needs to handle the case where it has no arguments correctly.
247 # Maintaining all of this as separate subs would be a major nightmare, as well
248 # as duplicate the %ATTRIBUTES hash, so instead we define an AUTOLOAD sub to
249 # define the constant subs on demand. To do that, we check the name of the
250 # called sub against the list of attributes, and if it's an all-caps version
251 # of one of them, we define the sub on the fly and then run it.
252 #
253 # If the environment variable ANSI_COLORS_DISABLED is set to a true value,
254 # just return the arguments without adding any escape sequences. This is to
255 # make it easier to write scripts that also work on systems without any ANSI
256 # support, like Windows consoles.
257 #
258 # Avoid using character classes like [:upper:] and \w here, since they load
259 # Unicode character tables and consume a ton of memory. All of our constants
260 # only use ASCII characters.
261 #
262 ## no critic (ClassHierarchies::ProhibitAutoloading)
263 ## no critic (Subroutines::RequireArgUnpacking)
264 ## no critic (RegularExpressions::ProhibitEnumeratedClasses)
265 sub AUTOLOAD {
266 29 ▶ my ($sub, $attr) = $AUTOLOAD =~ m{
Subroutine AUTOLOAD: called CC=5 SCAR=16.1
267 \A ( [a-zA-Z0-9:]* :: ([A-Z0-9_]+) ) \z
268 }xms;
269  
270 # Check if we were called with something that doesn't look like an
271 # attribute.
272 29 ▶ if (!($attr && defined($ATTRIBUTES{ lc $attr }))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr and defined $Term::ANSIColor::ATTRIBUTES{lc $attr}) 3 26 29
Condition100%3/3
$attr and defined $Term::ANSIColor::ATTRIBUTES{lc $attr}
!ll&&!rl&&r
1226
MC/DC100%2/2
$attr and defined $Term::ANSIColor::ATTRIBUTES{lc $attr}
$attr defined $Term::ANSIColor::ATTRIBUTES{lc $attr}
Truth table100%3/3
A and B
A= $attrB= defined $Term::ANSIColor::ATTRIBUTES{lc $attr}
ABresult
0X0
100
111
273 3 croak("undefined subroutine &$AUTOLOAD called");
274 }
275  
276 # If colors are disabled, just return the input. Do this without
277 # installing a sub for (marginal, unbenchmarked) speed.
278 26 ▶ if ($ENV{ANSI_COLORS_DISABLED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"ANSI_COLORS_DISABLED"}) 1 25 26
279 1 return join(q{}, @_);
280 }
281  
282 # We've untainted the name of the sub.
283 25 $AUTOLOAD = $sub;
284  
285 # Figure out the ANSI string to set the desired attribute.
286 25 my $escape = "\e[" . $ATTRIBUTES{ lc $attr } . 'm';
287  
288 # Save the current value of $@. We can't just use local since we want to
289 # restore it before dispatching to the newly-created sub. (The caller may
290 # be colorizing output that includes $@.)
291 25 my $eval_err = $@;
292  
293 # Generate the constant sub, which should still recognize some of our
294 # package variables. Use string eval to avoid a dependency on
295 # Sub::Install, even though it makes it somewhat less readable.
296 ## no critic (BuiltinFunctions::ProhibitStringyEval)
297 ## no critic (ValuesAndExpressions::ProhibitImplicitNewlines)
298 25 ▶ my $eval_result = eval qq{
Subroutine BLUE: called CC=6 SCAR=17.9
Subroutine BOLD: called CC=6 SCAR=17.9
Subroutine BRIGHT_RED: called CC=6 SCAR=17.9
Subroutine DARK: called CC=6 SCAR=17.9
Subroutine FAINT: called CC=6 SCAR=17.9
Subroutine GREEN: called CC=6 SCAR=17.9
Subroutine ITALIC: called CC=6 SCAR=17.9
Subroutine ON_BLUE: called CC=6 SCAR=17.9
Subroutine ON_BRIGHT_RED: called CC=6 SCAR=17.9
Subroutine ON_GREEN: called CC=6 SCAR=17.9
Subroutine RED: called CC=6 SCAR=17.9
Subroutine RESET: called CC=6 SCAR=17.9
Subroutine ANSI0: called CC=6 SCAR=17.9
Subroutine ANSI15: called CC=6 SCAR=17.9
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 42 branches
BranchTrueFalseTotal
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 4 28 32
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 6 22 28
elsif ($Term::ANSIColor::AUTORESET and @_) { } 6 16 22
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 3 24 27
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 3 21 24
elsif ($Term::ANSIColor::AUTORESET and @_) { } 4 17 21
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 7 8
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 6 7
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 5 6
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 2 11 13
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 2 9 11
elsif ($Term::ANSIColor::AUTORESET and @_) { } 2 7 9
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 2 11 13
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 2 9 11
elsif ($Term::ANSIColor::AUTORESET and @_) { } 2 7 9
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 8 9
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 7 8
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 6 7
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 6 7
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 5 6
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 4 5
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 7 8
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 6 7
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 5 6
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 6 7
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 5 6
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 4 5
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 9 10
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 8 9
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 7 8
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 9 10
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 8 9
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 7 8
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 31 32
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 30 31
elsif ($Term::ANSIColor::AUTORESET and @_) { } 2 28 30
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 5 6
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 4 5
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 3 4
if ($ENV{'ANSI_COLORS_DISABLED'}) { } 1 5 6
elsif ($Term::ANSIColor::AUTOLOCAL and @_) { } 1 4 5
elsif ($Term::ANSIColor::AUTORESET and @_) { } 1 3 4
Condition100%84/84
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
1936
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
1336
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
1833
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
1344
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
511
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
411
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
722
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
522
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
722
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
522
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
611
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
511
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
411
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
311
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
511
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
411
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
411
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
311
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
711
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
611
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
711
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
611
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
9211
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
2712
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
311
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
211
$Term::ANSIColor::AUTOLOCAL and @_
!ll&&!rl&&r
311
$Term::ANSIColor::AUTORESET and @_
!ll&&!rl&&r
211
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
MC/DC100%2/2
$Term::ANSIColor::AUTOLOCAL and @_
$Term::ANSIColor::AUTOLOCAL @_
MC/DC100%2/2
$Term::ANSIColor::AUTORESET and @_
$Term::ANSIColor::AUTORESET @_
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTOLOCALB= @_
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $Term::ANSIColor::AUTORESETB= @_
ABresult
0X0
100
111
299 sub $AUTOLOAD {
300 if (\$ENV{ANSI_COLORS_DISABLED}) {
301 return join(q{}, \@_);
302 } elsif (\$AUTOLOCAL && \@_) {
303 return PUSHCOLOR('$escape') . join(q{}, \@_) . POPCOLOR;
304 } elsif (\$AUTORESET && \@_) {
305 return '$escape' . join(q{}, \@_) . "\e[0m";
306 } else {
307 return '$escape' . join(q{}, \@_);
308 }
309 }
310 1;
311 };
312  
313 # Failure is an internal error, not a problem with the caller.
314 ## no critic (ErrorHandling::RequireCarping)
315 25 ▶ if (!$eval_result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_result) 1 24 25
316 1 die "failed to generate constant $attr: $@";
317 }
318  
319 # Restore $@.
320 ## no critic (Variables::RequireLocalizedPunctuationVars)
321 24 $@ = $eval_err;
322  
323 # Dispatch to the newly-created sub.
324 24 goto &$AUTOLOAD;
325 }
326 ## use critic
327  
328 # Append a new color to the top of the color stack and return the top of
329 # the stack.
330 #
331 # $text - Any text we're applying colors to, with color escapes prepended
332 #
333 # Returns: The text passed in
334 sub PUSHCOLOR {
335 32 ▶ my (@text) = @_;
Subroutine PUSHCOLOR: called CC=2 SCAR=6.9
Pod undocumented
336 32 my $text = join(q{}, @text);
337  
338 # Extract any number of color-setting escape sequences from the start of
339 # the string.
340 32 my ($color) = $text =~ m{ \A ( (?:\e\[ [\d;]+ m)+ ) }xms;
341  
342 # If we already have a stack, append these escapes to the set from the top
343 # of the stack. This way, each position in the stack stores the complete
344 # enabled colors for that stage, at the cost of some potential
345 # inefficiency.
346 32 ▶ if (@COLORSTACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Term::ANSIColor::COLORSTACK) 8 24 32
347 8 $color = $COLORSTACK[-1] . $color;
348 }
349  
350 # Push the color onto the stack.
351 32 push(@COLORSTACK, $color);
352 32 return $text;
353 }
354  
355 # Pop the color stack and return the new top of the stack (or reset, if
356 # the stack is empty).
357 #
358 # @text - Any text we're applying colors to
359 #
360 # Returns: The concatenation of @text prepended with the new stack color
361 sub POPCOLOR {
362 32 ▶ my (@text) = @_;
Subroutine POPCOLOR: called CC=2 SCAR=6.9
Pod undocumented
363 32 pop(@COLORSTACK);
364 32 ▶ if (@COLORSTACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Term::ANSIColor::COLORSTACK) { } 8 24 32
365 8 return $COLORSTACK[-1] . join(q{}, @text);
366 } else {
367 24 return RESET(@text);
368 }
369 }
370  
371 # Surround arguments with a push and a pop. The effect will be to reset the
372 # colors to whatever was on the color stack before this sequence of colors was
373 # applied.
374 #
375 # @text - Any text we're applying colors to
376 #
377 # Returns: The concatenation of the text and the proper color reset sequence.
378 sub LOCALCOLOR {
379 3 ▶ my (@text) = @_;
Subroutine LOCALCOLOR: called CC=1 SCAR=0.0
Pod undocumented
380 3 return PUSHCOLOR(join(q{}, @text)) . POPCOLOR();
381 }
382  
383 ##############################################################################
384 # Implementation (attribute string form)
385 ##############################################################################
386  
387 # Return the escape code for a given set of color attributes.
388 #
389 # @codes - A list of possibly space-separated color attributes
390 #
391 # Returns: The escape sequence setting those color attributes
392 # undef if no escape sequences were given
393 # Throws: Text exception for any invalid attribute
394 sub color {
395 100 ▶ my (@codes) = @_;
Subroutine color: called CC=11 SCAR=24.0
Pod documented
396  
397 # Return the empty string if colors are disabled.
398 100 ▶ if ($ENV{ANSI_COLORS_DISABLED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"ANSI_COLORS_DISABLED"}) 1 99 100
399 1 return q{};
400 }
401  
402 # Split on whitespace and expand aliases.
403 99 @codes = map { split } @codes;
404 99 ▶ @codes = map { defined($ALIASES{$_}) ? @{ $ALIASES{$_} } : $_ } @codes;
Branch: 1 branch
BranchTrueFalseTotal
defined $Term::ANSIColor::ALIASES{$_} ? : 22 98 120
405  
406 # Build the attribute string from semicolon-separated numbers.
407 ## no critic (RegularExpressions::ProhibitEnumeratedClasses)
408 99 my $attribute = q{};
409 99 for my $code (@codes) {
410 128 $code = lc($code);
411 128 ▶ if (defined($ATTRIBUTES{$code})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Term::ANSIColor::ATTRIBUTES{$code}) { } 94 34 128
elsif ($code =~ / \A (on_)? r([0-9]+) g([0-9]+) b([0-9]+) \z /msx) { } 18 16 34
412 94 $attribute .= $ATTRIBUTES{$code} . q{;};
413 } elsif ($code =~ m{ \A (on_)? r([0-9]+) g([0-9]+) b([0-9]+) \z }xms) {
414 18 my ($r, $g, $b) = ($2 + 0, $3 + 0, $4 + 0);
415 18 ▶ if ($r > 255 || $g > 255 || $b > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r > 255 or $g > 255 or $b > 255) 2 14 16
Condition100%6/6
$r > 255 or $g > 255 or $b > 255
l!l&&r!l&&!r
3114
$r > 255 or $g > 255
l!l&&r!l&&!r
2115
MC/DC100%3/3
$r > 255 or $g > 255 or $b > 255
$r > 255 $g > 255 $b > 255
Truth table100%4/4
A or B or C
A= $r > 255B= $g > 255C= $b > 255
ABCresult
1XX1
01X1
0011
0000
416 4 croak("Invalid attribute name $code");
417 }
418 14 ▶ my $prefix = $1 ? '48' : '38';
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 7 7 14
419 14 $attribute .= "$prefix;2;$r;$g;$b;";
420 } else {
421 16 croak("Invalid attribute name $code");
422 }
423 }
424 ## use critic
425  
426 # We added one too many semicolons for simplicity. Remove the last one.
427 79 chop($attribute);
428  
429 # Return undef if there were no attributes.
430 79 ▶ return ($attribute ne q{}) ? "\e[${attribute}m" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$attribute ne '' ? : 78 1 79
431 }
432  
433 # Return a list of named color attributes for a given set of escape codes.
434 # Escape sequences can be given with or without enclosing "\e[" and "m". The
435 # empty escape sequence '' or "\e[m" gives an empty list of attrs.
436 #
437 # There is one special case. 256-color codes start with 38 or 48, followed by
438 # a 5 and then the 256-color code.
439 #
440 # @escapes - A list of escape sequences or escape sequence numbers
441 #
442 # Returns: An array of attribute names corresponding to those sequences
443 # Throws: Text exceptions on invalid escape sequences or unknown colors
444 sub uncolor {
445 43 ▶ my (@escapes) = @_;
Subroutine uncolor: called CC=10 SCAR=23.0
Pod documented
446 43 my (@nums, @result);
447  
448 # Walk the list of escapes and build a list of attribute numbers.
449 43 for my $escape (@escapes) {
450 46 $escape =~ s{ \A \e\[ }{}xms;
451 46 $escape =~ s{ m \z } {}xms;
452 46 my ($attrs) = $escape =~ m{ \A ((?:\d+;)* \d*) \z }xms;
453 46 ▶ if (!defined($attrs)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attrs) 1 45 46
454 1 croak("Bad escape sequence $escape");
455 }
456  
457 # Pull off 256-color codes (38;5;n or 48;5;n) and true color codes
458 # (38;2;n;n;n or 48;2;n;n;n) as a unit.
459 45 my $regex = qr{
460 (
461 0*[34]8 ; 0*2 ; \d+ ; \d+ ; \d+
462 | 0*[34]8 ; 0*5 ; \d+
463 | \d+
464 )
465 (?: ; | \z )
466 }xms;
467 45 push(@nums, $attrs =~ m{$regex}xmsg);
468 }
469  
470 # Now, walk the list of numbers and convert them to attribute names.
471 # Strip leading zeroes from any of the numbers. (xterm, at least, allows
472 # leading zeroes to be added to any number in an escape sequence.)
473 42 for my $num (@nums) {
474 48 ▶ if ($num =~ m{ \A 0*([34])8 ; 0*2 ; (\d+) ; (\d+) ; (\d+) \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ / \A 0*([34])8 ; 0*2 ; (\d+) ; (\d+) ; (\d+) \z /msx) { } 15 33 48
475 15 my ($r, $g, $b) = ($2 + 0, $3 + 0, $4 + 0);
476 15 ▶ if ($r > 255 || $g > 255 || $b > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r > 255 or $g > 255 or $b > 255) 4 7 11
Condition100%6/6
$r > 255 or $g > 255 or $b > 255
l!l&&r!l&&!r
627
$r > 255 or $g > 255
l!l&&r!l&&!r
429
MC/DC100%3/3
$r > 255 or $g > 255 or $b > 255
$r > 255 $g > 255 $b > 255
Truth table100%4/4
A or B or C
A= $r > 255B= $g > 255C= $b > 255
ABCresult
1XX1
01X1
0011
0000
477 8 croak("No name for escape sequence $num");
478 }
479 7 ▶ my $prefix = ($1 == 4) ? 'on_' : q{};
Branch: 1 branch
BranchTrueFalseTotal
$1 == 4 ? : 3 4 7
480 7 push(@result, "${prefix}r${r}g${g}b${b}");
481 } else {
482 33 $num =~ s{ ( \A | ; ) 0+ (\d) }{$1$2}xmsg;
483 33 my $name = $ATTRIBUTES_R{$num};
484 33 ▶ if (!defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 15 18 33
485 15 croak("No name for escape sequence $num");
486 }
487 18 push(@result, $name);
488 }
489 }
490  
491 # Return the attribute names.
492 19 return @result;
493 }
494  
495 # Given a string and a set of attributes, returns the string surrounded by
496 # escape codes to set those attributes and then clear them at the end of the
497 # string. The attributes can be given either as an array ref as the first
498 # argument or as a list as the second and subsequent arguments.
499 #
500 # If $EACHLINE is set, insert a reset before each occurrence of the string
501 # $EACHLINE and the starting attribute code after the string $EACHLINE, so
502 # that no attribute crosses line delimiters (this is often desirable if the
503 # output is to be piped to a pager or some other program).
504 #
505 # $first - An anonymous array of attributes or the text to color
506 # @rest - The text to color or the list of attributes
507 #
508 # Returns: The text, concatenated if necessary, surrounded by escapes to set
509 # the desired colors and reset them afterwards
510 # Throws: Text exception on invalid attributes
511 sub colored {
512 27 ▶ my ($first, @rest) = @_;
Subroutine colored: called CC=6 SCAR=17.9
Pod documented
513 27 my ($string, @codes);
514 27 ▶ if (ref($first) && ref($first) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first and ref $first eq 'ARRAY') { } 2 25 27
Condition100%3/3
ref $first and ref $first eq "ARRAY"
!ll&&!rl&&r
2322
MC/DC100%2/2
ref $first and ref $first eq "ARRAY"
ref $first ref $first eq "ARRAY"
Truth table100%3/3
A and B
A= ref $firstB= ref $first eq "ARRAY"
ABresult
0X0
100
111
515 2 @codes = @{$first};
516 2 $string = join(q{}, @rest);
517 } else {
518 25 $string = $first;
519 25 @codes = @rest;
520 }
521  
522 # Return the string unmolested if colors are disabled.
523 27 ▶ if ($ENV{ANSI_COLORS_DISABLED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"ANSI_COLORS_DISABLED"}) 1 26 27
524 1 return $string;
525 }
526  
527 # Find the attribute string for our colors.
528 26 my $attr = color(@codes);
529  
530 # If $EACHLINE is defined, split the string on line boundaries, suppress
531 # empty segments, and then colorize each of the line sections.
532 25 ▶ if (defined($EACHLINE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Term::ANSIColor::EACHLINE) { } 7 18 25
533 18 ▶ my @text = map { ($_ ne $EACHLINE) ? $attr . $_ . "\e[0m" : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_ ne $Term::ANSIColor::EACHLINE ? : 9 9 18
534 7 grep { length > 0 }
535 split(m{ (\Q$EACHLINE\E) }xms, $string);
536 7 return join(q{}, @text);
537 } else {
538 18 return $attr . $string . "\e[0m";
539 }
540 }
541  
542 # Define a new color alias, or return the value of an existing alias.
543 #
544 # $alias - The color alias to define
545 # @color - The color attributes the alias will correspond to (optional)
546 #
547 # Returns: The standard color value of the alias as a string (may be multiple
548 # attributes separated by spaces)
549 # undef if one argument was given and the alias was not recognized
550 # Throws: Text exceptions for invalid alias names, attempts to use a
551 # standard color name as an alias, or an unknown standard color name
552 sub coloralias {
553 21 ▶ my ($alias, @color) = @_;
Subroutine coloralias: called CC=8 SCAR=20.8
Pod documented
554 21 ▶ if (!@color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@color) 3 18 21
555 3 ▶ if (exists($ALIASES{$alias})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Term::ANSIColor::ALIASES{$alias}) { } 2 1 3
556 2 return join(q{ }, @{ $ALIASES{$alias} });
557 } else {
558 1 return;
559 }
560 }
561  
562 # Avoid \w here to not load Unicode character tables, which increases the
563 # memory footprint of this module considerably.
564 #
565 ## no critic (RegularExpressions::ProhibitEnumeratedClasses)
566 18 ▶ if ($alias !~ m{ \A [a-zA-Z0-9._-]+ \z }xms) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $alias =~ / \A [a-zA-Z0-9._-]+ \z /msx) { } 3 15 18
elsif ($Term::ANSIColor::ATTRIBUTES{$alias}) { } 2 13 15
567 3 croak(qq{Invalid alias name "$alias"});
568 } elsif ($ATTRIBUTES{$alias}) {
569 2 croak(qq{Cannot alias standard color "$alias"});
570 }
571 ## use critic
572  
573 # Split on whitespace and expand aliases.
574 13 @color = map { split } @color;
575 13 ▶ @color = map { defined($ALIASES{$_}) ? @{ $ALIASES{$_} } : $_ } @color;
Branch: 1 branch
BranchTrueFalseTotal
defined $Term::ANSIColor::ALIASES{$_} ? : 2 16 18
576  
577 # Check that all of the attributes are valid.
578 13 for my $attribute (@color) {
579 18 ▶ if (!exists($ATTRIBUTES{$attribute})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Term::ANSIColor::ATTRIBUTES{$attribute}) 3 15 18
580 3 croak(qq{Invalid attribute name "$attribute"});
581 }
582 }
583  
584 # Set the alias and return.
585 10 $ALIASES{$alias} = [@color];
586 10 return join(q{ }, @color);
587 }
588  
589 # Given a string, strip the ANSI color codes out of that string and return the
590 # result. This removes only ANSI color codes, not movement codes and other
591 # escape sequences.
592 #
593 # @string - The list of strings to sanitize
594 #
595 # Returns: (array) The strings stripped of ANSI color escape sequences
596 # (scalar) The same, concatenated
597 sub colorstrip {
598 5 ▶ my (@string) = @_;
Subroutine colorstrip: called CC=3 SCAR=11.0
Pod documented
599 5 for my $string (@string) {
600 11 $string =~ s{ \e\[ [\d;]* m }{}xmsg;
601 }
602 5 ▶ return wantarray ? @string : join(q{}, @string);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
603 }
604  
605 # Given a list of color attributes (arguments for color, for instance), return
606 # true if they're all valid or false if any of them are invalid.
607 #
608 # @codes - A list of color attributes, possibly space-separated
609 #
610 # Returns: True if all the attributes are valid, false otherwise.
611 sub colorvalid {
612 39 ▶ my (@codes) = @_;
Subroutine colorvalid: called CC=8 SCAR=20.8
Pod documented
613 39 @codes = map { split(q{ }, lc) } @codes;
614 39 for my $code (@codes) {
615 44 ▶ next if defined($ATTRIBUTES{$code});
Branch: 1 branch
BranchTrueFalseTotal
if defined $Term::ANSIColor::ATTRIBUTES{$code} 12 32 44
616 32 ▶ next if defined($ALIASES{$code});
Branch: 1 branch
BranchTrueFalseTotal
if defined $Term::ANSIColor::ALIASES{$code} 7 25 32
617 25 ▶ if ($code =~ m{ \A (?: on_ )? r (\d+) g (\d+) b (\d+) \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ / \A (?: on_ )? r (\d+) g (\d+) b (\d+) \z /msx) 10 15 25
618 10 ▶ next if ($1 <= 255 && $2 <= 255 && $3 <= 255);
Branch: 1 branch
BranchTrueFalseTotal
if $1 <= 255 and $2 <= 255 and $3 <= 255 6 4 10
Condition100%6/6
$1 <= 255 and $2 <= 255 and $3 <= 255
!ll&&!rl&&r
316
$1 <= 255 and $2 <= 255
!ll&&!rl&&r
217
MC/DC100%3/3
$1 <= 255 and $2 <= 255 and $3 <= 255
$1 <= 255 $2 <= 255 $3 <= 255
Truth table100%4/4
A and B and C
A= $1 <= 255B= $2 <= 255C= $3 <= 255
ABCresult
0XX0
10X0
1100
1111
619 }
620 19 return;
621 }
622 20 return 1;
623 }
624  
625 ##############################################################################
626 # Module return value and documentation
627 ##############################################################################
628  
629 # Ensure we evaluate to true.
630 9 1;
631 __END__
↑ Summary