Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Creating a Sparkle in perl

by gj2666 (Beadle)
on Dec 29, 2005 at 20:06 UTC ( [id://519857]=perlquestion: print w/replies, xml ) Need Help??

gj2666 has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks, remember back in the days of DOS when some programs would put in an indicator that the program wasn't locked up but was actually doing something? It was a series of slashes and dashes overlaid over the top of the last so that it appeared to be a "sparkle" Sort of an animated asterisk.

So I was looking through "cool uses for perl" saw a posting with a "Text to animated pseudo-braille GIF" which looks like it would do a similar thing in a gif file. It got me thinking about it again.

So here's what I came up with but it's not very swanky and I have to do a system "cls"; between every display which makes the text flash as well (I'd use something besides cls if I knew how). I couldn't figure out any other way to control what line on the screen the sparkle sparkled on.

# !cqperl (that's all I have here at work) use strict; use warnings; my $count = 0; my @sparkle = qw(\ | / - |); while ($count < 100 ){ my $thingy=shift(@sparkle); system"cls"; print "This is a sparkle: $thingy\n"; sleep .99; push(@sparkle, $thingy); $count++; }
Any ideas? How could I make it better?

Thanks, Gretchen

(aka sparkle farkle) - Oh yeah? Well, if you get the ref you're that old too!

Replies are listed 'Best First'.
Re: Creating a Sparkle in perl
by ikegami (Patriarch) on Dec 29, 2005 at 20:15 UTC
    You'll find the following handy:
    print("\010"); # backspace Moves cursor left one. print("\r"); # carriage return. Moves cursor to start of line.

    Specifically:

    use strict; use warnings; use Time::HiRes qw( sleep ); my @sparkle = qw( \ | / - ); my $cur_sparkle = 0; $| = 1; # autoflush print(' '); for (;;) { $cur_sparkle = ($cur_sparkle + 1) % @sparkle; print("\010$sparkle[$cur_sparkle]"); # Do a work unit. sleep(0.250); last if not int(rand(50)); } print("\010done.\n"); # or: print("\010 \010");

    Update:
    Replaced Timer::HiRes with Time::HiRes.
    Replaced \08 with \010.
    Added $| = 1;
    Removed last | from @sparkle to smoothen the animation.

Re: Creating a Sparkle in perl
by McDarren (Abbot) on Dec 30, 2005 at 00:02 UTC
Re: Creating a Sparkle in perl
by renodino (Curate) on Dec 29, 2005 at 20:23 UTC
    use strict; use warnings; use Time::HiRes qw(sleep); my @sparkle = qw(| / - \ ); $| = 1; system "cls"; print "This is a sparkle:\n"; print "\r", $sparkle[$_%4] and sleep 0.5 for (0..99);
Re: Creating a Sparkle in perl
by jdhedden (Deacon) on Dec 30, 2005 at 14:45 UTC
    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.
      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.
Re: Creating a Sparkle in perl
by liverpole (Monsignor) on Dec 30, 2005 at 16:56 UTC
        I'd use something besides cls if I knew how

    You can get the equivalent of a cls without going to the shell if you use escape sequences:

    use Win32::CONSOLE::ANSI; # (This is only required in Windows) print "\e[H\e[J\n"; # Escape-H "homes" the cursor # Escape-J clears to end-of-screen
    A couple of the other escapes I use fairly frequently are:
    print "\e[K"; # Clear to end-of-line printf "\e[%d;%dH", $row, $col; # Positions cursor at given row & col +umn

    @ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-29 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found