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