Twiddle(3pm) | User Contributed Perl Documentation | Twiddle(3pm) |
Term::Twiddle - Twiddles a thingy while-u-wait
use Term::Twiddle; my $spinner = new Term::Twiddle; $spinner->start; system('tar', '-xvf', 'some_phat_tarfile.tar'); $spinner->stop; $spinner->random; ## makes it appear to really struggle at times! $spinner->start; &some_long_function(); $spinner->stop;
Always fascinated by the spinner during FreeBSD's loader bootstrap, I wanted to capture it so I could view it any time I wanted to--and I wanted to make other people find that same joy I did. Now, anytime you or your users have to wait for something to finish, instead of twiddling their thumbs, they can watch the computer twiddle its thumbs.
Once the twiddler/spinner is in motion you need to do something (e.g., unpack a tar file, call some long function, etc.). You can do almost anything in between start and stop as long as there are no sleep calls in there (unless the process has been forked, as in a Perl system call). From Time::HiRes:
Use of interval timers may interfere with alarm(), sleep(), and usleep(). In standard-speak the "interaction is unspecified", which means that anything may happen: it may work, it may not.
Try not to do any terminal I/O while the twiddler is going (unless you don't mind dragging the twiddler around with your cursor).
my $spinner = new Term::Twiddle;
Optionally initializes the Twiddle object:
## a moderately paced spinner my $spinner = new Term::Twiddle( { rate => 0.075 } );
$spinner->start;
$spinner->stop;
$spinner->thingy( [ "\\", "|", "/", "-" ] );
an arrow could be done like this:
$spinner->thingy( [
"---->",
" ----->",
" ----->",
" ----->",
" ----->|",
" ---->|",
" --->|",
" -->|",
" ->|",
" >|",
" |",
" "]);
Look at the test.pl file for this package for more fun thingy ideas.
$spinner->rate(0.075); ## faster!
In short, if you want the thingy to change rates often, set probability high. Otherwise set it low. If you don't want the rate to change ever, set it to 0 (zero). 0 is the default.
## half of all sequence changes will result in a new rate of change $spinner->probability(50); $spinner->start; do_something; $spinner->stop;
The purpose of this is to create a random rate of change for the thingy, giving the impression that whatever the user is waiting for is certainly doing a lot of work (e.g., as the rate slows, the computer is working harder, as the rate increases, the computer is working very fast. Either way your computer looks good!).
$spinner->random;
$spinner->stream(*STDERR);
Since version 2.70, Term::Twiddle objects support a couple of new spinners that aren't so "plain". 2.70 includes a bounceing ball and a swishing object (that's the best name I could think to call it).
The following methods are used to activate and customize these new spinners.
my $sp = new Term::Twiddle({ type => 'bounce' }); $sp->start;
or you can set it with this type method:
my $sp = new Term::Twiddle; $sp->type('bounce');
There is currently no way to add new types without some hacking (it's on the "to do" list).
my $sp = new Term::Twiddle({ type => 'bounce', width => 60 }); $sp->start;
or you can set it with this width method:
my $sp = new Term::Twiddle({ type => 'swish' }); $sp->width(74);
Show the user something while we unpack the archive:
my $sp = new Term::Twiddle; $sp->random; $sp->start; system('tar', '-zxf', '/some/tarfile.tar.gz'); $sp->stop;
Show the user a bouncing ball while we modify their configuration file:
my $sp = new Term::Twiddle( { type => 'bounce' } ); $sp->start; ## there must not be any 'sleep' calls in this! do_config_stuff(); $sp->stop;
Scott Wiersdorf, <scott@perlcode.org>
The timer code has since been replaced by Time::HiRes's setitimer function, but it is good to thank Mr. Christiansen for his goodness to Perl anyway.
perl.
2022-11-20 | perl v5.36.0 |