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