line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::ProgressBar; ## Produces a simple progress bar |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
400
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
5
|
1
|
|
|
1
|
|
2
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
6
|
1
|
|
|
1
|
|
14
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
572
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION='0.04'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# It is an OO class to produce a simple progress bar which can be used at |
13
|
|
|
|
|
|
|
# a shell. It does not use curses and has no large dependencies. |
14
|
|
|
|
|
|
|
# |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# SYNOPSIS |
17
|
|
|
|
|
|
|
# ======== |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
# use String::ProgressBar; |
20
|
|
|
|
|
|
|
# my $pr = String::ProgressBar->new( max => 30 ); |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
# $pr->update( 10 ); # step 10 of 30 |
23
|
|
|
|
|
|
|
# $pr->write; |
24
|
|
|
|
|
|
|
# |
25
|
|
|
|
|
|
|
# # shows that: |
26
|
|
|
|
|
|
|
# # [======= ] 33% [10/30] |
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# # If you want to print it by yourself: |
29
|
|
|
|
|
|
|
# |
30
|
|
|
|
|
|
|
# use String::ProgressBar; |
31
|
|
|
|
|
|
|
# my $pr = String::ProgressBar->new( max => 30 ); |
32
|
|
|
|
|
|
|
# |
33
|
|
|
|
|
|
|
# print $pr->update( 10 )->string()."\r"; |
34
|
|
|
|
|
|
|
# |
35
|
|
|
|
|
|
|
# |
36
|
|
|
|
|
|
|
# |
37
|
|
|
|
|
|
|
# |
38
|
|
|
|
|
|
|
# The progress bar has a fix matrix and look liket that: |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
# first [====================] 100% [30/30] |
41
|
|
|
|
|
|
|
# |
42
|
|
|
|
|
|
|
# |
43
|
|
|
|
|
|
|
# |
44
|
|
|
|
|
|
|
# LICENSE |
45
|
|
|
|
|
|
|
# ======= |
46
|
|
|
|
|
|
|
# You can redistribute it and/or modify it under the conditions of LGPL. |
47
|
|
|
|
|
|
|
# |
48
|
|
|
|
|
|
|
# AUTHOR |
49
|
|
|
|
|
|
|
# ====== |
50
|
|
|
|
|
|
|
# Andreas Hernitscheck ahernit(AT)cpan.org |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Constructor |
56
|
|
|
|
|
|
|
# |
57
|
|
|
|
|
|
|
# It can take several key value pairs (here you see also the default values): |
58
|
|
|
|
|
|
|
# |
59
|
|
|
|
|
|
|
# length => 20 # length of the bar |
60
|
|
|
|
|
|
|
# border_left => '[' # left bracked of the bar |
61
|
|
|
|
|
|
|
# border_right => ']', # right bracked of the bar |
62
|
|
|
|
|
|
|
# bar => '=', # used element of the bar |
63
|
|
|
|
|
|
|
# show_rotation => 0, # if it should show a rotating element |
64
|
|
|
|
|
|
|
# show_percent => 1, # show percent |
65
|
|
|
|
|
|
|
# show_amount => 1, # show value/max |
66
|
|
|
|
|
|
|
# text => '', # text before text bar. If empty, starts on the very left end |
67
|
|
|
|
|
|
|
# info => '', # text after the bar |
68
|
|
|
|
|
|
|
# print_return => 0, # whether it should return after last value with new line |
69
|
|
|
|
|
|
|
# text_length => 14, # length of the text before the bar |
70
|
|
|
|
|
|
|
# allow_overflow => 0, # allow values to exceed 100% |
71
|
|
|
|
|
|
|
# bar_overflow => 0, # allow bar to exceed normal width when value is over 100% |
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
sub new { # $object ( max => $int ) |
75
|
0
|
|
|
0
|
1
|
|
my $pkg = shift; |
76
|
0
|
|
|
|
|
|
my $self = bless {}, $pkg; |
77
|
0
|
|
|
|
|
|
my $v={@_}; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# default values |
80
|
0
|
|
|
|
|
|
my $def = { |
81
|
|
|
|
|
|
|
value => 0, |
82
|
|
|
|
|
|
|
length => 20, |
83
|
|
|
|
|
|
|
border_left => '[', |
84
|
|
|
|
|
|
|
border_right => ']', |
85
|
|
|
|
|
|
|
bar => '=', |
86
|
|
|
|
|
|
|
show_rotation => 0, |
87
|
|
|
|
|
|
|
show_percent => 1, |
88
|
|
|
|
|
|
|
show_amount => 1, |
89
|
|
|
|
|
|
|
text => '', |
90
|
|
|
|
|
|
|
info => '', |
91
|
|
|
|
|
|
|
print_return => 0, |
92
|
|
|
|
|
|
|
text_length => 14, |
93
|
|
|
|
|
|
|
allow_overflow => 0, |
94
|
|
|
|
|
|
|
bar_overflow => 0, |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# asign default values |
98
|
0
|
|
|
|
|
|
foreach my $k (keys %$def){ |
99
|
0
|
|
|
|
|
|
$self->{ $k } = $def->{ $k }; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if ( not $self->{"text"} ){ |
103
|
0
|
|
|
|
|
|
$self->{"text"} = 0; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
foreach my $k (keys %$v){ |
108
|
0
|
|
|
|
|
|
$self->{ $k } = $v->{ $k }; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my @req = qw( max ); |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
foreach my $r (@req){ |
116
|
0
|
0
|
|
|
|
|
if ( ! $self->{$r} ){ |
117
|
0
|
|
|
|
|
|
croak "\'$r\' required in constructor"; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $self; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# updates the bar with a new value |
128
|
|
|
|
|
|
|
# and returns the object itself. |
129
|
|
|
|
|
|
|
sub update { |
130
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
131
|
0
|
|
|
|
|
|
my $value = shift; |
132
|
|
|
|
|
|
|
|
133
|
0
|
0
|
0
|
|
|
|
if ( !$self->{'allow_overflow'} && $value > $self->{'max'} ){ |
134
|
0
|
|
|
|
|
|
$value = $self->{'max'}; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
$self->{'value'} = $value; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
return $self; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# updates text (before bar) with a new value |
144
|
|
|
|
|
|
|
# and returns the object itself. |
145
|
|
|
|
|
|
|
sub text { # $object ($string) |
146
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
147
|
0
|
|
|
|
|
|
my $value = shift; |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
$self->{'text'} = $value; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
return $self; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# updates info (after bar) with a new value |
156
|
|
|
|
|
|
|
# and returns the object itself. |
157
|
|
|
|
|
|
|
sub info { # $object ($string) |
158
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
159
|
0
|
|
|
|
|
|
my $value = shift; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
$self->{'info_last'} = $self->{'info'}; |
162
|
0
|
|
|
|
|
|
$self->{'info'} = $value; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
return $self; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# Writes the bar to STDOUT. |
171
|
|
|
|
|
|
|
sub write { # void () |
172
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
173
|
0
|
|
|
|
|
|
my $bar = $self->string(); |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
print "$bar\r"; |
176
|
|
|
|
|
|
|
|
177
|
0
|
0
|
0
|
|
|
|
if ( $self->{'print_return'} && ($self->{'value'} == $self->{'max'}) ){ |
178
|
0
|
|
|
|
|
|
print "\n"; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# returns the bar as simple string, so you may write it by |
185
|
|
|
|
|
|
|
# yourself. |
186
|
|
|
|
|
|
|
sub string { # $string |
187
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
188
|
0
|
|
|
|
|
|
my $str; |
189
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
my $ratio = $self->{'value'} / $self->{'max'}; |
191
|
0
|
|
|
|
|
|
my $percent = int( $ratio * 100 ); |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
my $bar_ratio = $ratio; |
194
|
0
|
0
|
0
|
|
|
|
$bar_ratio = 1 if $bar_ratio > 1 && !$self->{'bar_overflow'}; |
195
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
my $bar = $self->{'bar'} x ( $bar_ratio * $self->{'length'} ); |
197
|
0
|
|
|
|
|
|
$bar .= " " x ($self->{'length'} - length($bar) ); |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
$bar = $self->{'border_left'} . $bar . $self->{'border_right'}; |
200
|
|
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
$str = "$bar"; |
202
|
|
|
|
|
|
|
|
203
|
0
|
0
|
|
|
|
|
if ( $self->{'show_percent'} ){ |
204
|
0
|
|
|
|
|
|
$str.=" ".sprintf("%3s",$percent)."%"; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
0
|
0
|
|
|
|
|
if ( $self->{'show_amount'} ){ |
208
|
0
|
|
|
|
|
|
$str.=" [".sprintf("%".length($self->{'max'})."s",$self->{'value'})."/".$self->{'max'}."]"; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
0
|
0
|
|
|
|
|
if ( $self->{'show_rotation'} ){ |
212
|
0
|
|
|
|
|
|
my $char = $self->_getRotationChar(); |
213
|
0
|
|
|
|
|
|
$str.=" [$char]"; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
0
|
0
|
0
|
|
|
|
if ( $self->{'info'} || $self->{'info_used'} ){ |
217
|
0
|
|
|
|
|
|
$str.=" ".sprintf("%-".length($self->{'info_last'})."s", $self->{'info'}); |
218
|
0
|
|
|
|
|
|
$self->{'info_used'} = 1; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
|
223
|
0
|
0
|
|
|
|
|
if ( $self->{'text'} ){ |
224
|
0
|
|
|
|
|
|
$str=sprintf("%-".$self->{'text_length'}."s", $self->{'text'})." $str"; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
return $str; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
# Returns a rotating slash. |
231
|
|
|
|
|
|
|
# With every call one step further |
232
|
|
|
|
|
|
|
sub _getRotationChar { |
233
|
0
|
|
|
0
|
|
|
my $self = shift; |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
my @matrix = qw( / - \ | ); |
236
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
$self->{rotation_counter} = ($self->{rotation_counter}+1) % (scalar(@matrix)-1); |
238
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
return $matrix[ $self->{rotation_counter} ]; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
1; |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
#################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 NAME |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
String::ProgressBar - Produces a simple progress bar |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=head1 SYNOPSIS |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
use String::ProgressBar; |
257
|
|
|
|
|
|
|
my $pr = String::ProgressBar->new( max => 30 ); |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
$pr->update( 10 ); # step 10 of 30 |
260
|
|
|
|
|
|
|
$pr->write; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
# shows that: |
263
|
|
|
|
|
|
|
# [======= ] 33% [10/30] |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
# If you want to print it by yourself: |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
use String::ProgressBar; |
268
|
|
|
|
|
|
|
my $pr = String::ProgressBar->new( max => 30 ); |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
print $pr->update( 10 )->string()."\r"; |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
The progress bar has a fix matrix and look liket that: |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
first [====================] 100% [30/30] |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=head1 DESCRIPTION |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
It is an OO class to produce a simple progress bar which can be used at |
286
|
|
|
|
|
|
|
a shell. It does not use curses and has no large dependencies. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 REQUIRES |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
L |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head1 METHODS |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=head2 new |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
my $object = $this->new(max => $int); |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
Constructor |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
It can take several key value pairs (here you see also the default values): |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
length => 20 # length of the bar |
307
|
|
|
|
|
|
|
border_left => '[' # left bracked of the bar |
308
|
|
|
|
|
|
|
border_right => ']', # right bracked of the bar |
309
|
|
|
|
|
|
|
bar => '=', # used element of the bar |
310
|
|
|
|
|
|
|
show_rotation => 0, # if it should show a rotating element |
311
|
|
|
|
|
|
|
show_percent => 1, # show percent |
312
|
|
|
|
|
|
|
show_amount => 1, # show value/max |
313
|
|
|
|
|
|
|
text => '', # text before text bar. If empty, starts on the very left end |
314
|
|
|
|
|
|
|
info => '', # text after the bar |
315
|
|
|
|
|
|
|
print_return => 0, # whether it should return after last value with new line |
316
|
|
|
|
|
|
|
text_length => 14, # length of the text before the bar |
317
|
|
|
|
|
|
|
allow_overflow => 0, # allow values to exceed 100% |
318
|
|
|
|
|
|
|
bar_overflow => 0, # allow bar to exceed normal width when value is over 100% |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=head2 info |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
my $object = $this->info($string); |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
updates info (after bar) with a new value |
328
|
|
|
|
|
|
|
and returns the object itself. |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=head2 string |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
my $string = $this->string(); |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
returns the bar as simple string, so you may write it by |
336
|
|
|
|
|
|
|
yourself. |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=head2 text |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
my $object = $this->text($string); |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
updates text (before bar) with a new value |
344
|
|
|
|
|
|
|
and returns the object itself. |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
=head2 update |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
$this->update(); |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
updates the bar with a new value |
352
|
|
|
|
|
|
|
and returns the object itself. |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
=head2 write |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
$this->write(); |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
Writes the bar to STDOUT. |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
=head1 AUTHOR |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
Andreas Hernitscheck ahernit(AT)cpan.org |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
=head1 LICENSE |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
You can redistribute it and/or modify it under the conditions of LGPL. |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
=cut |
375
|
|
|
|
|
|
|
|