Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have a number of utilities written in Perl that take a while to complete -- e.g. scaling a large number of digital images. It would be nice to know when the task is finished, but usually the script is too small to warrant extravagant code for monitoring progress.

Fortunately, there's Term::ProgressBar. It lets you add a fancy wget/scp-style progress bar to your own ap, with only a few lines of code. Martyn J. Pearce has really made an effort to make things easy for us: For basic usage, you just need to call one function to set up the progress bar and another one to update it. He even threw in ETA (estimated time of arrival) calculation.

Let's look at some code. I like the ETA display, so I'm making it a bit more complicated than the very basic usage (two more lines added, horrors!). This is taken from my image scaling script:

use Term::ProgressBar; my $progress = Term::ProgressBar->new({name => 'Scaling images', count => scalar @imgfiles, ETA => 'linear'}); # update ETA once per second at most $progress->max_update_rate(1); foreach (@imgfiles) { perform_imaging_ops($_); $progress->update(); }

The output looks somewhat like this (note: I shortened the output here to make it fit on one code line):

crenz@tiffy:~ > scaleimages /my/directory Scaling images: 19% [========= + ]ETA 23:35

I'm using a German locale, so the script displays 23:35 instead of 11:35pm -- just in case you're wondering. When the estimated time needed is short, the display automatically changes:

Scaling images: 92% [============================================ + ]5m14s Left

I'm using the module on Mac OS X, but it should work on all standard Unix terminals. Not sure whether it works on Windows.


In reply to Term::ProgressBar by crenz

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found