Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

subsecond accuracy

  • Loading branch information...
commit c65f0fe2aa43ae5f79a4bb187005df290a895645 1 parent 14ceff4
@rjbs authored
Showing with 13 additions and 7 deletions.
  1. +13 −7 lib/Acme/ProgressBar.pm
View
20 lib/Acme/ProgressBar.pm
@@ -3,6 +3,8 @@ package Acme::ProgressBar;
use strict;
use warnings;
+use Time::HiRes ();
+
=head1 NAME
Acme::ProgressBar - a simple progress bar for the patient
@@ -28,13 +30,13 @@ our @EXPORT = qw(progress); ## no critic Export
sub progress(&) { ## no critic Prototype
my ($code) = @_;
local $| = 1; ## no critic
- _overprint(_message(0,10,0));
- my $begun = time;
+ _overprint(_message(0,10,undef));
+ my $begun = Time::HiRes::time;
$code->();
- my $total = time - $begun;
+ my $total = Time::HiRes::time - $begun;
for (1 .. 9) {
_overprint(_message($_,10,$total));
- sleep $total;
+ Time::HiRes::sleep($total);
}
_overprint(_message(10,10,$total));
print "\n";
@@ -47,9 +49,13 @@ sub _message {
. q{=} x $iteration
. q{ } x ($total - $iteration)
. '] ';
- $message .= $time
- ? ((($total - $iteration) * $time) . 's remaining' . q{ } x 25)
- : '(calculating time remaining)';
+
+ if (defined $time) {
+ $message .= sprintf '%0.0fs remaining%25s',
+ (($total - $iteration) * $time), q{ };
+ } else {
+ $message .= '(calculating time remaining)';
+ }
}
sub _overprint {
Please sign in to comment.
Something went wrong with that request. Please try again.