Steven Haryanto > Progress-Any > Progress::Any

Download:
Progress-Any-0.15.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.15   Source  

NAME ^

Progress::Any - Record progress to any output

VERSION ^

This document describes version 0.15 of Progress::Any (from Perl distribution Progress-Any), released on 2014-07-29.

SYNOPSIS ^

In your module:

 package MyApp;
 use Progress::Any;

 sub download {
     my @urls = @_;
     return unless @urls;
     my $progress = Progress::Any->get_indicator(
         task => "download", target=>~~@urls);
     for my $url (@urls) {
         # download the $url ...
         $progress->update(message => "Downloaded $url");
     }
     $progress->finish;
 }

In your application:

 use MyApp;
 use Progress::Any::Output;
 Progress::Any::Output->set('TermProgressBarColor');

 MyApp::download("url1", "url2", "url3", "url4", "url5");

When run, your application will display something like this, in succession:

  20% [====== Downloaded url1           ]0m00s Left
  40% [=======Downloaded url2           ]0m01s Left
  60% [=======Downloaded url3           ]0m01s Left
  80% [=======Downloaded url4==         ]0m00s Left

(At 100%, the output automatically cleans up the progress bar).

Another example, demonstrating multiple indicators and the LogAny output:

 use Progress::Any;
 use Progress::Any::Output;
 use Log::Any::App;

 Progress::Any::Output->set('LogAny', template => '[%-8t] [%P/%2T] %m');
 my $pdl = Progress::Any->get_indicator(task => 'download');
 my $pcp = Progress::Any->get_indicator(task => 'copy');

 $pdl->target(10);
 $pdl->update(message => "downloading A");
 $pcp->update(message => "copying A");
 $pdl->update(message => "downloading B");
 $pcp->update(message => "copying B");

will show something like:

 [download] [1/10] downloading A
 [copy    ] [1/ ?] copying A
 [download] [2/10] downloading B
 [copy    ] [2/ ?] copying B

DESCRIPTION ^

Progress::Any is an interface for applications that want to display progress to users. It decouples progress updating and output, rather similar to how Log::Any decouples log producers and consumers (output). The API is also rather similar to Log::Any, except Adapter is called Output and category is called task.

Progress::Any records position/target and calculates elapsed time, estimated remaining time, and percentage of completion. One or more output modules (Progress::Any::Output::*) display this information.

In your modules, you typically only need to use Progress::Any, get one or more indicators, set target and update it during work. In your application, you use Progress::Any::Output and set/add one or more outputs to display the progress. By setting output only in the application and not in modules, you separate the formatting/display concern from the logic.

The list of features:

STATUS ^

API might still change, will be stabilized in 1.0.

EXPORTS ^

$progress => OBJ

The root indicator. Equivalent to:

 Progress::Any->get_indicator(task => '')

ATTRIBUTES ^

Below are the attributes of an indicator/task:

task => STR* (default: from caller's package, or main)

Task name. If not specified will be set to caller's package (:: will be replaced with .), e.g. if you are calling this method from Foo::Bar::baz(), then task will be set to Foo.Bar. If caller is code inside eval, main will be used instead.

title => STR* (default: task name)

Specify task title. Task title is a longer description for a task and can contain spaces and other characters. It is displayed in some outputs, as well as using %t in fill_template(). For example, for a task called copy, its title might be Copying files to remote server.

target => POSNUM (default: 0)

The total number of items to finish. Can be set to undef to mean that we don't know (yet) how many items there are to finish (in which case, we cannot estimate percent of completion and remaining time).

pos => POSNUM* (default: 0)

The number of items that are already done. It cannot be larger than target, if target is defined. If target is set to a value smaller than pos or pos is set to a value larger than target, pos will be changed to be target.

state => STR (default: stopped)

State of task/indicator. Either: stopped, started, or finished. Initially it will be set to stopped, which means elapsed time won't be running and will stay at 0. update() will set the state to started to get elapsed time to run. At the end of task, you can call finish() (or alternatively set state to finished) to stop the elapsed time again.

The difference between stopped and finished is: when target and pos are both at 0, percent completed is assumed to be 0% when state is stopped, but 100% when state is finished.

METHODS ^

Progress::Any->get_indicator(%args) => OBJ

Get a progress indicator for a certain task. %args contain attribute values, at least task must be specified.

Note that this module maintains a list of indicator singleton objects for each task (in %indicators package variable), so subsequent get_indicator() for the same task will return the same object.

$progress->update(%args)

Update indicator. Will also, usually, update associated output(s) if necessary.

Arguments:

$progress->finish(%args)

Equivalent to:

 $progress->update(
     ( pos => $progress->target ) x !!defined($progress->target),
     state => 'finished',
     %args,
 );

$progress->start()

Set state to started.

$progress->stop()

Set state to stopped.

$progress->elapsed() => FLOAT

Get elapsed time. Just like a stop-watch, when state is started elapsed time will run and when state is stopped, it will freeze.

$progress->remaining() => undef|FLOAT

Give estimated remaining time until task is finished, which will depend on how fast the update() is called, i.e. how fast pos is approaching target. Will be undef if target is undef.

$progress->total_remaining() => undef|FLOAT

Give estimated remaining time added by all its subtasks' remaining. Return undef if any one of those time is undef.

$progress->total_pos() => FLOAT

Total of indicator's pos and all of its subtasks'.

$progress->total_target() => undef|FLOAT

Total of indicator's target and all of its subtasks'. Return undef if any one of those is undef.

$progress->percent_complete() => undef|FLOAT

Give percentage of completion, calculated using total_pos / total_target * 100. Undef if total_target is undef.

$progress->fill_template($template)

Fill template with values, like in sprintf(). Usually used by output modules. Available templates:

FAQ ^

Why don't you use Moo?

Perhaps. For now I'm trying to be minimal and as dependency-free as possible.

SEE ALSO ^

Other progress modules on CPAN: Term::ProgressBar, Term::ProgressBar::Simple, Time::Progress, among others.

Output modules: Progress::Any::Output::*

See examples on how Progress::Any is used by other modules: Perinci::CmdLine (supplying progress object to functions), Git::Bunch (using progress object).

HOMEPAGE ^

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

SOURCE ^

Source repository is at https://github.com/sharyanto/perl-Progress-Any.

BUGS ^

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

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 feature.

AUTHOR ^

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2014 by Steven Haryanto.

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

syntax highlighting: