line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Term::ProgressBar::IO; |
2
|
1
|
|
|
1
|
|
485
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '2.21'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
8
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
9
|
|
|
|
|
|
|
# Copyright 2014 by Don Armstrong . |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Term::ProgressBar::IO -- Display a progress bar while reading from a seekable filehandle |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $pb = Term::ProgressBar::IO->new($fh); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
while (<$fh>) { |
20
|
|
|
|
|
|
|
# do something |
21
|
|
|
|
|
|
|
$pb->update(); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Displays a progress bar using L which corresponds |
27
|
|
|
|
|
|
|
to reading from a filehandle. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
This module inherits from L and has all of its |
30
|
|
|
|
|
|
|
options. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 BUGS |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
None known. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=cut |
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
1
|
|
5
|
use parent qw(Term::ProgressBar); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
39
|
1
|
|
|
1
|
|
46
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
40
|
1
|
|
|
1
|
|
5
|
use Fcntl qw(:seek); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
391
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 METHODS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 new |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Create and return a new Term::ProgressBar::IO instance. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=over |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item ARGUMENTS |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=over |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item count |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
A valid filehandle or item count. L filehandles are |
57
|
|
|
|
|
|
|
also properly handled. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item OTHER ARGUMENTS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
All other arguments are documented in L |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=back |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=back |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub init { |
70
|
1
|
|
|
1
|
0
|
943
|
my $self = shift; |
71
|
1
|
|
|
|
|
9
|
my $count; |
72
|
1
|
50
|
|
|
|
3
|
if (@_==2) { |
73
|
0
|
|
|
|
|
0
|
$count = $_[1]; |
74
|
|
|
|
|
|
|
} else { |
75
|
1
|
50
|
|
|
|
3
|
croak |
76
|
|
|
|
|
|
|
sprintf("Term::ProgressBar::IO::new We don't handle this many arguments: %d", |
77
|
|
|
|
|
|
|
scalar @_) |
78
|
|
|
|
|
|
|
if @_ != 1; |
79
|
|
|
|
|
|
|
} |
80
|
1
|
|
|
|
|
10
|
my %config; |
81
|
1
|
50
|
|
|
|
7
|
if ( UNIVERSAL::isa ($_[0], 'HASH') ) { |
82
|
0
|
|
|
|
|
0
|
($count) = @{$_[0]}{qw(count)}; |
|
0
|
|
|
|
|
0
|
|
83
|
0
|
|
|
|
|
0
|
%config = %{$_[0]}; |
|
0
|
|
|
|
|
0
|
|
84
|
|
|
|
|
|
|
} else { |
85
|
1
|
|
|
|
|
2
|
($count) = @_; |
86
|
|
|
|
|
|
|
} |
87
|
1
|
50
|
33
|
|
|
14
|
if (ref($count) and $count->can("seek")) { |
88
|
1
|
|
|
|
|
4
|
$self->{__filehandle} = $count; |
89
|
1
|
|
|
|
|
3
|
$count = $self->__determine_max(); |
90
|
|
|
|
|
|
|
} |
91
|
1
|
|
|
|
|
2
|
$config{count} = $count; |
92
|
1
|
|
|
|
|
10
|
$self->SUPER::init(\%config); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 update |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Automatically update the progress bar based on the position of the |
98
|
|
|
|
|
|
|
filehandle given at construction time. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=over |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item ARGUMENTS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=over |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item so_far |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Current progress point; this defaults to the current position of the |
109
|
|
|
|
|
|
|
filehandle. [You probably don't actually want to ever give this.] |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=back |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=back |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub update { |
118
|
11
|
|
|
11
|
1
|
7458
|
my $self = shift; |
119
|
11
|
|
|
|
|
23
|
my $count = $self->__determine_count(); |
120
|
11
|
100
|
|
|
|
58
|
$self->SUPER::update(scalar @_? @_ : $count); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub __determine_max { |
124
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
125
|
|
|
|
|
|
|
# is this an IO::Uncompress handle? |
126
|
1
|
|
|
|
|
2
|
my $max = 0; |
127
|
1
|
50
|
|
|
|
5
|
if ($self->{__filehandle}->can('getHeaderInfo')) { |
128
|
0
|
|
|
|
|
0
|
$self->{__filehandle} = *$self->{__filehandle}{FH}; |
129
|
|
|
|
|
|
|
} |
130
|
1
|
|
|
|
|
1
|
eval { |
131
|
1
|
|
|
|
|
6
|
my $cur_pos = $self->{__filehandle}->tell; |
132
|
1
|
|
|
|
|
8
|
$self->{__filehandle}->seek(0,SEEK_END); |
133
|
1
|
|
|
|
|
8
|
$max = $self->{__filehandle}->tell; |
134
|
1
|
|
|
|
|
4
|
$self->{__filehandle}->seek($cur_pos,SEEK_SET); |
135
|
|
|
|
|
|
|
}; |
136
|
1
|
|
|
|
|
5
|
return $max; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub __determine_count { |
140
|
11
|
|
|
11
|
|
16
|
my $self = shift; |
141
|
11
|
|
|
|
|
13
|
my $count = 0; |
142
|
11
|
|
|
|
|
15
|
eval { |
143
|
11
|
|
|
|
|
34
|
$count = $self->{__filehandle}->tell; |
144
|
|
|
|
|
|
|
}; |
145
|
11
|
|
|
|
|
59
|
return $count; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
__END__ |