blib/lib/Term/Vspark.pm

filter: Statementstmt 91.6% 66 / 72 Branchbran 57.6% 15 / 26 Conditioncond 52.9% 9 / 17 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 100% 11 / 11 Podpod 50.0% 1 / 2 totaltotal 74.1% 103 / 139 CC 21 SCAR 24.8
CC
21
Cov
78%
_term_width
33.8
vspark
10.3
_bar
4.0
CRAP 11.9
↑ Summary
1 package Term::Vspark;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp qw{ croak };
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Exporter::Shiny qw/show_graph vspark/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = 0.34;
11  
12 48 ▶sub _bar {
Subroutine _bar: called CC=4 SCAR=13.9
13 48 my (%args) = @_;
14  
15 48 my $value = $args{value};
16 48 my $max = $args{max};
17 48 my $columns = $args{columns};
18 48 my $char = $args{char};
19  
20 48 ▶ my @char_list = $char
Branch: 1 branch
BranchTrueFalseTotal
$char ? : 6 42 48
21 ? ($char)
22 : (qw{ ▏ ▎ ▍ ▌ ▋ ▊ ▉ █});
23  
24 # calculate the length of the bar
25 48 my $length = $value * $columns / $max; # length of the bar
26 48 ▶ $length = $columns if $length > $columns;
Branch: 1 branch
BranchTrueFalseTotal
if $length > $columns 4 44 48
27  
28 # empty $bar
29 48 my $bar = '';
30  
31 # build integer portion of the bar
32 48 my $integer = int $length;
33 48 $bar .= $char_list[-1] x $integer;
34  
35 # build decimal portion of the bar
36 48 my $decimal = $length - $integer;
37 48 ▶ if ($decimal > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimal > 0) 9 39 48
38 9 my $index = int scalar @char_list * $decimal;
39 9 $bar .= $char_list[$index];
40 }
41  
42 48 return $bar;
43 }
44  
45 7 ▶sub vspark {
Subroutine vspark: called CC=10 SCAR=23.4
Pod documented
46 7 my %args = @_;
47  
48 croak 'values is not an ArrayRef'
49 7 ▶ if ref $args{'values'} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"values"} ne "ARRAY" 0 7 7
50  
51 croak 'labels is not an ArrayRef'
52 7 ▶ if $args{'labels'} && ref $args{'labels'} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"labels"} and ref $args{"labels"} ne "ARRAY" 0 7 7
Condition66%2/3
$args{"labels"} and ref $args{"labels"} ne "ARRAY"
!ll&&!rl&&r
160
MC/DC0%0/2
$args{"labels"} and ref $args{"labels"} ne "ARRAY"
$args{"labels"} ref $args{"labels"} ne "ARRAY"
Truth table66%2/3
A and B
A= $args{"labels"}B= ref $args{"labels"} ne "ARRAY"
ABresult
0X0
100
111
53  
54 7 my $max = $args{max};
55 7 my $columns = _term_width($args{columns});
56 7 ▶ my @labels = @{ $args{labels} || [] };
Condition100%2/2
$args{'labels'} || []
l!l
61
MC/DC100%1/1
$args{'labels'} || []
$args{'labels'}
Truth table100%2/2
A
A= $args{'labels'}
Aresult
11
00
57 7 my @values = @{ $args{values} };
58 7 my $char = $args{char};
59  
60 croak 'the number of labels and values must be equal'
61 7 ▶ if $args{labels} && scalar @labels != scalar @values;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"labels"} and scalar @labels != scalar @values 0 7 7
Condition66%2/3
$args{"labels"} and scalar @labels != scalar @values
!ll&&!rl&&r
160
MC/DC0%0/2
$args{"labels"} and scalar @labels != scalar @values
$args{"labels"} scalar @labels != scalar @values
Truth table66%2/3
A and B
A= $args{"labels"}B= scalar @labels != scalar @values
ABresult
0X0
100
111
62  
63 7 ▶ $max //= _max_value(@values);
Condition66%2/3
$max //= _max_value(@values)
l!l&&r!l&&!r
610
MC/DC0%0/2
$max //= _max_value(@values)
$max _max_value(@values)
Truth table66%2/3
A //= B
A= $maxB= _max_value(@values)
ABresult
1X1
011
000
64 7 my $label_width = _max_label_width(@labels);
65 7 my $bar_width = $columns - $label_width;
66 7 my $graph = q{};
67  
68 7 for my $value (@values) {
69 48 my $label = shift @labels;
70 48 my $bar = _bar(
71 value => $value,
72 max => $max,
73 columns => $bar_width,
74 char => $char,
75 );
76  
77 48 ▶ $graph .= sprintf("%${label_width}s", " $label ") if defined $label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label 36 12 48
78 48 $graph .= $bar . "\n";
79 }
80  
81 7 return $graph;
82 }
83  
84 # for backwards compatibility
85 7 ▶sub show_graph { vspark(@_) }
Subroutine show_graph: called CC=1 SCAR=0.0
Pod undocumented
86  
87 7 ▶sub _term_width {
Subroutine _term_width: called CC=7 SCAR=35.2
88 7 my $columns = shift;
89 7 ▶ return $columns if $columns && $columns ne 'max';
Branch: 1 branch
BranchTrueFalseTotal
if $columns and $columns ne "max" 7 0 7
Condition33%1/3
$columns and $columns ne "max"
!ll&&!rl&&r
007
MC/DC0%0/2
$columns and $columns ne "max"
$columns $columns ne "max"
Truth table33%1/3
A and B
A= $columnsB= $columns ne "max"
ABresult
0X0
100
111
90  
91 0 require Term::ReadKey;
92 0 my ($cols) = Term::ReadKey::GetTerminalSize(*STDOUT);
93 0 ▶ return 80 if !$cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols 0 0 0
94 0 ▶ return $cols if $columns && $columns eq 'max';
Branch: 1 branch
BranchTrueFalseTotal
if $columns and $columns eq "max" 0 0 0
Condition0%0/3
$columns and $columns eq "max"
!ll&&!rl&&r
000
MC/DC0%0/2
$columns and $columns eq "max"
$columns $columns eq "max"
Truth table0%0/3
A and B
A= $columnsB= $columns eq "max"
ABresult
0X0
100
111
95 0 ▶ return 80 if $cols > 80;
Branch: 1 branch
BranchTrueFalseTotal
if $cols > 80 0 0 0
96 0 return $cols;
97 }
98  
99 1 ▶sub _max_value {
Subroutine _max_value: called CC=2 SCAR=7.0
100 1 my @values = @_;
101 1 my @sorted = sort @values;
102 1 ▶ return 0 unless @sorted;
Branch: 1 branch
BranchTrueFalseTotal
unless @sorted 0 1 1
103 1 return $sorted[-1];
104 }
105  
106 7 ▶sub _max_label_width {
Subroutine _max_label_width: called CC=2 SCAR=6.9
107 7 my @labels = @_;
108  
109 7 ▶ return 0 if scalar @labels == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @labels == 0 1 6 7
110  
111 6 my @lengths = sort map { length $_ } @labels;
112 6 return $lengths[-1] + 2; # + 2 because of 1 space before and after label
113 }
114  
115 3 1;
116 __END__
↑ Summary