如何在Perl中制作动画加载标志? [英] How can you make an animated loading sign in Perl?
问题描述
我需要知道如何在Perl中制作一个动画加载标志,以便在程序检查更新时使人们保持娱乐.
I need to know how to make an animated loading sign in Perl for keeping people entertained while my program's checking for updates.
我已经尝试使用
print ".";
sleep(0.1);
print ".";
但是似乎不起作用.有人请帮助我!
but that doesn't seem to work. Somebody please help me!
print ".";
sleep(0.1);
print ".";
不起作用
我只希望程序等待1/10秒才能打印下一个.
I just want the program to wait 1/10th of a second to print the next .
推荐答案
使用 Time :: HiRes满足亚秒级计时需求的几种方法
Using Time::HiRes for sub-second timing needs, a few ways
use warnings;
use strict;
use feature 'say';
use Time::HiRes qw(sleep);
STDOUT->autoflush(1); # or $| = 1;
my $tot_sleep = 0;
# Print dots
print "Loading ";
while (1) {
$tot_sleep += sleep 0.1; print ".";
last if $tot_sleep >= 2;
}
say " done\n"; $tot_sleep = 0;
# Print "spinning" cursor while waiting
print "Loading ... ";
WAIT: while (1) {
for (qw(- \ | /)) {
print; $tot_sleep += sleep (0.1); print "\b";
last WAIT if $tot_sleep >= 2;
}
}
say "\b done\n";
# Print (overwrite) percentile completed (if you know the job size)
my $tot_percent = 0;
while ($tot_percent < 100) {
$tot_percent += 5;
print "Loading ... $tot_percent%\r";
sleep 0.1;
}
say "\n";
我通过等待2秒来模拟完成"(加载).因此,这次的 if
检查代表检查加载"是否完成,大概可以在代码中的那个时候完成(如果它是一个单独的线程/进程,或者该代码是否运行)在分叉的过程中.
I simulate "completion" (of loading) by adding up waits to 2 seconds. The if
checks of this time thus stand for checks of whether "loading" completed, what presumably can be done at that point in the code (if it is a separate thread/process, or if this code runs in a forked process).
对于一个也许更好的旋转者"可以使用
For a perhaps nicer "spinner" can use
use Time::HiRes qw(sleep);
use utf8;
use open qw(:std :encoding(UTF-8));
STDOUT->autoflush(1);
print "Waiting ... ";
WAIT: {
my $tot_sleep;
while (1) {
for ('◑', '◒', '◐', '◓') {
print; $tot_sleep += sleep 0.1; print "\b";
last WAIT if $tot_sleep >= 5;
}
}
};
say "\b done";
从 Term :: Spinner :: Color 借用的符号的想法.
这样的旋转者"当然不会像点子那样给出他们等待多长时间的视觉线索.
Such "spinners" of course don't give the visual clues of how long they wait(ed), like dots do.
这篇关于如何在Perl中制作动画加载标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!