Skip to content

Instantly share code, notes, and snippets.

@olegwtf
Last active January 20, 2018 09:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save olegwtf/c9c5a266352cba73dc5b to your computer and use it in GitHub Desktop.
Save olegwtf/c9c5a266352cba73dc5b 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;
$| = 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