Last active
January 20, 2018 09:58
-
-
Save olegwtf/c9c5a266352cba73dc5b to your computer and use it in GitHub Desktop.
Mojo::UserAgent progress bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN { $ENV{MOJO_MAX_MESSAGE_SIZE} = 1024**3 } | |
use strict; | |
use Mojo::UserAgent; | |
$| = 1; | |
my $ua = Mojo::UserAgent->new; | |
$ua->on(start => sub { | |
my ($ua, $tx) = @_; | |
$tx->req->once(finish => sub { | |
$tx->res->on(progress => sub { | |
my $msg = shift; | |
return unless my $len = $msg->headers->content_length; | |
my $size = $msg->content->progress; | |
print "\rProgress: ", $size == $len ? 100 : int($size / ($len / 100)), '%'; | |
}); | |
}); | |
}); | |
my $tx = $ua->get('http://mirror.yandex.ru/debian-cd/7.8.0/amd64/iso-cd/debian-7.8.0-amd64-CD-1.iso'); | |
print $tx->error ? "\nDownloading failed: ".$tx->error->{message} : "\nDownloading finished!\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment