File Coverage

blib/lib/Acme/ProgressBar.pm
Criterion Covered Total %
statement 30 30 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 40 40 100.0


line stmt bran cond sub pod time code
1 1     1   67628 use strict;
  1         12  
  1         32  
2 1     1   6 use warnings;
  1         2  
  1         45  
3             package Acme::ProgressBar 1.130;
4             # ABSTRACT: a simple progress bar for the patient
5              
6 1     1   510 use Time::HiRes ();
  1         1365  
  1         32  
7              
8             #pod =head1 SYNOPSIS
9             #pod
10             #pod use Acme::ProgressBar;
11             #pod progress { do_something_slow };
12             #pod
13             #pod =cut
14              
15 1     1   8 use base qw(Exporter);
  1         2  
  1         434  
16             our @EXPORT = qw(progress); ## no critic Export
17              
18             #pod =head1 DESCRIPTION
19             #pod
20             #pod Acme::ProgressBar provides a simple solution designed to provide accurate
21             #pod countdowns. No progress bar object needs to be created, and all the
22             #pod calculation of progress through total time required is handled by the module
23             #pod itself.
24             #pod
25             #pod =func progress
26             #pod
27             #pod progress { unlink $_ for <*> };
28             #pod progress { while (<>) { $ua->get($_) } };
29             #pod progress { sleep 5; }
30             #pod
31             #pod There is only one function exported by default, C. This function
32             #pod takes a coderef as its lone argument. It will execute this code and display a
33             #pod simple progress bar indicating the time required for ten iterations through the
34             #pod code.
35             #pod
36             #pod =cut
37              
38             sub progress(&) { ## no critic Prototype
39 2     2 1 85 my ($code) = @_;
40 2         11 local $| = 1; ## no critic
41 2         8 _overprint(_message(0,10,undef));
42              
43 2         12 my $begun = Time::HiRes::time;
44 2         7 $code->();
45 2         1000140 my $total = Time::HiRes::time - $begun;
46              
47 2         11 for (1 .. 9) {
48 18         139 _overprint(_message($_,10,$total));
49 18         9004141 Time::HiRes::sleep($total);
50             }
51              
52 2         20 _overprint(_message(10,10,$total));
53 2         39 print "\n";
54             }
55              
56             sub _message {
57 22     22   82 my ($iteration, $total, $time) = @_;
58 22         138 my $message = 'Progress: ['
59             . q{=} x $iteration
60             . q{ } x ($total - $iteration)
61             . '] ';
62              
63 22 100       83 if (defined $time) {
64 20         350 $message .= sprintf '%0.0fs remaining%25s',
65             (($total - $iteration) * $time), q{ };
66             } else {
67 2         9 $message .= '(calculating time remaining)';
68             }
69             }
70              
71             sub _overprint {
72 22     22   51 my ($message) = @_;
73 22         650 print $message, "\r";
74             }
75              
76             #pod =head1 TODO
77             #pod
78             #pod =for :list
79             #pod * allow other divisions of time (other than ten)
80             #pod
81             #pod =head1 SEE ALSO
82             #pod
83             #pod L, L,
84             #pod L, L
85             #pod
86             #pod =cut
87              
88             "48102931829 minutes remaining";
89              
90             __END__