Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

wget style progress bar

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

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%)

Replies are listed 'Best First'.
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 (Patriarch) 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); }
      $|++   ;#)
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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found