File Coverage

blib/lib/LWP/UserAgent/ProgressBar.pm
Criterion Covered Total %
statement 47 48 97.9
branch 8 12 66.6
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 66 71 92.9


line stmt bran cond sub pod time code
1 1     1   726 use 5.008;
  1         3  
  1         36  
2 1     1   5 use strict;
  1         1  
  1         26  
3 1     1   5 use warnings;
  1         1  
  1         56  
4              
5             package LWP::UserAgent::ProgressBar;
6             our $VERSION = '1.100810';
7             # ABSTRACT: An LWP user agent that can display a progress bar
8 1     1   1006 use Term::ProgressBar;
  1         107756  
  1         69  
9 1     1   9 use base 'LWP::UserAgent';
  1         1  
  1         1364  
10              
11             sub post_with_progress {
12 1     1 1 1909 my ($self, $url, $form, %args) = @_;
13 1         6 $self->_request_with_progress('post', $url, $form, %args);
14             }
15              
16             sub get_with_progress {
17 1     1 1 3194 my ($self, $url, %args) = @_;
18 1         6 $self->_request_with_progress('get', $url, undef, %args);
19             }
20              
21             sub _request_with_progress {
22 2     2   7 my ($self, $req, $url, $form, %args) = @_;
23              
24             # don't buffer the prints to make the status update
25 2         11 local $| = 1;
26 2         4 my $bar_name;
27 2 50       10 if (defined $args{bar_name}) {
28 2         4 $bar_name = $args{bar_name};
29 2         5 delete $args{bar_name};
30             }
31 2 50       92 my $progress = Term::ProgressBar->new(
32             { count => 1024,
33             (defined $bar_name ? (name => $bar_name) : ()),
34             ETA => 'linear',
35             }
36             );
37 2         9550 $progress->minor(0); # turns off the floating asterisks.
38 2         71 $progress->max_update_rate(1); # only relevant when ETA is used.
39 2         20 my $did_set_target = 0;
40 2         4 my $received_size = 0;
41 2         4 my $next_update = 0;
42 2         5 my $content = '';
43 2         11 delete $args{$_} for qw(:content_cb :read_size_hint);
44             $args{':content_cb'} = sub {
45 4     4   1306596 my ($data, $cb_response, $protocol) = @_;
46 4 100       23 unless ($did_set_target) {
47 2 50       11 if (my $content_length = $cb_response->content_length) {
48 2         77 $progress->target($content_length);
49 2         839 $did_set_target = 1;
50             } else {
51 0         0 $progress->target($received_size + 2 * length $data);
52             }
53             }
54 4         13 $received_size += length $data;
55 4         31 $content .= $data;
56 4 50       30 $next_update = $progress->update($received_size)
57             if $received_size >= $next_update;
58 2         19 };
59 2         5 $args{':read_size_hint'} = 8192;
60 2 100       38 my $response =
61             $self->$req($url, $req eq 'get' ? (%$form, %args) : ($form, %args));
62 2         112450 print "\n";
63 2         24 $response->content($content);
64 2         154 $response;
65             }
66             1;
67              
68              
69             __END__