如何在 Perl 中制作动画加载标志



我需要知道如何在Perl中制作一个动画加载标志,以便在我的程序检查更新时让人们娱乐。

我已经尝试过使用

print ".";
sleep(0.1);
print ".";

但这似乎行不通。有人请帮帮我!

print ".";
sleep(0.1);
print ".";

不起作用

我只希望程序等待 1/10 秒来打印下一个.

使用 Time::HiRes 满足亚秒级计时需求,几种方法

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 " donen";  $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 donen";
# 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检查代表检查"加载"是否完成,在代码中的那个点大概可以做什么(如果它是一个单独的线程/进程,或者如果这个代码在分叉进程中运行(。


对于也许更好的"微调器"可以使用

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";
符号的想法借用自术语::

微调器::颜色。

当然,这样的"微调器"不会像点那样提供它们等待多长时间的视觉线索。

标准睡眠函数以整数秒为单位运行。您可以使用 Time::HiRes 中的睡眠功能作为支持秒数部分的直接替代品。

use strict;
use warnings;
use Time::HiRes 'sleep';
sleep 0.1;

现有的解决方案假设你真的想睡觉/等待。他们不容易适应用实际工作代替睡眠/等待。我的解决方案是当你在做实际工作(例如加载(时,而不仅仅是等待某事。

use Time::HiRes qw( );
$| = 1;
{
   my @syms = qw( -  | / );
   my ( $i, $t );
   sub start_spinner {
      $t = Time::HiRes::time;
      $i = 0;
      print $syms[$i];
   }
   sub update_spinner {
      my $now = Time::HiRes::time;
      return if $now - $time < 0.1;  # Prevent spinner from spinning too fast.
      $time = $now;
      $i = ( $i + 1 ) % @syms;
      print "b$syms[$i]";
   }
   sub stop_spinner {
      print "b b";
   }
}
start_spinner();
for (1..500) {
   update_spinner();          # Call this as often as possible.
   Time::HiRes::sleep(0.01);  # Simulate a little bit of work.
}
stop_spinner();

关键是使用Time::HiRes的更高分辨率time(如有必要,还可以使用sleep(,以及速率限制器(return if $now - $time < 0.1;(。

如果您确实想打印一行点,则可以使用相同的方法。

{
   my $t;
   sub start_spinner {
      $t = Time::HiRes::time;
   }
   sub update_spinner {
      my $now = Time::HiRes::time;
      return if $now - $time < 0.1;  # Prevent spinner from spinning too fast.
      $time = $now;
      print ".";
   }
   sub stop_spinner {
      print "n";
   }
}

不使用模块的另一种方法是滥用 select((:

use warnings;
use strict;
$|=1;
while (1){
    print '.';
    select(undef, undef, undef, 0.1);
}

或者,一个 FreeBSD 风格的微调器来娱乐 (使用 Linux 系统调用来刷新屏幕。在 Windows 上,将clear更改为 cls (:

while (1){
    for (qw(-  | / -)){
        system 'clear';
        print $_;
        select(undef, undef, undef, 0.1);
    }
}

最新更新