in reply to Creating a Sparkle in perl

Just for the fun of it, here's YAPI (yet another progress indicator) - an OO version based on Object::InsideOut.

Put this part in a file called YAPI.pm somewhere in @INC:

package YAPI; { use strict; use warnings; use Object::InsideOut; my @count :Field; my @prog :Field; my %init_args :InitArgs = ( 'prog' => { 'Field' => \@prog, 'Default' => [ qw(/ - \ |) ], # Twirling bar 'Type' => 'List' } ); sub start { my ($self, $msg) = @_; $| = 1; # Autoflush print("$msg \e[?25l"); # Print 'msg' and hide cursor $self->progress(); } sub progress { my $self = shift; print("\b", $prog[$$self][$count[$$self]++ % @{$prog[$$self]}] +); } sub done { my $self = shift; my $msg = shift || ' '; print("\b$msg\e[?25h\n"); # Print 'msg' and restore curs +or } } 1;
And here's a sample on how to use it:
#!/usr/bin/perl use strict; use warnings; use YAPI; MAIN: { my $prog = YAPI->new('prog' => [ qw(^ > v <) ]); # My own invent +ion $prog->start('Working: '); foreach (1..10) { sleep(1); $prog->progress(); } $prog->done('done'); } exit(0); # EOF
Enjoy, and Happy New Year!

Remember: There's always one more bug.

Replies are listed 'Best First'.
Re^2: Creating a Sparkle in perl
by jdhedden (Deacon) on Sep 07, 2007 at 20:00 UTC
    An updated and more comprehensive version of this (now called Term::YAPI) is included with Object::InsideOut in the 'examples' directory of the distribution. Enjoy.