ntroduction
We all write programs to run on the terminal to do jobs that take time, like processing logs, sending out newsletter, etc. To know the progress of our task we print some messages to know our program is running well and doing its job. But wouldn't it be nice if we could add a progress bar to keep track of the completion of the task like wget, curl, etc.
Here's how we can do it in Perl, we'll use the perl module Term::ProgressBar to build and update the progress bar.
The Code
A very simple example:
Code: Perl
#!/usr/bin/perl
$| = 1;
use Term::ProgressBar;
use Time::HiRes qw/usleep/;
my $number_of_entries = 2000;
my $progress = Term::ProgressBar->new ({count => $number_of_entries ,name => 'Sending'});
for(1..$number_of_entries)
{
usleep(650);
$progress->update($_);
}
The output:
[root@deepzdev test]# ./prg_bar.pl
Sending: 40% [======================================= *]
If you like to print some message and/or show an time estimate of the remaining time, here's another example:
#!/usr/bin/perl
$| = 1;
use Term::ProgressBar;
use Time::HiRes qw/usleep/;
my $number_of_entries = 10;
my $progress = Term::ProgressBar->new ({count => $number_of_entries ,name => 'Sending',ETA=>'linear'});
for(1..$number_of_entries)
{
sleep(5);
$progress->update($_);
$progress->message("\rSent $_ of $number_of_entries");
}
The output
Code:
[root@deepzdev test]# ./prg_bar.pl
Sent 1 of 10
Sent 2 of 10
Sending: 20% [=================== ]0m40s Left
We all write programs to run on the terminal to do jobs that take time, like processing logs, sending out newsletter, etc. To know the progress of our task we print some messages to know our program is running well and doing its job. But wouldn't it be nice if we could add a progress bar to keep track of the completion of the task like wget, curl, etc.
Here's how we can do it in Perl, we'll use the perl module Term::ProgressBar to build and update the progress bar.
The Code
A very simple example:
Code: Perl
#!/usr/bin/perl
$| = 1;
use Term::ProgressBar;
use Time::HiRes qw/usleep/;
my $number_of_entries = 2000;
my $progress = Term::ProgressBar->new ({count => $number_of_entries ,name => 'Sending'});
for(1..$number_of_entries)
{
usleep(650);
$progress->update($_);
}
The output:
[root@deepzdev test]# ./prg_bar.pl
Sending: 40% [======================================= *]
If you like to print some message and/or show an time estimate of the remaining time, here's another example:
#!/usr/bin/perl
$| = 1;
use Term::ProgressBar;
use Time::HiRes qw/usleep/;
my $number_of_entries = 10;
my $progress = Term::ProgressBar->new ({count => $number_of_entries ,name => 'Sending',ETA=>'linear'});
for(1..$number_of_entries)
{
sleep(5);
$progress->update($_);
$progress->message("\rSent $_ of $number_of_entries");
}
The output
Code:
[root@deepzdev test]# ./prg_bar.pl
Sent 1 of 10
Sent 2 of 10
Sending: 20% [=================== ]0m40s Left
Comments
Post a Comment