Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
(Updated: Something bad happened to the code portion during cut-and-paste to the site. This is now fixed.)

Here's a quick bit of code that provides a simple, object-oriented way to draw progress bars. To use it, just create a progress bar like so:

my $bar = ProgressBar->new( width => 40 );
(Here, our bar will be 40 characters wide.) Then, to draw the bar, just pass in the current count of completed tasks and the total number of tasks:
$bar->draw( 3, 10 );
So, in the above example, we have completed 3 of 10 tasks and the following would be drawn:
============ 30%
A couple of points:
  • If the total number of tasks is fixed, you can provide it at the time you create the progress bar via the max_val parameter and omit it in calls to the draw() method.
  • The code works properly for progress bars that go forward, backward, or both. (See the demo.)

Here's the code:

#!/usr/bin/perl -w { # TGM 2004-10-05 # Progress bar code package ProgressBar; use Class::Struct width => '$', portion => '$', max_val => '$'; use List::Util qw( min ); sub draw { local $| = 1; my ($self, $x, $max_val) = @_; $max_val ||= $self->max_val; my $old_portion = $self->portion || 0; my $new_portion = int( $self->width * $x / $max_val + 0.5 ); print "\b" x ( 6 + $self->width - min( $new_portion, $old_portion ) ) if defined $self->portion; print "=" x ( $new_portion - $old_portion ), " " x ( $self->width - $new_portion ), sprintf " %3d%% ", int( 100 * $x / $max_val + 0.5 ); $self->portion( $new_portion ); } 1; } # DEMO my $pb = ProgressBar->new( width => 40, max_val => 10 ); # show progress 0 to 100% and then back down to 0% again do { $pb->draw( $_ ) ; sleep 1 } for 0 .. 10; do { $pb->draw( 9 - $_ ); sleep 1 } for 0 .. 9; print "\n";
Hope this helps.


In reply to Re: Progress Bar by tmoertel
in thread Progress Bar by Texan

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (2)
    As of 2018-04-24 22:07 GMT
    Find Nodes?
      Voting Booth?