.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Progress::Any 3pm" .TH Progress::Any 3pm "2022-10-30" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Progress::Any \- Record progress to any output .SH "VERSION" .IX Header "VERSION" This document describes version 0.220 of Progress::Any (from Perl distribution Progress-Any), released on 2022\-10\-18. .SH "SYNOPSIS" .IX Header "SYNOPSIS" Example of using in a script with terminal progress bar as output (progress bar will be cleared on \f(CW\*(C`finish()\*(C'\fR) (you'll need to install Progress::Any::Output::TermProgressBarColor as well): .PP .Vb 2 \& use Progress::Any \*(Aq$progress\*(Aq; \& use Progress::Any::Output \*(AqTermProgressBarColor\*(Aq; \& \& $progress\->target(10); \& for (1..10) { \& $progress\->update(message => "Doing item $_"); \& sleep 1; \& } \& $progress\->finish(); .Ve .PP Sample output: .PP .Vb 2 \& % ./script.pl \& 60% [Doing item 6==== ]3s left .Ve .PP Another example, this time with terminal message as output: .PP .Vb 2 \& use Progress::Any \*(Aq$progress\*(Aq; \& use Progress::Any::Output \*(AqTermMessage\*(Aq, template => \*(Aq[%n] %P/%T (%6.2p%%) %m\*(Aq; \& \& $progress\->target(10); \& for (1..10) { \& $progress\->update(message => "Item $_/10"); \& sleep 1; \& } \& sleep 1; \& $progress\->finish(message => "Finished!"); .Ve .PP Sample output: .PP .Vb 6 \& % ./script.pl \& [] 1/10 ( 10.00%) Item 1/10 \& [] 2/10 ( 20.00%) Item 2/10 \& ... \& [] 10/10 (100.00%) Item 10/10 \& [] 10/10 (100.00%) Finished! .Ve .PP Example of using in a module as well as script: .PP .Vb 3 \& # in lib/MyApp.pm \& package MyApp; \& use Progress::Any; \& \& sub download { \& my @urls = @_; \& return unless @urls; \& my $progress = Progress::Any\->get_indicator( \& task => "download", pos=>0, target=>scalar @urls); \& for my $url (@urls) { \& # download the $url ... \& $progress\->update(message => "Downloaded $url"); \& } \& $progress\->finish; \& } \& \& # in script.pl \& use MyApp; \& use Progress::Any::Output; \& Progress::Any::Output\->set(\*(AqTermProgressBarColor\*(Aq); \& \& MyApp::download("url1", "url2", "url3", "url4", "url5"); .Ve .PP Sample output: .PP .Vb 2 \& % ./script.pl \& 20% [====== Downloaded url1 ]0m00s Left .Ve .PP Example that demonstrates multiple indicator objects: .PP .Vb 2 \& use Progress::Any; \& use Progress::Any::Output; \& \& my $pdl = Progress::Any\->get_indicator(task => \*(Aqdownload\*(Aq); \& Progress::Any::Output\->set({task=>\*(Aqdownload\*(Aq}, \*(AqTermMessage\*(Aq, template => \*(Aq[%\-8t] [%P/%2T] %m\*(Aq); \& my $pcp = Progress::Any\->get_indicator(task => \*(Aqcopy\*(Aq); \& Progress::Any::Output\->set({task=>\*(Aqcopy\*(Aq }, \*(AqTermMessage\*(Aq, template => \*(Aq[%\-8t] [%P/%2T] %m\*(Aq); \& \& $pdl\->target(10); \& $pdl\->update(message => "downloading A"); \& $pcp\->update(message => "copying A"); \& sleep 1; \& $pdl\->update(message => "downloading B"); \& $pcp\->update(message => "copying B"); .Ve .PP will show something like: .PP .Vb 4 \& [download] [1/10] downloading A \& [copy ] [1/ ?] copying A \& [download] [2/10] downloading B \& [copy ] [2/ ?] copying B .Ve .SS "Example of using with Perinci::CmdLine" .IX Subsection "Example of using with Perinci::CmdLine" If you use Perinci::CmdLine, you can mark your function as expecting a Progress::Any object and it will be supplied to you in a special argument \&\f(CW\*(C`\-progress\*(C'\fR: .PP .Vb 10 \& use File::chdir; \& use Perinci::CmdLine; \& $SPEC{check_dir} = { \& v => 1.1, \& args => { \& dir => {summary=>"Path to check", schema=>"str*", req=>1, pos=>0}, \& }, \& features => {progress=>1}, \& }; \& sub check_dir { \& my %args = @_; \& my $progress = $args{\-progress}; \& my $dir = $args{dir}; \& (\-d $dir) or return [412, "No such dir: $dir"]; \& local $CWD = $dir; \& opendir my($dh), $dir; \& my @ent = readdir($dh); \& $progress\->pos(0); \& $progress\->target(scalar @ent); \& for (@ent) { \& # do the check ... \& $progress\->update(message => $_); \& sleep 1; \& } \& $progress\->finish; \& [200]; \& } \& Perinci::CmdLine\->new(url => \*(Aq/main/check_dir\*(Aq)\->run; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`Progress::Any\*(C'\fR 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 \s-1API\s0 is also rather similar to Log::Any, except \fIAdapter\fR is called \fIOutput\fR and \&\fIcategory\fR is called \fItask\fR. .PP 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. .PP 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 (you'll need to install one of the output modules as they are not included in this minimal distribution). By setting output only in the application and not in modules, you separate the formatting/display concern from the logic. .PP Screenshots: .SH "STATUS" .IX Header "STATUS" \&\s-1API\s0 might still change, will be stabilized in 1.0. .PP The list of features: .IP "\(bu" 4 multiple progress indicators .Sp You can use different indicator for each task/subtask. .IP "\(bu" 4 customizable output .Sp Output is handled by one of \f(CW\*(C`Progress::Any::Output::*\*(C'\fR modules. Currently available outputs: \f(CW\*(C`Null\*(C'\fR (no output), \f(CW\*(C`TermMessage\*(C'\fR (display as simple message on terminal), \f(CW\*(C`TermProgressBarColor\*(C'\fR (display as color progress bar on terminal), \f(CW\*(C`LogAny\*(C'\fR (log using Log::Any), \f(CW\*(C`Callback\*(C'\fR (call a subroutine). Other possible output ideas: IM/Twitter/SMS, \s-1GUI,\s0 web/AJAX, remote/RPC (over Riap for example, so that Perinci::CmdLine\-based command-line clients can display progress update from remote functions). .IP "\(bu" 4 multiple outputs .Sp One or more outputs can be used to display one or more indicators. .IP "\(bu" 4 hierarchical progress .Sp A task can be divided into subtasks. If a subtask is updated, its parent task (and its parent, and so on) are also updated proportionally. .IP "\(bu" 4 message .Sp Aside from setting a number/percentage, allow including a message when updating indicator. .IP "\(bu" 4 undefined target .Sp Target can be undefined, so a bar output might not show any bar (or show them, but without percentage indicator), but can still show messages. .IP "\(bu" 4 retargetting .Sp Target can be changed in the middle of things. .SH "EXPORTS" .IX Header "EXPORTS" .ie n .SS "$progress => \s-1OBJ\s0" .el .SS "\f(CW$progress\fP => \s-1OBJ\s0" .IX Subsection "$progress => OBJ" The root indicator. Equivalent to: .PP .Vb 1 \& Progress::Any\->get_indicator(task => \*(Aq\*(Aq) .Ve .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Below are the attributes of an indicator/task: .SS "task" .IX Subsection "task" String. Default: from caller's package, or \f(CW\*(C`main\*(C'\fR. .PP Task name. If not specified will be set to caller's package (\f(CW\*(C`::\*(C'\fR will be replaced with \f(CW\*(C`.\*(C'\fR), e.g. if you are calling this method from \&\f(CW\*(C`Foo::Bar::baz()\*(C'\fR, then task will be set to \f(CW\*(C`Foo.Bar\*(C'\fR. If caller is code inside eval, \f(CW\*(C`main\*(C'\fR will be used instead. .SS "title" .IX Subsection "title" String. Default: task name. .PP 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 \f(CW%t\fR in \f(CW\*(C`fill_template()\*(C'\fR. For example, for a task called \f(CW\*(C`copy\*(C'\fR, its title might be \f(CW\*(C`Copying files to remote server\*(C'\fR. .SS "target" .IX Subsection "target" Non-negative number. Default: 0. .PP 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). .SS "pos" .IX Subsection "pos" Non-negative number. Default: 0. .PP The number of items that are already done. It cannot be larger than \f(CW\*(C`target\*(C'\fR, if \f(CW\*(C`target\*(C'\fR is defined. If \f(CW\*(C`target\*(C'\fR is set to a value smaller than \f(CW\*(C`pos\*(C'\fR or \&\f(CW\*(C`pos\*(C'\fR is set to a value larger than \f(CW\*(C`target\*(C'\fR, \f(CW\*(C`pos\*(C'\fR will be changed to be \&\f(CW\*(C`target\*(C'\fR. .SS "state" .IX Subsection "state" String. Default: \f(CW\*(C`stopped\*(C'\fR. .PP State of task/indicator. Either: \f(CW\*(C`stopped\*(C'\fR, \f(CW\*(C`started\*(C'\fR, or \f(CW\*(C`finished\*(C'\fR. Initially it will be set to \f(CW\*(C`stopped\*(C'\fR, which means elapsed time won't be running and will stay at 0. \f(CW\*(C`update()\*(C'\fR will set the state to \f(CW\*(C`started\*(C'\fR to get elapsed time to run. At the end of task, you can call \f(CW\*(C`finish()\*(C'\fR (or alternatively set \f(CW\*(C`state\*(C'\fR to \f(CW\*(C`finished\*(C'\fR) to stop the elapsed time again. .PP The difference between \f(CW\*(C`stopped\*(C'\fR and \f(CW\*(C`finished\*(C'\fR is: when \f(CW\*(C`target\*(C'\fR and \f(CW\*(C`pos\*(C'\fR are both at 0, percent completed is assumed to be 0% when state is \f(CW\*(C`stopped\*(C'\fR, but 100% when state is \f(CW\*(C`finished\*(C'\fR. .SH "METHODS" .IX Header "METHODS" .SS "get_indicator" .IX Subsection "get_indicator" Usage: .PP .Vb 1 \& Progress::Any\->get_indicator(%args) => obj .Ve .PP Get a progress indicator for a certain task. \f(CW%args\fR contain attribute values, at least \f(CW\*(C`task\*(C'\fR must be specified. .PP Note that this module maintains a list of indicator singleton objects for each task (in \f(CW%indicators\fR package variable), so subsequent \f(CW\*(C`get_indicator()\*(C'\fR for the same task will return the same object. .SS "update" .IX Subsection "update" Usage: .PP .Vb 1 \& $progress\->update(%args) .Ve .PP Update indicator. Will also, usually, update associated output(s) if necessary. .PP Arguments: .IP "\(bu" 4 pos => \s-1NUM\s0 .Sp Set the new position. If unspecified, defaults to current position + 1. If pos is larger than target, outputs will generally still show 100%. Note that fractions are allowed. .IP "\(bu" 4 message => str|code .Sp Set a message to be displayed when updating indicator. .Sp Aside from a string, you can also pass a coderef here. It can be used to delay costly calculation. The message will only be calculated when actually sent to output. .IP "\(bu" 4 priority => str (\*(L"normal\*(R"|\*(L"low\*(R"|\*(L"high\*(R", default: \*(L"normal\*(R") .Sp Set importance level of this update. Default is \f(CW\*(C`normal\*(C'\fR. Output can choose to ignore updates lower than a certain level. .IP "\(bu" 4 state => \s-1STR\s0 .Sp Can be set to \f(CW\*(C`finished\*(C'\fR to finish a task. .IP "\(bu" 4 force_update => \s-1BOOL\s0 .Sp Default false. Some outputs choose only to update themselves after a certain amount of time or number of updates have passed; this forces their update. .SS "finish" .IX Subsection "finish" Usage: .PP .Vb 1 \& $progress\->finish(%args) .Ve .PP Equivalent to: .PP .Vb 5 \& $progress\->update( \& ( pos => $progress\->target ) x !!defined($progress\->target), \& state => \*(Aqfinished\*(Aq, \& %args, \& ); .Ve .SS "reset" .IX Subsection "reset" Usage: .PP .Vb 1 \& $progress\->reset(%args) .Ve .PP Equivalent to: .PP .Vb 5 \& $progress\->update( \& pos => 0, \& state => \*(Aqstarted\*(Aq, \& %args, \& ); .Ve .SS "start" .IX Subsection "start" Usage: .PP .Vb 1 \& $progress\->start() .Ve .PP Set state to \f(CW\*(C`started\*(C'\fR. .SS "stop" .IX Subsection "stop" Usage: .PP .Vb 1 \& $progress\->stop() .Ve .PP Set state to \f(CW\*(C`stopped\*(C'\fR. .SS "elapsed" .IX Subsection "elapsed" Usage: .PP .Vb 1 \& $progress\->elapsed() => float .Ve .PP Get elapsed time. Just like a stop-watch, when state is \f(CW\*(C`started\*(C'\fR elapsed time will run and when state is \f(CW\*(C`stopped\*(C'\fR, it will freeze. .SS "remaining" .IX Subsection "remaining" Usage: .PP .Vb 1 \& $progress\->remaining() => undef|float .Ve .PP Give estimated remaining time until task is finished, which will depend on how fast the \f(CW\*(C`update()\*(C'\fR is called, i.e. how fast \f(CW\*(C`pos\*(C'\fR is approaching \f(CW\*(C`target\*(C'\fR. Will be undef if \f(CW\*(C`target\*(C'\fR is undef. .SS "total_remaining" .IX Subsection "total_remaining" Usage: .PP .Vb 1 \& $progress\->total_remaining() => undef|FLOAT .Ve .PP Give estimated remaining time added by all its subtasks' remaining. Return undef if any one of those time is undef. .SS "total_pos" .IX Subsection "total_pos" Usage: .PP .Vb 1 \& $progress\->total_pos() => float .Ve .PP Total of indicator's pos and all of its subtasks'. .SS "total_target" .IX Subsection "total_target" Usage: .PP .Vb 1 \& $progress\->total_target() => undef|float .Ve .PP Total of indicator's target and all of its subtasks'. Return undef if any one of those is undef. .SS "percent_complete" .IX Subsection "percent_complete" Usage: .PP .Vb 1 \& $progress\->percent_complete() => undef|float .Ve .PP Give percentage of completion, calculated using \f(CW\*(C`total_pos / total_target * 100\*(C'\fR. Undef if total_target is undef. .SS "fill_template" .IX Subsection "fill_template" Usage: .PP .Vb 1 \& $progress\->fill_template($template) => str .Ve .PP Fill template with values, like in \f(CW\*(C`sprintf()\*(C'\fR. Usually used by output modules. Available templates: .IP "\(bu" 4 \&\f(CW\*(C`%(width)n\*(C'\fR .Sp Task name (the value of the \f(CW\*(C`task\*(C'\fR attribute). \f(CW\*(C`width\*(C'\fR is optional, an integer, like in \f(CW\*(C`sprintf()\*(C'\fR, can be negative to mean left-justify instead of right. .IP "\(bu" 4 \&\f(CW\*(C`%(width)t\*(C'\fR .Sp Task title (the value of the \f(CW\*(C`title\*(C'\fR attribute). .IP "\(bu" 4 \&\f(CW\*(C`%(width)e\*(C'\fR .Sp Elapsed time (the result from the \f(CW\*(C`elapsed()\*(C'\fR method). Currently using Time::Duration concise format, e.g. 10s, 1m40s, 16m40s, 1d4h, and so on. Format might be configurable and localizable in the future. Default width is \-8. Examples: .Sp .Vb 2 \& 2m30s \& 10s .Ve .IP "\(bu" 4 \&\f(CW\*(C`%(width)r\*(C'\fR .Sp Estimated remaining time (the result of the \f(CW\*(C`total_remaining()\*(C'\fR method). Currently using Time::Duration concise format, e.g. 10s, 1m40s, 16m40s, 1d4h, and so on. Will show \f(CW\*(C`?\*(C'\fR if unknown. Format might be configurable and localizable in the future. Default width is \-8. Examples: .Sp .Vb 2 \& 1m40s \& 5s .Ve .IP "\(bu" 4 \&\f(CW\*(C`%(width)R\*(C'\fR .Sp Estimated remaining time \fIor\fR elapsed time, if estimated remaining time is not calculatable (e.g. when target is undefined). Format might be configurable and localizable in the future. Default width is \-(8+1+7). Examples: .Sp .Vb 2 \& 30s left \& 1m40s elapsed .Ve .IP "\(bu" 4 \&\f(CW\*(C`%(width).(prec)p\*(C'\fR .Sp Percentage of completion (the result of the \f(CW\*(C`percent_complete()\*(C'\fR method). \&\f(CW\*(C`width\*(C'\fR and \f(CW\*(C`precision\*(C'\fR are optional, like \f(CW%f\fR in Perl's \f(CW\*(C`sprintf()\*(C'\fR, default is \f(CW\*(C`%3.0p\*(C'\fR. If percentage is unknown (due to target being undef), will show \f(CW\*(C`?\*(C'\fR. .IP "\(bu" 4 \&\f(CW\*(C`%(width)P\*(C'\fR .Sp Current position (the result of the \f(CW\*(C`total_pos()\*(C'\fR method). .IP "\(bu" 4 \&\f(CW\*(C`%(width)T\*(C'\fR .Sp Target (the result of the \f(CW\*(C`total_target()\*(C'\fR method). If undefined, will show \&\f(CW\*(C`?\*(C'\fR. .IP "\(bu" 4 \&\f(CW%m\fR .Sp Message (the \f(CW\*(C`update()\*(C'\fR parameter). If message is unspecified, will show empty string. .IP "\(bu" 4 \&\f(CW\*(C`%%\*(C'\fR .Sp A literal \f(CW\*(C`%\*(C'\fR sign. .SH "FAQ" .IX Header "FAQ" .SH "ENVIRONMENT" .IX Header "ENVIRONMENT" .SS "\s-1PROGRESS\s0" .IX Subsection "PROGRESS" Boolean. Default 1. Can be set to 0 to supress display progress output. .SH "HOMEPAGE" .IX Header "HOMEPAGE" Please visit the project's homepage at . .SH "SOURCE" .IX Header "SOURCE" Source repository is at . .SH "SEE ALSO" .IX Header "SEE ALSO" Progress::Any::Examples distribution contains example scripts. .PP Other progress modules on \s-1CPAN:\s0 Term::ProgressBar, Term::ProgressBar::Simple, Time::Progress, among others. .PP Output modules: \f(CW\*(C`Progress::Any::Output::*\*(C'\fR. You need to install at least one module to actually see progress being outputted/displayed somewhere, e.g. <> to Progress::Any::Output::TermProgressBarColor. .PP See examples on how Progress::Any is used by other modules: Perinci::CmdLine (supplying progress object to functions), Git::Bunch (using progress object). .SH "AUTHOR" .IX Header "AUTHOR" perlancar .SH "CONTRIBUTOR" .IX Header "CONTRIBUTOR" Steven Haryanto .SH "CONTRIBUTING" .IX Header "CONTRIBUTING" To contribute, you can send patches by email/via \s-1RT,\s0 or send pull requests on GitHub. .PP Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via: .PP .Vb 1 \& % prove \-l .Ve .PP If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla\- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2022, 2020, 2018, 2015, 2014, 2013, 2012 by perlancar . .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests on the bugtracker website .PP 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.