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; } }

In reply to Re: progress bars by PetaMem
in thread progress bars by PetaMem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.