blib/lib/Console/ProgressBar.pm

filter: Statementstmt 88.8% 48 / 54 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 62.5% 5 / 8 totaltotal 85.3% 70 / 82 CC 5 SCAR 4.8
CC
5
Cov
87%
new
3.0
next
2.0
back
2.0
CRAP 1.6
↑ Summary
1 package Console::ProgressBar;
2 5 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = "1.01";
8  
9 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
10 4 my ($class,$_title,$_maxValue,$configuration) = @_;
11 4 my $this = {
12 _index => 0,
13 title => $_title,
14 titleMaxSize => 30,
15 maxValue => $_maxValue,
16 length => 20,
17 segment => '#'
18 };
19 4 bless($this,$class);
20  
21 4 ▶ if(defined($configuration)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configuration) 1 3 4
22 1 my %conf = %{ $configuration };
23 1 foreach my $parameter (keys %conf) {
24 3 $this->{$parameter} = $conf{$parameter};
25 }
26 }
27  
28 4 return $this;
29 }
30  
31 7 ▶sub _calculateCurrentValue {
Subroutine _calculateCurrentValue: called CC=1 SCAR=0.0
32 7 my($this) = @_;
33 7 return int( ( $this->{_index} / $this->{maxValue} ) * 100 );
34 }
35  
36 7 ▶sub _getGraphicBars {
Subroutine _getGraphicBars: called CC=1 SCAR=0.0
37 7 my($this,$percentage) = @_;
38 7 my $bars = "";
39 7 my $nbrBars = int( ($percentage / 100) * $this->{length} );
40 7 $bars = $this->{segment} x $nbrBars;
41  
42 7 return $bars;
43 }
44  
45 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
46 1 my ($this) = @_;
47 1 $this->{_index} = 0;
48
49 1 return $this;
50 }
51  
52 1 ▶sub setTitle {
Subroutine setTitle: called CC=1 SCAR=0.0
Pod documented
53 1 my ($this,$title) = @_;
54 1 $this->{title} = $title;
55  
56 1 return $this;
57 }
58  
59 3 ▶sub setIndex {
Subroutine setIndex: called CC=1 SCAR=0.0
Pod documented
60 3 my ($this,$value) = @_;
61 3 $this->{_index} = $value;
62  
63 3 return $this;
64 }
65  
66 1 ▶sub getIndex {
Subroutine getIndex: called CC=1 SCAR=0.0
Pod undocumented
67 1 my ($this)= @_;
68 1 return $this->{_index};
69 }
70  
71 100 ▶sub next {
Subroutine next: called CC=2 SCAR=7.0
Pod documented
72 100 my ($this) = @_;
73 100 ▶ if( $this->{_index} < $this->{maxValue} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_index"} < $this->{"maxValue"}) 100 0 100
74 100 $this->{_index}++;
75 }
76 100 return $this;
77 }
78  
79 40 ▶sub back {
Subroutine back: called CC=2 SCAR=7.0
Pod documented
80 40 my ($this) = @_;
81 40 ▶ if($this->{_index} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"_index"} > 0) 40 0 40
82 40 $this->{_index}--;
83 }
84 40 return $this;
85 }
86  
87 0 ▶sub render {
Subroutine render: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my ($this) = @_;
89  
90 0 my $percentage = $this->_calculateCurrentValue();
91 0 my $bars = $this->_getGraphicBars($percentage);
92  
93 0 my $progressBar = sprintf("%-$this->{titleMaxSize}s [%-$this->{length}s] %d%%",$this->{title},$bars,$percentage);
94 0 print "$progressBar\r";
95 0 $|++;
96 }
97  
98 5 1;
99 __END__
↑ Summary