Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: progress bars

by PetaMem (Priest)
on Nov 04, 2001 at 13:21 UTC ( [id://123158]=note: print w/replies, xml ) Need Help??


in reply to progress bars

Hi,

yesterday I somewhat fixed a version of my "progress bar fillup" I created after this thread. Most of the ideas came from tye.
This version uses a closure, and might be of use for one or another of you. Usage is simple:

my $pb = pb_fillup(<showlen>,<char>,<barlen>);
Initializes the progress-bar. It defines how long the data to be munged are (showlen), what character should be used for progress visualization (char) and how long the progress bar shall be (barlen). All 3 are scalars. If you wish to init a progress bar with a [#######    ] look&feel, that should show progress reading a file and be 40 chars wide, you could init it like that:
my $pb = pb_fillup(-s $file,'#',40);
Where $file is a name of an existing file. The calls doing the actual visualization are like that:
print $pb->(length $_);
You either give the closure the length of data you have munged or - if you omit the parameter - pb_fillup just does an increment. I´m sure, the code can be much optimized and beautified, but it´s pretty easy to use and does accomplish its task for me.

Bye
Richard

Oh! The code:

sub pb_fillup { my ($max,$char,$len) = @_; # init look&feel my $old_slen = -1; # init old showlength my $cur = 0; # init current length return sub { $cur += @_ ? shift : 1; my $slen = int $cur/$max*$len; my $bar; if($slen != $old_slen) { $bar = $char x $slen; $bar .= ' ' x ($len - $slen); $| = 1; print '[',$bar,']'; print "\b" x ($len+2); $| = 0; $old_slen = $slen; } return; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://123158]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found