root/trunk/lib/whatbot/Progress.pm

Revision 192, 2.5 KB (checked in by oz, 6 months ago)

Fix indirect new.

  • Property svn:executable set to *
Line 
1###########################################################################
2# whatbot/Progress.pm
3###########################################################################
4# progress bar for whatbot
5###########################################################################
6# the whatbot project - http://www.whatbot.org
7###########################################################################
8
9use MooseX::Declare;
10
11class whatbot::Progress {
12    has 'max'              => ( is => 'rw', isa => 'Int' );
13    has 'restrict_updates' => ( is => 'rw', isa => 'Int' );
14    has 'show_count'       => ( is => 'rw', isa => 'Int' );
15
16    method update ( Int $current ) {
17        return if ( $self->restrict_updates and $current % $self->restrict_updates != 0 );
18        return unless ( $self->max and $self->max > 0 );
19
20        my $pct = int( ( $current / $self->max ) * 100 );
21        my $line = '[';
22        for ( my $c = 0; $c < int($pct * 0.7); $c++ ) {
23            $line .= '=';
24        }
25        for ( my $c = 0; $c < (65 - int($pct * 0.65)); $c++ ) {
26            $line .= '-';
27        }
28        $line .= '] ' . $pct . '% ';
29        if ( $self->show_count ) {
30            $line .= $current . '/' . $self->max;
31        }
32        for ( my $c = 0; $c < ( 80 - length($line) ); $c++ ) {
33            $line .= ' ';
34        }
35        $line .= "\r";
36        print $line;
37    }
38
39    method finish {
40        $self->restrict_updates(0);
41        $self->update( $self->max );
42        print "\n";
43    }
44}
45
461;
47
48=pod
49
50=head1 NAME
51
52whatbot::Progress - Provides a basic progress meter
53
54=head1 SYNOPSIS
55
56 use whatbot::Progress;
57 
58 my $progress = whatbot::Progress->new(
59    'restrict_updates' => 10,
60    'max'              => 100,
61    'show_count'       => 1
62 );
63 for ( my $i = 0; $i <= 100; $i++ ) {
64     $progress->update($i);
65 }
66 $progress->finish();
67
68=head1 DESCRIPTION
69
70whatbot::Progress provides a simple command line progress bar without any
71dependencies beyond what whatbot requires.
72
73=head1 PUBLIC ACCESSORS
74
75=over 4
76
77=item restrict_updates
78
79Divisor of the current progress where the progress bar should be updated.
80Leaving this undefined will cause the progress bar to update every time
81'update' is called, otherwise, will only update per multiple.
82
83=item max
84
85Value of the 100% mark
86
87=item show_count
88
89Display the current count at the end of the progress bar. This updates
90depending on the value of
91
92=back
93
94=head1 METHODS
95
96=over 4
97
98=item update
99
100Update the progress bar with the given value
101
102=back
103
104=head1 LICENSE/COPYRIGHT
105
106Be excellent to each other and party on, dudes.
107
108=cut
Note: See TracBrowser for help on using the browser.