| 1 | package Term::ProgressBar::IO; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | 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 <don@donarmstrong.com>. | ||||||||||||||||||||||||||||
| 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<Term::ProgressBar> which corresponds | ||||||||||||||||||||||||||||
| 27 | to reading from a filehandle. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | This module inherits from L<Term::ProgressBar> and has all of its | ||||||||||||||||||||||||||||
| 30 | options. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 BUGS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | None known. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use parent qw(Term::ProgressBar); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use Fcntl qw(:seek); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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<IO::Uncompress> filehandles are | ||||||||||||||||||||||||||||
| 57 | also properly handled. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =item OTHER ARGUMENTS | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | All other arguments are documented in L<Term::ProgressBar> | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =back | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =back | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub init { | ||||||||||||||||||||||||||||
| 70 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 1 | my $count; | |||||||||||||||||||||||||||
| 72 | 1 | ▶ | if (@_==2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $count = $_[1]; | |||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | croak | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | sprintf("Term::ProgressBar::IO::new We don't handle this many arguments: %d", | ||||||||||||||||||||||||||||
| 77 | scalar @_) | ||||||||||||||||||||||||||||
| 78 | if @_ != 1; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 1 | my %config; | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | if ( UNIVERSAL::isa ($_[0], 'HASH') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ($count) = @{$_[0]}{qw(count)}; | |||||||||||||||||||||||||||
| 83 | 0 | %config = %{$_[0]}; | |||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||
| 85 | 1 | ($count) = @_; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 1 | ▶ | if (ref($count) and $count->can("seek")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $count and $count->can("seek")
MC/DC0%0/2
ref $count and $count->can("seek")
ref $count
$count->can("seek")
Truth table33%1/3
A and B
A= ref $countB= $count->can("seek")
| |||||||||||||||||||||||||||||
| 88 | 1 | $self->{__filehandle} = $count; | |||||||||||||||||||||||||||
| 89 | 1 | $count = $self->__determine_max(); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1 | $config{count} = $count; | |||||||||||||||||||||||||||
| 92 | 1 | $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 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 11 | my $count = $self->__determine_count(); | |||||||||||||||||||||||||||
| 120 | 11 | ▶ | $self->SUPER::update(scalar @_? @_ : $count); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub __determine_max { | ||||||||||||||||||||||||||||
| 124 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__determine_max: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 125 | # is this an IO::Uncompress handle? | ||||||||||||||||||||||||||||
| 126 | 1 | my $max = 0; | |||||||||||||||||||||||||||
| 127 | 1 | ▶ | if ($self->{__filehandle}->can('getHeaderInfo')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $self->{__filehandle} = *$self->{__filehandle}{FH}; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 1 | eval { | |||||||||||||||||||||||||||
| 131 | 1 | my $cur_pos = $self->{__filehandle}->tell; | |||||||||||||||||||||||||||
| 132 | 1 | $self->{__filehandle}->seek(0,SEEK_END); | |||||||||||||||||||||||||||
| 133 | 1 | $max = $self->{__filehandle}->tell; | |||||||||||||||||||||||||||
| 134 | 1 | $self->{__filehandle}->seek($cur_pos,SEEK_SET); | |||||||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||||||||
| 136 | 1 | return $max; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub __determine_count { | ||||||||||||||||||||||||||||
| 140 | 11 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__determine_count: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 141 | 11 | my $count = 0; | |||||||||||||||||||||||||||
| 142 | 11 | eval { | |||||||||||||||||||||||||||
| 143 | 11 | $count = $self->{__filehandle}->tell; | |||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||
| 145 | 11 | return $count; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||