perl - 为线程进度制作漂亮的控制台输出

时间:2015-07-25 23:31:57

标签: perl terminal console printf

我的所有主题都希望将他们的进度打印到终端的同一行。我无法让他们选择不同的线条。我该怎么处理? (linux& windows,如果可能的话)

编辑:

我尝试了两种变体:

1

当我打印'\ n'时,线条会下移,\ b不会删除\ n字符:

if ( !($x % 10) )
  {
    local $| = 1; # Or use IO::Handle; STDOUT->autoflush;
    # remove prev progress
    print "\b" x length($progressString) if defined $progressString;
    # do lots of processing, update $counter
    $progressString = "Thread #$tid.  $x / $length1"; # No more newline
    print $progressString; # Will print, because auto-flush is on
  }

2

类似地

my $progress = Term::ProgressBar->new($length1);
$progress->update($x);

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

我的脚本的工作变体如下:

use Win32::Console::ANSI; # for Windows and nothing for linux
# ...
#---OUTPUT----------------------
if ( !($x % 10) ) {
  local $| = 1;
  # Choose the line for thread ($i - thread_id)
  print  "\n" x $i;
  # Remove prev. progress
  print  "\b" x length($progressString) if defined $progressString;

  # Do lots of processing, update
  $progressString = " Thread #$i.  $x / $length1";
  print $progressString; # Will print, because auto-flush is on
  print  "\e[A" x $i, "\r"; # Back to begin before printing
}
#-------------------------------