in reply to "percent task completed progress bar" in cli interface, how?

Update. Fixed sprintf format. Thanks booger

This is probably what you want, a wget-style progressbar.

#!/usr/bin/perl $|=1; do{ print progress_bar( $_, 100, 25, '=' ); select(undef,undef,undef, .1) } for 1..100; # figure out how to work it in to your program sub progress_bar { my ( $got, $total, $width, $char ) = @_; $width ||= 25; $char ||= '='; $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; }

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: "percent task completed progress bar" in cli interface, how?
by Booger (Pilgrim) on Jul 17, 2006 at 01:51 UTC
    Your line

    sprintf "|%-${width}s| Got %${num_width}s bytes of %s (%.2f%)\r",

    should really read

    sprintf "|%-${width}s| Got %${num_width}s bytes of %s (%.2f%%)\r",

    Cheers, Matt

      You may know more about sprintf's formats than me, but adding an extra % , as you suggest, dosn't change the behavior at all. Can you explain why?

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        If you use the warnings pragma you'll find that the original code produces the following error

        Invalid conversion in sprintf: "%)" at test.pl line 16.

        The sprintf function documentation specifies that a percent sign should be formatted '%%'. I'm not sure why excluding the extra '%' doesn't count as a syntax error.

        From what I can tell the warning has been emitted via warnings since around 5.8.0. FYI I'm using 5.8.7 from Ubuntu 6.06 with the patch "SPRINTF0 - fixes for sprintf formatting issues - CVE-2005-3962." This patch is applied by either the Debian team or Ubuntu team (not sure which one). I think the patch originated from http://www.cpan.org/modules/by-module/Compress/NWCLARK/sprintf-5.8.7.patch.

        So you might not be wrong per-se but sprintf in versions of Perl without this patch certainly don't do what the manual says it should.

        Cheers,
        Matt