blib/lib/Parse/ANSIColor/Tiny.pm

filter: Statementstmt 99.3% 159 / 160 Branchbran 94.6% 53 / 56 Conditioncond 100% 13 / 13 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 22 / 22 Podpod 100% 13 / 13 totaltotal 98.5% 268 / 272 CC 37 SCAR 16.1
CC
37
Cov
98%
process_reverse
10.0
normalize
8.0
parse
6.0
CRAP 5.0
↑ Summary
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of Parse-ANSIColor-Tiny
4 #
5 # This software is copyright (c) 2011 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Parse::ANSIColor::Tiny;
14 # git description: v0.700-3-ge88b308
15  
16 15 our $AUTHORITY = 'cpan:RWSTAUNER';
17 # ABSTRACT: Determine attributes of ANSI-Colored string
18 15 $Parse::ANSIColor::Tiny::VERSION = '0.800';
19 15 our @COLORS = qw( black red green yellow blue magenta cyan white );
20 our %FOREGROUND = (
21 120 (map { ( $COLORS[$_] => 30 + $_ ) } 0 .. $#COLORS),
22 15 (map { ( 'bright_' . $COLORS[$_] => 90 + $_ ) } 0 .. $#COLORS),
23 );
24 our %BACKGROUND = (
25 120 (map { ( 'on_' . $COLORS[$_] => 40 + $_ ) } 0 .. $#COLORS),
26 15 (map { ('on_bright_' . $COLORS[$_] => 100 + $_ ) } 0 .. $#COLORS),
27 );
28  
29 # Generating the 256-color codes involves a lot of codes and offsets that are
30 # not helped by turning them into constants.
31 ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
32  
33 15 our @COLORS256;
34  
35 # The first 16 256-color codes are duplicates of the 16 ANSI colors,
36 # included for completeness.
37 15 for my $code (0 .. 15) {
38 240 my $name = "ansi$code";
39 240 $FOREGROUND{$name} = "38;5;$code";
40 240 $BACKGROUND{"on_$name"} = "48;5;$code";
41 240 push @COLORS256, $name;
42 }
43  
44 # 256-color RGB colors. Red, green, and blue can each be values 0 through 5,
45 # and the resulting 216 colors start with color 16.
46 15 for my $r (0 .. 5) {
47 90 for my $g (0 .. 5) {
48 540 for my $b (0 .. 5) {
49 3240 my $code = 16 + (6 * 6 * $r) + (6 * $g) + $b;
50 3240 my $name = "rgb$r$g$b";
51 3240 $FOREGROUND{$name} = "38;5;$code";
52 3240 $BACKGROUND{"on_$name"} = "48;5;$code";
53 3240 push @COLORS256, $name;
54 }
55 }
56 }
57  
58 # The last 256-color codes are 24 shades of grey.
59 15 for my $n (0 .. 23) {
60 360 my $code = $n + 232;
61 360 my $name = "grey$n";
62 360 $FOREGROUND{$name} = "38;5;$code";
63 360 $BACKGROUND{"on_$name"} = "48;5;$code";
64 360 push @COLORS256, $name;
65 }
66  
67 15 our %ATTRIBUTES = (
68 clear => 0,
69 reset => 0,
70 bold => 1,
71 dark => 2,
72 faint => 2,
73 underline => 4,
74 underscore => 4,
75 blink => 5,
76 reverse => 7,
77 concealed => 8,
78 reverse_off => 27,
79 reset_foreground => 39,
80 reset_background => 49,
81 %FOREGROUND,
82 %BACKGROUND,
83 );
84  
85 # copied from Term::ANSIColor
86 15 our %ATTRIBUTES_R;
87 # Reverse lookup. Alphabetically first name for a sequence is preferred.
88 15 for (reverse sort keys %ATTRIBUTES) {
89 8355 $ATTRIBUTES_R{$ATTRIBUTES{$_}} = $_;
90 }
91  
92 # True color attributes (of the form "r255g128b0") are handled by pattern
93 # rather than lookup: there are sixteen million of each, so they can't go in
94 # %ATTRIBUTES (or %FOREGROUND or %BACKGROUND) the way the other colors do.
95 1796 ▶sub __is_foreground {
Subroutine __is_foreground: called CC=2 SCAR=6.9
96 1796 my ($attr) = @_;
97 1796 ▶ return exists($FOREGROUND{ $attr }) || $attr =~ /\Ar[0-9]+g[0-9]+b[0-9]+\z/;
Condition100%3/3
exists $Parse::ANSIColor::Tiny::FOREGROUND{$attr} || $attr =~ /\Ar[0-9]+g[0-9]+b[0-9]+\z/
l!l&&r!l&&!r
759321005
MC/DC100%2/2
exists $Parse::ANSIColor::Tiny::FOREGROUND{$attr} || $attr =~ /\Ar[0-9]+g[0-9]+b[0-9]+\z/
exists $Parse::ANSIColor::Tiny::FOREGROUND{$attr} $attr =~ /\Ar[0-9]+g[0-9]+b[0-9]+\z/
Truth table100%3/3
A || B
A= exists $Parse::ANSIColor::Tiny::FOREGROUND{$attr}B= $attr =~ /\Ar[0-9]+g[0-9]+b[0-9]+\z/
ABresult
1X1
011
000
98 }
99  
100 1708 ▶sub __is_background {
Subroutine __is_background: called CC=2 SCAR=6.9
101 1708 my ($attr) = @_;
102 1708 ▶ return exists($BACKGROUND{ $attr }) || $attr =~ /\Aon_r[0-9]+g[0-9]+b[0-9]+\z/;
Condition100%3/3
exists $Parse::ANSIColor::Tiny::BACKGROUND{$attr} || $attr =~ /\Aon_r[0-9]+g[0-9]+b[0-9]+\z/
l!l&&r!l&&!r
624141070
MC/DC100%2/2
exists $Parse::ANSIColor::Tiny::BACKGROUND{$attr} || $attr =~ /\Aon_r[0-9]+g[0-9]+b[0-9]+\z/
exists $Parse::ANSIColor::Tiny::BACKGROUND{$attr} $attr =~ /\Aon_r[0-9]+g[0-9]+b[0-9]+\z/
Truth table100%3/3
A || B
A= exists $Parse::ANSIColor::Tiny::BACKGROUND{$attr}B= $attr =~ /\Aon_r[0-9]+g[0-9]+b[0-9]+\z/
ABresult
1X1
011
000
103 }
104  
105  
106 28 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
107 28 my $class = shift;
108 my $self = {
109 remove_escapes => 1,
110 0 ▶ @_ == 1 ? %{ $_[0] } : @_,
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 28 28
111 };
112  
113 $self->{process} = 1
114 28 ▶ if $self->{auto_reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_reverse"} 4 24 28
115  
116 # fix incorrectly specified attributes
117 28 ▶ ($self->{background} ||= 'black') =~ s/^(on_)*/on_/;
Condition100%2/2
$self->{"background"} ||= "black"
l!l
622
MC/DC100%1/1
$self->{"background"} ||= "black"
$self->{"background"}
Truth table100%2/2
A
A= $self->{"background"}
Aresult
11
00
118 28 ▶ ($self->{foreground} ||= 'white') =~ s/^(on_)*//;
Condition100%2/2
$self->{"foreground"} ||= "white"
l!l
523
MC/DC100%1/1
$self->{"foreground"} ||= "white"
$self->{"foreground"}
Truth table100%2/2
A
A= $self->{"foreground"}
Aresult
11
00
119  
120 28 bless $self, $class;
121 }
122  
123  
124 1 ▶sub colors {
Subroutine colors: called CC=1 SCAR=0.0
Pod documented
125 1 return (@COLORS, @COLORS256);
126 }
127 1 ▶sub foreground_colors {
Subroutine foreground_colors: called CC=1 SCAR=0.0
Pod documented
128 return (
129 @COLORS,
130 1 (map { "bright_$_" } @COLORS),
131 @COLORS256,
132 );
133 }
134 1 ▶sub background_colors {
Subroutine background_colors: called CC=1 SCAR=0.0
Pod documented
135 return (
136 8 (map { "on_$_" } @COLORS),
137 8 (map { "on_bright_$_" } @COLORS),
138 1 (map { "on_$_" } @COLORS256),
139 );
140 }
141  
142  
143 1325 ▶sub __separate_and_normalize {
Subroutine __separate_and_normalize: called CC=3 SCAR=11.0
144 1325 my ($codes) = @_;
145  
146 # Treat empty as "clear".
147 1325 ▶ defined($codes) && length($codes)
Branch: 1 branch
BranchTrueFalseTotal
defined $codes and length $codes or return 0 1316 9 1325
Condition100%3/3
defined $codes and length $codes
!ll&&!rl&&r
181316
MC/DC100%2/2
defined $codes and length $codes
defined $codes length $codes
Truth table100%3/3
A and B
A= defined $codesB= length $codes
ABresult
0X0
100
111
148 or return 0;
149  
150 # Replace empty (clear) with zero to simplify parsing and return values.
151 1316 $codes =~ s/^;/0;/;
152 1316 $codes =~ s/;$/;0/;
153 # Insert a zero between two semicolons (use look-ahead to get /g to find all).
154 1316 $codes =~ s/;(?=;)/;0/g;
155  
156 # Remove any leading zeros from (sections of) codes.
157 1316 $codes =~ s/\b0+(?=\d)//g;
158  
159 # Return all matches (of extended sequences or digits).
160 1316 return $codes =~ m{ ( [34]8;5;\d+ | [34]8;2;\d+;\d+;\d+ | \d+) }xg;
161 }
162  
163 1451 ▶sub __attribute_name {
Subroutine __attribute_name: called CC=4 SCAR=13.9
164 1451 my ($code) = @_;
165  
166 1451 ▶ if( my ($ground, @rgb) = $code =~ m{ \A ([34])8;2;(\d+);(\d+);(\d+) \z }x ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = $code =~ / \A ([34])8;2;(\d+);(\d+);(\d+) \z /x) 25 1426 1451
167 # Anything over 255 isn't a color; drop it like any other unknown code.
168 25 ▶ return if grep { $_ > 255 } @rgb;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} @rgb 2 23 25
169 23 ▶ return ($ground == 4 ? 'on_' : '') . sprintf('r%dg%db%d', @rgb);
Branch: 1 branch
BranchTrueFalseTotal
$ground == 4 ? : 8 15 23
170 }
171  
172 1426 return $ATTRIBUTES_R{ $code };
173 }
174  
175 1318 ▶sub identify {
Subroutine identify: called CC=1 SCAR=0.0
Pod documented
176 1318 my ($self, @codes) = @_;
177 1318 local $_;
178 return
179 1449 grep { defined }
180 1451 map { __attribute_name($_) }
181 1318 map { __separate_and_normalize($_) }
182 @codes;
183 }
184  
185  
186 1293 ▶sub normalize {
Subroutine normalize: called CC=8 SCAR=20.8
Pod documented
187 1293 my $self = shift;
188 1293 my @norm;
189 1293 foreach my $attr ( @_ ){
190 2182 ▶ if( $attr eq 'clear' ){
Branch: 4 branches
BranchTrueFalseTotal
if ($attr eq 'clear') { } 588 1594 2182
elsif ($attr eq 'reverse_off') { } 8 1586 1594
elsif ($attr eq 'reset_foreground') { } 8 1578 1586
elsif ($attr eq 'reset_background') { } 5 1573 1578
191 588 @norm = ();
192 }
193 elsif( $attr eq 'reverse_off' ){
194 # reverse_off cancels reverse
195 8 @norm = grep { $_ ne 'reverse' } @norm;
196 }
197 elsif( $attr eq 'reset_foreground' ){
198 8 @norm = grep { !__is_foreground($_) } @norm;
199 }
200 elsif( $attr eq 'reset_background' ){
201 5 @norm = grep { !__is_background($_) } @norm;
202 }
203 else {
204 # remove previous (duplicate) occurrences of this attribute
205 1573 @norm = grep { $_ ne $attr } @norm;
206 # new fg color overwrites previous fg
207 1573 ▶ @norm = grep { !__is_foreground($_) } @norm if __is_foreground($attr);
Branch: 1 branch
BranchTrueFalseTotal
if __is_foreground($attr) 733 840 1573
208 # new bg color overwrites previous bg
209 1573 ▶ @norm = grep { !__is_background($_) } @norm if __is_background($attr);
Branch: 1 branch
BranchTrueFalseTotal
if __is_background($attr) 617 956 1573
210 1573 push @norm, $attr;
211 }
212 }
213 1293 return @norm;
214 }
215  
216  
217 39 ▶sub parse {
Subroutine parse: called CC=6 SCAR=17.9
Pod documented
218 39 my ($self, $orig) = @_;
219  
220 39 my $last_pos = 0;
221 39 my $last_attr = [];
222 39 my $processed = [];
223 39 my $parsed = [];
224  
225 # Strip escape sequences that we aren't going to use
226 $orig = $self->remove_escape_sequences($orig)
227 39 ▶ if $self->{remove_escapes};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"remove_escapes"} 38 1 39
228  
229 39 ▶ while( $orig =~ m/(\e\[([0-9;]*)m)/mg ){
Branch: 1 branch
BranchTrueFalseTotal
if ($orig =~ /(\e\[([0-9;]*)m)/gm) 1264 39 1303
230 1264 my $seq = $1;
231 1264 my $attrs = $2;
232  
233 1264 my $cur_pos = pos($orig);
234  
235 1264 my $len = ($cur_pos - length($seq)) - $last_pos;
236 1264 ▶ push @$parsed, [
Branch: 1 branch
BranchTrueFalseTotal
if $len 1241 23 1264
237 $processed,
238 substr($orig, $last_pos, $len)
239 ]
240 # don't bother with empty strings
241 if $len;
242  
243 1264 $last_pos = $cur_pos;
244 1264 $last_attr = [$self->normalize(@$last_attr, $self->identify($attrs))];
245 1264 ▶ $processed = $self->{process} ? [$self->process(@$last_attr)] : $last_attr;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'process'} ? : 26 1238 1264
246 }
247  
248 39 ▶ push @$parsed, [
Branch: 1 branch
BranchTrueFalseTotal
if $last_pos < length $orig 27 12 39
249 $processed,
250 substr($orig, $last_pos)
251 ]
252 # if there's any string left
253 if $last_pos < length($orig);
254  
255 39 return $parsed;
256 }
257  
258  
259 28 ▶sub process {
Subroutine process: called CC=2 SCAR=6.9
Pod documented
260 28 my ($self, @attr) = @_;
261 28 ▶ @attr = $self->process_reverse(@attr) if $self->{auto_reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_reverse"} 27 1 28
262 28 return @attr;
263 }
264  
265  
266 43 ▶sub process_reverse {
Subroutine process_reverse: called CC=10 SCAR=23.0
Pod documented
267 43 my $self = shift;
268 43 my ($rev, $fg, $bg, @attr);
269 43 my $i = 0;
270 43 foreach my $attr ( @_ ){
271 109 ▶ if( $attr eq 'reverse' ){
Branch: 3 branches
BranchTrueFalseTotal
if ($attr eq 'reverse') { } 29 80 109
elsif (__is_foreground($attr)) { } 32 48 80
elsif (__is_background($attr)) { } 12 36 48
272 29 $rev = 1;
273 29 next;
274 }
275 elsif( __is_foreground($attr) ){
276 32 $fg = $i;
277 }
278 elsif( __is_background($attr) ){
279 12 $bg = $i;
280 }
281 80 push @attr, $attr;
282 80 $i++;
283 }
284 # maintain order for consistency with other methods
285 43 ▶ if( $rev ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) 29 14 43
286 # if either color is missing then the default colors should be reversed
287 {
288 29 ▶ $attr[ $fg = $i++ ] = $self->{foreground} if !defined $fg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fg 8 21 29
289 29 ▶ $attr[ $bg = $i++ ] = $self->{background} if !defined $bg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bg 19 10 29
290 }
291 29 ▶ $attr[ $fg ] = 'on_' . $attr[ $fg ] if defined $fg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fg 29 0 29
292 29 ▶ $attr[ $bg ] = substr( $attr[ $bg ], 3 ) if defined $bg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bg 29 0 29
293 }
294 43 return @attr;
295 }
296  
297  
298 38 ▶sub remove_escape_sequences {
Subroutine remove_escape_sequences: called CC=1 SCAR=0.0
Pod documented
299 38 my ($self, $string) = @_;
300  
301 # This is in no way comprehensive or accurate...
302 # it just seems like most of the sequences match this.
303 # We could certainly expand this if the need arises.
304 38 $string =~ s{
305 \e\[
306 [0-9;]*
307 [a-ln-zA-Z]
308 }{}gx;
309  
310 38 return $string;
311 }
312  
313  
314 15 our @EXPORT_OK;
315 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
316 15 my @funcs = qw(identify normalize parse);
317 15 my $suffix = '_ansicolor';
318 15 local $_;
319 eval join '', ## no critic (StringyEval)
320 15 ▶ map { "sub ${_}$suffix { __PACKAGE__->new->$_(\@_) }" }
Subroutine identify_ansicolor: called CC=1 SCAR=0.0
Subroutine normalize_ansicolor: called CC=1 SCAR=0.0
Subroutine parse_ansicolor: called CC=1 SCAR=0.0
Pod documented
Pod documented
Pod documented
321 @funcs;
322 15 @EXPORT_OK = map { $_ . $suffix } @funcs;
323 }
324  
325 5 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
326 5 my $class = shift;
327 5 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 2 5
328  
329 2 my $caller = caller;
330 15 ▶ no strict 'refs'; ## no critic (NoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
331  
332 2 foreach my $arg ( @_ ){
333 die "'$arg' is not exported by $class"
334 4 ▶ unless grep { $arg eq $_ } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$arg eq $_;} @Parse::ANSIColor::Tiny::EXPORT_OK 1 3 4
335 3 *{"${caller}::$arg"} = *{"${class}::$arg"}{CODE};
336 }
337 }
338  
339 # TODO: option for blotting out 'concealed'? s/\S/ /g
340  
341 15 1;
342  
343 # NOTE: this synopsis is tested (eval'ed) in t/synopsis.t
344  
345 __END__
↑ Summary