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