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
|
package TextProgressBar;
use strict;
use warnings;
use List::Util qw( min max );
$| = 1;
sub new {
my ($class) = @_;
my $self = {
value => 0,
maximum => -1,
iteration => 0
};
return bless $self, $class;
}
sub clear
{
my ($self) = @_;
printf "\n";
$self->{iteration} = 0;
$self->{value} = 0;
$self->{maximum} = -1;
$self->{message} = undef;
}
sub update
{
my ($self) = @_;
++$self->{iteration};
if ($self->{maximum} > 0) {
# we know the maximum
# draw a progress bar
my $percent = $self->{value} * 100 / $self->{maximum};
my $hashes = $percent / 2;
my $progressbar = '#' x $hashes;
if ($percent % 2) {
$progressbar .= '>';
}
printf "\r[%-50s] %3d%% %s ",
$progressbar,
$percent,
$self->{message};
} else {
# we don't know the maximum, so we can't draw a progress bar
my $center = ($self->{iteration} % 48) + 1; # 50 spaces, minus 2
my $before = ' ' x max($center - 2, 0);
my $after = ' ' x min($center + 2, 50);
printf "\r[%s###%s] %s ",
$before, $after, $self->{message};
}
}
sub setMessage
{
my ($self, $m) = @_;
$self->{message} = $m;
}
sub setStatus
{
my ($self, $val, $max) = @_;
$self->{value} = $val;
$self->{maximum} = $max;
}
1;
|