lib/Term/ProgressBar/Simple.pm

filter: Statementstmt 100% 33 / 33 Branchbran 50.0% 3 / 6 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 94.3% 50 / 53 CC 5 SCAR 6.1
CC
5
Cov
93%
increment
3.0
DESTROY
2.0
new
2.0
CRAP 1.8
↑ Summary
1 package Term::ProgressBar::Simple;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Term::ProgressBar::Quiet;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use overload #
9 2 ▶ '++' => \&increment, #
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 '+=' => \&increment; #
11 # '--' => \&decrement; # add later
12  
13 2 our $VERSION = '0.03';
14  
15 =head1 NAME
16  
17 Term::ProgressBar::Simple - simpler progress bars
18  
19 =head1 SYNOPSIS
20  
21 # create some things to loop over
22 my @things = (...);
23 my $number_of_things = scalar @things;
24  
25 # create the progress bar object
26 my $progress = Term::ProgressBar::Simple->new( $number_of_things );
27  
28 # loop
29 foreach my $thing (@things) {
30  
31 # do some work
32 $thing->do_something();
33  
34 # increment the progress bar object to tell it a step has been taken.
35 $progress++;
36 }
37  
38 # See also use of '$progress += $number' later in pod
39  
40 =head1 DESCRIPTION
41  
42 Progress bars are handy - they tell you how much work has been done, how much is
43 left to do and estimate how long it will take.
44  
45 But they can be fiddly!
46  
47 This module does the right thing in almost all cases in a really convenient way.
48  
49 =head1 FEATURES
50  
51 Lots - does all the best practice:
52  
53 Wraps L<Term::ProgressBar::Quiet> so there is no output unless the code is
54 running interactively - lets you put them in cron scripts.
55  
56 Deals with minor updates - only refreshes the screen when it will change what
57 the user sees so it is efficient.
58  
59 Completes the progress bar when the progress object is destroyed (explicitly or
60 by going out of scope) - no more '99%' done.
61  
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 # Either...
68 my $progress = Term::ProgressBar::Simple->new($count);
69  
70 # ... or
71 my $progress = Term::ProgressBar::Simple->new(
72 {
73 count => $count, #
74 name => 'descriptive text',
75 }
76 );
77  
78 Create a new progress bar. Either just pass in the number of things to do, or a
79 config hash. See L<Term::ProgressBar> for details.
80  
81 =cut
82  
83 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
84 4 my $class = shift;
85 4 my $input = shift;
86  
87 # if we didn't get a hashref assume we got a count
88 4 ▶ $input = { count => $input } unless ref $input;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input 4 0 4
89  
90 # create the T::PB::Q args with defaults.
91 4 my $args = {
92 ETA => 'linear', # only sensible choice
93 name => 'progress', # seems reasonable
94 %$input,
95 };
96  
97 4 my $tpq = Term::ProgressBar::Quiet->new($args);
98  
99 4 my $self = {
100 args => $args,
101 tpq => $tpq,
102 count_so_far => 0,
103 next_update => 0,
104 };
105  
106 4 return bless $self, $class;
107 }
108  
109 =head2 increment ( ++ )
110  
111 $progress++;
112  
113 Incrementing the object causes the progress display to be updated. It is smart
114 about checking to see if the display needs to be updated.
115  
116 =head2 increment ( += )
117  
118 $progress += $number_done;
119  
120 Sometimes you'll have done more than one step between updates. A good example is
121 processing logfiles, where the time taken is relative to the size of the file.
122 In this case code like this would give a better feel for the progress made:
123  
124 # Get the total size of all the files
125 my $total_size = sum map { -s } @filenames;
126  
127 # Set up object with total size as steps to do
128 my $progress = Term::ProgressBar::Simple->new($total_size);
129  
130 # process each file and increment by the size of each file
131 foreach my $filename (@filenames) {
132 process_the_file($filename);
133 $progress += -s $filename;
134 }
135  
136 =cut
137  
138 20022 ▶sub increment {
Subroutine increment: called CC=3 SCAR=11.0
Pod documented
139 20022 my $self = shift;
140 20022 ▶ my $increment = shift || 1;
Condition100%2/2
shift() || 1
l!l
2020002
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
141  
142 20022 $self->{count_so_far} += $increment;
143 20022 my $now = $self->{count_so_far};
144  
145 20022 ▶ if ( $now >= $self->{next_update} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now >= $self->{"next_update"}) 20022 0 20022
146 20022 $self->{next_update} = $self->{tpq}->update($now);
147 }
148  
149 20022 return $self;
150 }
151  
152 =head2 message
153  
154 $progress->message('Copying $filename');
155  
156 Output a message. This is very much like print, but we try not to
157 disturb the terminal.
158  
159 =cut
160  
161 3 ▶sub message {
Subroutine message: called CC=1 SCAR=0.0
Pod documented
162 3 my($self, $message) = @_;
163 3 $self->{tpq}->message($message);
164 }
165  
166 # want to add this in a later version.
167 #
168 # while ( $progress-- ) {
169 # # do something
170 # }
171 #
172 # =head2 decrement
173 #
174 #
175 # =cut
176 #
177 # sub _decrement {
178 # my $self = shift;
179 #
180 # # increment and get the number done
181 # my $number_done = $self->increment;
182 #
183 # # return number remaining, or zero if overshot
184 # my $remaining = $self->{args}{count} - $number_done;
185 # $remaining = 0 if $remaining < 0;
186 #
187 # return $self;
188 # }
189  
190 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.1
191 4 my $self = shift;
192  
193 4 ▶ $self->{tpq}->update( $self->{args}{count} ) if $self->{tpq};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tpq"} 4 0 4
194  
195 4 return;
196 }
197  
198 =head1 SEE ALSO
199  
200 L<Term::ProgressBar> & L<Term::ProgressBar::Quiet>
201  
202 =head1 GOTCHAS
203  
204 Not all operators are overloaded, so things might blow up in interesting ways.
205 Patches welcome.
206  
207 =head1 THANKS
208  
209 Martyn J. Pearce for the orginal and great L<Term::ProgressBar>.
210  
211 Leon Brocard for doing the hard work in L<Term::ProgressBar::Quiet>, and for
212 submitting a patch with the code for C<+=>..
213  
214 YAPC::EU::2008 for providing the venue and coffee whilst the first version of
215 this module was written.
216  
217 =head1 AUTHOR
218  
219 Edmund von der Burg C<< <evdb@ecclestoad.co.uk> >>.
220  
221 L<http://www.ecclestoad.co.uk/>
222  
223 =head1 BUGS
224  
225 There are no tests - there should be. The smart way would be to trap the output
226 and check it is right.
227  
228 =head1 LICENCE AND COPYRIGHT
229  
230 Copyright (c) 2008, Edmund von der Burg C<< <evdb@ecclestoad.co.uk> >>.
231 All rights reserved.
232  
233 This module is free software; you can redistribute it and/or modify it under
234 the same terms as Perl itself.
235  
236 =cut
237  
238 2 1;
↑ Summary