Perl script command line progress bar

I am trying to print progress in% on the command line. But it does not work properly.

I want to print the progress as: Status 10% Completed when 20% are completed, the status 20% completed in the same place is not on a new line. Could you help me.

Code::

$count++;
$per=($count/$total)*100;
print "\nStatus:  $per Completed.\r";
sleep 1;
+4
source share
3 answers

You can do something like this:

use strict;
use warnings;

use Time::HiRes qw(usleep);
local $| = 1;

my @nums = 1 .. 20;

foreach my $c (@nums) {
  print "$c";
  usleep(100000);
  print ("\b" x length($c));
}
print "\n";
+2
source

The Term :: ProgressBar module seems to be able to do what you need.

Notice I have not tried.

Edit Well, out of curiosity, I now tried a small script:

use warnings;
use strict;

use Term::ProgressBar;

my $total = 50;
my $progress_bar = Term::ProgressBar->new($total);


for my $i (1 .. $total) {

  sleep (1);

  $progress_bar->update($i);

}

, (, Status nn% completed), , -

  10% [=====                                              ]

, .

+12

A small modification of the source code: \ n and \ r are deleted and autoflush and "\ 033 [G" and \ 033 [J

$|=1; #autoflush

$count = 0;
$total = 100;
while ($count != $total) {
   $count++; 
   $per=($count/$total)*100; 
   print "\033[JStatus: ${per}% Completed."."\033[G"; # man console_codes, ECMA-48 CSI sequences, "CHA"
   sleep 1
 }  
+2
source

All Articles