Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Progress Bar

by jrm (Novice)
on Jan 09, 2002 at 06:09 UTC ( #137346=perlquestion: print w/replies, xml ) Need Help??
jrm has asked for the wisdom of the Perl Monks concerning the following question:

I would like to add a simple progress bar/indicator to a script. For example, print a %age progress of a for loop. My specific questions are:

- how do I print the figure in the same place on the screen for each iteration.

- is there a module for more fluffy dispays, e.g. a bar graph with automatic scale calibration etc.


p.s. my boss says "this is business critical and if it is not done by the time he gets in in the moring I have to stay at my desk for another 70 hours".

Replies are listed 'Best First'.
(Ovid) Re: Progress Bar
by Ovid (Cardinal) on Jan 09, 2002 at 06:18 UTC

    I am sure there are modules that can do the graph for you, but to print something in the same place on the screen, you should be able to use Curses.

    For more information: perldoc -q "How do I do fancy stuff with the keyboard/screen/mouse?"


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Progress Bar
by talexb (Canon) on Jan 09, 2002 at 07:13 UTC
    If you are looking for a really simple solution (like something that would run from a command line) then you can use a bar graph terminated with a "\r" ready for the next bar graph to overwrite it. So you'd get something like
    [---+---|---+---|---+---|---+---] 0% [XXX+---|---+---|---+---|---+---] 10% [XXXXXXXXXXXX---|---+---|---+---] 37% [XXXXXXXXXXXXXXXX---+---|---+---] 50% [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-] 97%
    I'm sure printf ( "%2d" .. ) and the x operator would come in handy to implement that. For something a little classier, Ovid's suggestion of curses will work great.

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.

      Many thanks Ovid and talexb for 2 helpful replies.

      After some thought I have gone for the Curses option, (a good excuse to learn Curses ?).

      A quick attempt at the %age worked well, and once I spotted the endwin() function I was happy.

      There does, however, seem to be a lack of perl-specific-Curses documentation...

      Thanks again

Re: Progress Bar
by Anonymous Monk on Jan 09, 2002 at 08:24 UTC
    Here's an graphical example using Tk. I wrote this to measure the progress of a compile (count's .o's in the build directory), but it can easily be tweakified for your own purposes.
    use Tk; sub tick; $MYBUILDDIR="c:\\build"; #Dont forget to backslash the backslashes $total_objects = 303; #Number of object files a full build would creat +e $progressbar_width = 2*$total_objects; $progressbar_height = 50; $timer_interval=10; #in seconds $MW = MainWindow->new; $MW->bind('<Control-c>' => \&exit); $MW->bind('<Control-q>' => \&exit); $canvas1 = $MW->Canvas( '-width' => $progressbar_width, -height => $progressbar_height, -background => 'black' ) -> pack; $newval = 10; $start = $MW->Button( -text => 'Start', -command => sub {tick}, ); $start->pack(-side => 'left', -fill => 'both', -expand => 'yes'); sub tick { # Update the counter every 5 seconds $MW->after($timer_interval * 1000, \&tick); $num_objs = @objects = glob($MYBUILDIR . "*.o"); print "\n$num_objs out of $total_objects built so far..."; $newval = ($num_objs / $total_objects)*$progressbar_width; $canvas1->create ('rectangle','0','0',$newval,$progressbar_height, +-fill=>'red'); } MainLoop;
      Oops, I posted anonymously by mistaked. Oh well, no biggy.
Re: Progress Bar
by TStanley (Canon) on Jan 09, 2002 at 18:54 UTC
    A quick search for progressbars on turned up several modules that you could use, such as Tk::ProgressBar and Term::ProgressBar.

    "Suppose you were an idiot... And suppose you were a
    member of Congress... But I repeat myself." -- Mark Twain

Re: Progress Bar
by count0 (Friar) on Jan 09, 2002 at 20:39 UTC
    If you fancy GUI's, gtk-perl has support for libgnomeui (Gnome::* Perl modules), which has a progress bar widget (Gnome::AppBar).
Re: Progress Bar
by unb0rn (Scribe) on Jan 10, 2002 at 07:21 UTC
    - how do I print the figure in the same place on the screen for each iteration

    a _simple_ solution would be the escapes chars, like \r ou \b.
    \r returns to the beggining of line; \b backs one caracter.
    use inside print :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://137346]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2018-04-26 02:46 GMT
Find Nodes?
    Voting Booth?