blib/lib/Progress/Any/Output/TermProgressBarColor.pm

filter: Statementstmt 70.0% 117 / 167 Branchbran 42.2% 38 / 90 Conditioncond 40.9% 18 / 44 MC/DCmcdc 8.0% 2 / 25 Subroutinesub 63.1% 12 / 19 Podpod 75.0% 3 / 4 totaltotal 54.4% 190 / 349 CC 63 SCAR 29.3
CC
63
Cov
57%
_handle_unknown_conversion
30.2
_template_length
27.7
__ANON__
20.0
CRAP 18.8
↑ Summary
1 package Progress::Any::Output::TermProgressBarColor;
2  
3 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 2 our $DATE = '2020-08-15'; # DATE
5 2 our $DIST = 'Progress-Any-Output-TermProgressBarColor'; # DIST
6 2 our $VERSION = '0.249'; # VERSION
7  
8 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Color::ANSI::Util qw(ansifg ansibg);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶require Win32::Console::ANSI if $^O =~ /Win/;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /Win/ 0 2 2
14  
15 2 $|++;
16  
17 2 ▶sub _patch {
Subroutine _patch: called CC=6 SCAR=29.8
18 2 my $out = shift;
19  
20 2 ▶ return if $out->{patch_handle1};
Branch: 1 branch
BranchTrueFalseTotal
if $out->{"patch_handle1"} 0 2 2
21  
22 2 require Monkey::Patch::Action;
23 2 ▶ if (defined &{"Log::Any::Adapter::Screen::hook_before_log"}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined &{'Log::Any::Adapter::Screen::hook_before_log';}) { } 0 2 2
elsif (defined &{'Log::ger::Output::Screen::hook_before_log';}) { } 0 2 2
24 $out->{patch_handle1} = Monkey::Patch::Action::patch_package(
25 'Log::Any::Adapter::Screen', 'hook_before_log', 'replace',
26 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 # we install a hook to clean up progress indicator first before
28 # we print log message to the screen.
29 0 $out->cleanup(1);
30 0 $Progress::Any::output_data{"$out"}{force_update} = 1;
31 }
32 0 );
33 2 } elsif (defined &{"Log::ger::Output::Screen::hook_before_log"}) {
34 $out->{patch_handle1} = Monkey::Patch::Action::patch_package(
35 'Log::ger::Output::Screen', 'hook_before_log', 'replace',
36 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 # we install a hook to clean up progress indicator first before
38 # we print log message to the screen.
39 0 $out->cleanup(1);
40 0 $Progress::Any::output_data{"$out"}{force_update} = 1;
41 }
42 0 );
43 }
44  
45 2 ▶ if (defined &{"Log::Any::Adapter::Screen::hook_after_log"}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined &{'Log::Any::Adapter::Screen::hook_after_log';}) { } 0 2 2
elsif (defined &{'Log::ger::Output::Screen::hook_after_log';}) { } 0 2 2
46 $out->{patch_handle2} = Monkey::Patch::Action::patch_package(
47 'Log::Any::Adapter::Screen', 'hook_after_log', 'replace',
48 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
49 0 my ($self, $msg) = @_;
50  
51 # make sure we print a newline after logging so progress bar
52 # starts at column 1
53 0 ▶ print { $self->{_fh} } "\n" unless $msg =~ /\R\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\R\z/ 0 0 0
54  
55 # reset show_delay because we have displayed something
56 0 ▶ $out->keep_delay_showing if $out->{show_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $out->{"show_delay"} 0 0 0
57  
58 # redisplay progress bar if were cleaned up
59 0 ▶ print { $self->{_fh} } $out->{_bar} if $out->{_bar};
Branch: 1 branch
BranchTrueFalseTotal
if $out->{"_bar"} 0 0 0
60 }
61 0 );
62 2 } elsif (defined &{"Log::ger::Output::Screen::hook_after_log"}) {
63 $out->{patch_handle2} = Monkey::Patch::Action::patch_package(
64 'Log::ger::Output::Screen', 'hook_after_log', 'replace',
65 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
66 0 my ($ctx, $msg) = @_;
67 # make sure we print a newline after logging so progress bar
68 # starts at column 1
69 0 ▶ print { $ctx->{_fh} } "\n" unless $msg =~ /\R\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\R\z/ 0 0 0
70  
71 # reset show_delay because we have displayed something
72 0 ▶ $out->keep_delay_showing if $out->{show_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $out->{"show_delay"} 0 0 0
73  
74 # redisplay progress bar if were cleaned up
75 0 ▶ print { $ctx->{_fh} } $out->{_bar} if $out->{_bar};
Branch: 1 branch
BranchTrueFalseTotal
if $out->{"_bar"} 0 0 0
76 }
77 0 );
78 }
79 }
80  
81 0 ▶sub _unpatch {
Subroutine _unpatch: not called CC=1 SCAR=6.9
82 0 my $self = shift;
83 0 undef $self->{patch_handle1};
84 0 undef $self->{patch_handle2};
85 }
86  
87 2 ▶sub _template_length {
Subroutine _template_length: called CC=17 SCAR=33.2
88 2 require Progress::Any; # for $template_regex
89 2 ▶ no warnings 'once'; # $Progress::Any::template_regex
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 2 my ($self, $template) = @_;
92  
93 2 my $template_length = length($template);
94  
95 2 ▶ while ($template =~ /$Progress::Any::template_regex/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template =~ /$Progress::Any::template_regex/g) 8 2 10
96 8 my ($all, $width, $dot, $prec, $conv) =
97 ($1, $2, $3, $4, $5);
98  
99 8 ▶ if (defined $template_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $template_length) 8 0 8
100  
101 8 ▶ if ($conv eq '%') {
Branch: 6 branches
BranchTrueFalseTotal
if ($conv eq '%') { } 2 6 8
elsif ($conv eq 'b' or $conv eq 'B') { } 2 4 6
elsif ($conv eq 'p') { } 2 2 4
elsif ($conv eq 'e') { } 0 2 2
elsif ($conv eq 'r') { } 0 2 2
elsif ($conv eq 'R') { } 2 0 2
102 2 ▶ $width //= 1;
Condition50%1/2
$width //= 1
l!l
02
MC/DC0%0/1
$width //= 1
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
103 ▶ } elsif ($conv eq 'b' || $conv eq 'B') {
Condition66%2/3
$conv eq "b" or $conv eq "B"
l!l&&r!l&&!r
024
MC/DC50%1/2
$conv eq "b" or $conv eq "B"
$conv eq "b" $conv eq "B"
Truth table66%2/3
A or B
A= $conv eq "b"B= $conv eq "B"
ABresult
1X1
011
000
104 2 ▶ $width //= $self->{_default_b_width};
Condition33%1/3
$width //= $self->{"_default_b_width"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$width //= $self->{"_default_b_width"}
$width $self->{"_default_b_width"}
Truth table33%1/3
A //= B
A= $widthB= $self->{"_default_b_width"}
ABresult
1X1
011
000
105 } elsif ($conv eq 'p') {
106 2 ▶ $width //= 3;
Condition50%1/2
$width //= 3
l!l
02
MC/DC0%0/1
$width //= 3
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
107 } elsif ($conv eq 'e') {
108 0 ▶ $width //= -8;
Condition0%0/2
$width //= -8
l!l
00
MC/DC0%0/1
$width //= -8
$width
Truth table0%0/2
A
A= $width
Aresult
11
00
109 } elsif ($conv eq 'r') {
110 0 ▶ $width //= -8;
Condition0%0/2
$width //= -8
l!l
00
MC/DC0%0/1
$width //= -8
$width
Truth table0%0/2
A
A= $width
Aresult
11
00
111 } elsif ($conv eq 'R') {
112 2 ▶ $width //= -(8 + 1 + 7);
Condition50%1/2
$width //= -16
l!l
02
MC/DC0%0/1
$width //= -16
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
113 }
114  
115 8 ▶ if (defined $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $width) { } 8 0 8
116 8 $template_length += abs($width) - length($all);
117 } else {
118 0 $template_length = undef;
119 }
120  
121 }
122 }
123  
124 2 $template_length;
125 }
126  
127 2 ▶sub new {
Subroutine new: called CC=13 SCAR=26.7
Pod documented
128 2 my ($class, %args0) = @_;
129  
130 2 my %args;
131  
132 2 $args{width} = delete($args0{width});
133 2 ▶ if (!defined($args{width})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"width"}) 2 0 2
134 2 my ($cols, $rows);
135 2 ▶ if ($ENV{COLUMNS}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'COLUMNS'}) { } 0 2 2
elsif (eval { do { require Term::Size; 1 } }) { } 2 0 2
136 0 $cols = $ENV{COLUMNS};
137 2 } elsif (eval { require Term::Size; 1 }) {
138 2 ($cols, $rows) = Term::Size::chars(*STDOUT{IO});
139 }
140 2 ▶ $cols //= 80;
Condition50%1/2
$cols //= 80
l!l
02
MC/DC0%0/1
$cols //= 80
$cols
Truth table50%1/2
A
A= $cols
Aresult
11
00
141 # on windows if we print at rightmost column, cursor will move to the
142 # next line, so we try to avoid that
143 2 ▶ $args{width} = $^O =~ /Win/ ? $cols-1 : $cols;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /Win/ ? : 0 2 2
144 }
145  
146 2 $args{fh} = delete($args0{fh});
147 2 ▶ $args{fh} //= \*STDERR;
Condition100%2/2
$args{"fh"} //= \*STDERR
l!l
11
MC/DC100%1/1
$args{"fh"} //= \*STDERR
$args{"fh"}
Truth table100%2/2
A
A= $args{"fh"}
Aresult
11
00
148  
149 2 $args{show_delay} = delete($args0{show_delay});
150  
151 2 $args{freq} = delete($args0{freq});
152  
153 2 $args{wide} = delete($args0{wide});
154  
155 2 $args{rownum} = delete($args0{rownum});
156 2 ▶ $args{rownum} //= 0;
Condition50%1/2
$args{"rownum"} //= 0
l!l
02
MC/DC0%0/1
$args{"rownum"} //= 0
$args{"rownum"}
Truth table50%1/2
A
A= $args{"rownum"}
Aresult
11
00
157  
158 2 ▶ $args{template} = delete($args0{template}) //
Condition50%1/2
delete $args0{"template"} // "<color ffff00>%p%%</color> <color 808000>[</color>%B<color 808000>]</color><color ffff00>%R</color>"
l!l
02
MC/DC0%0/1
delete $args0{"template"} // "<color ffff00>%p%%</color> <color 808000>[</color>%B<color 808000>]</color><color ffff00>%R</color>"
delete $args0{"template"}
Truth table50%1/2
A
A= delete $args0{"template"}
Aresult
11
00
159 '<color ffff00>%p%%</color> <color 808000>[</color>%B<color 808000>]</color><color ffff00>%R</color>';
160  
161 2 ▶ keys(%args0) and die "Unknown output parameter(s): ".
Branch: 1 branch
BranchTrueFalseTotal
keys %args0 and die 'Unknown output parameter(s): ' . join(", ", keys %args0) 0 2 2
162 join(", ", keys(%args0));
163  
164 2 $args{_last_hide_time} = time();
165  
166 2 require Text::ANSI::Util;
167 2 ▶ if ($args{wide}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"wide"}) 0 2 2
168 0 require Text::ANSI::WideUtil;
169 }
170  
171 2 my $self = bless \%args, $class;
172  
173 # determine the default width for %b and %B
174 {
175 2 $self->{_default_b_width} = 0;
176 2 (my $template = $args{template}) =~ s!<color \w+>|</color>!!g;
177 2 ▶ my $len = $self->_template_length($template) // 16;
Condition50%1/2
$self->_template_length($template) // 16
l!l
20
MC/DC0%0/1
$self->_template_length($template) // 16
$self->_template_length($template)
Truth table50%1/2
A
A= $self->_template_length($template)
Aresult
11
00
178 2 $self->{_default_b_width} = $args{width} - $len;
179 }
180  
181 # render color in template
182 2 ▶ ($self->{_template} = $self->{template}) =~ s!<color (\w+)>|<(/)color>!$1 ? ansifg($1) : "\e[0m"!eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 8 8 16
183  
184 2 $self;
185 }
186  
187 2 ▶sub _handle_unknown_conversion {
Subroutine _handle_unknown_conversion: called CC=13 SCAR=34.1
188 2 my %args = @_;
189  
190 2 my $conv = $args{conv};
191 2 ▶ return () unless $conv eq 'b' || $conv eq 'B';
Branch: 1 branch
BranchTrueFalseTotal
unless $conv eq "b" or $conv eq "B" 0 2 2
Condition33%1/3
$conv eq "b" or $conv eq "B"
l!l&&r!l&&!r
020
MC/DC0%0/2
$conv eq "b" or $conv eq "B"
$conv eq "b" $conv eq "B"
Truth table33%1/3
A or B
A= $conv eq "b"B= $conv eq "B"
ABresult
1X1
011
000
192  
193 2 my $p = $args{indicator};
194 2 my $self = $args{self};
195  
196 2 my $tottgt = $p->total_target;
197 2 my $totpos = $p->total_pos;
198  
199 2 my $bar_bar = '';
200 2 ▶ my $bwidth = abs($args{width} // $self->{_default_b_width});
Condition33%1/3
$args{"width"} // $self->{"_default_b_width"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{"width"} // $self->{"_default_b_width"}
$args{"width"} $self->{"_default_b_width"}
Truth table33%1/3
A // B
A= $args{"width"}B= $self->{"_default_b_width"}
ABresult
1X1
011
000
201  
202 2 ▶ if ($tottgt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tottgt) { } 2 0 2
203 2 my $bfilled = int($totpos / $tottgt * $bwidth);
204 2 ▶ $bfilled = $bwidth if $bfilled > $bwidth;
Branch: 1 branch
BranchTrueFalseTotal
if $bfilled > $bwidth 0 2 2
205 2 $bar_bar = ("=" x $bfilled) . (" " x ($bwidth-$bfilled));
206 } else {
207 # display 15% width of bar just moving right
208 0 my $bfilled = int(0.15 * $bwidth);
209 0 ▶ $bfilled = 1 if $bfilled < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bfilled < 1 0 0 0
210 0 $self->{_x}++;
211 0 ▶ if ($self->{_x} > $bwidth-$bfilled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_x"} > $bwidth - $bfilled) 0 0 0
212 0 $self->{_x} = 0;
213 }
214 $bar_bar = (" " x $self->{_x}) . ("=" x $bfilled) .
215 0 (" " x ($bwidth-$self->{_x}-$bfilled));
216 }
217  
218 my $msg = $args{args}{'message.alt.output.TermProgressBarColor'} //
219 2 ▶ $args{args}{message};
Condition33%1/3
$args{"args"}{"message.alt.output.TermProgressBarColor"} // $args{"args"}{"message"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{"args"}{"message.alt.output.TermProgressBarColor"} // $args{"args"}{"message"}
$args{"args"}{"message.alt.output.TermProgressBarColor"} $args{"args"}{"message"}
Truth table33%1/3
A // B
A= $args{"args"}{"message.alt.output.TermProgressBarColor"}B= $args{"args"}{"message"}
ABresult
1X1
011
000
220 2 ▶ if ($conv eq 'B' && defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conv eq "B" and defined $msg) 2 0 2
Condition33%1/3
$conv eq "B" and defined $msg
!ll&&!rl&&r
002
MC/DC0%0/2
$conv eq "B" and defined $msg
$conv eq "B" defined $msg
Truth table33%1/3
A and B
A= $conv eq "B"B= defined $msg
ABresult
0X0
100
111
221 2 ▶ if ($msg =~ m!</elspan!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ m[</elspan]) 0 2 2
222 0 require String::Elide::Parts;
223 0 $msg = String::Elide::Parts::elide($msg, $bwidth);
224 }
225 2 my $mwidth;
226 2 ▶ if ($self->{wide}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'wide'}) { } 0 2 2
227 0 $msg = Text::ANSI::WideUtil::ta_mbtrunc($msg, $bwidth);
228 0 $mwidth = Text::ANSI::WideUtil::ta_mbswidth($msg);
229 } else {
230 2 $msg = Text::ANSI::Util::ta_trunc($msg, $bwidth);
231 2 $mwidth = Text::ANSI::Util::ta_length($msg);
232 }
233 2 $bar_bar = $msg . substr($bar_bar, $mwidth);
234 }
235  
236 2 return ("%s", $bar_bar);
237 }
238  
239 2 ▶sub update {
Subroutine update: called CC=9 SCAR=27.1
Pod documented
240 2 my ($self, %args) = @_;
241  
242 2 ▶ return unless $ENV{PROGRESS_TERM_BAR} // $ENV{PROGRESS} // (-t $self->{fh});
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"PROGRESS_TERM_BAR"} // $ENV{"PROGRESS"}) // -t $self->{"fh"} 0 2 2
Condition33%2/6
($ENV{"PROGRESS_TERM_BAR"} // $ENV{"PROGRESS"}) // -t $self->{"fh"}
l!l&&r!l&&!r
200
$ENV{"PROGRESS_TERM_BAR"} // $ENV{"PROGRESS"}
l!l&&r!l&&!r
020
MC/DC0%0/3
($ENV{"PROGRESS_TERM_BAR"} // $ENV{"PROGRESS"}) // -t $self->{"fh"}
$ENV{"PROGRESS_TERM_BAR"} $ENV{"PROGRESS"} -t $self->{"fh"}
Truth table25%1/4
A // B // C
A= $ENV{"PROGRESS_TERM_BAR"}B= $ENV{"PROGRESS"}C= -t $self->{"fh"}
ABCresult
1XX1
01X1
0011
0000
243  
244 2 my $now = time();
245  
246 # if there is show_delay, don't display until we've surpassed it
247 2 ▶ if (defined $self->{show_delay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"show_delay"}) 0 2 2
248 0 ▶ return if $now - $self->{show_delay} < $self->{_last_hide_time};
Branch: 1 branch
BranchTrueFalseTotal
if $now - $self->{"show_delay"} < $self->{"_last_hide_time"} 0 0 0
249 }
250  
251 2 $self->_patch;
252  
253 2 $self->cleanup;
254  
255 2 my $p = $args{indicator};
256 2 my $is_finished = $p->{state} eq 'finished';
257 2 ▶ if ($is_finished) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_finished) 0 2 2
258 0 ▶ if ($self->{_lastlen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_lastlen"}) 0 0 0
259 0 $self->{_last_hide_time} = $now;
260 }
261 0 return;
262 }
263  
264 my $bar = $p->fill_template(
265 {
266 template => $self->{_template},
267 2 ▶ handle_unknown_conversion => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
268 2 _handle_unknown_conversion(
269 self => $self,
270 @_,
271 );
272 },
273 },
274 2 %args,
275 );
276  
277 2 my $len = Text::ANSI::Util::ta_length($bar);
278 $self->{_bar} = join(
279 "",
280 "\n" x $self->{rownum},
281 $bar,
282 ("\b" x $len),
283 2 ▶ $self->{rownum} > 0 ? "\e[$self->{rownum}A" : "", # up N lines
Branch: 1 branch
BranchTrueFalseTotal
$self->{'rownum'} > 0 ? : 0 2 2
284 );
285 2 print { $self->{fh} } $self->{_bar};
286 2 $self->{_lastlen} = $len;
287 }
288  
289 2 ▶sub cleanup {
Subroutine cleanup: called CC=4 SCAR=22.3
Pod undocumented
290 2 my ($self, $dont_reset_lastlen) = @_;
291  
292 # sometimes (e.g. when a subtask's target is undefined) we don't get
293 # state=finished at the end. but we need to cleanup anyway at the end of
294 # app, so this method is provided and will be called by e.g.
295 # Perinci::CmdLine
296  
297 2 my $ll = $self->{_lastlen};
298 2 ▶ return unless $ll;
Branch: 1 branch
BranchTrueFalseTotal
unless $ll 2 0 2
299 my $clean_str = join(
300 "",
301 "\n" x $self->{rownum},
302 " " x $ll,
303 "\b" x $ll,
304 0 ▶ $self->{rownum} > 0 ? "\e[$self->{rownum}A" : "", # up N lines
Branch: 1 branch
BranchTrueFalseTotal
$self->{'rownum'} > 0 ? : 0 0 0
305 );
306 0 print { $self->{fh} } $clean_str;
307 0 ▶ undef $self->{_lastlen} unless $dont_reset_lastlen;
Branch: 1 branch
BranchTrueFalseTotal
unless $dont_reset_lastlen 0 0 0
308 }
309  
310 0 ▶sub keep_delay_showing {
Subroutine keep_delay_showing: not called CC=1 SCAR=6.9
Pod documented
311 0 my $self = shift;
312  
313 0 $self->{_last_hide_time} = time();
314 }
315  
316 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
317 0 my $self = shift;
318 0 $self->_unpatch;
319 }
320  
321 2 1;
322 # ABSTRACT: Output progress to terminal as color bar
323  
324 __END__
↑ Summary