File Coverage

blib/lib/ProgressMonitor/Stringify/Fields/Percentage.pm
Criterion Covered Total %
statement 54 54 100.0
branch 10 18 55.5
condition 1 3 33.3
subroutine 12 12 100.0
pod 1 1 100.0
total 78 88 88.6


line stmt bran cond sub pod time code
1             package ProgressMonitor::Stringify::Fields::Percentage;
2            
3 2     2   41515 use warnings;
  2         4  
  2         69  
4 2     2   12 use strict;
  2         5  
  2         74  
5            
6 2     2   13 use constant PERCENT => '%';
  2         4  
  2         136  
7 2     2   9 use constant DECIMAL_POINT => '.';
  2         2  
  2         130  
8            
9             require ProgressMonitor::Stringify::Fields::AbstractField if 0;
10            
11             # Attributes:
12             # unknown
13             # Precomputed string when total is undef (unknown)
14             #
15             use classes
16 2         15 extends => 'ProgressMonitor::Stringify::Fields::AbstractField',
17             new => 'new',
18 2     2   11 attrs_pr => ['unknown'];
  2         4  
19            
20             sub new
21             {
22 1     1   14 my $class = shift;
23 1         3 my $cfg = shift;
24            
25 1         14 my $self = $class->SUPER::_new($cfg, $CLASS);
26            
27 1         10 $cfg = $self->_get_cfg;
28            
29 1         4 my $dec = $cfg->get_decimals;
30 1 50       13 $self->_set_width(3 + ($dec ? 1 : 0) + $dec + 1);
31            
32 1 50       3 $self->{$ATTR_unknown} =
    50          
33             $cfg->get_unknownCharacter x 3 . ($dec ? DECIMAL_POINT : '') . ($dec ? $cfg->get_unknownCharacter x $dec : '') . PERCENT;
34            
35 1         29 return $self;
36             }
37            
38             sub render
39             {
40 31     31 1 32 my $self = shift;
41 31         36 my $state = shift;
42 31         31 my $ticks = shift;
43 31         31 my $totalTicks = shift;
44 31         30 my $clean = shift;
45            
46 31 100       83 return $self->{$ATTR_unknown} unless defined($totalTicks);
47            
48 20         48 my $cfg = $self->_get_cfg;
49 20         54 my $dec = $cfg->get_decimals;
50 20 50 33     132 my $pct = defined($totalTicks) && $totalTicks > 0 ? ($ticks / $totalTicks) * 100 : 0;
51 20 50       177 my $rendition = $dec ? sprintf("%*.*f%s", 4 + $dec, $dec, $pct, PERCENT) : sprintf("%3u%s", int($pct), PERCENT);
52 20         46 my $fc = $cfg->get_fillCharacter;
53 20         107 $rendition =~ s/ /$fc/g;
54            
55 20         61 return $rendition;
56             }
57            
58             ###
59            
60             package ProgressMonitor::Stringify::Fields::PercentageConfiguration;
61            
62 2     2   1430 use strict;
  2         4  
  2         74  
63 2     2   19 use warnings;
  2         5  
  2         115  
64            
65             # Attributes
66             # decimals
67             # The number of decimals on the percentage
68             # unknownCharacter
69             # The character to use when the total is unknown
70             # fillCharacter
71             # The character to use instead of a space (i.e. ' 50%' vs '100%')
72             #
73             use classes
74 2         13 extends => 'ProgressMonitor::Stringify::Fields::AbstractFieldConfiguration',
75             attrs => ['decimals', 'unknownCharacter', 'fillCharacter'],
76 2     2   10 ;
  2         4  
77            
78             sub defaultAttributeValues
79             {
80 1     1   6 my $self = shift;
81            
82             return {
83 1         3 %{$self->SUPER::defaultAttributeValues()},
  1         9  
84             decimals => 2,
85             unknownCharacter => '?',
86             fillCharacter => ' ',
87             };
88             }
89            
90             sub checkAttributeValues
91             {
92 1     1   2 my $self = shift;
93            
94 1         10 $self->SUPER::checkAttributeValues;
95            
96 1 50       4 X::Usage->throw("decimals can not be negative") if $self->get_decimals < 0;
97 1 50       9 X::Usage->throw("unknownCharacter must have length 1") if length($self->get_unknownCharacter) != 1;
98 1 50       8 X::Usage->throw("fillCharacter must have length 1") if length($self->get_fillCharacter) != 1;
99            
100 1         18 return;
101             }
102            
103             ############################
104            
105             =head1 NAME
106            
107             ProgressMonitor::Stringify::Field::Percentage - a field implementation that
108             renders progress as a percentage.
109            
110             =head1 SYNOPSIS
111            
112             # call someTask and give it a monitor to call us back
113             #
114             my $pct = ProgressMonitor::Stringify::Fields::Percentage->new;
115             someTask(ProgressMonitor::Stringify::ToStream->new({fields => [ $pct ]});
116            
117             =head1 DESCRIPTION
118            
119             This is a fixed size field representing progress as a percentage, e.g. ' 52.34 %'.
120            
121             Inherits from ProgressMonitor::Stringify::Fields::AbstractField.
122            
123             =head1 METHODS
124            
125             =over 2
126            
127             =item new( $hashRef )
128            
129             Configuration data:
130            
131             =over 2
132            
133             =item decimals (default => 2)
134            
135             The number of decimals on the percentage.
136            
137             =item unknownCharacter (default => '?')
138            
139             The character to use when the total is unknown.
140            
141             =item fillCharacter (default => ' ')
142            
143             The character to use for the space reserved for the 10 & 100 location when they are still 0.
144            
145             =back
146            
147             =back
148            
149             =head1 AUTHOR
150            
151             Kenneth Olwing, C<< >>
152            
153             =head1 BUGS
154            
155             I wouldn't be surprised! If you can come up with a minimal test that shows the
156             problem I might be able to take a look. Even better, send me a patch.
157            
158             Please report any bugs or feature requests to
159             C, or through the web interface at
160             L.
161             I will be notified, and then you'll automatically be notified of progress on
162             your bug as I make changes.
163            
164             =head1 SUPPORT
165            
166             You can find general documentation for this module with the perldoc command:
167            
168             perldoc ProgressMonitor
169            
170             =head1 ACKNOWLEDGEMENTS
171            
172             Thanks to my family. I'm deeply grateful for you!
173            
174             =head1 COPYRIGHT & LICENSE
175            
176             Copyright 2006,2007 Kenneth Olwing, all rights reserved.
177            
178             This program is free software; you can redistribute it and/or modify it
179             under the same terms as Perl itself.
180            
181             =cut
182            
183             1; # End of ProgressMonitor::Stringify::Fields::Percentage