Synchronize/ProgressBar.pm

filter: Statementstmt 32.6% 15 / 46 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 5 / 10 Podpod 100% 4 / 4 totaltotal 30.0% 24 / 80 CC 9 SCAR 22.5
CC
9
Cov
24%
add
20.0
new
12.0
done
12.0
CRAP 9.5
« Synchronize/Timerange.pm ↑ Summary Synchronize/Logger.pm »
1 package Image::Synchronize::ProgressBar;
2
3 1 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 =head1 NAME
6
7 Image::Synchronize::ProgressBar - A simple progress bar that also
8 works in closures.
9
10 =head1 SYNOPSIS
11
12 =head1 DESCRIPTION
13
14 This class implements a simple progress bar based on
15 L<Term::ProgressBar>, somewhat similar to
16 L<Term::ProgressBar::Simple>. The latter doesn't work well in
17 closures; the current implementation does.
18
19 =head1 METHODS
20
21 =cut
22
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 1 ▶use IO::Interactive qw(is_interactive);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Term::ProgressBar;
Subroutine BEGIN: called CC=1 SCAR=0.0
28
29 =head2 new
30
31 $pb = Image::Synchronize::ProgressBar->new(%options);
32
33 Creates a new instance of the progress bar. Accepts the following
34 options (mostly similar to L<Term::ProgressBar>):
35
36 =over
37
38 =item count
39
40 The target count for the progress bar. When the progress count
41 reaches this value, then the task is assumed to be complete.
42
43 =item fh
44
45 The filehandle to output to. Defaults to STDERR.
46
47 =item name
48
49 The name of the progress bar. This name is printed at the left-hand
50 side of the progress bar.
51
52 =item show_bar
53
54 Should the progress bar be displayed? If set to a true value, then
55 the progress bar is always displayed. If set to a false value, then
56 the progress bar is not displayed, but the L<message> method still
57 prints to standard output (unlike for the C<silent> option). If not
58 set, then the progress bar is displayed only if the program detects
59 that it is running interactively.
60
61 This option is not inherited from C<Term::ProgressBar>.
62
63 =item silent
64
65 If set to a true value, then the progress bar does nothing. In
66 particular, it does not print anything when you call the L<message>
67 method on it. Is ignored if C<show_bar> is defined. For an
68 alternative, see C<show_bar>.
69
70 =item term_width
71
72 The width of the terminal. Use if the automatic detection fails.
73
74 =back
75
76 The progress bar is removed when the task is complete, and displays an
77 estimate of how long it will take for the task to complete.
78
79 =cut
80
81 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
82 0 my ( $class, $options ) = @_;
83 0 my $self;
84 my $interactive;
85 0 ▶ if ( defined $options->{show_bar} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{'show_bar'}) { } 0 0 0
86 0 $interactive = $options->{show_bar};
87 0 delete $options->{show_bar};
88 0 delete $options->{silent}; # show_bar trumps silent
89 }
90 else {
91 0 $interactive = is_interactive();
92 }
93 0 ▶ if ($interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interactive) { } 0 0 0
94 0 $options->{ETA} = 'linear';
95 0 $self = bless {
96 backend => Term::ProgressBar->new($options),
97 progress => 0,
98 next_update => 0,
99 }, $class;
100 0 $self->{target} = $options->{count};
101 0 $self->{backend}->remove(1);
102 }
103 else {
104 0 $self = bless {}, $class;
105 }
106 0 $self->{interactive} = $interactive;
107 0 return $self;
108 }
109
110 =head2 add
111
112 $pb->add($amount);
113 $pb->add; # adds 1
114
115 Add the indicated C<$amount>, or 1, toward the target of the progress
116 bar.
117
118 =cut
119
120 0 ▶sub add {
Subroutine add: not called CC=4 SCAR=30.0
Pod documented
121 0 my ( $self, $amount ) = @_;
122 0 ▶ if ( $self->{interactive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"interactive"}) 0 0 0
123 0 ▶ $amount //= 1;
Condition0%0/2
$amount //= 1
l!l
00
MC/DC0%0/1
$amount //= 1
$amount
Truth table0%0/2
A
A= $amount
Aresult
11
00
124 0 $self->{progress} += $amount;
125 0 ▶ if ( $self->{progress} >= $self->{next_update} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"progress"} >= $self->{"next_update"}) 0 0 0
126 0 $self->{next_update} = $self->{backend}->update( $self->{progress} );
127 }
128 }
129 0 return $self;
130 }
131
132 =head2 done
133
134 $pb->done;
135
136 Declare the task to be complete.
137
138 =cut
139
140 0 ▶sub done {
Subroutine done: not called CC=3 SCAR=24.8
Pod documented
141 0 my ($self) = @_;
142 0 ▶ $self->{backend}->update( $self->{target} ) if $self and $self->{backend};
Branch: 1 branch
BranchTrueFalseTotal
if $self and $self->{"backend"} 0 0 0
Condition0%0/3
$self and $self->{"backend"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self and $self->{"backend"}
$self $self->{"backend"}
Truth table0%0/3
A and B
A= $selfB= $self->{"backend"}
ABresult
0X0
100
111
143 }
144
145 =head2 message
146
147 $pb->message(@message);
148
149 Prints a message (like L<print>), taking into account the progress
150 bar.
151
152 =cut
153
154 0 ▶sub message {
Subroutine message: not called CC=2 SCAR=17.9
Pod documented
155 0 my ( $self, @message ) = @_;
156 0 ▶ if ( $self->{interactive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'interactive'}) { } 0 0 0
157 0 $self->{backend}->message(@message);
158 }
159 else {
160 0 print @message;
161 }
162 0 return $self;
163 }
164
165 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
166 0 my ($self) = @_;
167 0 $self->done;
168 }
169
170 =head1 DEPENDENCIES
171
172 This module uses the following non-core Perl modules:
173
174 =over
175
176 =item
177
178 L<IO::Interactive>
179
180 =item
181
182 L<Term::ProgressBar>
183
184 =back
185
186 =head1 AUTHOR
187
188 Louis Strous E<lt>LS@quae.nlE<gt>
189
190 =head1 LICENSE AND COPYRIGHT
191
192 Copyright (c) 2016-2018 Louis Strous.
193
194 This program is free software; you can redistribute it and/or modify
195 it under the same terms as Perl itself.
196
197 =cut
198
199 1 1;
« Synchronize/Timerange.pm ↑ Summary Synchronize/Logger.pm »