ProgressMonitor/Stringify/Fields/Counter.pm

filter: Statementstmt 94.8% 74 / 78 Branchbran 61.5% 16 / 26 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 86.0% 111 / 129 CC 18 SCAR 19.2
CC
18
Cov
86%
render
10.7
checkAttributeValues
9.0
new
2.0
CRAP 6.8
« ProgressMonitor/Stringify/Fields/Bar.pm ↑ Summary ProgressMonitor/AbstractStatefulMonitor.pm »
1 package ProgressMonitor::Stringify::Fields::Counter;
2
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 2 ▶use ProgressMonitor::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 require ProgressMonitor::Stringify::Fields::AbstractField if 0;
8
9 # Attributes:
10 # overflow
11 # Precomputed string to show when the field overflows
12 # unknown
13 # Precomputed string to show when the field is unknown
14 # index
15 # Keeps track of which idle delimiter should be rendered
16 # lastCount
17 # The previous count string (to trigger idle rendering)
18 #
19 use classes
20 2 ▶ extends => 'ProgressMonitor::Stringify::Fields::AbstractField',
Subroutine BEGIN: called CC=1 SCAR=0.0
21 new => 'new',
22 2 attrs_pr => ['overflow', 'unknown', 'index', 'lastLeft', 'lastRight'];
23
24 sub new
25 1 ▶{
Subroutine new: called CC=2 SCAR=6.9
26 1 my $class = shift;
27 1 my $cfg = shift;
28
29 1 my $self = $class->SUPER::_new($cfg, $CLASS);
30
31 1 $cfg = $self->_get_cfg;
32
33 1 my $digits = $cfg->get_digits;
34 1 my $delim = $cfg->get_delimiter;
35
36 # compute the width depending on what will be rendered
37 #
38 1 ▶ $self->_set_width($digits + ($cfg->get_showTotal ? length($delim) + $digits : 0));
Branch: 1 branch
BranchTrueFalseTotal
$cfg->get_showTotal ? : 1 0 1
39
40 1 $self->{$ATTR_overflow} = $cfg->get_overflowCharacter x $digits;
41 1 $self->{$ATTR_unknown} = $cfg->get_unknownCharacter x $digits;
42 1 $self->{$ATTR_index} = 0;
43 1 $self->{$ATTR_lastLeft} = '';
44 1 $self->{$ATTR_lastRight} = '';
45
46 1 return $self;
47 }
48
49 sub render
50 23 ▶{
Subroutine render: called CC=10 SCAR=23.7
Pod documented
51 23 my $self = shift;
52 23 my $state = shift;
53 23 my $ticks = shift;
54 23 my $totalTicks = shift;
55 23 my $clean = shift;
56
57 23 my $cfg = $self->_get_cfg;
58 23 my $hasOverflow = 0;
59 23 my $digits = $cfg->get_digits;
60
61 # first render the left part, but show overflow if it gets too large
62 #
63 23 my $l = sprintf("%.*u", $digits, $ticks);
64 23 ▶ if (length($l) > $digits)
Branch: 1 branch
BranchTrueFalseTotal
if (length $l > $digits) 0 23 23
65 {
66 0 $l = $self->{$ATTR_overflow};
67 0 $hasOverflow = 1;
68 }
69
70 23 my $delim = '';
71 23 my $r = '';
72
73 23 ▶ if ($cfg->get_showTotal)
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->get_showTotal) 23 0 23
74 {
75 # now render the right part, and watch for overflow
76 #
77 23 ▶ $r = defined($totalTicks) ? sprintf("%.*u", $digits, $totalTicks) : $self->{$ATTR_unknown};
Branch: 1 branch
BranchTrueFalseTotal
defined $totalTicks ? : 12 11 23
78 23 ▶ if (length($r) > $digits)
Branch: 1 branch
BranchTrueFalseTotal
if (length $r > $digits) 0 23 23
79 {
80 0 $r = $self->{$ATTR_overflow};
81 0 $hasOverflow = 1;
82 }
83
84 # unless we're requested to be clean and if there was overflow, or no
85 # change in the left/right parts), twirl the idle sequence
86 #
87 23 $delim = $cfg->get_delimiter;
88 23 ▶ if ($state != STATE_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if ($state != 3) 22 1 23
89 {
90 22 ▶ if (!$clean && ($hasOverflow || ($l eq $self->{$ATTR_lastLeft} && $r eq $self->{$ATTR_lastRight})))
Branch: 1 branch
BranchTrueFalseTotal
if (not $clean and $hasOverflow || $l eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastLeft} && $r eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastRight}) 10 12 22
Condition77%7/9
not $clean and $hasOverflow || $l eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastLeft} && $r eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastRight}
!ll&&!rl&&r
01210
$hasOverflow or $l eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastLeft} and $r eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastRight}
l!l&&r!l&&!r
01012
$l eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastLeft} and $r eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastRight}
!ll&&!rl&&r
11110
MC/DC50%2/4
not $clean and $hasOverflow || $l eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastLeft} && $r eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastRight}
not $clean $hasOverflow $l eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastLeft} $r eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastRight}
Truth table60%3/5
not(A) and B or C and D
A= not $cleanB= $hasOverflowC= $l eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastLeft}D= $r eq $self->{$ProgressMonitor::Stringify::Fields::Counter::ATTR_lastRight}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
91 {
92 10 my $seq = $cfg->get_idleDelimiterSequence;
93 10 $delim = $seq->[$self->{$ATTR_index}++ % @$seq];
94 }
95 }
96 }
97
98 23 $self->{$ATTR_lastLeft} = $l;
99 23 $self->{$ATTR_lastRight} = $r;
100
101 23 return "$l$delim$r";
102 }
103
104 ###
105
106 package ProgressMonitor::Stringify::Fields::CounterConfiguration;
107
108 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
110
111 # Attributes
112 # digits (integer)
113 # The number of digits in the counter field (and the total)
114 # delimiter (string)
115 # The delimiter between counter and total
116 # idleDelimiterSequence (array ref with strings)
117 # The sequence of states that the delimiter should show when nothing else
118 # moves (note: defining this to [<delimiter>] effectively turns off idle twirl)
119 # overflowCharacter (character)
120 # The character to use if the field overflows (e.g. digits are 2 and counter is 100)
121 # unknownCharacter (character)
122 # The character to use for the total if undef (unknown)
123 # showTotal (boolean)
124 # Whether total field should be shown (this includes delimiter, and if no
125 # total, then no delimiter, then no idle delimiter twirl)
126 #
127 use classes
128 2 ▶ extends => 'ProgressMonitor::Stringify::Fields::AbstractFieldConfiguration',
Subroutine BEGIN: called CC=1 SCAR=0.0
129 attrs => ['digits', 'delimiter', 'idleDelimiterSequence', 'overflowCharacter', 'unknownCharacter', 'showTotal'],
130 2 ;
131
132 sub defaultAttributeValues
133 1 ▶{
Subroutine defaultAttributeValues: called CC=1 SCAR=0.0
134 1 my $self = shift;
135
136 return {
137 1 %{$self->SUPER::defaultAttributeValues()},
138 digits => 5,
139 delimiter => '/',
140 idleDelimiterSequence => ['\\', '/'],
141 overflowCharacter => '#',
142 unknownCharacter => '?',
143 showTotal => 1,
144 };
145 }
146
147 sub checkAttributeValues
148 1 ▶{
Subroutine checkAttributeValues: called CC=8 SCAR=22.0
149 1 my $self = shift;
150
151 1 $self->SUPER::checkAttributeValues;
152
153 1 ▶ X::Usage->throw("digits must be > 0") if $self->get_digits < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_digits < 1 0 1 1
154 1 ▶ X::Usage->throw("overflowCharacter must have length 1") if length($self->get_overflowCharacter) != 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->get_overflowCharacter != 1 0 1 1
155 1 ▶ X::Usage->throw("unknownCharacter must have length 1") if length($self->get_unknownCharacter) != 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->get_unknownCharacter != 1 0 1 1
156 1 ▶ X::Usage->throw("delimiter must have a length") if length($self->get_delimiter) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->get_delimiter < 1 0 1 1
157 1 my $seq = $self->get_idleDelimiterSequence;
158 1 ▶ X::Usage->throw("idleDelimiterSequence must be an array") unless ref($seq) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $seq eq "ARRAY" 0 1 1
159 1 my $len = length($self->get_delimiter);
160 1 for (@$seq)
161 {
162 2 ▶ X::Usage->throw("all idleDelimiterSequence elements must have same length as delimiter") if length($_) != $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ != $len 0 2 2
163 }
164
165 1 return;
166 }
167
168 ############################
169
170 =head1 NAME
171
172 ProgressMonitor::Stringify::Field::Counter - a field implementation that renders progress
173 as a counter.
174
175 =head1 SYNOPSIS
176
177 # call someTask and give it a monitor to call us back
178 #
179 my $counter = ProgressMonitor::Stringify::Fields::Counter->new;
180 someTask(ProgressMonitor::Stringify::ToStream->new({fields => [ $counter ]});
181
182 =head1 DESCRIPTION
183
184 This is a fixed size field representing progress as a counter with or without the total
185 displayed alongside, e.g. '00512/03000' meaning 512 ticks completed out of 3000.
186
187 Inherits from ProgressMonitor::Stringify::Fields::AbstractField.
188
189 =head1 METHODS
190
191 =over 2
192
193 =item new( $hashRef )
194
195 Configuration data:
196
197 =over 2
198
199 =item digits (default => 5)
200
201 The number of digits it should use. With the default it can thus indicate up
202 to '99999'. Values above that will be printed using the overflow character.
203
204 =item delimiter (default => '/')
205
206 The delimiter string between the current and total values.
207
208 =item idleDelimiterSequence (default => ['\\', '/'])
209
210 When progress is made but with tick count not advancing, this sequence is used
211 to show that something is happening. It should be a list of strings with each string
212 having the same length as the delimiter. As idle work is done, the sequence will
213 be stepped through, but immediately revert to the regular delimiter as soon as
214 a tick is detected.
215
216 =item overflowCharacter (default => '#')
217
218 The character to be used when the value is to large to fit using the given amount
219 of digits.
220
221 =item unknownCharacter (default => '?')
222
223 The character to use when the total is unknown.
224
225 =item showTotal (default => 1)
226
227 Turns on or off the total display. With no total displayed, no delimiter displayed
228 and hence, no idleness shown either.
229
230 =back
231
232 =back
233
234 =head1 AUTHOR
235
236 Kenneth Olwing, C<< <knth at cpan.org> >>
237
238 =head1 BUGS
239
240 I wouldn't be surprised! If you can come up with a minimal test that shows the
241 problem I might be able to take a look. Even better, send me a patch.
242
243 Please report any bugs or feature requests to
244 C<bug-progressmonitor at rt.cpan.org>, or through the web interface at
245 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ProgressMonitor>.
246 I will be notified, and then you'll automatically be notified of progress on
247 your bug as I make changes.
248
249 =head1 SUPPORT
250
251 You can find general documentation for this module with the perldoc command:
252
253 perldoc ProgressMonitor
254
255 =head1 ACKNOWLEDGEMENTS
256
257 Thanks to my family. I'm deeply grateful for you!
258
259 =head1 COPYRIGHT & LICENSE
260
261 Copyright 2006,2007 Kenneth Olwing, all rights reserved.
262
263 This program is free software; you can redistribute it and/or modify it
264 under the same terms as Perl itself.
265
266 =cut
267
268 2 1; # End of ProgressMonitor::Stringify::Fields::Counter
« ProgressMonitor/Stringify/Fields/Bar.pm ↑ Summary ProgressMonitor/AbstractStatefulMonitor.pm »