Concise.pm

filter: Statementstmt 86.0% 204 / 237 Branchbran 71.8% 92 / 128 Conditioncond 60.8% 42 / 69 MC/DCmcdc 30.5% 11 / 36 Subroutinesub 93.9% 31 / 33 Podpod 41.1% 7 / 17 totaltotal 74.4% 387 / 520 CC 80 SCAR 26.4
CC
80
Cov
78%
map_to_256
30.0
ansi_numbers
29.4
apply_color
24.5
CRAP 14.0
« Concise/Color.pm ↑ Summary Concise/ColorUtils.pm »
1 # -*- indent-tabs-mode: nil -*-
2  
3 package Term::ANSIColor::Concise;
4  
5 5 our $VERSION = "3.05";
6  
7 5 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 our @EXPORT = qw();
13 5 our @EXPORT_OK = qw(
14 ansi_color ansi_color_24 ansi_code ansi_pair csi_code csi_report
15 cached_ansi_color
16 map_256_to_6 map_to_256
17 );
18 5 our %EXPORT_TAGS = (all => [ @EXPORT_OK ]);
19  
20 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 $Data::Dumper::Sortkeys = 1;
23 5 ▶use List::Util qw(min max first);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use Term::ANSIColor::Concise::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Term::ANSIColor::Concise::Transform qw(transform $mod_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 ▶use aliased;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶my $Color = alias 'Term::ANSIColor::Concise::' . ($ENV{TAC_COLOR_PACKAGE} || 'ColorObject');
Condition50%1/2
$ENV{'TAC_COLOR_PACKAGE'} || 'ColorObject'
l!l
05
MC/DC0%0/1
$ENV{'TAC_COLOR_PACKAGE'} || 'ColorObject'
$ENV{'TAC_COLOR_PACKAGE'}
Truth table50%1/2
A
A= $ENV{'TAC_COLOR_PACKAGE'}
Aresult
11
00
30 80 ▶sub Color { $Color }
Subroutine Color: called CC=1 SCAR=0.0
Pod undocumented
31  
32 5 ▶our $NO_NO_COLOR //= $ENV{ANSICOLOR_NO_NO_COLOR};
Condition33%1/3
our $NO_NO_COLOR //= $ENV{"ANSICOLOR_NO_NO_COLOR"}
l!l&&r!l&&!r
005
MC/DC0%0/2
our $NO_NO_COLOR //= $ENV{"ANSICOLOR_NO_NO_COLOR"}
our $NO_NO_COLOR $ENV{"ANSICOLOR_NO_NO_COLOR"}
Truth table33%1/3
A //= B
A= our $NO_NO_COLORB= $ENV{"ANSICOLOR_NO_NO_COLOR"}
ABresult
1X1
011
000
33 5 ▶our $NO_COLOR //= !$NO_NO_COLOR && defined $ENV{NO_COLOR};
Condition60%3/5
our $NO_COLOR //= !$Term::ANSIColor::Concise::NO_NO_COLOR && defined $ENV{"NO_COLOR"}
l!l
05
!$Term::ANSIColor::Concise::NO_NO_COLOR && defined $ENV{'NO_COLOR'}
!ll&&!rl&&r
041
MC/DC0%0/1
our $NO_COLOR //= !$Term::ANSIColor::Concise::NO_NO_COLOR && defined $ENV{"NO_COLOR"}
our $NO_COLOR
Truth table50%1/2
A
A= our $NO_COLOR
Aresult
11
00
34 5 ▶our $RGB24 //= $ENV{ANSICOLOR_RGB24} // ($ENV{COLORTERM}//'' eq 'truecolor');
Condition50%3/6
our $RGB24 //=
l!l
05
$ENV{"ANSICOLOR_RGB24"} //
l!l
05
$ENV{"COLORTERM"} //
l!l
05
MC/DC0%0/1
our $RGB24 //=
our $RGB24
Truth table50%1/2
A
A= our $RGB24
Aresult
11
00
35 5 ▶our $LINEAR_256 //= $ENV{ANSICOLOR_LINEAR_256};
Condition33%1/3
our $LINEAR_256 //= $ENV{"ANSICOLOR_LINEAR_256"}
l!l&&r!l&&!r
005
MC/DC0%0/2
our $LINEAR_256 //= $ENV{"ANSICOLOR_LINEAR_256"}
our $LINEAR_256 $ENV{"ANSICOLOR_LINEAR_256"}
Truth table33%1/3
A //= B
A= our $LINEAR_256B= $ENV{"ANSICOLOR_LINEAR_256"}
ABresult
1X1
011
000
36 5 ▶our $LINEAR_GRAY //= $ENV{ANSICOLOR_LINEAR_GRAY};
Condition33%1/3
our $LINEAR_GRAY //= $ENV{"ANSICOLOR_LINEAR_GRAY"}
l!l&&r!l&&!r
005
MC/DC0%0/2
our $LINEAR_GRAY //= $ENV{"ANSICOLOR_LINEAR_GRAY"}
our $LINEAR_GRAY $ENV{"ANSICOLOR_LINEAR_GRAY"}
Truth table33%1/3
A //= B
A= our $LINEAR_GRAYB= $ENV{"ANSICOLOR_LINEAR_GRAY"}
ABresult
1X1
011
000
37 5 ▶our $NO_RESET_EL //= $ENV{ANSICOLOR_NO_RESET_EL};
Condition33%1/3
our $NO_RESET_EL //= $ENV{"ANSICOLOR_NO_RESET_EL"}
l!l&&r!l&&!r
005
MC/DC0%0/2
our $NO_RESET_EL //= $ENV{"ANSICOLOR_NO_RESET_EL"}
our $NO_RESET_EL $ENV{"ANSICOLOR_NO_RESET_EL"}
Truth table33%1/3
A //= B
A= our $NO_RESET_ELB= $ENV{"ANSICOLOR_NO_RESET_EL"}
ABresult
1X1
011
000
38 5 ▶our $SPLIT_ANSI //= $ENV{ANSICOLOR_SPLIT_ANSI};
Condition33%1/3
our $SPLIT_ANSI //= $ENV{"ANSICOLOR_SPLIT_ANSI"}
l!l&&r!l&&!r
005
MC/DC0%0/2
our $SPLIT_ANSI //= $ENV{"ANSICOLOR_SPLIT_ANSI"}
our $SPLIT_ANSI $ENV{"ANSICOLOR_SPLIT_ANSI"}
Truth table33%1/3
A //= B
A= our $SPLIT_ANSIB= $ENV{"ANSICOLOR_SPLIT_ANSI"}
ABresult
1X1
011
000
39 5 ▶our $NO_CUMULATIVE //= $ENV{ANSICOLOR_NO_CUMULATIVE};
Condition33%1/3
our $NO_CUMULATIVE //= $ENV{"ANSICOLOR_NO_CUMULATIVE"}
l!l&&r!l&&!r
005
MC/DC0%0/2
our $NO_CUMULATIVE //= $ENV{"ANSICOLOR_NO_CUMULATIVE"}
our $NO_CUMULATIVE $ENV{"ANSICOLOR_NO_CUMULATIVE"}
Truth table33%1/3
A //= B
A= our $NO_CUMULATIVEB= $ENV{"ANSICOLOR_NO_CUMULATIVE"}
ABresult
1X1
011
000
40  
41 5 my @nonlinear = do {
42 5 map { ( $_->[0] ) x $_->[1] } (
43 [ 0, 75 ], # 0 .. 74
44 [ 1, 40 ], # 75 .. 114
45 [ 2, 40 ], # 115 .. 154
46 [ 3, 40 ], # 155 .. 194
47 [ 4, 40 ], # 195 .. 234
48 [ 5, 21 ], # 235 .. 255
49 );
50 };
51  
52 306 ▶sub map_256_to_6 {
Subroutine map_256_to_6: called CC=2 SCAR=6.9
Pod undocumented
53 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 306 my $i = shift;
55 306 ▶ if ($LINEAR_256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Term::ANSIColor::Concise::LINEAR_256) { } 12 294 306
56 12 5 * $i / 255;
57 } else {
58 # ( $i - 35 ) / 40;
59 294 $nonlinear[$i];
60 }
61 }
62  
63 0 ▶sub map_to_256 {
Subroutine map_to_256: not called CC=5 SCAR=34.0
Pod undocumented
64 0 my($base, $i) = @_;
65 0 ▶ if ($i == 0) { 0 }
Branch: 4 branches
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
elsif ($base == 6) { } 0 0 0
elsif ($base == 12) { } 0 0 0
elsif ($base == 24) { } 0 0 0
66 0 elsif ($base == 6) { $i * 40 + 55 }
67 0 elsif ($base == 12) { $i * 20 + 35 }
68 0 elsif ($base == 24) { $i * 10 + 25 }
69 0 else { die }
70 }
71  
72 14 ▶sub ansi256_number {
Subroutine ansi256_number: called CC=7 SCAR=19.6
Pod undocumented
73 14 my $code = shift;
74 14 my($r, $g, $b, $gray);
75 14 ▶ if ($code =~ /^([0-5])([0-5])([0-5])$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ /^([0-5])([0-5])([0-5])$/u) { } 9 5 14
elsif (my($n) = $code =~ /^L(\d+)/iu) { } 5 0 5
76 9 ($r, $g, $b) = ($1, $2, $3);
77 }
78 elsif (my($n) = $code =~ /^L(\d+)/i) {
79 5 ▶ $n > 25 and croak "Color spec error: $code.";
Branch: 1 branch
BranchTrueFalseTotal
$n > 25 and croak("Color spec error: $code.") 0 5 5
80 5 ▶ if ($n == 0 or $n == 25) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0 or $n == 25) { } 2 3 5
Condition100%3/3
$n == 0 or $n == 25
l!l&&r!l&&!r
113
MC/DC100%2/2
$n == 0 or $n == 25
$n == 0 $n == 25
Truth table100%3/3
A or B
A= $n == 0B= $n == 25
ABresult
1X1
011
000
81 2 $r = $g = $b = $n / 5;
82 } else {
83 3 $gray = $n - 1;
84 }
85 }
86 else {
87 0 croak "Color spec error: $code.";
88 }
89 14 ▶ defined $gray ? ($gray + 232) : ($r*36 + $g*6 + $b + 16);
Branch: 1 branch
BranchTrueFalseTotal
defined $gray ? : 3 11 14
90 }
91  
92 104 ▶sub rgb24_number {
Subroutine rgb24_number: called CC=8 SCAR=21.6
Pod undocumented
93 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 104 my($rx, $gx, $bx) = @_;
95 104 my($r, $g, $b, $gray);
96 104 ▶ if ($rx != 0 and $rx != 255 and $rx == $gx and $rx == $bx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx != 0 and $rx != 255 and $rx == $gx and $rx == $bx) { } 2 102 104
Condition100%9/9
$rx != 0 and $rx != 255 and $rx == $gx and $rx == $bx
!ll&&!rl&&r
10112
$rx != 0 and $rx != 255 and $rx == $gx
!ll&&!rl&&r
64373
$rx != 0 and $rx != 255
!ll&&!rl&&r
164840
MC/DC100%4/4
$rx != 0 and $rx != 255 and $rx == $gx and $rx == $bx
$rx != 0 $rx != 255 $rx == $gx $rx == $bx
Truth table100%5/5
A and B and C and D
A= $rx != 0B= $rx != 255C= $rx == $gxD= $rx == $bx
ABCDresult
0XXX0
10XX0
110X0
11100
11111
97 2 ▶ if ($LINEAR_GRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Term::ANSIColor::Concise::LINEAR_GRAY) { } 0 2 2
98 ##
99 ## Divide area into 25 segments, and map to BLACK and 24 GRAYS
100 ##
101 0 $gray = $rx * 25 / 255 - 1;
102 0 ▶ if ($gray < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gray < 0) 0 0 0
103 0 $r = $g = $b = 0;
104 0 $gray = undef;
105 }
106 } else {
107 ## map to 8, 18, 28, ... 238
108 2 $gray = min(23, ($rx - 3) / 10);
109 }
110 } else {
111 102 ($r, $g, $b) = map { map_256_to_6 $_ } $rx, $gx, $bx;
112 }
113 104 ▶ defined $gray ? ($gray + 232) : ($r*36 + $g*6 + $b + 16);
Branch: 1 branch
BranchTrueFalseTotal
defined $gray ? : 2 102 104
114 }
115  
116 25 ▶sub rgb24 {
Subroutine rgb24: called CC=3 SCAR=11.3
Pod undocumented
117 25 my $rgb = shift;
118 25 $rgb =~ s/^#//;
119 25 my $len = length $rgb;
120 25 ▶ croak "$rgb: Invalid RGB value." if $len == 0 || $len % 3;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 or $len % 3 0 25 25
Condition33%1/3
$len == 0 or $len % 3
l!l&&r!l&&!r
0025
MC/DC0%0/2
$len == 0 or $len % 3
$len == 0 $len % 3
Truth table33%1/3
A or B
A= $len == 0B= $len % 3
ABresult
1X1
011
000
121 25 $len /= 3;
122 25 my $max = (2 ** ($len * 4)) - 1;
123 25 map { hex($_) * 255 / $max } $rgb =~ /[0-9a-z]{$len}/gi;
124 }
125  
126 121 ▶sub rgbseq {
Subroutine rgbseq: called CC=3 SCAR=11.0
Pod undocumented
127 121 my($mod, @rgb) = @_;
128 121 ▶ if ($mod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) 31 90 121
129 31 @rgb = transform($mod, @rgb);
130 }
131 120 ▶ if ($RGB24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Term::ANSIColor::Concise::RGB24) { } 16 104 120
132 16 return (2, @rgb);
133 } else {
134 104 return (5, rgb24_number @rgb);
135 }
136 }
137  
138 5 my %numbers = (
139 ';' => undef, # ; : NOP
140 N => undef, # N : None (NOP)
141 E => 'EL', # E : Erase Line
142 Z => 0, # Z : Zero (Reset)
143 D => 1, # D : Double Strike (Bold)
144 P => 2, # P : Pale (Dark)
145 I => 3, # I : Italic
146 U => 4, # U : Underline
147 F => 5, # F : Flash (Blink: Slow)
148 Q => 6, # Q : Quick (Blink: Rapid)
149 S => 7, # S : Stand out (Reverse)
150 H => 8, # H : Hide (Concealed)
151 X => 9, # X : Cross out
152 K => 30, k => 90, # K : Kuro (Black)
153 R => 31, r => 91, # R : Red
154 G => 32, g => 92, # G : Green
155 Y => 33, y => 93, # Y : Yellow
156 B => 34, b => 94, # B : Blue
157 M => 35, m => 95, # M : Magenta
158 C => 36, c => 96, # C : Cyan
159 W => 37, w => 97, # W : White
160 T => 39, t => 39, # T : Terminal (default color)
161 );
162  
163 5 my $colorspec_re = qr{
164 (?<spec>
165 (?<toggle> /) # /
166 | (?<reset> \^) # ^
167 # Fullcolor with modifier
168 | ((?<fullcolor>
169 (?<hex> [0-9a-f]{6} ## RGB 24bit hex
170 | \#([0-9a-f]{3})+ ) ## RGB generic hex
171 | (?<dec>(rgb)? (?&TRIPLET) ) ## RGB 24bit decimal (0-255, 0-255, 0-255)
172 | (?<hsl> hsl (?&TRIPLET) ) ## HSL (0-360, 0-100, 0-100)
173 | (?<lch> lch (?&TRIPLET) ) ## LCHab (0-100, 0~130, 0-360)
174 | (?<lab> lab (?&TRIPLET) ) ## Lab (0-100, -128-127, -128-127)
175 | < (?<name> \w+ ) > ## <colorname>
176 )
177 (?<mod> $mod_re* ) ## color modifiers
178 )
179 # Basic 256/16 colors
180 | (?<c256> [0-5][0-5][0-5] # 216 (6x6x6) colors
181 | L([01][0-9]|[2][0-5]) ) # 24 gray levels + B/W
182 | (?<c16> [KRGYBMCWT] ) # 16 colors, and the default
183 # Effects and controls
184 | (?<efct> ~[DPIUFQSHX] # ~effect
185 | [DPIUFQSHX]:[\d:]* # effect:style
186 | [;NZDPIUFQSHX] ) # effect
187 | (?<csi> \{ (?<csi_name>[A-Z]+) # other CSI
188 (?<P> \( )? # optional (
189 (?<csi_param>[\d,:;]*) # 0;1;2
190 (?(<P>) \) ) # closing )
191 \}
192 | (?<csi_abbr>[E]) ) # abbreviation
193 )
194 (?(DEFINE)
195 (?<DIGIT> -? \d+ (\.\d+)? )
196 (?<TRIPLET> \( (?&DIGIT), (?&DIGIT), (?&DIGIT) \) )
197 )
198 }xi;
199  
200 180 ▶sub ansi_numbers {
Subroutine ansi_numbers: called CC=29 SCAR=33.8
Pod undocumented
201 180 ▶ local $_ = shift // '';
Condition50%1/2
shift() // ""
l!l
1800
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
202 180 my @numbers;
203 180 my $toggle = ToggleValue->new(value => 10);
204 180 my %F;
205 180 ▶ my $rgb_numbers = sub { 38 + $toggle->value, rgbseq($F{mod}, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
206  
207 180 ▶ while (m{\G (?: $colorspec_re | (?<err> .+ ) ) }xig) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G (?: $colorspec_re | (?<err> .+ ) ) /giux) 270 172 442
208 270 %F = %+;
209 270 ▶ if ($+{toggle}) {
Branch: 13 branches
BranchTrueFalseTotal
if ($+{'toggle'}) { } 23 247 270
elsif ($+{'reset'}) { } 0 247 247
elsif ($+{'hex'}) { } 25 222 247
elsif (my $dec = $+{'dec'}) { } 6 216 222
elsif (my $hsl = $+{'hsl'}) { } 21 195 216
elsif (my $lch = $+{'lch'}) { } 7 188 195
elsif (my $lab = $+{'lab'}) { } 12 176 188
elsif ($+{'name'}) { } 51 125 176
elsif ($+{'c256'}) { } 14 111 125
elsif ($+{'c16'}) { } 36 75 111
elsif ($+{'efct'}) { } 56 19 75
elsif (my $csi = $+{'csi'}) { } 15 4 19
elsif (my $err = $+{'err'}) { } 4 0 4
210 23 $toggle->toggle;
211 }
212 elsif ($+{reset}) {
213 0 $toggle->reset;
214 }
215 elsif ($+{hex}) {
216 25 my @rgb = rgb24($+{hex});
217 25 push @numbers, $rgb_numbers->(@rgb);
218 }
219 elsif (my $dec = $+{dec}) {
220 6 my @rgb = $dec =~ /\d+/g;
221 6 ▶ croak "Unexpected value: $dec." if grep { $_ > 255 } @rgb;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} @rgb 0 6 6
222 6 push @numbers, $rgb_numbers->(@rgb);
223 }
224 elsif (my $hsl = $+{hsl}) {
225 21 ▶ Color->can('hsl') or die "HSL format is not supported.\n";
Branch: 1 branch
BranchTrueFalseTotal
Color()->can("hsl") or die "HSL format is not supported.\n" 21 0 21
226 21 my @hsl = $hsl =~ /\d+/g;
227 21 my @rgb = Color->hsl(@hsl)->rgb;
228 21 push @numbers, $rgb_numbers->(@rgb);
229 }
230 elsif (my $lch = $+{lch}) {
231 7 ▶ Color->can('lch') or die "LCHab format is not supported.\n";
Branch: 1 branch
BranchTrueFalseTotal
Color()->can("lch") or die "LCHab format is not supported.\n" 7 0 7
232 7 my($L, $C, $H) = $lch =~ /-?\d+/g;
233 7 my @rgb = Color->lch($L, $C, $H)->rgb;
234 7 push @numbers, $rgb_numbers->(@rgb);
235 }
236 elsif (my $lab = $+{lab}) {
237 12 ▶ Color->can('lab') or die "Lab format is not supported.\n";
Branch: 1 branch
BranchTrueFalseTotal
Color()->can("lab") or die "Lab format is not supported.\n" 12 0 12
238 12 my($L, $a, $b) = $lab =~ /-?\d+/g;
239 12 my @rgb = Color->lab($L, $a, $b)->rgb;
240 12 push @numbers, $rgb_numbers->(@rgb);
241 }
242 elsif ($+{name}) {
243 51 require Graphics::ColorNames;
244 51 state $colornames = Graphics::ColorNames->new;
245 51 ▶ if (my @rgb = $colornames->rgb($+{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rgb) = $colornames->rgb($+{'name'})) { } 50 1 51
246 50 push @numbers, $rgb_numbers->(@rgb);
247 } else {
248 1 croak "Unknown color name: $+{name}.";
249 }
250 }
251 elsif ($+{c256}) {
252 14 push @numbers, 38 + $toggle->value, 5, ansi256_number $+{c256};
253 }
254 elsif ($+{c16}) {
255 36 push @numbers, $numbers{$+{c16}} + $toggle->value;
256 }
257 elsif ($+{efct}) {
258 56 my $efct = uc $+{efct};
259 56 ▶ my $offset = $efct =~ s/^~// ? 20 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$efct =~ s/^~//u ? : 10 46 56
260 56 ▶ my $style = $efct =~ s/(:[\d:]*)$// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$efct =~ s/(:[\d:]*)$//u ? : 12 44 56
261 56 ▶ if (defined (my $n = $numbers{$efct})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = $numbers{$efct})) 52 4 56
262 52 push @numbers, ($n + $offset) . $style;
263 }
264 }
265 elsif (my $csi = $+{csi}) {
266 15 push @numbers, do {
267 15 ▶ if ($+{csi_abbr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'csi_abbr'}) { } 3 12 15
268 3 [ $numbers{uc $+{csi_abbr}} ];
269 } else {
270 12 my($name, $param) = (uc $+{csi_name}, $+{csi_param});
271 12 ▶ croak "Color spec error: \"$csi\" in \"$_\"."
Branch: 1 branch
BranchTrueFalseTotal
if $name ne "SGR" and $param =~ /:/u 2 10 12
Condition100%3/3
$name ne "SGR" and $param =~ /:/u
!ll&&!rl&&r
732
MC/DC100%2/2
$name ne "SGR" and $param =~ /:/u
$name ne "SGR" $param =~ /:/u
Truth table100%3/3
A and B
A= $name ne "SGR"B= $param =~ /:/u
ABresult
0X0
100
111
272 if $name ne q(SGR) and $param =~ /:/;
273 10 [ $name, $param =~ /[\d:]+/g ];
274 }
275 };
276 }
277 elsif (my $err = $+{err}) {
278 4 croak "Color spec error: \"$err\" in \"$_\"."
279 }
280 else {
281 0 croak "$_: Something strange.";
282 }
283 } continue {
284 262 ▶ if ($SPLIT_ANSI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Term::ANSIColor::Concise::SPLIT_ANSI) 3 259 262
285 3 ▶ my $index = first { not ref $numbers[$_] } keys @numbers;
Subroutine __ANON__: called CC=1 SCAR=0.0
286 3 ▶ if (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) 2 1 3
287 2 my @sgr = splice @numbers, $index;
288 2 push @numbers, [ 'SGR', @sgr ];
289 }
290 }
291 }
292 172 @numbers;
293 }
294  
295 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
296 5 CSI => "\e[", # Control Sequence Introducer
297 RESET => "\e[m", # SGR Reset
298 EL => "\e[K", # Erase Line
299 5 };
300  
301 5 my %csi_terminator = (
302 ICH => '@', # Insert Character
303 CUU => 'A', # Cursor up
304 CUD => 'B', # Cursor Down
305 CUF => 'C', # Cursor Forward
306 CUB => 'D', # Cursor Back
307 CNL => 'E', # Cursor Next Line
308 CPL => 'F', # Cursor Previous line
309 CHA => 'G', # Cursor Horizontal Absolute
310 CUP => 'H', # Cursor Position
311 ED => 'J', # Erase in Display (0 after, 1 before, 2 entire, 3 w/buffer)
312 EL => 'K', # Erase in Line (0 after, 1 before, 2 entire)
313 IL => 'L', # Insert Line
314 DL => 'M', # Delete Line
315 DCH => 'P', # Delete Character
316 SU => 'S', # Scroll Up
317 SD => 'T', # Scroll Down
318 ECH => 'X', # Erase Character
319 VPA => 'd', # Vertical Position Absolute
320 VPR => 'e', # Vertical Position Relative
321 HVP => 'f', # Horizontal Vertical Position
322 SGR => 'm', # Select Graphic Rendition
323 DSR => 'n', # Device Status Report (6 cursor position)
324 SCP => 's', # Save Cursor Position
325 RCP => 'u', # Restore Cursor Position
326  
327 # Non-standard
328 CPR => 'R', # Cursor Position Report – VT100 to Host
329 STBM => 'r', # Set Top and Bottom Margins
330 SLRM => 's', # Set Left Right Margins
331 );
332  
333 5 my %other_sequence = (
334 CSI => "\e[", # Control Sequence Introducer
335 OSC => "\e]", # Operating System Command
336 RIS => "\ec", # Reset to Initial State
337 DECSC => "\e7", # DEC Save Cursor
338 DECRC => "\e8", # DEC Restore Cursor
339 DECEC => "\e[?25h", # DEC Enable Cursor
340 DECDC => "\e[?25l", # DEC Disable Cursor
341 DECELRM => "\e[?69h", # DEC Enable Left Right Margin Mode
342 DECDLRM => "\e[?69l", # DEC Disable Left Right Margin Mode
343 );
344  
345 179 ▶sub csi_code {
Subroutine csi_code: called CC=6 SCAR=19.6
Pod documented
346 179 my $name = shift;
347 179 ▶ if (my $seq = $other_sequence{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq = $other_sequence{$name}) 0 179 179
348 0 return $seq;
349 }
350 179 ▶ my $c = $csi_terminator{$name} or die "$name: Unknown ANSI name.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $c = $csi_terminator{$name} or die "${name}: Unknown ANSI name.\n" 179 0 179
351 179 ▶ if ($name eq 'SGR' and @_ == 1 and $_[0] =~ /^0+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "SGR" and @_ == 1 and $_[0] =~ /^0+$/u) 0 179 179
Condition83%5/6
$name eq "SGR" and @_ == 1 and $_[0] =~ /^0+$/u
!ll&&!rl&&r
151280
$name eq "SGR" and @_ == 1
!ll&&!rl&&r
614528
MC/DC0%0/3
$name eq "SGR" and @_ == 1 and $_[0] =~ /^0+$/u
$name eq "SGR" @_ == 1 $_[0] =~ /^0+$/u
Truth table75%3/4
A and B and C
A= $name eq "SGR"B= @_ == 1C= $_[0] =~ /^0+$/u
ABCresult
0XX0
10X0
1100
1111
352 0 @_ = ();
353 }
354 179 CSI . join(';', @_) . $c;
355 }
356  
357 0 ▶sub csi_report {
Subroutine csi_report: not called CC=2 SCAR=17.9
Pod documented
358 0 my($name, $n, $report) = @_;
359 0 ▶ my $c = $csi_terminator{$name} or die "$name: Unknown ANSI name.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $c = $csi_terminator{$name} or die "${name}: Unknown ANSI name.\n" 0 0 0
360 0 my $format = quotemeta(CSI) . join(';', ('(\d+)') x $n) . $c;
361 0 $report =~ /$format/;
362 }
363  
364 180 ▶sub ansi_code {
Subroutine ansi_code: called CC=5 SCAR=16.1
Pod documented
365 180 my $spec = shift;
366 180 my @numbers = ansi_numbers $spec;
367 172 my @code;
368 172 ▶ while (@numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@numbers) 179 172 351
369 179 my $item = shift @numbers;
370 179 ▶ if (ref($item) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 15 164 179
371 15 push @code, csi_code @$item;
372 } else {
373 164 my @sgr = ($item);
374 164 ▶ while (@numbers and not ref $numbers[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@numbers and not ref $numbers[0]) 353 164 517
Condition100%3/3
@numbers and not ref $numbers[0]
!ll&&!rl&&r
1586353
MC/DC100%2/2
@numbers and not ref $numbers[0]
@numbers not ref $numbers[0]
Truth table100%3/3
A and not(B)
A= @numbersB= not ref $numbers[0]
ABresult
0X0
100
111
375 353 push @sgr, shift @numbers;
376 }
377 164 push @code, csi_code 'SGR', @sgr;
378 }
379 }
380 172 join '', @code;
381 }
382  
383 16 ▶sub ansi_pair {
Subroutine ansi_pair: called CC=6 SCAR=18.5
Pod documented
384 16 my $spec = shift;
385 16 my $el = 0;
386 16 ▶ my $start = ansi_code $spec // '';
Condition50%1/2
$spec // ""
l!l
160
MC/DC0%0/1
$spec // ""
$spec
Truth table50%1/2
A
A= $spec
Aresult
11
00
387 16 ▶ my $end = $start eq '' ? '' : do {
Branch: 1 branch
BranchTrueFalseTotal
$start eq '' ? : 2 14 16
388 14 ▶ if ($start =~ /(.*)(\e\[[0;]*K)(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start =~ /(.*)(\e\[[0;]*K)(.*)/u) { } 2 12 14
389 2 $el = 1;
390 2 ▶ if ($3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($3) { } 0 2 2
391 0 $1 . EL . RESET;
392 } else {
393 2 EL . RESET;
394 }
395 } else {
396 12 ▶ if ($NO_RESET_EL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Term::ANSIColor::Concise::NO_RESET_EL) { } 0 12 12
397 0 RESET;
398 } else {
399 12 RESET . EL;
400 }
401 }
402 };
403 16 ($start, $end, $el);
404 }
405  
406 44 ▶sub ansi_color {
Subroutine ansi_color: called CC=1 SCAR=0.0
Pod documented
407 44 cached_ansi_color(state $cache = {}, @_);
408 }
409  
410 2 ▶sub ansi_color_24 {
Subroutine ansi_color_24: called CC=1 SCAR=0.0
Pod documented
411 2 local $RGB24 = 1;
412 2 cached_ansi_color(state $cache = {}, @_);
413 }
414  
415 46 ▶sub cached_ansi_color {
Subroutine cached_ansi_color: called CC=6 SCAR=18.2
Pod documented
416 46 my $cache = shift;
417 46 my @result;
418 46 ▶ while (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 46 46 92
419 46 my($spec, $text) = splice @_, 0, 2;
420 46 ▶ for my $color (ref $spec eq 'ARRAY' ? @$spec : $spec) {
Branch: 1 branch
BranchTrueFalseTotal
ref $spec eq 'ARRAY' ? : 0 46 46
421 46 $text = apply_color($cache, $color, $text);
422 }
423 46 push @result, $text;
424 }
425 46 ▶ croak "Wrong number of parameters." if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 46 46
426 46 ▶ wantarray ? @result : join('', @result);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 46 46
427 }
428  
429 5 ▶sub IsEOL {
Subroutine IsEOL: called CC=1 SCAR=0.0
Pod undocumented
430 5 <<"END";
431 0000\t0000
432 000A\t000D
433 2028\t2029
434 END
435 }
436  
437 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
438  
439 46 ▶sub apply_color {
Subroutine apply_color: called CC=10 SCAR=32.0
Pod undocumented
440 46 (my($cache, $color), local($_)) = @_;
441 46 ▶ if (ref $color eq 'CODE') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $color eq 'CODE') { } 0 46 46
elsif (builtin::blessed($color) and $color->can('call')) { } 0 46 46
elsif ($Term::ANSIColor::Concise::NO_COLOR) { } 18 28 46
elsif ($Term::ANSIColor::Concise::NO_CUMULATIVE) { } 0 28 28
442 0 return $color->($_);
443 }
444 ▶ elsif (blessed $color and $color->can('call')) {
Condition33%1/3
builtin::blessed($color) and $color->can("call")
!ll&&!rl&&r
4600
MC/DC0%0/2
builtin::blessed($color) and $color->can("call")
builtin::blessed($color) $color->can("call")
Truth table33%1/3
A and B
A= builtin::blessed($color)B= $color->can("call")
ABresult
0X0
100
111
445 0 return $color->call;
446 }
447 elsif ($NO_COLOR) {
448 18 return $_;
449 }
450 elsif ($NO_CUMULATIVE) { # old behavior
451 0 ▶ my($s, $e, $el) = @{ $cache->{$color} //= [ ansi_pair($color) ] };
Condition0%0/2
$cache->{$color} //= [ansi_pair($color)]
l!l
00
MC/DC0%0/1
$cache->{$color} //= [ansi_pair($color)]
$cache->{$color}
Truth table0%0/2
A
A= $cache->{$color}
Aresult
11
00
452 0 state $reset = qr{ \e\[[0;]*m (?: \e\[[0;]*[Km] )* }x;
453 0 ▶ if ($el) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el) { } 0 0 0
454 0 s/(\A|(?<=\p{IsEOL})|$reset)\K(?<x>[^\e\p{IsEOL}]+|(?<!\n))/${s}$+{x}${e}/g;
455 } else {
456 0 s/(\A|(?<=\p{IsEOL})|$reset)\K(?<x>[^\e\p{IsEOL}]+)/${s}$+{x}${e}/g;
457 }
458 0 return $_;
459 }
460 else {
461 28 ▶ my($s, $e, $el) = @{ $cache->{$color} //= [ ansi_pair($color) ] };
Condition100%2/2
$cache->{$color} //= [ansi_pair($color)]
l!l
1414
MC/DC100%1/1
$cache->{$color} //= [ansi_pair($color)]
$cache->{$color}
Truth table100%2/2
A
A= $cache->{$color}
Aresult
11
00
462 28 state $reset = qr{ \e\[[0;]*m (?: \e\[[0;]*[Km] )* }x;
463 28 ▶ if ($el) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el) { } 0 28 28
464 0 s/(?:\A|(?:\p{IsEOL}(?!\z)|$reset++))\K/${s}/g;
465 0 s/(\p{IsEOL}|(?<!\p{IsEOL})\z)/${e}${1}/g;
466 } else {
467 28 s/(?:\A|\p{IsEOL}|$reset++)(?=.)\K/${s}/g;
468 28 s/(?<!\e\[[Km])(\p{IsEOL}|(?<=\P{IsEOL})\z)/${e}${1}/g;
469 }
470 28 return $_;
471 }
472 }
473  
474 5 1;
475  
476 __END__
« Concise/Color.pm ↑ Summary Concise/ColorUtils.pm »