File Coverage

blib/lib/ProgressMonitor/Stringify/Fields/Spinner.pm
Criterion Covered Total %
statement 39 39 100.0
branch 2 4 50.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 52 54 96.3


line stmt bran cond sub pod time code
1             package ProgressMonitor::Stringify::Fields::Spinner;
2            
3 2     2   20383 use warnings;
  2         3  
  2         49  
4 2     2   9 use strict;
  2         3  
  2         97  
5            
6             require ProgressMonitor::Stringify::Fields::AbstractField if 0;
7            
8             # Attributes:
9             # index
10             # Keeps track of which part of the spinner should be rendered this iteration
11             #
12             use classes
13 2         11 extends => 'ProgressMonitor::Stringify::Fields::AbstractField',
14             new => 'new',
15 2     2   8 attrs_pr => ['index',];
  2         3  
16            
17             sub new
18             {
19 1     1   17 my $class = shift;
20 1         3 my $cfg = shift;
21            
22 1         14 my $self = $class->SUPER::_new($cfg, $CLASS);
23            
24 1         10 $cfg = $self->_get_cfg;
25            
26 1         4 $self->{$ATTR_index} = 0;
27 1         3 $self->_set_width(length($cfg->get_sequence->[0]));
28            
29 1         13 return $self;
30             }
31            
32             sub render
33             {
34 13     13 1 16 my $self = shift;
35             # arguments not used...
36             # my $state = shift;
37             # my $ticks = shift;
38             # my $totalTicks = shift;
39             # my $clean = shift;
40            
41 13         36 my $seq = $self->_get_cfg->get_sequence;
42 13         81 return $seq->[$self->{$ATTR_index}++ % @$seq];
43             }
44            
45             ###
46            
47             package ProgressMonitor::Stringify::Fields::SpinnerConfiguration;
48            
49 2     2   760 use strict;
  2         3  
  2         45  
50 2     2   9 use warnings;
  2         3  
  2         64  
51            
52             # Attributes
53             # sequence (array ref with strings)
54             # For every rendition the next string will be printed in a round-robin
55             # fashion. Note that all strings must be of equal length. Default is a single
56             # character spinner, but consider ['OOO', 'XXX'] or
57             # ['>....', '.>...', '..>..', '...>.', '....>'] as fun variations. I'm sure you
58             # can come up with others :-). And of course, ['Hello'] just devolves to
59             # the same effect as the Fixed field...
60             #
61             use classes
62 2         8 extends => 'ProgressMonitor::Stringify::Fields::AbstractFieldConfiguration',
63             attrs => ['sequence'],
64 2     2   8 ;
  2         8  
65            
66             sub defaultAttributeValues
67             {
68 1     1   2 my $self = shift;
69            
70 1         2 return {%{$self->SUPER::defaultAttributeValues()}, sequence => ['-', '\\', '|', '/']};
  1         11  
71             }
72            
73             sub checkAttributeValues
74             {
75 1     1   2 my $self = shift;
76            
77 1         8 $self->SUPER::checkAttributeValues;
78            
79 1         4 my $seq = $self->get_sequence;
80 1 50       8 X::Usage->throw("sequence must be an array") unless ref($seq) eq 'ARRAY';
81 1         4 my $len = length($seq->[0]);
82 1         3 for (@$seq)
83             {
84 4 50       10 X::Usage->throw("all sequence elements must have same length") if length($_) != $len;
85             }
86            
87 1         5 return;
88             }
89            
90             ############################
91            
92             =head1 NAME
93            
94             ProgressMonitor::Stringify::Field::Spinner - a field implementation that renders progress
95             as a spinner.
96            
97             =head1 SYNOPSIS
98            
99             # call someTask and give it a monitor to call us back
100             #
101             my $spinner = ProgressMonitor::Stringify::Fields::Spinner->new;
102             someTask(ProgressMonitor::Stringify::ToStream->new({fields => [ $spinner ]});
103            
104             =head1 DESCRIPTION
105            
106             This is a fixed size field representing progress as sequence of strings to display while work
107             is progressing.
108            
109             Inherits from ProgressMonitor::Stringify::Fields::AbstractField.
110            
111             =head1 METHODS
112            
113             =over 2
114            
115             =item new( $hashRef )
116            
117             Configuration data:
118            
119             =over 2
120            
121             =item sequence (default => ['-', '\\', '|', '/'])
122            
123             The sequence of strings to alternate between. All strings must be of the same length.
124            
125             =back
126            
127             =back
128            
129             =head1 AUTHOR
130            
131             Kenneth Olwing, C<< >>
132            
133             =head1 BUGS
134            
135             I wouldn't be surprised! If you can come up with a minimal test that shows the
136             problem I might be able to take a look. Even better, send me a patch.
137            
138             Please report any bugs or feature requests to
139             C, or through the web interface at
140             L.
141             I will be notified, and then you'll automatically be notified of progress on
142             your bug as I make changes.
143            
144             =head1 SUPPORT
145            
146             You can find general documentation for this module with the perldoc command:
147            
148             perldoc ProgressMonitor
149            
150             =head1 ACKNOWLEDGEMENTS
151            
152             Thanks to my family. I'm deeply grateful for you!
153            
154             =head1 COPYRIGHT & LICENSE
155            
156             Copyright 2006,2007 Kenneth Olwing, all rights reserved.
157            
158             This program is free software; you can redistribute it and/or modify it
159             under the same terms as Perl itself.
160            
161             =cut
162            
163             1; # End of ProgressMonitor::Stringify::Fields::Spinner