our $DATE = '2020-07-09'; # DATE
our $DIST = 'Progress-Any-Examples'; # DIST
our $VERSION = '0.005'; # VERSION

use 5.010001;
use strict;
use warnings;

use Progress::Any;
use Progress::Any::Output -task=>'download', 'TermProgressBarColor', freq=>0.1;
use Time::HiRes qw(sleep);

my $progress = Progress::Any->get_indicator(task => "download");

my ($file_size1, $file_size2) = @ARGV;
$file_size1 //= 10_000;
$file_size2 //= 30_000;

my $step;

# download file1
$step = int($file_size1/20); $step = 1 if $step < 1;
for (1.. $file_size1) {
    if ($_ % $step == 0) {
        $progress->update(message => "Downloading file1 ($_ bytes)", pos => $_);
        sleep 0.2;
$progress->update(message => "Downloaded file1", pos=>$file_size1, force_update=>1);
sleep 2;
$progress->finish(); # optional

# reset progress bar

# download file2
$step = int($file_size2/20); $step = 1 if $step < 1;
for (1.. $file_size2) {
    if ($_ % $step == 0) {
        $progress->update(message => "Downloading file2 ($_ bytes)", pos => $_);
        sleep 0.2;
$progress->update(message => "Downloaded file2", pos=>$file_size2, force_update=>1);
sleep 2;
$progress->finish; # optional

# ABSTRACT: Reusing the same task to track progress of two activities
# PODNAME: progress-any-eg-progressbar-06-reusing-task



=encoding UTF-8

=head1 NAME

progress-any-eg-progressbar-06-reusing-task - Reusing the same task to track progress of two activities

=head1 VERSION

This document describes version 0.005 of progress-any-eg-progressbar-06-reusing-task (from Perl distribution Progress-Any-Examples), released on 2020-07-09.


 % progress-any-eg-progressbar-06-reusing-task [file_size1=10000] [file_size2=30000]


This example script shows the progress of simulated downloading of two files
(the file sizes can be set via argument). The progress of each download will be
tracked with the same task: C<download>.

After the downloading of the first file is complete, we perform a C<reset()>
(a.k.a. C<< update(pos => 0) >>) to the task to change its state from
C<finished> to C<started> again.

Also notice the use of C<force_update> argument in the C<update()> to refresh
the progress bar with a "Downloaded xxx" message.


Please visit the project's homepage at L<https://metacpan.org/release/Progress-Any-Examples>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-Progress-Any-Examples>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Progress-Any-Examples>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired

=head1 AUTHOR

perlancar <perlancar@cpan.org>


This software is copyright (c) 2020 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.