script/tpb.pl untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 62 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 253 CC 14 SCAR 30.6
CC
14
Cov
0%
_prepare_io
42.0
_cat_raw
30.0
_cat_tpb
30.0
CRAP 21.3
1 #!/usr/bin/env perl
2  
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use Carp;
7 use Getopt::Long;
8 use Pod::Usage;
9 use version; our $VERSION = qv('0.0.1');
10  
11 GetOptions(
12 \%ARGV,
13 'input|i=s', 'output|o=s', 'error|e=s',
14 'size|s=i', 'name|n=s', 'type|t=s',
15 _meta_options( \%ARGV ),
16 )
17 and $ARGV{input} or @ARGV or $ARGV{size}
18 or pod2usage( -verbose => 1 );
19  
20 my ( $INH, $OUTH, $ERRH ) = _prepare_io( \%ARGV, \@ARGV );
21  
22 my ($name, $input_size) = (@ARGV{qw/name size/});
23  
24 $ARGV{type} ||= 'raw';
25 $input_size ||= 0;
26  
27 if (@ARGV) {
28 $input_size += -s $_ for @ARGV;
29 }
30  
31 my %dispatch = (
32 raw => \&_cat_raw,
33 tpb => \&_cat_tpb,
34 );
35  
36 if (@ARGV) {
37 while (my $file = shift @ARGV) {
38 open $INH, q{<}, $file or die qq{Cant read $file: $!};
39 $dispatch{$ARGV{type}}->( $INH, $OUTH, $ERRH, $name, $input_size );
40 }
41 }
42 else {
43 $dispatch{$ARGV{type}}->( $INH, $OUTH, $ERRH, $name, $input_size );
44 }
45 close $INH;
46 close $OUTH;
47  
48  
49 sub _cat_raw {
50 my ($inh, $outh, $errh, $name, $input_size) = @_;
51  
52 my $BUFFER_SIZE = 4096;
53 my $UPDATE_PROB = 1/1000;
54 my $buffer = q{};
55 my $total_read = 0;
56  
57 my @byte_units = qw/B kiB MiB GiB TiB PiB/;
58 my $size_modifier = 1;
59  
60 $input_size /= 1024
61 and $size_modifier *= 1024
62 and shift @byte_units
63 until $input_size < 1024;
64  
65 my @time_units = qw/s m h/;
66  
67 my $rate = 0;
68  
69 use Time::HiRes qw(time);
70 my $start = time;
71  
72 while (my $read = sysread $inh, $buffer, $BUFFER_SIZE) {
73  
74 if (syswrite $outh, $buffer) {
75  
76 next unless -t $errh;
77  
78 $total_read += $read;
79  
80 unless (int rand 1/$UPDATE_PROB) {
81
82 my $current = Time::HiRes::time - $start;
83  
84 $rate = ($total_read / $size_modifier) / ($current);
85  
86 $current /= 60 ** (3 - @time_units);
87
88 $current /= 60 and shift @time_units
89 until $current < 60;
90  
91 printf $errh "\r%s: [ %.2f/%.2f%s ] %.2f/%.2f%s (%.2f%s/%s) ",
92 $name || 'progress',
93 $total_read / $size_modifier,
94 $input_size,
95 $byte_units[0] || 'units',
96 $current,
97 $rate ? $input_size / $rate : 0,
98 $time_units[0] || 'units',
99 $rate,
100 $byte_units[0] || 'units',
101 $time_units[0] || 'units',
102 }
103  
104 }
105 }
106 print $errh "\n";
107 }
108  
109  
110 sub _cat_tpb {
111 my ($inh, $outh, $errh, $name, $input_size) = @_;
112  
113 eval {require Term::ProgressBar;};
114 die "$0: '--type tpb' requires Term::ProgressBar to be installed" if $@;
115 die "$0: '--type tpb' requires input size to be given via '-s INT' if monitoring STDIN";
116  
117 my $progress = Term::ProgressBar->new( {
118 name => $name || 'progress',
119 count => $input_size,
120 ETA => 'linear',
121 fh => \$errh,
122 } );
123  
124 my $BUFFER_SIZE = 4096;
125 my $UPDATE_PROB = 1/2;
126 my $next_update = 0;
127 my $total_read = 0;
128 my $buffer = q{};
129  
130 while (my $read = sysread $inh, $buffer, $BUFFER_SIZE) {
131  
132 if (syswrite $outh, $buffer) {
133 $total_read += $read;
134 $next_update = $progress->update($total_read)
135 if $total_read >= $next_update;
136 }
137 }
138 $progress->update($input_size)
139 if $input_size >= $next_update;
140 }
141  
142  
143 sub _meta_options {
144 my ($opt) = @_;
145  
146 return (
147 'quiet' => sub { $opt->{quiet} = 1; $opt->{verbose} = 0 },
148 'verbose:i' => sub { $opt->{verbose} = $_[1] // 1; $opt->{quiet} = 0 },
149 'version' => sub { pod2usage( -sections => ['VERSION', 'REVISION'],
150 -verbose => 99 ) },
151 'license' => sub { pod2usage( -sections => ['AUTHOR', 'COPYRIGHT'],
152 -verbose => 99 ) },
153 'usage' => sub { pod2usage( -sections => ['SYNOPSIS'],
154 -verbose => 99 ) },
155 'help' => sub { pod2usage( -verbose => 1 ) },
156 'manual' => sub { pod2usage( -verbose => 2 ) },
157 );
158 }
159  
160 sub _prepare_io {
161 my ($opt, $argv) = @_;
162  
163 my ($INH, $OUTH, $ERRH);
164
165 # If user explicitly sets -i, put the argument in @$argv
166 unshift @$argv, $opt->{input} if exists $opt->{input};
167  
168 # Allow in-situ arguments (equal input and output filenames)
169 if ( exists $opt->{input} and exists $opt->{output}
170 and $opt->{input} eq $opt->{output} ) {
171 open $INH, q{<}, $opt->{input}
172 or croak "Can't read $opt->{input}: $!";
173 unlink $opt->{output};
174 }
175 else { $INH = *STDIN }
176  
177 # Redirect STDOUT to a file if so specified
178 if ( exists $opt->{output} and q{-} ne $opt->{output} ) {
179 open $OUTH, q{>}, $opt->{output}
180 or croak "Can't write $opt->{output}: $!";
181 }
182 else { $OUTH = *STDOUT }
183  
184 # Log STDERR if so specified
185 if ( exists $opt->{error} and q{-} ne $opt->{error} ) {
186 open $ERRH, q{>}, $opt->{error}
187 or croak "Can't write $opt->{error}: $!";
188 }
189 elsif ( exists $opt->{quiet} and $opt->{quiet} ) {
190 use File::Spec;
191 open $ERRH, q{>}, File::Spec->devnull
192 or croak "Can't write $opt->{error}: $!";
193 }
194 else { $ERRH = *STDERR }
195  
196 return ( $INH, $OUTH, *STDERR = $ERRH );
197 }
198  
199 __DATA__