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;
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.