]> git.donarmstrong.com Git - term-progressbar.git/blobdiff - lib/Term/ProgressBar.pm
Add a 'silent' option to the constructor.
[term-progressbar.git] / lib / Term / ProgressBar.pm
index 1dfea0601ab0a71cf87fff0ab0bc6c4a413ab37a..fb52eef56247a7f893822330e3ad80cf9ed7c42e 100644 (file)
@@ -1,5 +1,3 @@
-# (X)Emacs mode: -*- cperl -*-
-
 package Term::ProgressBar;
 
 #XXX TODO Redo original test with count=20
@@ -13,18 +11,18 @@ package Term::ProgressBar;
 #         If name is wider than term, trim name
 #         Don't update progress bar on new?
 
-=head1 NAME
+=head1 NAME
 
 Term::ProgressBar - provide a progress meter on a standard terminal
 
-=head1 SYNOPSIS
+=head1 SYNOPSIS
 
   use Term::ProgressBar;
 
   $progress = Term::ProgressBar->new ({count => $count});
   $progress->update ($so_far);
 
-=head1 DESCRIPTION
+=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,
@@ -149,7 +147,7 @@ distribution set (it is not installed as part of the module.
 
   my $progress = Term::ProgressBar->new({name  => 'Powers',
                                          count => $max,
-                                         ETA   => linear, });
+                                         ETA   => 'linear', });
   $progress->max_update_rate(1);
   my $next_update = 0;
 
@@ -242,6 +240,7 @@ use constant DEFAULTS => {
                           term_width => undef,
                           term       => undef,
                           remove     => 0,
+                          silent     => 0,
                          };
 
 use constant ETA_TYPES => { map { $_ => 1 } qw( linear ) };
@@ -254,7 +253,7 @@ use constant DEBUG => 0;
 
 use vars qw($PACKAGE $VERSION);
 $PACKAGE = 'Term-ProgressBar';
-$VERSION = '2.10';
+$VERSION = '2.14';
 
 # ----------------------------------
 # CLASS CONSTRUCTION
@@ -286,7 +285,8 @@ sub __force_term {
 # ----------------------------------
 
 sub term_size {
-  my ($fh) = @_;
+  my ( $self, $fh ) = @_;
+  return if $self->silent;
 
   eval {
     require Term::ReadKey;
@@ -360,6 +360,25 @@ 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
@@ -425,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;
@@ -471,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;
   }
 
@@ -496,7 +517,7 @@ sub init {
     } else {
       $config{bar_width}  = $target;
       die "configured bar_width $config{bar_width} < 1"
-       if $config{bar_width} < 1;
+      if $config{bar_width} < 1;
     }
   }
 
@@ -585,6 +606,24 @@ 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
@@ -615,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 /],
@@ -625,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;
@@ -632,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;
@@ -640,6 +682,7 @@ sub term_width {
 
 sub target {
   my $self = shift;
+  return if $self->silent;
 
   if ( @_ ) {
     my ($target) = @_;
@@ -658,7 +701,7 @@ sub target {
 
 sub ETA {
   my $self = shift;
-
+  return if $self->silent;
   if (@_) {
     my ($type) = @_;
     croak "Invalid ETA type: $type\n"
@@ -726,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 ) {
@@ -820,10 +864,10 @@ sub update {
       }
     }
     for ($self->{last_printed}) {
-       unless (defined and $_ eq $to_print) {
-           print $fh $to_print;
-       }
-       $_ = $to_print;
+        unless (defined and $_ eq $to_print) {
+            print $fh $to_print;
+        }
+        $_ = $to_print;
     }
 
     $next -= $self->offset;
@@ -905,6 +949,7 @@ The message to output.
 
 sub message {
   my $self = shift;
+  return if $self->silent;
   my ($string) = @_;
   chomp ($string);
 
@@ -924,13 +969,9 @@ sub message {
 
 # ----------------------------------------------------------------------
 
-=head1 BUGS
-
-Z<>
-
 =head1 REPORTING BUGS
 
-Email the author.
+via RT: L<https://rt.cpan.org/Dist/Display.html?Name=Pipe>
 
 =head1 COMPATIBILITY
 
@@ -940,22 +981,22 @@ 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
+=head1 AUTHOR
 
 Martyn J. Pearce fluffy@cpan.org
 
 Significant contributions from Ed Avis, amongst others.
 
+=head1 MAINTAINER
+
+Gabor Szabo L<http://szabgab.com/>
+
 =head1 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.
 
-=head1 SEE ALSO
-
-Z<>
-
 =cut
 
 1; # keep require happy.