Term-ProgressBar-2.21/0000755000175000017500000000000013140323747014134 5ustar manwarmanwarTerm-ProgressBar-2.21/t/0000755000175000017500000000000013140323747014377 5ustar manwarmanwarTerm-ProgressBar-2.21/t/02_term_progressbar_io.t0000644000175000017500000000116613016530155021133 0ustar manwarmanwar# -*- mode: cperl; -*- use Test::More; use_ok('Term::ProgressBar::IO'); use IO::File; use Capture::Tiny qw(capture_stderr); my $fh = IO::File->new('t/random_file','r') or die "Unable to open t/random_file for reading: $!"; Term::ProgressBar->__force_term (50); my $pb; my $err = capture_stderr { $pb = Term::ProgressBar::IO->new($fh); }; ok($pb->target() == 9*2+3,'Correct number of bytes in __DATA__'); while (<$fh>) { $err = capture_stderr { $pb->update(); }; } print STDERR $pb->last_update(); ok($pb->last_update() == $pb->target(),'Last position is now target'); close($fh); done_testing(); Term-ProgressBar-2.21/t/v2-mobile.t0000644000175000017500000000242213016530155016353 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the moving target functionality of Term::ProgressBar. =cut use Test::More tests => 7; use Test::Exception; use Capture::Tiny qw(capture_stderr); use_ok 'Term::ProgressBar'; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--7: Count 1-20 Create a progress bar with 10 things. Update it it from 1 to 5. Change target to 20. Update it from 11 to 20. (1) Check no exception thrown on creation (2) Check no exception thrown on update (1..5) (3) Check no exception thrown on target update (4) Check no exception thrown on update (6..10) (5) Check bar is complete (6) Check bar number is 100% =cut my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-20 (1)'; lives_ok { $p->update($_) for 1..5 } 'Count 1-20 (2)'; lives_ok { $p->target(20) } 'Count 1-20 (3)'; lives_ok { $p->update($_) for 11..20 } 'Count 1-20 (4)'; }; $err =~ s!^.*\r!!gm; diag "ERR:\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; like $lines[-1], qr/\[=+\]/, 'Count 1-20 (5)'; like $lines[-1], qr/^\s*100%/, 'Count 1-20 (6)'; Term-ProgressBar-2.21/t/silent.t0000644000175000017500000000472213130662507016066 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; use Term::ProgressBar; =head1 Unit Test Package for Term::ProgressBar This package tests the basic functionality of Term::ProgressBar. =cut use Test::More tests => 13; use Test::Exception; use Capture::Tiny qw(capture_stderr); my $MESSAGE1 = 'Walking on the Milky Way'; # ------------------------------------- =head2 Test 1--5 Create a progress bar with 10 things. Update it it from 1 to 10. Verify that it has no output. =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new({ count => 10, silent => 1}); } 'Count 1-10 (1)'; lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)'; lives_ok { $p->message($MESSAGE1) } 'Count 1-10 (3)'; lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)'; }; diag "ERR:\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; ok !$err, 'We should have no output'; } # ------------------------------------- =head2 Tests 6--8: Message Check Run a progress bar from 0 to 100, each time calling a message after an update. Check that we still have no output. =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new({ count => 100, silent => 1}); } 'Message Check ( 1)'; lives_ok { for (0..100) { $p->update($_); $p->message("Hello") } } 'Message Check ( 2)'; }; ok !$err, 'We should still have no output'; } # ---------------------------------------------------------------------------- =head2 Tests 9--13: Message Check Run a progress bar from 0 to 1000, each time calling a message after an update. Check that we still have no output. =cut { my $err = capture_stderr { my $p; my $max_value = 1000; my $half_value = int($max_value/2); lives_ok { $p = Term::ProgressBar->new({ count => $max_value, silent => 1}); } 'Count 1-1000 (1)'; my $next_value = 0; lives_ok { for (my $i=0; $i<$half_value; $i++) { $next_value = $p->update($i) if ($i >= $next_value); } } 'Count 1-1000 (2)'; lives_ok { $p->message($MESSAGE1) } 'Count 1-1000 (3)'; lives_ok { for (my $i=$half_value; $i<$max_value; $i++) { $next_value = $p->update($i) if ($i >= $next_value); } } 'Count 1-1000 (4)'; }; diag "ERR:\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; ok !$err, 'We should still have no output even with warnings enabled'; } # ---------------------------------------------------------------------------- Term-ProgressBar-2.21/t/zero.t0000644000175000017500000000353413016530155015543 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the zero-progress handling of progress bar. =cut use Test::More tests => 9; use Test::Exception; use Capture::Tiny qw(capture_stderr); use_ok 'Term::ProgressBar'; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--5: V1 mode Create a progress bar with 0 things. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update (3) Check bar displays name (3) Check bar says nothing to do =cut { my $p; my $name = 'doing nothing'; my $err = capture_stderr { lives_ok { $p = Term::ProgressBar->new($name, 0); } 'V1 mode ( 1)'; lives_ok { $p->update($_) for 1..10 } 'V1 mode ( 2)'; }; my @lines = grep { $_ ne ''} split /\r/, $err; diag explain @lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/^$name:/, 'V1 mode ( 3)'; like $lines[-1], qr/\(nothing to do\)/, 'V1 mode ( 4)'; } # ------------------------------------- =head2 Tests 6--9: V2 mode Create a progress bar with 0 things. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update (3) Check bar displays name (4) Check bar says nothing to do =cut { my $p; my $name = 'zero'; my $err = capture_stderr { lives_ok { $p = Term::ProgressBar->new({ count => 0, name => $name }); } 'V2 mode ( 1)'; lives_ok { $p->update($_) for 1..10 } 'V2 mode ( 2)'; }; my @lines = grep {$_ ne ''} split /\r/, $err; diag explain @lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/^$name:/, 'V2 mode ( 3)'; like $lines[-1], qr/\(nothing to do\)/, 'V2 mode ( 4)'; } # ---------------------------------------------------------------------------- Term-ProgressBar-2.21/t/eta-linear.t0000644000175000017500000000333713016530155016606 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the basic functionality of Term::ProgressBar. =cut use Test::More tests => 9; use Test::Exception; use Capture::Tiny qw(capture_stderr); use Term::ProgressBar; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--10: Count 1-10 Create a progress bar with 10 things. Invoke ETA and name on it. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update 1..5 (3) Check no exception thrown on message issued (4) Check no exception thrown on update 6..10 (5) Check message seen (6) Check bar is complete (7) Check bar number is 100% (8) Check --DONE-- issued (9) Check estimation done =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new({count => 10, name => 'fred', ETA => 'linear'}); } 'Count 1-10 (1)'; lives_ok { for (1..5) { $p->update($_); sleep 1 } } 'Count 1-10 (2)'; lives_ok { $p->message('Hello Mum!') } 'Count 1-10 (3)'; lives_ok { for (6..10) { $p->update($_); sleep 1 } } 'Count 1-10 (4)'; }; my @lines = grep $_ ne '', split /[\n\r]+/, $err; diag explain \@lines if $ENV{TEST_DEBUG}; ok grep $_ eq 'Hello Mum!', @lines; like $lines[-1], qr/\[=+\]/, 'Count 1-10 (6)'; like $lines[-1], qr/^fred: \s*100%/, 'Count 1-10 (7)'; like $lines[-1], qr/D[ \d]\dh\d{2}m\d{2}s$/, 'Count 1-10 (8)'; like $lines[-2], qr/ Left$/, 'Count 1-10 (9)'; } # ---------------------------------------------------------------------------- Term-ProgressBar-2.21/t/compat.t0000644000175000017500000000250613016530155016045 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar v1.0 Compatibility This script is based on the test script for Term::ProgressBar version 1.0, and is intended to test compatibility with that version. =cut # Utility ----------------------------- use Test::More tests => 9; use Term::ProgressBar; use POSIX qw; use Capture::Tiny qw(capture); $| = 1; my $count = 100; diag 'create a bar'; my $test_str = 'test'; my $tp; { my ($out, $err) = capture { $tp = Term::ProgressBar->new($test_str, $count); }; isa_ok $tp, 'Term::ProgressBar'; is $out, '', 'empty stdout'; is $err, "$test_str: "; } diag 'do half the stuff and check half the bar has printed'; my $halfway = floor($count / 2); { my ($out, $err) = capture { $tp->update foreach (0 .. $halfway - 1) }; is $out, '', 'empty stdout'; is $err, ('#' x floor(50 / 2)); } # do the rest of the stuff and check the whole bar has printed { my ($out, $err) = capture { $tp->update foreach ($halfway .. $count - 1) }; is $out, '', 'empty stdout'; is $err, ('#' x ceil(50 / 2)) . "\n"; } # try to do another item and check there is an error eval { $tp->update }; my $err = $@; ok defined($err); is substr($err, 0, length(Term::ProgressBar::ALREADY_FINISHED)), Term::ProgressBar::ALREADY_FINISHED; Term-ProgressBar-2.21/t/v2-simple.t0000644000175000017500000000456513016530155016407 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the basic functionality of Term::ProgressBar. =cut use Test::More tests => 31; use Test::Exception; use Capture::Tiny qw(capture_stderr); use_ok 'Term::ProgressBar'; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--16: Count 1-10 Create a progress bar with 10 things. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update (3) Check bar is complete (4) Check bar number is 100% (5--15) Check bar has no minor characters at any point =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)'; lives_ok { $p->update($_) for 1..10 } 'Count 1-10 (2)'; }; my @lines = grep {$_ ne ''} split /\r/, $err; diag explain \@lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/\[=+\]/, 'Count 1-10 (3)'; like $lines[-1], qr/^\s*100%/, 'Count 1-10 (4)'; like $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-10 (%d)', 5+$_) for 0..10; } # ------------------------------------- =head2 Tests 17--30: Count 1-9 Create a progress bar with 10 things. Update it it from 1 to 9. (1) Check no exception thrown on creation (2) Check no exception thrown on update (3) Check bar is incomplete (4) Check bar number is 90% (5--14) Check bar has no minor characters at any point =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-9 (1)'; lives_ok { $p->update($_) for 1..9 } 'Count 1-9 (2)'; }; my @lines = grep $_ ne '', split /\r/, $err; diag explain \@lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/\[=+ +\]/, 'Count 1-9 (3)'; like $lines[-1], qr/^\s*90%/, 'Count 1-9 (4)'; like $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-9 (%d)', 5+$_) for 0..9; } # ------------------------------------- =head2 Test 31 Make sure the same progress bar text is not printed twice to the terminal (in the case of an update that is too little to affect the percentage or displayed bar). =cut { my $err = capture_stderr { my $tp = Term::ProgressBar->new(1000000); $tp->update($_) foreach (0, 1); }; my @lines = grep {$_ ne ''} split /\r/, $err; diag explain \@lines if $ENV{TEST_DEBUG}; is scalar @lines, 1; } Term-ProgressBar-2.21/t/name.t0000644000175000017500000000754513016530155015512 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the name functionality of Term::ProgressBar. =cut use Test::More tests => 20; use Test::Exception; use Capture::Tiny qw(capture_stderr); my $MESSAGE1 = 'The Gospel of St. Jude'; my $NAME1 = 'Algenon'; my $NAME2 = 'Smegma'; use_ok 'Term::ProgressBar'; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--10: Count 1-10 Create a progress bar with 10 things, and a name 'Algenon'. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update (1..3) (3) Check bar number is 30% (4) Check bar is 30% along (5) Check no exception thrown on message send (6) Check no exception thrown on update (6..10) (7) Check message seen (8) Check bar is complete (9) Check bar number is 100% =cut { my $p; my $err = capture_stderr { lives_ok { $p = Term::ProgressBar->new({count => 10, name => $NAME1}); } 'Count 1-10 ( 1)'; lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)'; }; $err =~ s!^.*\r!!gm; diag "ERR (1) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; like $lines[-1], qr/^@{[$NAME1]}: \s*\b30%/, 'Count 1-10 ( 3)'; my ($bar, $space) = $lines[-1] =~ /\[(=*)(\s*)\]/; my $length = length($bar) + length($space); print STDERR ("LENGTHS (1) :BAR:", length($bar), ":SPACE:", length($space), "\n") if $ENV{TEST_DEBUG}; my $barexpect = $length * 0.3; cmp_ok length($bar), '>', $barexpect -1; cmp_ok length($bar), '<', $barexpect+1; $err = capture_stderr { lives_ok { $p->message($MESSAGE1) } 'Count 1-10 ( 5)'; lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)'; }; $err =~ s!^.*\r!!gm; diag "ERR (2) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; @lines = split /\n/, $err; is $lines[0], $MESSAGE1, 'Count 1-10 ( 7)'; like $lines[-1], qr/\[=+\]/, 'Count 1-10 ( 8)'; like $lines[-1], qr/^@{[$NAME1]}: \s*100%/, 'Count 1-10 ( 9)'; } # ------------------------------------- =head2 Tests 11--20: Count 1-20 Create a progress bar with 20 things, and a name 'Smegma'. Update it it from 1 to 20. Use v1 mode (1) Check no exception thrown on creation (2) Check no exception thrown on update (1..12) (3) Check bar number is 60% (4) Check bar is 60% along (5) Check no exception thrown on message send (6) Check no exception thrown on update (13..20) (7) Check message seen (8) Check bar is complete (9) Check bar number is 100% =cut { my $p; my $err = capture_stderr { lives_ok { $p = Term::ProgressBar->new($NAME2, 10); } 'Count 1-10 ( 1)'; lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)'; }; $err =~ s!^.*\r!!gm; diag "ERR (1) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; like $lines[-1], qr/^@{[$NAME2]}: \s*\b30%/, 'Count 1-10 ( 3)'; my ($bar, $space) = $lines[-1] =~ /(\#*)(\s*)/; my $length = length($bar) + length($space); diag ("LENGTHS (1) :BAR:" . length($bar) . ":SPACE:" . length($space)) if $ENV{TEST_DEBUG}; my $barexpect = $length * 0.3; cmp_ok length($bar), '>', $barexpect -1; cmp_ok length($bar), '<', $barexpect+1; $err = capture_stderr { lives_ok { $p->message($MESSAGE1) } 'Count 1-10 ( 5)'; lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)'; }; $err =~ s!^.*\r!!gm; diag "ERR (2) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; @lines = split /\n/, $err; like $lines[-1], qr/^@{[$NAME2]}: \s*\d+% \#*$/, 'Count 1-10 ( 8)'; like $lines[-1], qr/^@{[$NAME2]}: \s*100%/, 'Count 1-10 ( 9)'; } # ------------------------------------- Term-ProgressBar-2.21/t/v1-message.t0000644000175000017500000000257413016530155016537 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the basic functionality of Term::ProgressBar. =cut use Test::More tests => 8; use Test::Exception; use Capture::Tiny qw(capture_stderr); my $MESSAGE1 = 'Walking on the Milky Way'; use_ok 'Term::ProgressBar'; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--8: Count 1-10 Create a progress bar with 10 things, and a name 'bob'. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update (1..5) (3) Check no exception thrown on message send (4) Check no exception thrown on update (6..10) (5) Check message output. (5) Check bar is complete (6) Check bar number is 100% =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new('bob', 10); } 'Count 1-10 (1)'; lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)'; lives_ok { $p->message($MESSAGE1) } 'Count 1-10 (3)'; lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)'; }; $err =~ s!^.*\r!!gm; diag "ERR:\n$err\nlength: ", length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; is $lines[0], $MESSAGE1; like $lines[-1], qr/bob:\s+\d+% \#+/, 'Count 1-10 (6)'; like $lines[-1], qr/^bob:\s+100%/, 'Count 1-10 (7)'; } Term-ProgressBar-2.21/t/random_file0000644000175000017500000000002513016530155016571 0ustar manwarmanwar1 2 3 4 5 6 7 8 9 10 Term-ProgressBar-2.21/t/lessthanzero.t0000755000175000017500000000445713016530155017315 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the zero-progress handling of progress bar. =cut use Test::More tests => 10; use Test::Exception; use Capture::Tiny qw(capture_stderr); use_ok 'Term::ProgressBar'; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--4: V1 mode Create a progress bar with fewer than -1 things. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update (3) Check bar displays name =cut { my $p; my $name = 'doing nothing'; my $err = capture_stderr { lives_ok { $p = Term::ProgressBar->new($name, -1); } 'V1 mode ( 1)'; lives_ok { $p->update($_) for 1..10 } 'V1 mode ( 2)'; }; my @lines = grep { $_ ne ''} split /\r/, $err; diag explain @lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/^$name...$/, 'V1 mode ( 3)'; } # ------------------------------------- =head2 Tests 5--7: V2 mode Create a progress bar with -1 things. Update it it from 1 to 10. (1) Check no exception thrown on creation (2) Check no exception thrown on update (3) Check bar displays name =cut { my $p; my $name = 'doing nothing'; my $err = capture_stderr { lives_ok { $p = Term::ProgressBar->new({ count => -1, name => $name }); } 'V2 mode ( 1)'; lives_ok { $p->update($_) for 1..10 } 'V2 mode ( 2)'; }; my @lines = grep {$_ ne ''} split /\r/, $err; diag explain @lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/^$name...$/, 'V2 mode ( 3)'; } # ------------------------------------- =head2 Tests 8--10: V2 mode Create a progress bar with -1 things and remove = 1. Update it with -1 (1) Check no exception thrown on creation (2) Check no exception thrown on update (3) Check bar is removed =cut { my $p; my $name = 'doing nothing'; my $err = capture_stderr { lives_ok { $p = Term::ProgressBar->new({ count => -1, name => $name, remove => 1 }); } 'V2 mode ( 1)'; lives_ok { $p->update(-1) } 'V2 mode ( 2)'; }; my @lines = grep {$_ ne ''} split /\r/, $err; diag explain @lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/^\s*$/, 'V2 mode ( 3)'; } # ---------------------------------------------------------------------------- Term-ProgressBar-2.21/t/v2-message.t0000644000175000017500000000451113016530155016531 0ustar manwarmanwar# (X)Emacs mode: -*- cperl -*- use strict; use warnings; =head1 Unit Test Package for Term::ProgressBar This package tests the basic functionality of Term::ProgressBar. =cut use Test::More tests => 11; use Test::Exception; use Capture::Tiny qw(capture_stderr); my $MESSAGE1 = 'Walking on the Milky Way'; =head2 Test 1: compilation This test confirms that the test script and the modules it calls compiled successfully. =cut use_ok 'Term::ProgressBar'; Term::ProgressBar->__force_term (50); # ------------------------------------- =head2 Tests 2--8: Count 1-10 Create a progress bar with 10 things. Update it it from 1 to 10. Output a message halfway through. (1) Check no exception thrown on creation (2) Check no exception thrown on update (1..5) (3) Check no exception thrown on message send (4) Check no exception thrown on update (6..10) (5) Check message was issued. (6) Check bar is complete (7) Check bar number is 100% =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)'; lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)'; lives_ok { $p->message($MESSAGE1) } 'Count 1-10 (3)'; lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)'; }; $err =~ s!^.*\r!!gm; diag "ERR:\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; is $lines[0], $MESSAGE1; like $lines[-1], qr/\[=+\]/, 'Count 1-10 (5)'; like $lines[-1], qr/^\s*100%/, 'Count 1-10 (6)'; } # ------------------------------------- =head2 Tests 9--11: Message Check Run a progress bar from 0 to 100, each time calling a message after an update. This is to check that message preserves the progress bar value correctly. ( 1) Check no exception thrown on creation ( 2) Check no exception thrown on update, message (0..100). ( 3) Check last progress is 100% =cut { my $err = capture_stderr { my $p; lives_ok { $p = Term::ProgressBar->new(100); } 'Message Check ( 1)'; lives_ok { for (0..100) { $p->update($_); $p->message("Hello") } } 'Message Check ( 2)'; }; my @err_lines = split /\n/, $err; (my $last_line = $err_lines[-1]) =~ tr/\r//d; is substr($last_line, 0, 4), '100%', 'Message Check ( 3)'; } # ---------------------------------------------------------------------------- Term-ProgressBar-2.21/lib/0000755000175000017500000000000013140323747014702 5ustar manwarmanwarTerm-ProgressBar-2.21/lib/Term/0000755000175000017500000000000013140323747015611 5ustar manwarmanwarTerm-ProgressBar-2.21/lib/Term/ProgressBar/0000755000175000017500000000000013140323747020042 5ustar manwarmanwarTerm-ProgressBar-2.21/lib/Term/ProgressBar/IO.pm0000644000175000017500000000540613137040044020704 0ustar manwarmanwarpackage Term::ProgressBar::IO; use strict; use warnings; our $VERSION = '2.21'; # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # Copyright 2014 by Don Armstrong . =head1 NAME Term::ProgressBar::IO -- Display a progress bar while reading from a seekable filehandle =head1 SYNOPSIS my $pb = Term::ProgressBar::IO->new($fh); while (<$fh>) { # do something $pb->update(); } =head1 DESCRIPTION Displays a progress bar using L which corresponds to reading from a filehandle. This module inherits from L and has all of its options. =head1 BUGS None known. =cut use parent qw(Term::ProgressBar); use Carp; use Fcntl qw(:seek); =head1 METHODS =head2 new Create and return a new Term::ProgressBar::IO instance. =over =item ARGUMENTS =over =item count A valid filehandle or item count. L filehandles are also properly handled. =item OTHER ARGUMENTS All other arguments are documented in L =back =back =cut sub init { my $self = shift; my $count; if (@_==2) { $count = $_[1]; } else { croak sprintf("Term::ProgressBar::IO::new We don't handle this many arguments: %d", scalar @_) if @_ != 1; } my %config; if ( UNIVERSAL::isa ($_[0], 'HASH') ) { ($count) = @{$_[0]}{qw(count)}; %config = %{$_[0]}; } else { ($count) = @_; } if (ref($count) and $count->can("seek")) { $self->{__filehandle} = $count; $count = $self->__determine_max(); } $config{count} = $count; $self->SUPER::init(\%config); } =head2 update Automatically update the progress bar based on the position of the filehandle given at construction time. =over =item ARGUMENTS =over =item so_far Current progress point; this defaults to the current position of the filehandle. [You probably don't actually want to ever give this.] =back =back =cut sub update { my $self = shift; my $count = $self->__determine_count(); $self->SUPER::update(scalar @_? @_ : $count); } sub __determine_max { my $self = shift; # is this an IO::Uncompress handle? my $max = 0; if ($self->{__filehandle}->can('getHeaderInfo')) { $self->{__filehandle} = *$self->{__filehandle}{FH}; } eval { my $cur_pos = $self->{__filehandle}->tell; $self->{__filehandle}->seek(0,SEEK_END); $max = $self->{__filehandle}->tell; $self->{__filehandle}->seek($cur_pos,SEEK_SET); }; return $max; } sub __determine_count { my $self = shift; my $count = 0; eval { $count = $self->{__filehandle}->tell; }; return $count; } 1; __END__ Term-ProgressBar-2.21/lib/Term/ProgressBar.pm0000644000175000017500000007320313137040044020375 0ustar manwarmanwarpackage Term::ProgressBar; use strict; use warnings; our $VERSION = '2.21'; #XXX TODO Redo original test with count=20 # Amount Output # Amount Prefix/Suffix # Tinker with $0? # Test use of last_update (with update(*undef*)) with scales # Choice of FH other than STDERR # If no term, output no progress bar; just progress so far # Use of simple term with v2.0 bar # If name is wider than term, trim name # Don't update progress bar on new? =head1 NAME Term::ProgressBar - provide a progress meter on a standard terminal =head1 VERSION Version 2.21 =head1 SYNOPSIS use Term::ProgressBar; my $progress = Term::ProgressBar->new ({count => 10_000}); $progress->update(5_000); =head1 DESCRIPTION Term::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 L 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 C<< $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 B characters. For long-running jobs, this may be too slow, so two additional features are available: a linear completion time estimator, and/or a B character: this is a character that I 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. =head1 EXAMPLES =head2 A 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 F in the distribution set (it is not installed as part of the module). =head2 A smoother bar update 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 == $_; } $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 I call C<< $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 F in the distribution set (it is not installed as part of the module. =head2 A (much) more efficient update my $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 L to only call it again when needed; secondly, it switches off the use of minor characters to update a lot less frequently (C<< $progress->minor(0); >>. The use of the return value of L means that the call of C<< $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 F in the distribution set (it is not installed as part of the module. =head2 When the maximum number of items is sometimes unknown Sometimes 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. =head2 Using Completion Time Estimation my $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 L option to switch on completion estimation. Also, the update return is tuned to try to update the bar approximately once per second, with the L call. See the documentation for the L method for details of the format(s) used. This example also provides an example of the use of the L function to output messages to the same filehandle whilst keeping the progress bar intact The complete text of this example is in F in the distribution set (it is not installed as part of the module. =cut use Carp qw( croak ); use Class::MethodMaker 1.02 qw( ); use Fatal qw( open sysopen close seek ); use POSIX qw( ceil strftime ); use constant MINUTE => 60; use constant HOUR => 60 * MINUTE; use constant DAY => 24 * HOUR; # The point past which to give ETA of just date, rather than time use constant ETA_DATE_CUTOFF => 3 * DAY; # The point past which to give ETA of time, rather time left use constant ETA_TIME_CUTOFF => 10 * MINUTE; # The ratio prior to which to not dare any estimates use constant PREDICT_RATIO => 0.01; use constant DEFAULTS => { lbrack => '[', rbrack => ']', minor_char => '*', major_char => '=', fh => \*STDERR, name => undef, ETA => undef, max_update_rate => 0.5, # The following defaults are never used, but the keys # are valuable for error checking count => undef, bar_width => undef, term_width => undef, term => undef, remove => 0, silent => 0, }; use constant ETA_TYPES => { map { $_ => 1 } qw( linear ) }; use constant ALREADY_FINISHED => 'progress bar already finished'; # This is here to allow testing to redirect away from the terminal but still # see terminal output, IYSWIM my $__FORCE_TERM = 0; # ---------------------------------- # CLASS HIGHER-LEVEL FUNCTIONS # ---------------------------------- # ---------------------------------- # CLASS HIGHER-LEVEL PROCEDURES # ---------------------------------- sub __force_term { my $class = shift; ($__FORCE_TERM) = @_; } # ---------------------------------- # CLASS UTILITY FUNCTIONS # ---------------------------------- sub term_size { my ( $self, $fh ) = @_; return if $self->silent; eval { require Term::ReadKey; }; if ($@) { warn "Guessing terminal width due to problem with Term::ReadKey\n"; return 50; } my $result; eval { $result = (Term::ReadKey::GetTerminalSize($fh))[0]; $result-- if ($^O eq "MSWin32" or $^O eq "cygwin"); }; if ( $@ ) { warn "error from Term::ReadKey::GetTerminalSize(): $@"; } # If GetTerminalSize() failed it should (according to its docs) # return an empty list. It doesn't - that's why we have the eval {} # above - but also it may appear to succeed and return a width of # zero. # if ( ! $result ) { $result = 50; warn "guessing terminal width $result\n"; } return $result; } # Don't document hash keys until tested that the give the desired affect! =head1 INSTANCE CONSTRUCTION =head2 new Create & return a new Term::ProgressBar instance. =over 4 =item ARGUMENTS If one argument is provided, and it is a hashref, then the hash is treated as a set of key/value pairs, with the following keys; otherwise, it is treated as a number, being equivalent to the C key. =over 4 =item count The item count. The progress is marked at 100% when update I is invoked, and proportionally until then. If you specify a count less than zero, just the name (if specified) will be displayed and (if the remove flag is set) removed when the progress bar is updated with a number lower than zero. This allows you to use the progress bar when the count is sometimes known and sometimes not without making multiple changes throughout your code. =item name A name to prefix the progress bar with. =item fh The filehandle to output to. Defaults to stderr. Do not try to use *foo{THING} syntax if you want Term capabilities; it does not work. Pass in a globref instead. =item term_width Sometimes we can't correctly determine the terminal width. You can use this parameter to force a term width of a particular size. Use a positive integer, please :) =item silent If passed a true value, Term::ProgressBar will do nothing at all. Useful in scripts where the progress bar is optional (or just plain doesn't work due to issues with modules it relies on). Instead, tell the constructor you want it to be silent and you don't need to change the rest of your program: my $progress = Term::ProgressBar->new( { count => $count, silent => $silent } ); # later $progress->update; # does nothing =item ETA A total time estimation to use. If enabled, a time finished estimation is printed on the RHS (once sufficient updates have been performed to make such an estimation feasible). Naturally, this is an I; no guarantees are made. The format of the estimate Note that the format is intended to be as compact as possible while giving over the relevant information. Depending upon the time remaining, the format is selected to provide some resolution whilst remaining compact. Since the time remaining decreases, the format typically changes over time. As the ETA approaches, the format will state minutes & seconds left. This is identifiable by the word C<'Left'> at the RHS of the line. If the ETA is further away, then an estimate time of completion (rather than time left) is given, and is identifiable by C<'ETA'> at the LHS of the ETA box (on the right of the progress bar). A time or date may be presented; these are of the form of a 24 hour clock, e.g. C<'13:33'>, a time plus days (e.g., C<' 7PM+3'> for around in over 3 days time) or a day/date, e.g. C<' 1Jan'> or C<'27Feb'>. If ETA is switched on, the return value of L is also affected: the idea here is that if the progress bar seems to be moving quicker than the eye would normally care for (and thus a great deal of time is spent doing progress updates rather than "real" work), the next value is increased to slow it. The maximum rate aimed for is tunable via the L component. The available values for this are: =over 4 =item undef Do not do estimation. The default. =item linear Perform linear estimation. This is simply that the amount of time between the creation of the progress bar and now is divided by the current amount done, and completion estimated linearly. =back =back =item EXAMPLES my $progress = Term::ProgressBar->new(100); # count from 1 to 100 my $progress = Term::ProgressBar->new({ count => 100 }); # same # Count to 200 thingies, outputting to stdout instead of stderr, # prefix bar with 'thingy' my $progress = Term::ProgressBar->new({ count => 200, fh => \*STDOUT, name => 'thingy' }); =back =cut Class::MethodMaker->import (new_with_init => 'new', new_hash_init => 'hash_init',); sub init { my $self = shift; # V1 Compatibility return $self->init({count => $_[1], name => $_[0], term_width => 50, bar_width => 50, major_char => '#', minor_char => '', lbrack => '', rbrack => '', term => '0 but true', silent => 0,}) if @_ == 2; my $target; croak sprintf("Term::ProgressBar::new We don't handle this many arguments: %d", scalar @_) if @_ != 1; my %config; if ( UNIVERSAL::isa ($_[0], 'HASH') ) { ($target) = @{$_[0]}{qw(count)}; %config = %{$_[0]}; # Copy in, so later playing does not tinker externally } else { ($target) = @_; } if ( my @bad = grep ! exists DEFAULTS->{$_}, keys %config ) { croak sprintf("Input parameters (%s) to %s not recognized\n", join(':', @bad), 'Term::ProgressBar::new'); } croak "Target count required for Term::ProgressBar new\n" unless defined $target; $config{$_} = DEFAULTS->{$_} for grep ! exists $config{$_}, keys %{DEFAULTS()}; delete $config{count}; $config{term} = -t $config{fh} unless defined $config{term}; if ( $__FORCE_TERM ) { $config{term} = 1; $config{term_width} = $__FORCE_TERM; die "term width $config{term_width} (from __force_term) too small" if $config{term_width} < 5; } elsif ( $config{term} and ! defined $config{term_width}) { $config{term_width} = $self->term_size($config{fh}); die if $config{term_width} < 5; } unless ( defined $config{bar_width} ) { if ( defined $config{term_width} ) { # 5 for the % marker $config{bar_width} = $config{term_width} - 5; $config{bar_width} -= $_ for map(( defined $config{$_} ? length($config{$_}) : 0), qw( lbrack rbrack name )); $config{bar_width} -= 2 # Extra for ': ' if defined $config{name}; $config{bar_width} -= 10 if defined $config{ETA}; if ( $config{bar_width} < 1 ) { warn "terminal width $config{term_width} too small for bar; defaulting to 10\n"; $config{bar_width} = 10; } # } elsif ( ! $config{term} ) { # $config{bar_width} = 1; # $config{term_width} = defined $config{ETA} ? 12 : 5; } else { $config{bar_width} = $target; die "configured bar_width $config{bar_width} < 1" if $config{bar_width} < 1; } } $config{start} = time; select(((select $config{fh}), $| = 1)[0]); $self->ETA(delete $config{ETA}); $self->hash_init (%config, offset => 0, scale => 1, last_update => 0, last_position => 0, ); $self->target($target); $self->minor($config{term} && $target > $config{bar_width} ** 1.5); $self->update(0); # Initialize the progress bar } # ---------------------------------- # INSTANCE FINALIZATION # ---------------------------------- # ---------------------------------- # INSTANCE COMPONENTS # ---------------------------------- =head1 INSTANCE COMPONENTS =cut =head2 Scalar Components. See L for usage. =over 4 =item target The final target. Updates are measured in terms of this. Changes will have no effect until the next update, but the next update value should be relative to the new target. So $p = Term::ProgressBar({count => 20}); # Halfway $p->update(10); # Double scale $p->target(40) $p->update(21); will cause the progress bar to update to 52.5% =item max_update_rate This value is taken as being the maximum speed between updates to aim for. B It defaults to 0.5, being the number of seconds between updates. =back =head2 Boolean Components See L for usage. =over 4 =item minor Default: set. If unset, no minor scale will be calculated or updated. Minor characters are used on the progress bar to give the user the idea of progress even when there are so many more tasks than the terminal is wide that the granularity would be too great. By default, Term::ProgressBar makes a guess as to when minor characters would be valuable. However, it may not always guess right, so this method may be called to force it one way or the other. Of course, the efficiency saving is minimal unless the client is utilizing the return value of L. See F and F to see minor characters in action, and not in action, respectively. =back =head2 Configuration =over 4 =item lbrack Left bracket ( defaults to [ ) $progress->lbrack('<'); =item rbrack Right bracket ( defaults to ] ) $progress->rbrack('>'); =back =cut # Private Scalar Components # offset ) Default: 0. Added to any value supplied to update. # scale ) Default: 1. Any value supplied to update is multiplied by # this. # major_char) Default: '='. The character printed for the major scale. # minor_char) Default: '*'. The character printed for the minor scale. # name ) Default: undef. The name to print to the side of the bar. # fh ) Default: STDERR. The filehandle to output progress to. # Private Counter Components # last_update ) Default: 0. The so_far value last time update was invoked. # last_position) Default: 0. The number of the last progress mark printed. # Private Boolean Components # term ) Default: detected (by C). # If unset, we assume that we are not connected to a terminal (or # at least, not a suitably intelligent one). Then, we attempt # minimal functionality. Class::MethodMaker->import ( get_set => [qw/ major_units major_char minor_units minor_char lbrack rbrack name offset scale fh start max_update_rate silent /], counter => [qw/ last_position last_update /], boolean => [qw/ minor name_printed pb_ended remove /], # let it be boolean to handle 0 but true get_set => [qw/ term /], ); # We generate these by hand since we want to check the values. sub bar_width { my $self = shift; return $self->{bar_width} if not @_; croak 'wrong number of arguments' if @_ != 1; croak 'bar_width < 1' if $_[0] < 1; $self->{bar_width} = $_[0]; } sub term_width { my $self = shift; return $self->{term_width} if not @_; croak 'wrong number of arguments' if @_ != 1; croak 'term_width must be at least 5' if $self->term and $_[0] < 5; $self->{term_width} = $_[0]; } sub target { my $self = shift; if ( @_ ) { my ($target) = @_; if ( $target ) { $self->major_units($self->bar_width / $target); $self->minor_units($self->bar_width ** 2 / $target); $self->minor ( defined $self->term_width and $self->term_width < $target ); } $self->{target} = $target; } return $self->{target}; } sub ETA { my $self = shift; return if $self->silent; if (@_) { my ($type) = @_; croak "Invalid ETA type: $type\n" if defined $type and ! exists ETA_TYPES->{$type}; $self->{ETA} = $type; } return $self->{ETA}; } # ---------------------------------- # INSTANCE HIGHER-LEVEL FUNCTIONS # ---------------------------------- # ---------------------------------- # INSTANCE HIGHER-LEVEL PROCEDURES # ---------------------------------- =head1 INSTANCE HIGHER-LEVEL PROCEDURES Z<> =cut sub no_minor { warn sprintf("%s: This method is deprecated. Please use %s instead\n", (caller (0))[3], '$x->minor (0)',); $_[0]->clear_minor (0); } # ------------------------------------- =head2 update Update the progress bar. =over 4 =item ARGUMENTS =over 4 =item so_far Current progress point, in whatever units were passed to C. If not defined, assumed to be 1+ whatever was the value last time C was called (starting at 0). =back =item RETURNS =over 4 =item next_call The next value of so_far at which to call C. =back =back =cut sub update { my $self = shift; # returning target+1 as next value should avoid calling update # method in the smooth form of using the progress bar return $self->target+1 if $self->silent; my ($so_far) = @_; if ( ! defined $so_far ) { $so_far = $self->last_update + 1; } my $input_so_far = $so_far; $so_far *= $self->scale unless $self->scale == 1; $so_far += $self->offset; my $target = my $next = $self->target; my $name = $self->name; my $fh = $self->fh; if ( $target < 0 ) { if($input_so_far <= 0 or $input_so_far == $self->last_update) { print $fh "\r", ' ' x $self->term_width, "\r"; if(defined $name) { if(!$self->remove or $input_so_far >= 0) { print $fh "$name..."; } if(!$self->remove and $input_so_far < 0) { print $fh "\n"; } } } $self->last_update($input_so_far); return 2**32-1; } elsif ( $target == 0 ) { print $fh "\r"; printf $fh "$name: " if defined $name; print $fh "(nothing to do)\n"; return 2**32-1; } my $biggies = $self->major_units * $so_far; my @chars = (' ') x $self->bar_width; $chars[$_] = $self->major_char for 0..$biggies-1; if ( $self->minor ) { my $smally = $self->minor_units * $so_far % $self->bar_width; $chars[$smally] = $self->minor_char unless $so_far == $target; $next *= ($self->minor_units * $so_far + 1) / ($self->bar_width ** 2); } else { $next *= ($self->major_units * $so_far + 1) / $self->bar_width; } local $\ = undef; if ( $self->term > 0 ) { local $\ = undef; my $to_print = "\r"; $to_print .= "$name: " if defined $name; my $ratio = $so_far / $target; # Rounds down % $to_print .= (sprintf ("%3d%% %s%s%s", $ratio * 100, $self->lbrack, join ('', @chars), $self->rbrack)); my $ETA = $self->ETA; if ( defined $ETA and $ratio > 0 ) { if ( $ETA eq 'linear' ) { if ( $ratio == 1 ) { my $taken = time - $self->start; my $ss = $taken % 60; my $mm = int(($taken % 3600) / 60); my $hh = int($taken / 3600); if ( $hh > 99 ) { $to_print .= sprintf('D %2dh%02dm', $hh, $mm, $ss); } else { $to_print .= sprintf('D%2dh%02dm%02ds', $hh, $mm, $ss); } } elsif ( $ratio < PREDICT_RATIO ) { # No safe prediction yet $to_print .= 'ETA ------'; } else { my $time = time; my $left = (($time - $self->start) * ((1 - $ratio) / $ratio)); if ( $left < ETA_TIME_CUTOFF ) { $to_print .= sprintf '%1dm%02ds Left', int($left / 60), $left % 60; } else { my $eta = $time + $left; my $format; if ( $left < DAY ) { $format = 'ETA %H:%M'; } elsif ( $left < ETA_DATE_CUTOFF ) { $format = sprintf('ETA %%l%%p+%d',$left/DAY); } else { $format = 'ETA %e%b'; } $to_print .= strftime($format, localtime $eta); } # Calculate next to be at least SEC_PER_UPDATE seconds away if ( $left > 0 ) { my $incr = ($target - $so_far) / ($left / $self->max_update_rate); $next = $so_far + $incr if $so_far + $incr > $next; } } } else { croak "Bad ETA type: $ETA\n"; } } for ($self->{last_printed}) { unless (defined and $_ eq $to_print) { print $fh $to_print; } $_ = $to_print; } $next -= $self->offset; $next /= $self->scale unless $self->scale == 1; if ( $so_far >= $target and $self->remove and ! $self->pb_ended) { print $fh "\r", ' ' x $self->term_width, "\r"; $self->pb_ended; } } else { local $\ = undef; if ( $self->term ) { # special case for backwards compat. if ( $so_far == 0 and defined $name and ! $self->name_printed ) { print $fh "$name: "; $self->set_name_printed; } my $position = int($self->bar_width * ($input_so_far / $target)); my $add = $position - $self->last_position; $self->last_position_incr ($add) if $add; print $fh $self->major_char x $add; $next -= $self->offset; $next /= $self->scale unless $self->scale == 1; } else { my $pc = int(100*$input_so_far/$target); printf $fh "[%s] %s: %3d%%\n", scalar(localtime), $name, $pc; $next = ceil($target * ($pc+1)/100); } if ( $input_so_far >= $target ) { if ( $self->pb_ended ) { croak ALREADY_FINISHED; } else { if ( $self->term ) { print $fh "\n" } $self->set_pb_ended; } } } $next = $target if $next > $target; $self->last_update($input_so_far); return $next; } # ------------------------------------- =head2 message Output a message. This is very much like print, but we try not to disturb the terminal. =over 4 =item ARGUMENTS =over 4 =item string The message to output. =back =back =cut sub message { my $self = shift; return if $self->silent; my ($string) = @_; chomp ($string); my $fh = $self->fh; local $\ = undef; if ( $self->term ) { print $fh "\r", ' ' x $self->term_width; print $fh "\r$string\n"; } else { print $fh "\n$string\n"; print $fh $self->major_char x $self->last_position; } undef $self->{last_printed}; $self->update($self->last_update); } # ---------------------------------------------------------------------- =head1 REPORTING BUGS via RT: L =head1 COMPATIBILITY If exactly two arguments are provided, then L 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. =head1 AUTHOR Martyn J. Pearce fluffy@cpan.org Significant contributions from Ed Avis, amongst others. =head1 MAINTAINER Gabor Szabo L L =head1 LICENSE AND COPYRIGHT Copyright (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. =cut 1; __END__ Term-ProgressBar-2.21/Makefile.PL0000644000175000017500000000172613137040044016104 0ustar manwarmanwaruse 5.006; use strict; use warnings FATAL => 'all'; use ExtUtils::MakeMaker; my $mm_ver = $ExtUtils::MakeMaker::VERSION; if ($mm_ver =~ /_/) { # dev version $mm_ver = eval $mm_ver; die $@ if $@; } WriteMakefile ( NAME => 'Term::ProgressBar', VERSION_FROM => 'lib/Term/ProgressBar.pm', ABSTRACT_FROM => 'lib/Term/ProgressBar.pm', AUTHOR => 'Martyn J. Pearce', LICENSE => 'perl', PREREQ_PM => { 'Class::MethodMaker' => '1.02', 'Term::ReadKey' => '2.14', 'Carp' => 0, 'Fatal' => 0, 'POSIX' => 0, }, BUILD_REQUIRES => { 'Capture::Tiny' => '0.13', 'Test::More' => '0.80', 'Test::Exception' => '0.31', 'File::Temp' => 0, }, META_MERGE => { resources => { repository => 'https://github.com/manwar/Term-ProgressBar', }, }, clean => { FILES => 'Term-ProgressBar-*' }, ($mm_ver >= 6.48 ? (MIN_PERL_VERSION => 5.006) : () ), ); Term-ProgressBar-2.21/README0000644000175000017500000000155613016530155015016 0ustar manwarmanwarModule Term-ProgressBar (2.09): Description: A progress bar for things that take a while. It looks like 50% [===== ] and is as long as the terminal. Linear estimation of the time left for the process to run is available. Modules & Classes Provided: Term::ProgressBar - provide a progress meter on a standard terminal Required Packages: Class::MethodMaker 1.02 Term::ReadKey 2.14 Required Perl Version: 5.006 Package Maintainer: Martyn J. Pearce fluffy@cpan.org Copyright: Copyright (c) 2005, 2004, 2003, 2002, 2001 Martyn J. Pearce. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Copyright (c) 2000 Ed Avis. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 13th March, 2005 Term-ProgressBar-2.21/META.json0000664000175000017500000000245313140323747015563 0ustar manwarmanwar{ "abstract" : "provide a progress meter on a standard terminal", "author" : [ "Martyn J. Pearce" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.24, CPAN::Meta::Converter version 2.150005", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Term-ProgressBar", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "Capture::Tiny" : "0.13", "File::Temp" : "0", "Test::Exception" : "0.31", "Test::More" : "0.80" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Carp" : "0", "Class::MethodMaker" : "1.02", "Fatal" : "0", "POSIX" : "0", "Term::ReadKey" : "2.14", "perl" : "5.006" } } }, "release_status" : "stable", "resources" : { "repository" : { "url" : "https://github.com/manwar/Term-ProgressBar" } }, "version" : "2.21", "x_serialization_backend" : "JSON::PP version 2.27400" } Term-ProgressBar-2.21/MANIFEST0000644000175000017500000000114713140323747015270 0ustar manwarmanwarChanges examples/powers examples/powers2 examples/powers3 examples/powers4 examples/powers5 examples/simple_use.pl examples/smooth_bar.pl examples/stdinorfile.pl lib/Term/ProgressBar.pm lib/Term/ProgressBar/IO.pm Makefile.PL MANIFEST This list of files MANIFEST.SKIP README t/02_term_progressbar_io.t t/compat.t t/eta-linear.t t/lessthanzero.t t/name.t t/random_file t/silent.t t/v1-message.t t/v2-message.t t/v2-mobile.t t/v2-simple.t t/zero.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Term-ProgressBar-2.21/examples/0000755000175000017500000000000013140323747015752 5ustar manwarmanwarTerm-ProgressBar-2.21/examples/simple_use.pl0000644000175000017500000000052413016530155020450 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; 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($_); } } Term-ProgressBar-2.21/examples/powers30000644000175000017500000000077513016530155017303 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; use Term::ProgressBar 2.00; use constant MAX => 100_000; my $max = int($ARGV[0] || MAX); my $progress = Term::ProgressBar->new({ name => 'Powers', count => $max, remove => 3, }); $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; Term-ProgressBar-2.21/examples/powers40000644000175000017500000000103413016530155017271 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; use Term::ProgressBar 2.00; use constant MAX => 1_000_000; my $max = int($ARGV[0] || MAX); my $progress = Term::ProgressBar->new({ name => 'Powers', count => $max, ETA => 'linear', }); #$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 == $_; } sleep 1 if $_ % 4 == 0; $next_update = $progress->update($_) if $_ > $next_update; } $progress->update($max) if $max >= $next_update; Term-ProgressBar-2.21/examples/powers0000644000175000017500000000054313016530155017211 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; use Term::ProgressBar 2.00; use constant MAX => 100_000; my $max = int($ARGV[0]+0) || MAX; 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($_); } } Term-ProgressBar-2.21/examples/powers50000644000175000017500000000114213016530155017272 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; use Term::ProgressBar 2.00; use constant MAX => 10_000_000; my $max = int($ARGV[0]+0) || MAX; my $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; Term-ProgressBar-2.21/examples/smooth_bar.pl0000644000175000017500000000046313016530155020442 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; use Term::ProgressBar 2.00; my $max = shift || 100; 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 == $_; } $progress->update($_) } Term-ProgressBar-2.21/examples/powers20000644000175000017500000000050613016530155017272 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; use Term::ProgressBar 2.00; use constant MAX => 100_000; my $max = int($ARGV[0] || MAX); 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 == $_; } $progress->update($_); } Term-ProgressBar-2.21/examples/stdinorfile.pl0000755000175000017500000000215413016530155020631 0ustar manwarmanwar#!/usr/bin/perl use strict; use warnings; use Term::ProgressBar 2.00; 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', for reading: $!"; 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', for writing: $!"; } 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"; Term-ProgressBar-2.21/META.yml0000664000175000017500000000141613140323747015411 0ustar manwarmanwar--- abstract: 'provide a progress meter on a standard terminal' author: - 'Martyn J. Pearce' build_requires: Capture::Tiny: '0.13' File::Temp: '0' Test::Exception: '0.31' Test::More: '0.80' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.24, CPAN::Meta::Converter version 2.150005' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Term-ProgressBar no_index: directory: - t - inc requires: Carp: '0' Class::MethodMaker: '1.02' Fatal: '0' POSIX: '0' Term::ReadKey: '2.14' perl: '5.006' resources: repository: https://github.com/manwar/Term-ProgressBar version: '2.21' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Term-ProgressBar-2.21/MANIFEST.SKIP0000644000175000017500000000030413016530155016022 0ustar manwarmanwar^(.*/)?CVS/.* ^.git ^Makefile(.old)?$ ^Build$ ^Clean$ ^RollingBuild$ ^blib/.* ^pm_to_blib$ ^(.*/)?.cvsignore$ ^MANIFEST.bak$ ^*~$ ^make[-.]pm$ ^INFO.yaml$ ^_build/ ^MYMETA.* cover_db/ .travis.yml Term-ProgressBar-2.21/Changes0000644000175000017500000000765213140323635015435 0ustar manwarmanwarRevision history for Perl extension Term::ProgressBar 2.21 2017-08-02 MANWAR - Added key 'clean' to Makefile.PL script. 2.20 2017-07-11 MANWAR - silent mode avoids uninitialized value messages (GFIREBALL) 2.19 2017-07-10 MANWAR - Fixed inconsistent version as reported by CPANTS. 2.18 2016-12-06 MANWAR - Add MANIFEST file. - Removed META.yml file. - Updated .gitignore to exclude MANIFEST file. - General tidy up pod document. 2.17 2015-01-23 - Handle case when the maximum number of items is unknown (LukeGoodsell) 2.16 2014-09-09 17:00:00 - Cygwin width issue RT #8344 (LukeGoodsell) - Remove unused DEBUG variables. - Stop exorting $PACKAGE $VERSION. - Add use warnings; 2.15 2014-04-13 23:23:23 - Add Term::ProgressBar::IO (DON) 2.14 2013-07-21 13:34:52 - Document the term_width argument to the constructor (OVID) - Add a "silent" option to the constructor (OVID) 2.13 2012-05-18 06:24:42 - remove unused and invalid SIGNATURE file - move content of BUGS to Changes - Remove the INSTALL and configure files, people should use the standard CPAN installation tools - Add standard prerequisites to Makefile.PL 2.12 2012-05-16 12:47:16 - use strict; use warnings; in examples - remove bareword from POD (JBAKER) - make lbrack and rbrack official. 2.11 2012-02-17 12:31:04 - skip the signature verification 2.10 2011-12-21 11:18:26 - remove Build.PL (keep the Makefile.PL only) - Replace home-made testing tools with CPAN-ish tools - Require Capture::Tiny for testing - New co-maintainer: Gabor Szabo 2.09 2005-03-13 21:17 GMT - Fix for incorrect formatting of 'D...' time done at end 2.08 2005-03-12 11:47 GMT - Add remove option - Add patch to account for weird terminal sizing under Windoze (thanks to Andrew Peters for the patch). 2.07 2005-03-06 13:31 GMT - Correct handling of non-term mode to output stats but no PB - Print time taken to complete in ETA mode when Done - Add use of 'name' to example in 'new' doc - Add doc of use of minor characters to description - Add doc. for name value to new 2.06 2004-03-14 10:46 GMT - Add patch to cope when terminal size cannot be detected or is too small. Thanks to Ed Avis () for the patch. - Add patch to test to avoid failure on windoze due to unlinking open files - Add patch to cope when Term::ReadKey fails to initialize for some reason. Thanks to Scott Cain () for the patch. - Add patch to suppress unnecessary terminal updates Thanks to Ed Avis () for the patch. 2.05 2003-08-30 16:23 GMT - Fix test.pm to handle OS (e.g., Solaris) who refuse to delete the cwd 2.04 2003-08-14 16:38 GMT - Change build system to accomodate CPAN & automated tests 2.03 2003-01-11 15:47 GMT - Fix incorrect reset of progress bar in message method Thanks to Frank Maas () for the patch. - Improve documentation of ETA display formats. 2.02 2002-11-19 10:08 GMT - Fix behaviour in terminals where GetTerminalSize fails (e.g., resized Emacs term windows). Thanks to Ed Avis for the patch. 2.01 2002-10-07 21:12 GMT - Make it 5.005_03-compatible, with thanks to Ed Avis 2.00 2002-03-10 05:26 GMT - New API added; now takes one hashref as argument (see docs) - v1 API remains, but is deprecated - Add message method to Term::ProgressBar - Add v2 tests 1.51 2001-12-02 12:22 GMT - Correct Bug #001 Wrong minor character (= should be *) selected by default. 1.50 2001-12-01 13:11 GMT - Merged with Utility::Progress by Martyn J. Pearce 1.00 2001-10-30 - original version - by Edward Avis,