File Coverage

blib/lib/Pod/Simple/Progress.pm
Criterion Covered Total %
statement 6 32 18.7
branch 0 18 0.0
condition 0 5 0.0
subroutine 2 9 22.2
pod 0 7 0.0
total 8 71 11.2


line stmt bran cond sub pod time code
1             package Pod::Simple::Progress;
2 1     1   856 use strict;
  1         2  
  1         29  
3 1     1   4 use warnings;
  1         3  
  1         536  
4              
5             our $VERSION = '3.45';
6              
7             # Objects of this class are used for noting progress of an
8             # operation every so often. Messages delivered more often than that
9             # are suppressed.
10             #
11             # There's actually nothing in here that's specific to Pod processing;
12             # but it's ad-hoc enough that I'm not willing to give it a name that
13             # implies that it's generally useful, like "IO::Progress" or something.
14             #
15             # -- sburke
16             #
17             #--------------------------------------------------------------------------
18              
19             sub new {
20 0     0 0   my($class,$delay) = @_;
21 0   0       my $self = bless {'quiet_until' => 1}, ref($class) || $class;
22 0           $self->to(*STDOUT{IO});
23 0 0         $self->delay(defined($delay) ? $delay : 5);
24 0           return $self;
25             }
26              
27             sub copy {
28 0     0 0   my $orig = shift;
29 0           bless {%$orig, 'quiet_until' => 1}, ref($orig);
30             }
31             #--------------------------------------------------------------------------
32              
33             sub reach {
34 0     0 0   my($self, $point, $note) = @_;
35 0 0         if( (my $now = time) >= $self->{'quiet_until'}) {
36 0           my $goal;
37 0           my $to = $self->{'to'};
38             print $to join('',
39             ($self->{'quiet_until'} == 1) ? () : '... ',
40             (defined $point) ? (
41             '#',
42 0 0 0       ($goal = $self->{'goal'}) ? (
    0          
    0          
    0          
43             ' ' x (length($goal) - length($point)),
44             $point, '/', $goal,
45             ) : $point,
46             $note ? ': ' : (),
47             ) : (),
48             $note || '',
49             "\n"
50             );
51 0           $self->{'quiet_until'} = $now + $self->{'delay'};
52             }
53 0           return $self;
54             }
55              
56             #--------------------------------------------------------------------------
57              
58             sub done {
59 0     0 0   my($self, $note) = @_;
60 0           $self->{'quiet_until'} = 1;
61 0           return $self->reach( undef, $note );
62             }
63              
64             #--------------------------------------------------------------------------
65             # Simple accessors:
66              
67             sub delay {
68 0 0   0 0   return $_[0]{'delay'} if @_ == 1; $_[0]{'delay'} = $_[1]; return $_[0] }
  0            
  0            
69             sub goal {
70 0 0   0 0   return $_[0]{'goal' } if @_ == 1; $_[0]{'goal' } = $_[1]; return $_[0] }
  0            
  0            
71             sub to {
72 0 0   0 0   return $_[0]{'to' } if @_ == 1; $_[0]{'to' } = $_[1]; return $_[0] }
  0            
  0            
73              
74             #--------------------------------------------------------------------------
75              
76             unless(caller) { # Simple self-test:
77             my $p = __PACKAGE__->new->goal(5);
78             $p->reach(1, "Primus!");
79             sleep 1;
80             $p->reach(2, "Secundus!");
81             sleep 3;
82             $p->reach(3, "Tertius!");
83             sleep 5;
84             $p->reach(4);
85             $p->reach(5, "Quintus!");
86             sleep 1;
87             $p->done("All done");
88             }
89              
90             #--------------------------------------------------------------------------
91             1;
92             __END__