]> git.donarmstrong.com Git - term-progressbar.git/commitdiff
Add a 'silent' option to the constructor.
authorOvid <curtis_ovid_poe@yahoo.com>
Fri, 12 Jul 2013 11:18:10 +0000 (13:18 +0200)
committerOvid <curtis_ovid_poe@yahoo.com>
Fri, 12 Jul 2013 11:18:10 +0000 (13:18 +0200)
Changes
lib/Term/ProgressBar.pm
t/silent.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 0c9a75efd1846250823617bdf3444a4aa5b7a2c3..ad2050033939d118f1bd5adf34a491845cffe768 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
 Revision history for Perl extension Term-ProgressBar.
 
+2.14
+    - Document the term_width argument to the constructor
+    - Add a "silent" option to the constructor
+
 2.13 Fri May 18 06:24:42 2012
        - remove unused and invalid SIGNATURE file
        - move content of BUGS to Changes
index eecbaa35daa6162c9cfd67875f871be5abc88fd6..fb52eef56247a7f893822330e3ad80cf9ed7c42e 100644 (file)
@@ -240,6 +240,7 @@ use constant DEFAULTS => {
                           term_width => undef,
                           term       => undef,
                           remove     => 0,
+                          silent     => 0,
                          };
 
 use constant ETA_TYPES => { map { $_ => 1 } qw( linear ) };
@@ -284,7 +285,8 @@ sub __force_term {
 # ----------------------------------
 
 sub term_size {
-  my ($fh) = @_;
+  my ( $self, $fh ) = @_;
+  return if $self->silent;
 
   eval {
     require Term::ReadKey;
@@ -364,6 +366,19 @@ 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
@@ -429,13 +444,15 @@ Class::MethodMaker->import (new_with_init => 'new',
 
 sub init {
   my $self = shift;
+  return if $self->silent;
 
   # 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', })
+                      term       => '0 but true',
+                      silent     => 0,})
     if @_ == 2;
 
   my $target;
@@ -475,7 +492,7 @@ sub init {
     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} = term_size($config{fh});
+    $config{term_width} = $self->term_size($config{fh});
     die if $config{term_width} < 5;
   }
 
@@ -637,6 +654,7 @@ Class::MethodMaker->import
                          offset      scale
                          fh          start
                          max_update_rate
+                         silent
                      /],
    counter       => [qw/ last_position last_update /],
    boolean       => [qw/ minor name_printed pb_ended remove /],
@@ -647,6 +665,7 @@ Class::MethodMaker->import
 # We generate these by hand since we want to check the values.
 sub bar_width {
     my $self = shift;
+    return if $self->silent;
     return $self->{bar_width} if not @_;
     croak 'wrong number of arguments' if @_ != 1;
     croak 'bar_width < 1' if $_[0] < 1;
@@ -654,6 +673,7 @@ sub bar_width {
 }
 sub term_width {
     my $self = shift;
+    return if $self->silent;
     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;
@@ -662,6 +682,7 @@ sub term_width {
 
 sub target {
   my $self = shift;
+  return if $self->silent;
 
   if ( @_ ) {
     my ($target) = @_;
@@ -680,7 +701,7 @@ sub target {
 
 sub ETA {
   my $self = shift;
-
+  return if $self->silent;
   if (@_) {
     my ($type) = @_;
     croak "Invalid ETA type: $type\n"
@@ -748,6 +769,7 @@ The next value of so_far at which to call C<update>.
 
 sub update {
   my $self = shift;
+  return if $self->silent;
   my ($so_far) = @_;
 
   if ( ! defined $so_far ) {
@@ -927,6 +949,7 @@ The message to output.
 
 sub message {
   my $self = shift;
+  return if $self->silent;
   my ($string) = @_;
   chomp ($string);
 
diff --git a/t/silent.t b/t/silent.t
new file mode 100644 (file)
index 0000000..664087c
--- /dev/null
@@ -0,0 +1,61 @@
+# (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 => 8;
+use Test::Exception;
+
+use Capture::Tiny qw(capture_stderr);
+
+my $MESSAGE1 = 'Walking on the Milky Way';
+
+# -------------------------------------
+
+=head2 Test
+
+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 9--11: 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 sill have no output';
+}
+
+# ----------------------------------------------------------------------------