Skip to content

Instantly share code, notes, and snippets.

@foursixnine
Forked from olegwtf/gist:c9c5a266352cba73dc5b
Last active June 20, 2017 11:46
Show Gist options
  • Save foursixnine/13d3c2735fddcf44704bafb6eca04ab4 to your computer and use it in GitHub Desktop.
Save foursixnine/13d3c2735fddcf44704bafb6eca04ab4 to your computer and use it in GitHub Desktop.
Mojo::UserAgent progress bar
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