Re: Subroutine for showing the end user the progress of the program
by dragonchild (Archbishop) on Feb 02, 2005 at 14:27 UTC
|
| [reply] |
|
|
Silly me, I forgot the main place to look. I had only hit google.
This is great though, I posted this question to beginners AT perl DOT org three days ago, but it only took about 5 mins to get a reply here!!
Unfortunately, that means I will be back much more often.
Thanks.
Just getting into the best language ever...
Fancy a yourname@perl.me.uk? Just ask!!!
| [reply] |
|
|
| [reply] |
|
|
|
|
|
|
Re: Subroutine for showing the end user the progress of the program
by g0n (Priest) on Feb 02, 2005 at 14:41 UTC
|
If you want something simpler than a status bar, on a text based STDOUT, I've occasionally used something like this (pseudocode) $length = 1;
while (doing operation)
{
for (1..$length){print "\b"}
print $numberOfRecords;
$length = length($numberOfRecords);
}
In other words, for each iteration of your loop, print the number of \b's equal to the string length of your last status message, then print your status message again. That way you get a constantly changing status message on STDOUT, rather than a scrolling '1 records\n','2 records\n' etc
I hope that makes sense!
VGhpcyBtZXNzYWdlIGludGVudGlvbmFsbHkgcG9pbnRsZXNz
| [reply] [d/l] |
|
|
| [reply] |
Re: Subroutine for showing the end user the progress of the program
by holli (Abbot) on Feb 02, 2005 at 15:02 UTC
|
I have once have written my own little Module for that purpose. I donīt know if it works on Unix (if not please tell me), but for Windows it works fine. Other than a progressbar it is capable to display a rotating line, a widening/narrowing line or rotating brackets.
That is especially useful when you donīt know the amount of work before it is done.
package Progress;
use strict;
our $VERSION = '0.02';
sub new
{
my $classname = shift;
my %attr = @_;
my $self = bless \%attr, $classname;
$self->{style} = 'simple'
unless defined $self->{style};
$self->{styles} =
{
simple => ["-", "\\", "|", "/"],
bracket => ["]", ")", "|", "(", "[", "(", "|", ")"],
line => [" ", " - ", " --- ", "-----", " --- ", " -
+ "],
};
if ( defined $self->{freestyles} && ref ( $self->{freestyles} ) eq
+ "ARRAY" )
{
$self->{styles}->{"free"} = $self->{freestyles};
$self->{style} = 'free';
}
$self->reset;
return $self;
}
sub reset
{
my $self = shift;
print chr(8) x $self->{last_char_length};
print " " x $self->{last_char_length};
print chr(8) x $self->{last_char_length};
$self->{_chars} = $self->{styles}->{$self->{style}};
}
sub next
{
print STDOUT "";
$|=1;
my $self=shift;
print chr(8) x ($self->{last_char_length} );
my $char = shift @{ $self->{_chars} };
push @{ $self->{_chars} }, $char;
$self->{last_char_length} = length ($char);
print $char;
}
Use it as follows:
use Progress;
my $p = Progress->new(style=>"line");
# or
# my $p = Progress->new(style=>"bracket");
# or
# my $p = Progress->new(style=>"simple");
# or (equiv. to simple)
# my $p = Progress->new();
# or
# my $p = Progress->new( style=>"free", freestyles => [("a".."z")]);
for ($i=0;$i<10;$i++)
{
$p->next();
sleep(1);
}
$p->reset();
Update:
To "install" this, simply copy & paste the first code-block and save it as "Progress.pm" to the perl-library path (most probably "perl/lib").
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: Subroutine for showing the end user the progress of the program
by saintmike (Vicar) on Feb 02, 2005 at 15:02 UTC
|
If you're up for something truly perlish, handy and clever, use Smart::Comments:
use Smart::Comments;
for (my $j=500; $j>0; $j--) { ### Compiling===[%] done
select undef, undef, undef, 0.01;
}
The trick lies in the ### comment, turning into a progress bar at runtime.
| [reply] [d/l] |
|
|
| [reply] |
|
|
The basic usage is to call a subroutine, once every loop, through your long process. Like:
#!/usr/bin/perl
use strict;
use warnings;
my $spinner_pos = 0;
sub spin {
my $spinner = '|\-/';
print STDERR substr($spinner, $spinner_pos++%length($spinner), 1)."\r"
+;
}
while (1) {
qx(cat /etc/termcap); #simulate doing important stuff
spin();
}
So you have to design your application, and progress indicator together. There is no 1 "foolproof way" to use them. So what type of proram are you running? If you show us, we can help you with placing an indicator somewhere in the code. It's "fun" and educational to experiment yourself.
Take the snippets you've received here, and play with them.
You can't break anything. You will get the idea of how they work eventually.
In some "event driven programs" like Tk, you can make your
indicator an object, and just do things like
$progressindicator->start;
$progressindicator->stop;
here is another widely used style of indicator used by wget
#!/usr/bin/perl
$|=1;
do{ print progress_bar( $_, 100, 25, '=' ); sleep 1 } for 1..100;
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
| [reply] [d/l] [select] |
|
|