Colorist/Colorizer.pm

filter: Statementstmt 87.7% 179 / 204 Branchbran 63.8% 46 / 72 Conditioncond 71.4% 15 / 21 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 87.1% 34 / 39 Podpod 100% 17 / 17 totaltotal 81.3% 296 / 364 CC 48 SCAR 18.7
CC
48
Cov
81%
readline
14.9
colorize
13.2
eval_color
11.6
CRAP 6.5
« Colorist.pm ↑ Summary Colorist/Ruleset.pm »
1 package App::Colorist::Colorizer;
2 1 $App::Colorist::Colorizer::VERSION = '0.150460';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: the brain behind App::Colorist
14  
15  
16 1 has configuration => (
17 is => 'ro',
18 isa => 'Str',
19 required => 1,
20 );
21  
22  
23 1 has ruleset => (
24 is => 'ro',
25 isa => 'Str',
26 required => 1,
27 default => 'rules',
28 );
29  
30  
31 1 has colorset => (
32 is => 'ro',
33 isa => 'Str',
34 required => 1,
35 default => 'colors',
36 );
37  
38  
39 has include => (
40 is => 'ro',
41 isa => 'ArrayRef',
42 traits => [ 'Array' ],
43 required => 1,
44 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 1 handles => {
46 'include_paths' => 'elements',
47 },
48 );
49  
50  
51 1 has debug => (
52 is => 'ro',
53 isa => 'Bool',
54 required => 1,
55 default => 0,
56 );
57  
58  
59 1 has inputs => (
60 is => 'ro',
61 isa => 'ArrayRef',
62 lazy_build => 1,
63 traits => [ 'Array' ],
64 handles => {
65 all_inputs => 'elements',
66 },
67 );
68  
69 0 ▶sub _build_inputs { [ \*ARGV ] }
Subroutine _build_inputs: not called CC=1 SCAR=6.9
70  
71  
72 1 has selected_inputs => (
73 is => 'ro',
74 isa => 'IO::Select',
75 lazy_build => 1,
76 );
77  
78 1 ▶sub _build_selected_inputs {
Subroutine _build_selected_inputs: called CC=1 SCAR=0.0
79 1 my $self = shift;
80 1 my $s = IO::Select->new;
81 1 $s->add($self->all_inputs);
82 1 return $s;
83 }
84  
85  
86 1 has input_buffers => (
87 is => 'ro',
88 isa => 'HashRef',
89 lazy_build => 1,
90 traits => [ 'Hash' ],
91 handles => {
92 input_buffer_keys => 'keys',
93 get_input_buffer => 'get',
94 set_input_buffer => 'set',
95 },
96 );
97  
98 1 ▶sub _build_input_buffers { +{} }
Subroutine _build_input_buffers: called CC=1 SCAR=0.0
99  
100  
101 1 has output => (
102 is => 'ro',
103 lazy_build => 1,
104 );
105  
106 0 ▶sub _build_output { \*STDOUT }
Subroutine _build_output: not called CC=1 SCAR=6.9
107  
108  
109 1 has search_path => (
110 is => 'ro',
111 isa => 'ArrayRef',
112 lazy_build => 1,
113 traits => [ 'Array' ],
114 handles => {
115 all_search_paths => 'elements',
116 first_path_that => 'first',
117 },
118 );
119  
120 1 ▶sub _build_search_path {
Subroutine _build_search_path: called CC=2 SCAR=8.1
121 1 my $self = shift;
122  
123 return [
124 $self->include_paths,
125 0 ▶ (grep { $_ } split /:/, ($ENV{COLORIST_CONFIG}||'')),
Condition50%1/2
$ENV{'COLORIST_CONFIG'} || ''
l!l
01
MC/DC0%0/1
$ENV{'COLORIST_CONFIG'} || ''
$ENV{'COLORIST_CONFIG'}
Truth table50%1/2
A
A= $ENV{'COLORIST_CONFIG'}
Aresult
11
00
126 "$ENV{HOME}/.colorist",
127 '/etc/colorist',
128 ];
129 }
130  
131  
132 1 has ruleset_file => (
133 is => 'ro',
134 isa => 'Str',
135 lazy_build => 1,
136 );
137  
138 1 ▶sub _build_ruleset_file {
Subroutine _build_ruleset_file: called CC=2 SCAR=7.3
139 1 my $self = shift;
140  
141 1 my $config = $self->configuration;
142 1 my $ruleset = $self->ruleset;
143  
144 1 ▶ my $path = $self->first_path_that(sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
145 1 ▶ return 0 unless -d "$_/$config";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$_/$config" 0 1 1
146 1 ▶ return 1 if -f "$_/$config/$ruleset.pl";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$_/$config/$ruleset.pl" 1 0 1
147 0 return 0;
148 1 });
149  
150 1 ▶ croak(qq[Unable to locate rules "$ruleset" in paths: ], join(' ', $self->all_search_paths))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 1 1
151 unless defined $path;
152  
153 1 return "$path/$config/$ruleset.pl";
154 }
155  
156  
157 1 has colorset_file => (
158 is => 'ro',
159 isa => 'Str',
160 lazy_build => 1,
161 );
162  
163 1 ▶sub _build_colorset_file {
Subroutine _build_colorset_file: called CC=2 SCAR=7.3
164 1 my $self = shift;
165  
166 1 my $config = $self->configuration;
167 1 my $colorset = $self->colorset;
168  
169 1 ▶ my $path = $self->first_path_that(sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
170 1 ▶ return 0 unless -d "$_/$config";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$_/$config" 0 1 1
171 1 ▶ return 1 if -f "$_/$config/$colorset.yml";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$_/$config/$colorset.yml" 1 0 1
172 0 return 0;
173 1 });
174  
175 1 ▶ croak(qq[Unable to locate colors "$colorset" in paths: ], join(' ', $self->all_search_paths))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 1 1
176 unless defined $path;
177  
178 1 return "$path/$config/$colorset.yml";
179 }
180  
181  
182 1 has colors_mtime => (
183 is => 'rw',
184 isa => 'Int',
185 default => 0,
186 );
187  
188  
189 has colors => (
190 is => 'rw',
191 isa => 'HashRef',
192 1 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
193 1 my $self = shift;
194 1 $self->colors_mtime( (stat $self->colorset_file)[9] )
195 },
196 1 );
197  
198  
199 1 has rules_mtime => (
200 is => 'rw',
201 isa => 'Int',
202 default => 0,
203 );
204  
205  
206 has rules => (
207 is => 'rw',
208 isa => 'ArrayRef',
209 traits => [ 'Array' ],
210 1 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
211 1 my $self = shift;
212 1 $self->rules_mtime( (stat $self->ruleset_file)[9] )
213 },
214 1 handles => {
215 rule_pairs => [ 'natatime', 2 ],
216 },
217 );
218  
219  
220 1 ▶sub load_colorset_file {
Subroutine load_colorset_file: called CC=1 SCAR=0.0
Pod documented
221 1 my $self = shift;
222 1 return YAML::LoadFile($self->colorset_file);
223 }
224  
225  
226 15 ▶sub refresh_colorset_file {
Subroutine refresh_colorset_file: called CC=2 SCAR=6.9
Pod documented
227 15 my $self = shift;
228 15 ▶ if ( (stat $self->colorset_file)[9] > $self->colors_mtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $self->colorset_file)[9] > $self->colors_mtime) 1 14 15
229 1 $self->colors( $self->load_colorset_file );
230 }
231 }
232  
233  
234 1 ▶sub load_ruleset_file {
Subroutine load_ruleset_file: called CC=2 SCAR=6.9
Pod documented
235 1 my $self = shift;
236  
237 1 my $ruleset_file = $self->ruleset_file;
238 1 my $rules;
239  
240 {
241 1 package
242 ruleset;
243 1 ▶ use App::Colorist::Ruleset;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 1 ▶ $rules = do "$ruleset_file"
Branch: 1 branch
BranchTrueFalseTotal
$rules = do "$ruleset_file" or Carp::croak(qq[Failed to read rule set "$ruleset_file": $@]) 1 0 1
245 or Carp::croak(qq[Failed to read rule set "$ruleset_file": $@]);
246 1 push @$rules, qr{.*}, [ 'DEFAULT' ];
247 }
248  
249 1 return $rules;
250 }
251  
252  
253 15 ▶sub refresh_ruleset_file {
Subroutine refresh_ruleset_file: called CC=2 SCAR=6.9
Pod documented
254 15 my $self = shift;
255 15 ▶ if ( (stat $self->ruleset_file)[9] > $self->rules_mtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $self->ruleset_file)[9] > $self->rules_mtime) 1 14 15
256 1 $self->rules( $self->load_ruleset_file );
257 }
258 }
259  
260 Readonly my %color_names => (
261 black => 0, gray => 8,
262 maroon => 1, red => 9,
263 green => 2, lime => 10,
264 olive => 3, yellow => 11,
265 navy => 4, blue => 12,
266 purple => 5, fuschia => 13,
267 teal => 6, aqua => 14,
268 silver => 7, white => 15,
269 1 map { ($_ => $_) } (0 .. 255),
270 );
271  
272  
273 15 ▶sub print_reset_line {
Subroutine print_reset_line: called CC=2 SCAR=7.2
Pod documented
274 15 my $self = shift;
275 15 my $fh = $self->output;
276  
277 15 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 15 0 15
278 15 $fh->print("{reset}");
279 15 return;
280 }
281  
282 0 $fh->print("\e[0m");
283 }
284  
285  
286 190 ▶sub get_fg {
Subroutine get_fg: called CC=3 SCAR=12.0
Pod documented
287 190 my ($self, $fg) = @_;
288  
289 190 ▶ return '' unless defined $fg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fg 0 190 190
290 190 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) { } 190 0 190
291 190 return "{$fg}";
292 }
293 else {
294 0 return sprintf "\e[38;5;%03dm", $fg;
295 }
296 }
297  
298  
299 190 ▶sub get_bg {
Subroutine get_bg: called CC=3 SCAR=17.3
Pod documented
300 190 my ($self, $bg) = @_;
301  
302 190 ▶ return '' unless defined $bg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bg 190 0 190
303  
304 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
305 0 return "{$bg}";
306 }
307 else {
308 0 return sprintf "\e[48;5;%03dm", $bg;
309 }
310 }
311  
312  
313 0 ▶sub gray {
Subroutine gray: not called CC=1 SCAR=6.9
Pod documented
314 0 my ($self, $offset) = @_;
315 0 return 232 + $offset;
316 }
317  
318  
319 0 ▶sub rgb {
Subroutine rgb: not called CC=1 SCAR=6.9
Pod documented
320 0 my ($self, $r, $g, $b) = @_;
321 0 return 16 + $r*36 + $g*6 + $b;
322 }
323  
324  
325 380 ▶sub eval_color {
Subroutine eval_color: called CC=5 SCAR=24.5
Pod documented
326 380 my ($self, $c) = @_;
327  
328 return !defined($c) ? undef
329 : !ref($c) ? $color_names{$c}
330 0 ▶ : @{$c} == 1 ? gray(@{$c})
Branch: 1 branch
BranchTrueFalseTotal
@{$c;} == 3 ? : 0 0 0
331 0 ▶ : @{$c} == 3 ? rgb(@{$c})
Branch: 3 branches
BranchTrueFalseTotal
!defined($c) ? : 190 190 380
!ref($c) ? : 190 0 190
@{$c;} == 1 ? : 0 0 0
332 : croak("unknown color type");
333 }
334  
335  
336 190 ▶sub fg {
Subroutine fg: called CC=1 SCAR=0.0
Pod documented
337 190 my ($self, $c) = @_;
338 190 $self->get_fg($self->eval_color($c));
339 }
340  
341  
342 190 ▶sub bg($) {
Subroutine bg: called CC=1 SCAR=0.0
Pod documented
343 190 my ($self, $c) = @_;
344 190 $self->get_bg($self->eval_color($c));
345 }
346  
347  
348 190 ▶sub c {
Subroutine c: called CC=3 SCAR=11.8
Pod documented
349 190 my ($self, $n) = @_;
350  
351 190 my $c = $self->colors->{$n};
352 190 ▶ return unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 190 190
353  
354 190 my ($fg, $bg);
355 190 ▶ if (ref $c eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH') { } 0 190 190
356 0 $fg = $c->{fg};
357 0 $bg = $c->{bg};
358 }
359 else {
360 190 $fg = $c;
361 }
362  
363 190 return $self->fg($fg).$self->bg($bg);
364 }
365  
366  
367 1 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
368 1 my $self = shift;
369  
370 1 $self->loop_and_colorize;
371 }
372  
373  
374 15 ▶sub _split {
Subroutine _split: called CC=1 SCAR=0.0
375 15 my ($line) = @_;
376 15 return split /^/, $line, 2;
377 }
378  
379 16 ▶sub readline {
Subroutine readline: called CC=14 SCAR=27.0
Pod documented
380 16 my ($self) = @_;
381  
382 16 my $s = $self->selected_inputs;
383  
384 # Empty pending buffers first
385 16 for my $key ($self->input_buffer_keys) {
386 15 my $buffer = $self->get_input_buffer($key);
387  
388 15 ▶ if (defined $buffer && $buffer =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer and $buffer =~ /\n/) 13 2 15
Condition100%3/3
defined $buffer and $buffer =~ /\n/
!ll&&!rl&&r
1113
MC/DC100%2/2
defined $buffer and $buffer =~ /\n/
defined $buffer $buffer =~ /\n/
Truth table100%3/3
A and B
A= defined $bufferB= $buffer =~ /\n/
ABresult
0X0
100
111
389 13 my ($first_line, $rest) = _split($buffer);
390 13 $self->set_input_buffer($key, $rest);
391 13 return $first_line;
392 }
393 }
394  
395 # We will keep trying this until we get a full line
396 3 while (1) {
397  
398 # Quit if we've run out of handles
399 4 ▶ return unless $s->count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->count > 0 1 3 4
400  
401 # Otherwise, block until we have something to read
402 3 my @ready = $s->can_read;
403 3 for my $fh (@ready) {
404 3 $fh->blocking(0);
405  
406 # Start with the existing buffer
407 3 my $line = $self->get_input_buffer(refaddr($fh));
408 3 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 1 3
409  
410 # Read it until we run out of input or until we hit at least one newline
411 3 my ($eof, $buffer);
412 3 ▶ READ: while (!defined $eof || ($eof != 0 && $line !~ /\n/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $eof or $eof != 0 and not $line =~ /\n/) 3 3 6
Condition66%4/6
not defined $eof or $eof != 0 and not $line =~ /\n/
l!l&&r!l&&!r
303
$eof != 0 and not $line =~ /\n/
!ll&&!rl&&r
120
MC/DC33%1/3
not defined $eof or $eof != 0 and not $line =~ /\n/
not defined $eof $eof != 0 not $line =~ /\n/
Truth table75%3/4
not(A) or B and not(C)
A= not defined $eofB= $eof != 0C= not $line =~ /\n/
ABCresult
1XX1
0111
00X0
0100
413 3 $eof = sysread($fh, $buffer, 1024);
414 3 ▶ if (not defined $eof) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $eof) 0 3 3
415 0 ▶ if ($! == POSIX::EAGAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 11) { } 0 0 0
416 0 select undef, undef, undef, 0.1;
417 0 next READ;
418 }
419 else {
420 0 croak("Error while reading handle: $!");
421 }
422 }
423 3 $line .= $buffer;
424 }
425  
426 3 ▶ $s->remove($fh) if $eof == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $eof == 0 1 2 3
427  
428 # If we got a newline, return the first line and buffer the rest
429 3 ▶ if ($line =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\n/) { } 2 1 3
430 2 my ($first_line, $rest) = _split($line);
431 2 $self->set_input_buffer(refaddr($fh), $rest);
432 2 return $first_line;
433 }
434  
435 # Otherwise, we got nothing, buffer all of it and keep going
436 else {
437 1 $self->set_input_buffer(refaddr($fh), $line);
438 }
439  
440 # Guess we will try the next ready file handle
441 }
442  
443 # Guess we'll go around again and wait for ready buffers again
444 }
445 }
446  
447  
448 1 ▶sub loop_and_colorize {
Subroutine loop_and_colorize: called CC=2 SCAR=6.9
Pod documented
449 1 my $self = shift;
450  
451 1 ▶ while (my $line = $self->readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->readline) 15 1 16
452 15 $self->refresh_ruleset_file;
453 15 $self->refresh_colorset_file;
454  
455 15 $self->colorize($line);
456 }
457 }
458  
459  
460 15 ▶sub colorize {
Subroutine colorize: called CC=13 SCAR=25.8
Pod documented
461 15 my ($self, $line) = @_;
462 15 local $_ = $line;
463  
464 15 my $fh = $self->output;
465  
466 15 my $iter = $self->rule_pairs;
467 15 ▶ RULE: while (my ($rule, $names) = $iter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 66 0 66
468  
469 66 ▶ if (/^$rule$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$rule$/) 15 51 66
470  
471 # This sort is a little complex, so here's the explanation:
472 #
473 # We want to keep the parenthetical nesting in the correct order.
474 # This is easy when the parenthesis is separated by index. This is
475 # not easy otherwise. Here are some sample cases to explain:
476 #
477 # a(b)c - we can sort just by string position
478 # a(b(c - DITTO
479 # a)b)c - DITTO
480 # a)b(c - DITTO
481 #
482 # Hard cases:
483 #
484 # 11 <--- indexes in @- and @+
485 # a()b - sorting by group index order, ascending works
486 # XY <--- starting parenthesis = X, ending parenthesis = Y
487 #
488 # 12 <--- indexes in @- and @+
489 # a((b - we need to sort by group index order, ascending
490 # XX <--- starting parenthesis = X, ending parenthesis = Y
491 #
492 # * 21 <--- indexes in @- and @+
493 # * a))b - we need to sort by group index order, descending
494 # * YY <--- starting parenthesis = X, ending parenthesis = Y
495 #
496 # 12 <--- indexes in @- and @+
497 # a)(b - we need to sort by group index order, ascending
498 # YX <--- starting parenthesis = X, ending parenthesis = Y
499  
500 my @pos = sort {
501 923 ▶ $a->[0] <=> $b->[0] # match index first
Branch: 1 branch
BranchTrueFalseTotal
$App::Colorist::Colorizer::b->[1] eq 'Y' ? : 11 0 11
Condition83%5/6
$App::Colorist::Colorizer::a->[0] <=> $App::Colorist::Colorizer::b->[0] || $App::Colorist::Colorizer::a->[1] eq 'Y' && ($App::Colorist::Colorizer::b->[1] eq 'Y' ? $App::Colorist::Colorizer::b->[2] <=> $App::Colorist::Colorizer::a->[2] : $App::Colorist::Colorizer::a->[2] <=> $App::Colorist::Colorizer::b->[2])
l!l&&r!l&&!r
9001112
$App::Colorist::Colorizer::a->[1] eq "Y" and $App::Colorist::Colorizer::b->[1] eq "Y" ? $App::Colorist::Colorizer::b->[2] <=> $App::Colorist::Colorizer::a->[2] : $App::Colorist::Colorizer::a->[2] <=> $App::Colorist::Colorizer::b->[2]
!ll&&!rl&&r
12011
MC/DC66%2/3
$App::Colorist::Colorizer::a->[0] <=> $App::Colorist::Colorizer::b->[0] || $App::Colorist::Colorizer::a->[1] eq 'Y' && ($App::Colorist::Colorizer::b->[1] eq 'Y' ? $App::Colorist::Colorizer::b->[2] <=> $App::Colorist::Colorizer::a->[2] : $App::Colorist::Colorizer::a->[2] <=> $App::Colorist::Colorizer::b->[2])
$App::Colorist::Colorizer::a->[0] <=> $App::Colorist::Colorizer::b->[0] $App::Colorist::Colorizer::a->[1] eq "Y" $App::Colorist::Colorizer::b->[1] eq "Y" ? $App::Colorist::Colorizer::b->[2] <=> $App::Colorist::Colorizer::a->[2] : $App::Colorist::Colorizer::a->[2] <=> $App::Colorist::Colorizer::b->[2]
Truth table75%3/4
A || B and C
A= $App::Colorist::Colorizer::a->[0] <=> $App::Colorist::Colorizer::b->[0]B= $App::Colorist::Colorizer::a->[1] eq "Y"C= $App::Colorist::Colorizer::b->[1] eq "Y" ? $App::Colorist::Colorizer::b->[2] <=> $App::Colorist::Colorizer::a->[2] : $App::Colorist::Colorizer::a->[2] <=> $App::Colorist::Colorizer::b->[2]
ABCresult
1XX1
0111
00X0
0100
502 || ($a->[1] eq 'Y' and $b->[1] eq 'Y' ? $b->[2] <=> $a->[2] # X? name index (asc)
503 : $a->[2] <=> $b->[2]) # Y? XY? YX? index (desc)
504 } (
505 160 ▶ (map { [ ($-[$_] // 0), 'X', $_ ] } 0 .. $#- ),
Condition50%1/2
$-[$_] // 0
l!l
1600
MC/DC0%0/1
$-[$_] // 0
$-[$_]
Truth table50%1/2
A
A= $-[$_]
Aresult
11
00
506 15 ▶ (map { [ ($+[$_] // 0), 'Y', $_ ] } 0 .. $#+ ),
Condition50%1/2
$+[$_] // 0
l!l
1600
MC/DC0%0/1
$+[$_] // 0
$+[$_]
Truth table50%1/2
A
A= $+[$_]
Aresult
11
00
507 );
508 15 @pos = ([ 0, 'X', undef ], @pos, [ length, 'Y', undef ]);
509 #warn YAML::Dump(\@pos);
510  
511 15 my $offset = 0;
512 15 my @stack;
513 15 for my $pos (@pos) {
514 350 my ($i, $d, $n) = @$pos;
515  
516 350 my $color;
517 350 ▶ if ($d eq 'X') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq 'X') { } 175 175 350
518 175 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) { } 160 15 175
519 160 $color = $self->c($names->[$n]);
520 }
521 else {
522 15 $color = $self->c('DEFAULT');
523 }
524  
525 175 push @stack, $color;
526 }
527 else {
528 175 pop @stack;
529 175 ▶ if (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) { } 160 15 175
530 160 $color = $stack[-1];
531 }
532 else {
533 15 $color = $self->c('DEFAULT');
534 }
535 }
536  
537 350 ▶ if (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $color) 350 0 350
538 350 substr($_, $i + $offset, 0) = $color;
539 350 $offset += length $color;
540 }
541 }
542  
543 15 last RULE;
544 }
545 }
546  
547 15 $fh->print($_);
548 15 $self->print_reset_line;
549 }
550  
551 1 __PACKAGE__->meta->make_immutable;
552  
553 __END__
« Colorist.pm ↑ Summary Colorist/Ruleset.pm »