Re: Spining "stick"
by atcroft (Abbot) on Jul 03, 2004 at 09:30 UTC
|
| [reply] |
|
|
while( $waiting or 1 ) {
spin();
}
{
my $c=0; # closure to remember spin state
sub spin {
local $| = 1;
print "\r", qw( | / - \ )[$c++%4];
select undef, undef, undef, 0.25; # sleep 250 msec
}
}
Unbuffered output as per beebles note.
| [reply] [d/l] |
|
|
This was producing no output on my system, unless I commented out the select line. Replacing select with a sleep also produced no output. But I worked it out, by adding a line saying local $| = 1; to the start of the program, I got output.
| [reply] [d/l] |
Re: Spining "stick"
by haoess (Curate) on Jul 03, 2004 at 12:06 UTC
|
perl -e '{print "$_\b" for qw( | / - \ ); redo}'
-- Frank | [reply] [d/l] |
|
|
smokin fast....hurry up and wait if ever I saw it ;-)
| [reply] |
|
|
| [reply] |
Re: Spining "stick"
by beable (Friar) on Jul 03, 2004 at 09:52 UTC
|
Here's one which uses the Curses module:
#!/usr/bin/perl
use strict;
use warnings;
use Curses;
run();
sub run
{
initscr;
while (1)
{
spin_stick();
# do something else
sleep 1;
}
endwin;
}
BEGIN
{
my $stick_state = 0;
my @sticks = qw(| / - \ );
sub spin_stick
{
addstr(1, 0, "Please wait while this stick keeps spinning...")
+;
addch(1, 46, $sticks[$stick_state++]);
move(0,0);
$stick_state = $stick_state % scalar(@sticks);
refresh;
}
}
__END__
| [reply] [d/l] |
Re: Spining "stick"
by BrowserUk (Patriarch) on Jul 03, 2004 at 19:05 UTC
|
The problem with these solutions is that whilst the "busy" spinner is spinning, the only thing that the program is busy doing is spinning the spinner.
Here's one that allows you to do something useful while it spins.
#! perl -slw
use strict;
use threads qw[ async yield ];
use threads::shared;
my $ready : shared = 0;
async{
local $|=1;
while( !$ready ) {
do{
select undef, undef, undef, 0.2;
printf "\r ($_)"
} for qw[ / - \ | * ];
}
print "\rReady";
$ready = 0;
}->detach;
for ( 1 .. 10 ) {
## Busy, busy, busy
sleep 1;
}
$ready = 1;
yield while $ready;
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
| [reply] [d/l] |
|
|
This alternative should print a line only if it succeeds in what is doing as job. That way, there is a real significance of the spinning wheel (sort of:). When the job is not succeeding, the wheel should stay calm (panic!).
use strict;
use threads qw[ async yield ];
use threads::shared;
my $ready : shared = 0;
my $isOk : shared = 0;
async{
local $|=1;
while( !$ready ) {
do{
select undef, undef, undef, 0.2;
printf "\r ($_)" if ($isOk)
} for qw[ / - \ | * ];
}
print "\rReady";
$ready = 0;
}->detach;
for ( 1 .. 10 ) {
## Busy, busy, busy
$isOk=1;
sleep 1;
$isOk=0;
}
$isOk=0;
$ready = 1;
yield while $ready;
.{\('v')/}
_`(___)' __________________________
| [reply] [d/l] |
Re: Spining "stick"
by theorbtwo (Prior) on Jul 03, 2004 at 20:36 UTC
|
Note that unless you have no idea how long the operation will take to complete, you should use something that conveys to the user how far along the process is. Term::ProgressBar looks good for this.
| [reply] |
Re: Spining "stick"
by chiburashka (Initiate) on Jul 03, 2004 at 14:01 UTC
|
| [reply] |