szabgab/Term-ProgressBar — pull request #7 by LukeGoodsell (2014-09-26 13:41:34+00:00)
Title: Add support for occasions when max count is unknown

I have scripts that sometimes know the maximum count (eg when taken from a file) but sometimes don't (eg taken from STDIN). Rather than add extra code that allows me to either use the progress bar, or not, and either use progress->message() or not, this little change allows you to set the count = -1 and keep using the other progress bar code.

This code means that on the first call to update() from init(), or following a call to message() it simply prints the name of the progress bar. If it is called with a value < 0, (such as updating to the 'count' when 'count' < 0, it will blank and reset the line.

For example:

defined($num_items) or $num_items = -1;

my $progress = Term::ProgressBar->new ({
count	=> $num_items,
remove	=> 1,
ETA	=> 'linear',
name	=> 'Retrieving item details',
silent	=> $parameters->{silent},
});

my $counter = 0;

while(1) {
my $next_item = get_next_item() or last;
$progress->update($counter++);
$progress->message("I can still use this functionality whether I know the max number or not");
}

$progress->update($num_items);
# The line is blank.