| 1 | package ProgressMonitor::Stringify::Fields::Percentage; | ||||||||||||||||||||||||||||
| 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 constant PERCENT => '%'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use constant DECIMAL_POINT => '.'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | 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 | ▶ | extends => 'ProgressMonitor::Stringify::Fields::AbstractField', | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | new => 'new', | ||||||||||||||||||||||||||||
| 18 | 2 | attrs_pr => ['unknown']; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub new | ||||||||||||||||||||||||||||
| 21 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 22 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 1 | my $cfg = shift; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my $self = $class->SUPER::_new($cfg, $CLASS); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | $cfg = $self->_get_cfg; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | my $dec = $cfg->get_decimals; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | $self->_set_width(3 + ($dec ? 1 : 0) + $dec + 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | $self->{$ATTR_unknown} = | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 33 | $cfg->get_unknownCharacter x 3 . ($dec ? DECIMAL_POINT : '') . ($dec ? $cfg->get_unknownCharacter x $dec : '') . PERCENT; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | return $self; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub render | ||||||||||||||||||||||||||||
| 39 | 31 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 31 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | 31 | my $state = shift; | |||||||||||||||||||||||||||
| 42 | 31 | my $ticks = shift; | |||||||||||||||||||||||||||
| 43 | 31 | my $totalTicks = shift; | |||||||||||||||||||||||||||
| 44 | 31 | my $clean = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 31 | ▶ | return $self->{$ATTR_unknown} unless defined($totalTicks); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 20 | my $cfg = $self->_get_cfg; | |||||||||||||||||||||||||||
| 49 | 20 | my $dec = $cfg->get_decimals; | |||||||||||||||||||||||||||
| 50 | 20 | ▶ | my $pct = defined($totalTicks) && $totalTicks > 0 ? ($ticks / $totalTicks) * 100 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $totalTicks and $totalTicks > 0
MC/DC0%0/2
defined $totalTicks and $totalTicks > 0
defined $totalTicks
$totalTicks > 0
Truth table33%1/3
A and B
A= defined $totalTicksB= $totalTicks > 0
| |||||||||||||||||||||||||||||
| 51 | 20 | ▶ | my $rendition = $dec ? sprintf("%*.*f%s", 4 + $dec, $dec, $pct, PERCENT) : sprintf("%3u%s", int($pct), PERCENT); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 20 | my $fc = $cfg->get_fillCharacter; | |||||||||||||||||||||||||||
| 53 | 20 | $rendition =~ s/ /$fc/g; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 20 | return $rendition; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | ### | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | package ProgressMonitor::Stringify::Fields::PercentageConfiguration; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | ▶ | extends => 'ProgressMonitor::Stringify::Fields::AbstractFieldConfiguration', | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | attrs => ['decimals', 'unknownCharacter', 'fillCharacter'], | ||||||||||||||||||||||||||||
| 76 | 2 | ; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub defaultAttributeValues | ||||||||||||||||||||||||||||
| 79 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
defaultAttributeValues: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | return { | ||||||||||||||||||||||||||||
| 83 | 1 | %{$self->SUPER::defaultAttributeValues()}, | |||||||||||||||||||||||||||
| 84 | decimals => 2, | ||||||||||||||||||||||||||||
| 85 | unknownCharacter => '?', | ||||||||||||||||||||||||||||
| 86 | fillCharacter => ' ', | ||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub checkAttributeValues | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
checkAttributeValues: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 92 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | $self->SUPER::checkAttributeValues; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | X::Usage->throw("decimals can not be negative") if $self->get_decimals < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | X::Usage->throw("unknownCharacter must have length 1") if length($self->get_unknownCharacter) != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | X::Usage->throw("fillCharacter must have length 1") if length($self->get_fillCharacter) != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | 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<< <knth at cpan.org> >> | ||||||||||||||||||||||||||||
| 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<bug-progressmonitor at rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 160 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ProgressMonitor>. | ||||||||||||||||||||||||||||
| 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 | 2 | 1; # End of ProgressMonitor::Stringify::Fields::Percentage | |||||||||||||||||||||||||||