line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Carmel::ProgressBar; |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
27
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
5
|
use Class::Tiny qw( quiet width total _prev ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1030
|
use POSIX qw(ceil); |
|
1
|
|
|
|
|
5518
|
|
|
1
|
|
|
|
|
5
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
1203
|
use parent qw(Exporter); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw(progress); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub progress { |
12
|
0
|
|
|
0
|
0
|
|
my($args, $code) = @_; |
13
|
|
|
|
|
|
|
|
14
|
0
|
0
|
|
|
|
|
return unless @$args; |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
my $class = __PACKAGE__; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
my $self = $class->new( |
19
|
|
|
|
|
|
|
width => 40, |
20
|
|
|
|
|
|
|
total => scalar(@$args), |
21
|
|
|
|
|
|
|
quiet => !-t STDOUT, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
|
local $| = 1 |
25
|
|
|
|
|
|
|
unless $self->quiet; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
$self->update(0); |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
for my $i (0..$#$args) { |
30
|
0
|
|
|
|
|
|
$code->($args->[$i]); |
31
|
0
|
|
|
|
|
|
$self->update($i+1); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
$self->clear; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub update { |
38
|
0
|
|
|
0
|
0
|
|
my($self, $count) = @_; |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
|
return if $self->quiet; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $width = $self->width; |
43
|
0
|
|
|
|
|
|
my $done = ceil($count * $width / $self->total); |
44
|
0
|
0
|
|
|
|
|
my $head = $width == $done ? 0 : 1; |
45
|
0
|
|
|
|
|
|
my $remain = ($width - $done - $head); |
46
|
0
|
|
|
|
|
|
my $len = length $self->total; |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $line = sprintf "[%s%s%s] %${len}d/%d", |
49
|
|
|
|
|
|
|
("=" x $done), (">" x $head), (" " x $remain), |
50
|
|
|
|
|
|
|
$count, $self->total; |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
0
|
|
|
|
return if $self->_prev && $line eq $self->_prev; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
print "\r", $line; |
55
|
0
|
|
|
|
|
|
$self->_prev($line); |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub clear { |
61
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
62
|
0
|
|
|
|
|
|
print "\r", " " x ($self->width + 2*length($self->total) + 4), "\r"; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |