| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Acme::ProgressBar 1.129; | ||||||||||
| 4 | # ABSTRACT: a simple progress bar for the patient | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Time::HiRes (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | 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<progress>. 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 | ▶ | my ($code) = @_; | ||||||||
|
Subroutine
progress: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 40 | 2 | local $| = 1; ## no critic | |||||||||
| 41 | 2 | _overprint(_message(0,10,undef)); | |||||||||
| 42 | |||||||||||
| 43 | 2 | my $begun = Time::HiRes::time; | |||||||||
| 44 | 2 | $code->(); | |||||||||
| 45 | 2 | my $total = Time::HiRes::time - $begun; | |||||||||
| 46 | |||||||||||
| 47 | 2 | for (1 .. 9) { | |||||||||
| 48 | 18 | _overprint(_message($_,10,$total)); | |||||||||
| 49 | 18 | Time::HiRes::sleep($total); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 2 | _overprint(_message(10,10,$total)); | |||||||||
| 53 | 2 | print "\n"; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | sub _message { | ||||||||||
| 57 | 22 | ▶ | my ($iteration, $total, $time) = @_; | ||||||||
|
Subroutine
_message: called CC=2 SCAR=6.9
| |||||||||||
| 58 | 22 | my $message = 'Progress: [' | |||||||||
| 59 | . q{=} x $iteration | ||||||||||
| 60 | . q{ } x ($total - $iteration) | ||||||||||
| 61 | . '] '; | ||||||||||
| 62 | |||||||||||
| 63 | 22 | ▶ | if (defined $time) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 20 | $message .= sprintf '%0.0fs remaining%25s', | |||||||||
| 65 | (($total - $iteration) * $time), q{ }; | ||||||||||
| 66 | } else { | ||||||||||
| 67 | 2 | $message .= '(calculating time remaining)'; | |||||||||
| 68 | } | ||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | sub _overprint { | ||||||||||
| 72 | 22 | ▶ | my ($message) = @_; | ||||||||
|
Subroutine
_overprint: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 22 | 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<Term::ProgressBar>, L<Term::ProgressBar::Simple>, | ||||||||||
| 84 | #pod L<Progress::Any::Output::TermProgressBarColor>, L<Smart::Comments> | ||||||||||
| 85 | #pod | ||||||||||
| 86 | #pod =cut | ||||||||||
| 87 | |||||||||||
| 88 | 1 | "48102931829 minutes remaining"; | |||||||||
| 89 | |||||||||||
| 90 | __END__ | ||||||||||