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