Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

wget style progress bar

by tachyon (Chancellor)
on Mar 16, 2003 at 09:34 UTC ( #243454=snippet: print w/ replies, xml ) Need Help??

Description: Print a wget style progress bar. Inputs should be self explanatory
do{ print progress_bar( $_, 100, 25, '=' ); sleep 1 } for 1..100;

# get a progress bar string, inputs should be self explanatory otherwi
+se just RTFS!
sub progress_bar {
    my ( $got, $total, $width, $char ) = @_;
    $width ||= 25;
    $char  ||= '=';
    my $num_width = length $total;
    sprintf "|%-${width}s| Got %${num_width}s bytes of %s (%.2f%%)\r",
+ 
        $char x (($width-1)*$got/$total). '>', $got, $total, 100*$got/
+$total;
}
__DATA__
|==========>              | Got  42 bytes of 100 (42.00%)


Comment on wget style progress bar
Download Code
Re: wget style progress bar
by artist (Parson) on Mar 16, 2003 at 17:01 UTC
    Very neat application tachyon.
    Now How I can integrate it to see the progress of my subroutine ?
    my routine = sub { open OUT, ">file"; foreach (1..200){ print OUT "HelloWorld\n" }; sleep 1; }; print_progress_bar($routine);

    artist

      I'm not sure what you're asking for. This, perhaps?
      sub foo { open my $fh, ">", "file"; foreach (1..200){ print OUT "HelloWorld\n"; print progress_bar($_, 200, 25, '='); } sleep 1; }

      Makeshifts last the longest.

        Almost similar, but I am looking for functionality where I can call and mesure the progress for the routine as it is executed. So the line 'print progress_bar' should be outside the 'foo' here. I should have independent way of measuring the progress of the subroutine. I guess the only way to achive that is to implement the call back routines or implement threads .

        artist

Re: wget style progress bar
by morbus (Sexton) on May 28, 2003 at 02:37 UTC
    Incidentally, for this to work under strict/warnings, change "(%.2f%)" to "(%.2f%%)" (escaping the literal % is necessary).
Re: wget style progress bar
by ikegami (Pope) on Oct 06, 2004 at 02:51 UTC

    Doesn't display anything for the longuest time because STDOUT is not being flushed. Setting STDOUT to outflush or adding a call to flush (defined below) after the print does the trick.

    sub flush { my $h = select($_[0]); my $a=$|; $|=1; $|=$a; select($h); }
      $|++   ;#)

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://243454]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2014-09-21 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls