Skip to content
Newer
Older
100644 112 lines (76 sloc) 2.08 KB
d467186 @rjbs initial import
authored Aug 9, 2005
1 package Acme::ProgressBar;
2
3 use strict;
4 use warnings;
5
c65f0fe @rjbs subsecond accuracy
authored Nov 28, 2010
6 use Time::HiRes ();
7
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
8 =head1 NAME
9
e9e0f5e @rjbs r27917@minion101: rjbs | 2006-11-13 10:56:18 -0500
authored Nov 13, 2006
10 Acme::ProgressBar - a simple progress bar for the patient
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
11
12 =head1 VERSION
13
14ceff4 @rjbs bump ver
authored Jan 16, 2009
14 version 1.125
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
15
16 =cut
17
14ceff4 @rjbs bump ver
authored Jan 16, 2009
18 our $VERSION = '1.125';
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
19
20 =head1 SYNOPSIS
21
22 use Acme::ProgressBar;
23 progress { do_something_slow };
24
25 =cut
d467186 @rjbs initial import
authored Aug 9, 2005
26
27 use base qw(Exporter);
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
28 our @EXPORT = qw(progress); ## no critic Export
d467186 @rjbs initial import
authored Aug 9, 2005
29
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
30 sub progress(&) { ## no critic Prototype
d467186 @rjbs initial import
authored Aug 9, 2005
31 my ($code) = @_;
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
32 local $| = 1; ## no critic
c65f0fe @rjbs subsecond accuracy
authored Nov 28, 2010
33 _overprint(_message(0,10,undef));
34 my $begun = Time::HiRes::time;
d467186 @rjbs initial import
authored Aug 9, 2005
35 $code->();
c65f0fe @rjbs subsecond accuracy
authored Nov 28, 2010
36 my $total = Time::HiRes::time - $begun;
d467186 @rjbs initial import
authored Aug 9, 2005
37 for (1 .. 9) {
38 _overprint(_message($_,10,$total));
c65f0fe @rjbs subsecond accuracy
authored Nov 28, 2010
39 Time::HiRes::sleep($total);
d467186 @rjbs initial import
authored Aug 9, 2005
40 }
41 _overprint(_message(10,10,$total));
42 print "\n";
43 }
44
45 sub _message {
46 my ($iteration, $total, $time) = @_;
2045cf5 @rjbs r27916@minion101: rjbs | 2006-11-13 10:54:41 -0500
authored Nov 13, 2006
47 my $message
48 = 'Progress: ['
49 . q{=} x $iteration
50 . q{ } x ($total - $iteration)
51 . '] ';
c65f0fe @rjbs subsecond accuracy
authored Nov 28, 2010
52
53 if (defined $time) {
54 $message .= sprintf '%0.0fs remaining%25s',
55 (($total - $iteration) * $time), q{ };
56 } else {
57 $message .= '(calculating time remaining)';
58 }
d467186 @rjbs initial import
authored Aug 9, 2005
59 }
60
61 sub _overprint {
62 my ($message) = @_;
63 print $message, "\r";
64 }
65
66 "48102931829 minutes remaining";
67
68 __END__
69
70
71 =head1 DESCRIPTION
72
73 Acme::ProgressBar provides a simple solution designed to provide accurate
74 countdowns. No progress bar object needs to be created, and all the
75 calculation of progress through total time required is handled by the module
76 itself.
77
78 =head1 FUNCTIONS
79
80 =head2 C<< progress >>
81
82 progress { unlink $_ for <*> };
83 progress { while (<>) { $ua->get($_) } };
84 progress { sleep 5; }
85
86 There is only one function exported by default, C<progress>. This function
87 takes a coderef as its lone argument. It will execute this code and display a
88 simple progress bar indicating the time required for ten iterations through the
89 code.
90
91 =head1 TODO
92
93 =over
94
95 =item *
96
97 allow other divisions of time (other than ten)
98
99 =back
100
101 =head1 AUTHOR
102
103 Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
104
105 Thanks to Steve Lidie for pointing out a stupid error in 1.001: I couldn't
106 count to ten, and he could.
107
108 =head1 COPYRIGHT
109
110 Copyright (C) 2004, Ricardo SIGNES. Available under the same terms as Perl
111 itself.
Something went wrong with that request. Please try again.