-
-
Save foursixnine/13d3c2735fddcf44704bafb6eca04ab4 to your computer and use it in GitHub Desktop.
Mojo::UserAgent progress bar
This file contains 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; | |
my $ua = Mojo::UserAgent->new; | |
$ua->on(start => sub { | |
my ($ua, $tx) = @_; | |
my $progress = 0; | |
$tx->res->on(progress => sub { | |
my $msg = shift; | |
return unless my $len = $msg->headers->content_length; | |
my $size = $msg->content->progress; | |
my $current = int($size / ($len / 100)); | |
# print each % increase, to avoid spam | |
if ($progress < $current) { | |
local $| = 1; | |
$progress = $current; | |
print "\rDownloading: ", $size == $len ? 100 : $progress, "%\n"; | |
} | |
}); | |
}); | |
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