|
NAMETerm::ProgressBar - provide a progress meter on a standard terminalVERSIONVersion 2.22SYNOPSISuse Term::ProgressBar; my $progress = Term::ProgressBar->new ({count => 10_000}); $progress->update(5_000); DESCRIPTIONTerm::ProgressBar provides a simple progress bar on the terminal, to let the user know that something is happening, roughly how much stuff has been done, and maybe an estimate at how long remains.A typical use sets up the progress bar with a number of items to do, and then calls update to update the bar whenever an item is processed. Often, this would involve updating the progress bar many times with no user-visible change. To avoid unnecessary work, the update method returns a value, being the update value at which the user will next see a change. By only calling update when the current value exceeds the next update value, the call overhead is reduced. Remember to call the "$progress->update($max_value)" when the job is done to get a nice 100% done bar. A progress bar by default is simple; it just goes from left-to-right, filling the bar with '=' characters. These are called major characters. For long-running jobs, this may be too slow, so two additional features are available: a linear completion time estimator, and/or a minor character: this is a character that moves from left-to-right on the progress bar (it does not fill it as the major character does), traversing once for each major-character added. This exponentially increases the granularity of the bar for the same width. EXAMPLESA really simple use#!/usr/bin/perl use Term::ProgressBar 2.00; use constant MAX => 100_000; my $progress = Term::ProgressBar->new(MAX); for (0..MAX) { my $is_power = 0; for (my $i = 0; 2**$i <= $_; $i++) { $is_power = 1 if 2**$i == $_; } if ($is_power) { $progress->update($_); } } see eg/simle_use.pl Here is a simple example. The process considers all the numbers between 0 and MAX, and updates the progress bar whenever it finds one. Note that the progress bar update will be very erratic. See below for a smoother example. Note also that the progress bar will never complete; see below to solve this. The complete text of this example is in examples/powers in the distribution set (it is not installed as part of the module). A smoother bar updatemy $progress = Term::ProgressBar->new($max); for (0..$max) { my $is_power = 0; for (my $i = 0; 2**$i <= $_; $i++) { $is_power = 1 if 2**$i == $_; } $progress->update($_) } See eg/smooth_bar.pl This example calls update for each value considered. This will result in a much smoother progress update, but more program time is spent updating the bar than doing the "real" work. See below to remedy this. This example does not call "$progress->update($max);" at the end, since it is unnecessary, and ProgressBar will throw an exception at an attempt to update a finished bar. The complete text of this example is in examples/powers2 in the distribution set (it is not installed as part of the module. A (much) more efficient updatemy $progress = Term::ProgressBar->new({name => 'Powers', count => $max, remove => 1}); $progress->minor(0); my $next_update = 0; for (0..$max) { my $is_power = 0; for (my $i = 0; 2**$i <= $_; $i++) { $is_power = 1 if 2**$i == $_; } $next_update = $progress->update($_) if $_ >= $next_update; } $progress->update($max) if $max >= $next_update; This example does two things to improve efficiency: firstly, it uses the value returned by update to only call it again when needed; secondly, it switches off the use of minor characters to update a lot less frequently ("$progress->minor(0);". The use of the return value of update means that the call of "$progress->update($max);" at the end is required to ensure that the bar ends on 100%, which gives the user a nice feeling. This example also sets the name of the progress bar. This example also demonstrates the use of the 'remove' flag, which removes the progress bar from the terminal when done. The complete text of this example is in examples/powers3 in the distribution set (it is not installed as part of the module. When the maximum number of items is sometimes unknownSometimes you may wish to use the progress bar when the number of items may or may not be known. One common example is when you write a script that can take input piped from the output of another command, and then pipe the output to yet another command. eg:some_command --arg value | my_script.pl | some_other_command Or ... my_script.pl input_file output_file This example shows how you can iterate over a file specified on the command line with the progress bar. Since the input file may be read from STDIN, the number of lines may not be known. Term::ProgressBar handles this by just taking '-1' as the count value and with no further changes to the code. By calling update with the same count value, you ensure the progress bar is removed afterwards. my $input_file = shift; my $output_file = shift; my $in_fh = \*STDIN; my $out_fh = \*STDOUT; my $message_fh = \*STDERR; my $num_lines = -1; if (defined($input_file) and $input_file ne '-') { open($in_fh, $input_file) or die "Couldn't open file, '$input_file': $!"; my $wc_output = `wc -l $input_file`; chomp($wc_output); $wc_output =~ /^\s*(\d+)(\D.*)?/ or die "Couldn't parse wc output: $wc_output"; $num_lines = $1; } if(defined($output_file)) { !-f $output_file or die "Specified output file, '$output_file', already exists"; open($out_fh, '>', $output_file) or die "Couldn't open output file, '$output_file': $!"; } my $progress = Term::ProgressBar->new({ name => 'file processor', count => $num_lines, remove => 1, fh => $message_fh, }); while (my $line = <$in_fh>) { chomp($line); print $out_fh "I found a line: $line\n"; $progress->message("Found 10000!") if($line =~ /10000/); $progress->update(); } $progress->update($num_lines); print $message_fh "Finished\n"; When the file is defined explicitly, the progress bar displays the linewise progress through the file. Since the progress bar by default prints output to stderr, your scripts output to STDOUT will not be affected. Using Completion Time Estimationmy $progress = Term::ProgressBar->new({ name => 'Powers', count => $max, ETA => 'linear', }); $progress->max_update_rate(1); my $next_update = 0; for (0..$max) { my $is_power = 0; for (my $i = 0; 2**$i <= $_; $i++) { if ( 2**$i == $_ ) { $is_power = 1; $progress->message(sprintf "Found %8d to be 2 ** %2d", $_, $i); } } $next_update = $progress->update($_) if $_ > $next_update; } $progress->update($max) if $max >= $next_update; This example uses the ETA option to switch on completion estimation. Also, the update return is tuned to try to update the bar approximately once per second, with the max_update_rate call. See the documentation for the new method for details of the format(s) used. This example also provides an example of the use of the message function to output messages to the same filehandle whilst keeping the progress bar intact The complete text of this example is in examples/powers5 in the distribution set (it is not installed as part of the module. INSTANCE CONSTRUCTIONnewCreate & return a new Term::ProgressBar instance.
INSTANCE COMPONENTSScalar Components.See "get_set" in Class::MethodMaker for usage.
Boolean ComponentsSee "get_set" in Class::MethodMaker for usage.
Configuration
INSTANCE HIGHER-LEVEL PROCEDURESupdateUpdate the progress bar.
messageOutput a message. This is very much like print, but we try not to disturb the terminal.
REPORTING BUGSvia RT: <https://rt.cpan.org/Dist/Display.html?Name=Term-ProgressBar>COMPATIBILITYIf exactly two arguments are provided, then new operates in v1 compatibility mode: the arguments are considered to be name, and item count. Various other defaults are set to emulate version one (e.g., the major output character is '#', the bar width is set to 50 characters and the output filehandle is not treated as a terminal). This mode is deprecated.AUTHORMartyn J. Pearce fluffy@cpan.orgSignificant contributions from Ed Avis, amongst others. MAINTAINERGabor Szabo <http://szabgab.com/> <http://perlmaven.com/>LICENSE AND COPYRIGHTCopyright (c) 2001, 2002, 2003, 2004, 2005 Martyn J. Pearce. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |