| 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/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = 0.32; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub _bar { | ||||||||||||||||||||||||||||
| 13 | 48 | ▶ | my (%args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_bar: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 38 | 9 | my $index = int scalar @char_list * $decimal; | |||||||||||||||||||||||||||
| 39 | 9 | $bar .= $char_list[$index]; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 48 | return $bar; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub vspark { | ||||||||||||||||||||||||||||
| 46 | 7 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
vspark: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | croak 'values is not an ArrayRef' | ||||||||||||||||||||||||||||
| 49 | 7 | ▶ | if ref $args{'values'} ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | croak 'labels is not an ArrayRef' | ||||||||||||||||||||||||||||
| 52 | 7 | ▶ | if $args{'labels'} && ref $args{'labels'} ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"labels"} and ref $args{"labels"} ne "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 7 | my $max = $args{max}; | |||||||||||||||||||||||||||
| 55 | 7 | my $columns = _term_width($args{columns}); | |||||||||||||||||||||||||||
| 56 | 7 | ▶ | my @labels = @{ $args{labels} || [] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$args{"labels"} and scalar @labels != scalar @values
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
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 7 | ▶ | $max //= _max_value(@values); | ||||||||||||||||||||||||||
|
Condition66%2/3
$max //= _max_value(@values)
MC/DC0%0/2
$max //= _max_value(@values)
$max
_max_value(@values)
Truth table66%2/3
A //= B
A= $maxB= _max_value(@values)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | sub _term_width { | ||||||||||||||||||||||||||||
| 88 | 7 | ▶ | my $columns = shift; | ||||||||||||||||||||||||||
|
Subroutine
_term_width: called CC=6 SCAR=31.7
| |||||||||||||||||||||||||||||
| 89 | 7 | ▶ | return $columns if $columns && $columns ne 'max'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$columns and $columns ne "max"
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"
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | require Term::ReadKey; | |||||||||||||||||||||||||||
| 92 | 0 | my ($cols) = Term::ReadKey::GetTerminalSize(*STDOUT); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | return 80 if !$cols; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | return $cols if $columns eq 'max'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | return 80 if $cols > 80; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | return $cols; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _max_value { | ||||||||||||||||||||||||||||
| 100 | 1 | ▶ | my @values = @_; | ||||||||||||||||||||||||||
|
Subroutine
_max_value: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 101 | 1 | my @sorted = sort @values; | |||||||||||||||||||||||||||
| 102 | 1 | ▶ | return 0 unless @sorted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 1 | return $sorted[-1]; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub _max_label_width { | ||||||||||||||||||||||||||||
| 107 | 7 | ▶ | my @labels = @_; | ||||||||||||||||||||||||||
|
Subroutine
_max_label_width: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 7 | ▶ | return 0 if scalar @labels == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||