1 # This program is free software; you can redistribute it and/or
2 # modify it under the same terms as Perl itself.
3 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
5 package Term::ProgressBar::IO;
9 Term::ProgressBar::IO -- Display a progress bar while reading from a seekable filehandle
13 my $pb = Term::ProgressBar::IO->new($fh);
22 Displays a progress bar using L<Term::ProgressBar> which corresponds
23 to reading from a filehandle.
25 This module inherits from L<Term::ProgressBar> and has all of its
36 use vars qw($VERSION $DEBUG);
38 use parent qw(Term::ProgressBar);
44 $DEBUG = 0 unless defined $DEBUG;
51 Create and return a new Term::ProgressBar::IO instance.
61 A valid filehandle or item count. L<IO::Uncompress> filehandles are
62 also properly handled.
66 All other arguments are documented in L<Term::ProgressBar>
81 sprintf("Term::ProgressBar::IO::new We don't handle this many arguments: %d",
86 if ( UNIVERSAL::isa ($_[0], 'HASH') ) {
87 ($count) = @{$_[0]}{qw(count)};
92 if (ref($count) and $count->can("seek")) {
93 $self->{__filehandle} = $count;
94 $count = $self->__determine_max();
96 $config{count} = $count;
97 $self->SUPER::init(\%config);
102 Automatically update the progress bar based on the position of the
103 filehandle given at construction time.
113 Current progress point; this defaults to the current position of the
114 filehandle. [You probably don't actually want to ever give this.]
124 my $count = $self->__determine_count();
125 $self->SUPER::update(scalar @_? @_ : $count);
128 sub __determine_max {
130 # is this an IO::Uncompress handle?
132 if ($self->{__filehandle}->can('getHeaderInfo')) {
133 $self->{__filehandle} = *$self->{__filehandle}{FH};
136 my $cur_pos = $self->{__filehandle}->tell;
137 $self->{__filehandle}->seek(0,SEEK_END);
138 $max = $self->{__filehandle}->tell;
139 $self->{__filehandle}->seek($cur_pos,SEEK_SET);
144 sub __determine_count {
148 $count = $self->{__filehandle}->tell;