blib/lib/Term/ANSIColor.pm

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