blib/lib/Term/Fabulous/Chart/Scale/Category.pm

filter: Statementstmt 100% 114 / 114 Branchbran 91.6% 22 / 24 Conditioncond 74.2% 26 / 35 MC/DCmcdc 47.3% 9 / 19 Subroutinesub 100% 20 / 20 Podpod 100% 9 / 9 totaltotal 90.5% 200 / 221 CC 28 SCAR 22.2
CC
28
Cov
94%
fit
19.4
slot_cells
5.1
position
4.0
CRAP 9.2
« blib/lib/Term/Fabulous/Widget/Table/Frame.pm ↑ Summary blib/lib/Term/Fabulous/Scrolling.pm »
1 package Term::Fabulous::Chart::Scale::Category;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.05';
9  
10 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Term::Fabulous::Chart::Scale;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 class Term::Fabulous::Chart::Scale::Category :isa(Term::Fabulous::Chart::Scale) {
15 1 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶ use constant SHORTEST_CUT_LABEL => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 field $labels :param;
22 field $band :param = 1;
23 1 field $offset :param = 0; # cells before the first slot
24 1 field $pitch :param = undef; # cells per slot; undef: the cells shared evenly
25  
26 1 ▶ method kind () { return 'category' }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
27  
28 2 ▶ method is_band () {
Subroutine is_band: called CC=1 SCAR=0.0
Pod documented
29 2 return $band;
30 1 }
31  
32 1 ▶ method labels () {
Subroutine labels: called CC=1 SCAR=0.0
Pod documented
33 1 return @$labels;
34 1 }
35  
36 1 ▶ method count () {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
37 1 return scalar @$labels;
38 1 }
39  
40 # The label of category number $index (from 0); undef past the last.
41 3 ▶ method label_at ($index) {
Subroutine label_at: called CC=1 SCAR=0.0
Pod documented
42 3 return $labels->[$index];
43 1 }
44  
45 # The position of category number $index (from 0).
46 35 ▶ method position ($index) {
Subroutine position: called CC=4 SCAR=13.9
Pod documented
47 35 ▶ my $count = @$labels || 1;
Condition50%1/2
@$labels || 1
l!l
350
MC/DC0%0/1
@$labels || 1
@$labels
Truth table50%1/2
A
A= @$labels
Aresult
11
00
48 35 ▶ return ( $offset + ( $index + 0.5 ) * $self->slot_cells ) / $self->cells if $band;
Branch: 1 branch
BranchTrueFalseTotal
if $band 32 3 35
49 3 ▶ return $count > 1 ? $index / ( $count - 1 ) : 0.5;
Branch: 1 branch
BranchTrueFalseTotal
$count > 1 ? : 2 1 3
50 1 }
51  
52 3 ▶ method value_at ($position) {
Subroutine value_at: called CC=3 SCAR=11.0
Pod documented
53 3 ▶ my $count = @$labels || 1;
Condition50%1/2
@$labels || 1
l!l
30
MC/DC0%0/1
@$labels || 1
@$labels
Truth table50%1/2
A
A= @$labels
Aresult
11
00
54 3 ▶ return ( $position * $self->cells - $offset ) / $self->slot_cells - 0.5 if $band;
Branch: 1 branch
BranchTrueFalseTotal
if $band 2 1 3
55 1 return $position * ( $count - 1 );
56 1 }
57  
58 # The cells one category's slot takes.
59 38 ▶ method slot_cells () {
Subroutine slot_cells: called CC=5 SCAR=16.2
Pod documented
60 38 ▶ my $count = @$labels || 1;
Condition50%1/2
@$labels || 1
l!l
380
MC/DC0%0/1
@$labels || 1
@$labels
Truth table50%1/2
A
A= @$labels
Aresult
11
00
61 38 ▶ return $pitch // $self->cells / $count if $band;
Branch: 1 branch
BranchTrueFalseTotal
if $band 37 1 38
Condition100%2/2
$pitch // $self->cells / $count
l!l
2611
MC/DC100%1/1
$pitch // $self->cells / $count
$pitch
Truth table100%2/2
A
A= $pitch
Aresult
11
00
62 1 ▶ return $count > 1 ? ( $self->cells - 1 ) / ( $count - 1 ) : $self->cells;
Branch: 1 branch
BranchTrueFalseTotal
$count > 1 ? : 1 0 1
63 1 }
64  
65 # Every category is a tick. On a horizontal axis whose slots are too
66 # narrow for the labels, only every second (third, ...) one is
67 # labeled; labels that are only a little too wide are cut by the chart.
68 9 ▶ method fit :common (%options) {
Subroutine fit: called CC=19 SCAR=29.6
Pod documented
69 9 ▶ my $cells = $options{cells} // croak "$class: fit needs cells";
Condition100%2/2
$options{"cells"} // croak("${class}: fit needs cells")
l!l
81
MC/DC100%1/1
$options{"cells"} // croak("${class}: fit needs cells")
$options{"cells"}
Truth table100%2/2
A
A= $options{"cells"}
Aresult
11
00
70 8 ▶ croak "$class: cells must be a positive integer, got $cells" unless $cells =~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cells =~ /\A[1-9][0-9]*\z/u 0 8 8
71 8 ▶ my @labels = ( $options{labels} // [] )->@*;
Condition50%1/2
$options{"labels"} // []
l!l
80
MC/DC0%0/1
$options{"labels"} // []
$options{"labels"}
Truth table50%1/2
A
A= $options{"labels"}
Aresult
11
00
72 8 ▶ my $band = $options{band} // 1;
Condition100%2/2
$options{"band"} // 1
l!l
26
MC/DC100%1/1
$options{"band"} // 1
$options{"band"}
Truth table100%2/2
A
A= $options{"band"}
Aresult
11
00
73 8 ▶ my $measure = $options{measure} // sub ($label) { length $label };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$options{"measure"} // sub ($label) { length $label; }
l!l&&r!l&&!r
170
MC/DC0%0/2
$options{"measure"} // sub ($label) { length $label; }
$options{"measure"} sub ($label) { length $label; }
Truth table66%2/3
A // B
A= $options{"measure"}B= sub ($label) { length $label; }
ABresult
1X1
011
000
74 8 ▶ my $vertical = ( $options{orientation} // 'horizontal' ) eq 'vertical';
Condition100%2/2
$options{"orientation"} // "horizontal"
l!l
17
MC/DC100%1/1
$options{"orientation"} // "horizontal"
$options{"orientation"}
Truth table100%2/2
A
A= $options{"orientation"}
Aresult
11
00
75  
76 8 ▶ my $count = @labels || 1;
Condition50%1/2
@labels || 1
l!l
80
MC/DC0%0/1
@labels || 1
@labels
Truth table50%1/2
A
A= @labels
Aresult
11
00
77 8 ▶ my $slot = $band ? $cells / $count : ( $count > 1 ? ( $cells - 1 ) / ( $count - 1 ) : $cells );
Branch: 2 branches
BranchTrueFalseTotal
$band ? : 6 2 8
$count > 1 ? : 1 1 2
78  
79 # Slots of whole cells keep the gaps between bars equal; the cells
80 # left over go to both ends, unless that wastes too much room.
81 8 my ( $pitch, $offset ) = ( undef, 0 );
82 8 ▶ if ( $band && $slot >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($band and $slot >= 1) 5 3 8
Condition100%3/3
$band and $slot >= 1
!ll&&!rl&&r
215
MC/DC100%2/2
$band and $slot >= 1
$band $slot >= 1
Truth table100%3/3
A and B
A= $bandB= $slot >= 1
ABresult
0X0
100
111
83 5 my $whole = int $slot;
84 5 my $spare = $cells - $whole * $count;
85 5 ▶ ( $pitch, $offset, $slot ) = ( $whole, int( $spare / 2 ), $whole ) if $spare <= List::Util::max( 3, $cells / 5 );
Branch: 1 branch
BranchTrueFalseTotal
if $spare <= &List::Util::max(3, $cells / 5) 4 1 5
86 }
87 8 my $widest = List::Util::max( 0, map { $measure->($_) } @labels );
88 8 my $stride = 1;
89 8 ▶ if ( !$vertical && $widest + 1 > $slot && $slot < SHORTEST_CUT_LABEL + 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $vertical and $widest + 1 > $slot and $slot < 5) { } 2 6 8
elsif ($vertical and $slot < 1) { } 1 10 11
Condition83%5/6
not $vertical and $widest + 1 > $slot and $slot < 5
!ll&&!rl&&r
602
not $vertical and $widest + 1 > $slot
!ll&&!rl&&r
152
MC/DC66%2/3
not $vertical and $widest + 1 > $slot and $slot < 5
not $vertical $widest + 1 > $slot $slot < 5
Truth table75%3/4
not(A) and B and C
A= not $verticalB= $widest + 1 > $slotC= $slot < 5
ABCresult
0XX0
10X0
1100
1111
90 2 ▶ $stride = ceil( ( List::Util::min( $widest, SHORTEST_CUT_LABEL ) + 1 ) / ( $slot || 1 ) );
Condition50%1/2
$slot || 1
l!l
20
MC/DC0%0/1
$slot || 1
$slot
Truth table50%1/2
A
A= $slot
Aresult
11
00
91 }
92 ▶ elsif ( $vertical && $slot < 1 ) {
Condition66%2/3
$vertical and $slot < 1
!ll&&!rl&&r
501
MC/DC50%1/2
$vertical and $slot < 1
$vertical $slot < 1
Truth table66%2/3
A and B
A= $verticalB= $slot < 1
ABresult
0X0
100
111
93 1 $stride = ceil( 1 / $slot );
94 }
95 8 my @ticks = map { { value => $_, label => $labels[$_] } } grep { $_ % $stride == 0 } 0 .. $#labels;
96 8 return $class->new( min => 0, max => List::Util::max( 0, $#labels ), cells => $cells, used => $cells, labels => \@labels, band => $band, pitch => $pitch, offset => $offset, ticks => \@ticks );
97 1 }
98 }
99  
100 1 1;
101  
102 __END__
« blib/lib/Term/Fabulous/Widget/Table/Frame.pm ↑ Summary blib/lib/Term/Fabulous/Scrolling.pm »