line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Term::ProgressBar::IO; |
2
|
1
|
|
|
1
|
|
638
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '2.23'; |
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
|
|
|
|
|
6
|
|
39
|
1
|
|
|
1
|
|
46
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
40
|
1
|
|
|
1
|
|
5
|
use Fcntl qw(:seek); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
450
|
|
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
|
1010
|
my $self = shift; |
71
|
1
|
|
|
|
|
1
|
my $count; |
72
|
1
|
50
|
|
|
|
4
|
if (@_==2) { |
73
|
0
|
|
|
|
|
0
|
$count = $_[1]; |
74
|
|
|
|
|
|
|
} else { |
75
|
1
|
50
|
|
|
|
5
|
croak |
76
|
|
|
|
|
|
|
sprintf("Term::ProgressBar::IO::new We don't handle this many arguments: %d", |
77
|
|
|
|
|
|
|
scalar @_) |
78
|
|
|
|
|
|
|
if @_ != 1; |
79
|
|
|
|
|
|
|
} |
80
|
1
|
|
|
|
|
1
|
my %config; |
81
|
1
|
50
|
|
|
|
6
|
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
|
|
|
11
|
if (ref($count) and $count->can("seek")) { |
88
|
1
|
|
|
|
|
3
|
$self->{__filehandle} = $count; |
89
|
1
|
|
|
|
|
3
|
$count = $self->__determine_max(); |
90
|
|
|
|
|
|
|
} |
91
|
1
|
|
|
|
|
3
|
$config{count} = $count; |
92
|
1
|
|
|
|
|
5
|
$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
|
9051
|
my $self = shift; |
119
|
11
|
|
|
|
|
30
|
my $count = $self->__determine_count(); |
120
|
11
|
100
|
|
|
|
38
|
$self->SUPER::update(scalar @_? @_ : $count); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub __determine_max { |
124
|
1
|
|
|
1
|
|
1
|
my $self = shift; |
125
|
|
|
|
|
|
|
# is this an IO::Uncompress handle? |
126
|
1
|
|
|
|
|
2
|
my $max = 0; |
127
|
1
|
50
|
|
|
|
6
|
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
|
|
|
|
|
23
|
$max = $self->{__filehandle}->tell; |
134
|
1
|
|
|
|
|
7
|
$self->{__filehandle}->seek($cur_pos,SEEK_SET); |
135
|
|
|
|
|
|
|
}; |
136
|
1
|
|
|
|
|
10
|
return $max; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub __determine_count { |
140
|
11
|
|
|
11
|
|
14
|
my $self = shift; |
141
|
11
|
|
|
|
|
12
|
my $count = 0; |
142
|
11
|
|
|
|
|
13
|
eval { |
143
|
11
|
|
|
|
|
32
|
$count = $self->{__filehandle}->tell; |
144
|
|
|
|
|
|
|
}; |
145
|
11
|
|
|
|
|
57
|
return $count; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
__END__ |