1 # This module is part of IO, and is released
2 # under the terms of the GPL version 3, or any later version at your option. See the
3 # file README and COPYING for more information.
4 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
6 package Term::ProgressBar::IO;
10 Term::ProgressBar::IO -- Display a progress bar while reading from a seekable filehandle
14 my $pb = Term::ProgressBar::IO->new($fh);
23 Displays a progress bar using L<Term::ProgressBar> which corresponds
24 to reading from a filehandle.
26 This module inherits from L<Term::ProgressBar> and has all of its
37 use vars qw($VERSION $DEBUG);
39 use parent qw(Term::ProgressBar);
45 $DEBUG = 0 unless defined $DEBUG;
52 Create and return a new Term::ProgressBar::IO instance.
62 A valid filehandle or item count. L<IO::Uncompress> filehandles are
63 also properly handled.
67 All other arguments are documented in L<Term::ProgressBar>
82 sprintf("Term::ProgressBar::IO::new We don't handle this many arguments: %d",
87 if ( UNIVERSAL::isa ($_[0], 'HASH') ) {
88 ($count) = @{$_[0]}{qw(count)};
93 if (ref($count) and $count->can("seek")) {
94 $self->{__filehandle} = $count;
95 $count = $self->__determine_max();
97 $config{count} = $count;
98 $self->SUPER::init(\%config);
103 Automatically update the progress bar based on the position of the
104 filehandle given at construction time.
114 Current progress point; this defaults to the current position of the
115 filehandle. [You probably don't actually want to ever give this.]
125 my $count = $self->__determine_count();
126 $self->SUPER::update(scalar @_? @_ : $count);
129 sub __determine_max {
131 # is this an IO::Uncompress handle?
133 if ($self->{__filehandle}->can('getHeaderInfo')) {
134 $self->{__filehandle} = *$self->{__filehandle}{FH};
137 my $cur_pos = $self->{__filehandle}->tell;
138 $self->{__filehandle}->seek(0,SEEK_END);
139 $max = $self->{__filehandle}->tell;
140 $self->{__filehandle}->seek($cur_pos,SEEK_SET);
145 sub __determine_count {
149 $count = $self->{__filehandle}->tell;