blib/lib/Term/Fabulous/Chart/Marker.pm

filter: Statementstmt 97.4% 153 / 157 Branchbran 90.3% 56 / 62 Conditioncond 81.1% 56 / 69 MC/DCmcdc 60.5% 23 / 38 Subroutinesub 95.6% 22 / 23 Podpod 100% 8 / 8 totaltotal 89.0% 318 / 357 CC 68 SCAR 25.7
CC
68
Cov
92%
_two_color_cell
27.1
_dots_cell
15.5
_glyph_cell
12.0
CRAP 13.0
« blib/lib/Term/Fabulous/Widget/Table/Borders.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Scale/Time.pm »
1 package Term::Fabulous::Chart::Marker;
2  
3 3 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.05';
9  
10 3 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 class Term::Fabulous::Chart::Marker :strict(params) {
13 3 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # The subpixels of a cell are numbered row by row from the top-left
16 # corner: index = row * columns + column. A mask has bit i set for
17 # subpixel i.
18  
19 # Braille dots by subpixel (two columns, four rows).
20 3 my @BRAILLE_BIT = ( 0x01, 0x08, 0x02, 0x10, 0x04, 0x20, 0x40, 0x80 );
21  
22 my @QUADRANT
23 3 = ( ' ', "\x{2598}", "\x{259D}", "\x{2580}", "\x{2596}", "\x{258C}", "\x{259E}", "\x{259B}", "\x{2597}", "\x{259A}", "\x{2590}", "\x{259C}", "\x{2584}", "\x{2599}", "\x{259F}", "\x{2588}" );
24  
25 192 ▶ sub _sextant_glyph ($mask) {
Subroutine _sextant_glyph: called CC=7 SCAR=19.5
26 192 ▶ return ' ' if $mask == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == 0 3 189 192
27 189 ▶ return "\x{2588}" if $mask == 63;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == 63 3 186 189
28 186 ▶ return "\x{258C}" if $mask == 21;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == 21 3 183 186
29 183 ▶ return "\x{2590}" if $mask == 42;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == 42 3 180 183
30 180 ▶ return chr( 0x1FB00 + $mask - 1 - ( $mask > 21 ? 1 : 0 ) - ( $mask > 42 ? 1 : 0 ) );
Branch: 2 branches
BranchTrueFalseTotal
$mask > 21 ? : 120 60 180
$mask > 42 ? : 60 120 180
31 }
32  
33 # Vertical eighths: subpixel 0 is the top eighth. The lower blocks fill
34 # from the bottom; of the upper ones only the half and the eighth exist.
35 3 ▶ sub _vertical_block_glyphs () {
Subroutine _vertical_block_glyphs: called CC=2 SCAR=6.9
36 3 my %glyph_of_mask = ( 0 => ' ', 0xFF => "\x{2588}", 0x0F => "\x{2580}", 0x01 => "\x{2594}" );
37 3 $glyph_of_mask{ ( ( 1 << $_ ) - 1 ) << ( 8 - $_ ) } = chr( 0x2580 + $_ ) foreach 1 .. 7;
38 3 return \%glyph_of_mask;
39 }
40  
41 # Horizontal eighths: subpixel 0 is the left eighth.
42 3 ▶ sub _horizontal_block_glyphs () {
Subroutine _horizontal_block_glyphs: called CC=2 SCAR=6.9
43 3 my %glyph_of_mask = ( 0 => ' ', 0xFF => "\x{2588}", 0xF0 => "\x{2590}", 0x80 => "\x{2595}" );
44 3 $glyph_of_mask{ ( 1 << $_ ) - 1 } = chr( 0x2590 - $_ ) foreach 1 .. 7;
45 3 return \%glyph_of_mask;
46 }
47  
48 my %SPEC = (
49 braille => { columns => 2, rows => 4, dots => 1 },
50 half => { columns => 1, rows => 2, glyphs => { 0 => ' ', 1 => "\x{2580}", 2 => "\x{2584}", 3 => "\x{2588}" } },
51 48 quadrant => { columns => 2, rows => 2, glyphs => { map { $_ => $QUADRANT[$_] } 0 .. 15 } },
52 3 sextant => { columns => 2, rows => 3, glyphs => { map { $_ => _sextant_glyph($_) } 0 .. 63 } },
53 block => { columns => 1, rows => 8, glyphs => _vertical_block_glyphs() },
54 'block-horizontal' => { columns => 8, rows => 1, glyphs => _horizontal_block_glyphs() },
55 );
56  
57 3 my %INSTANCE;
58  
59 0 ▶ field $name :param :reader;
Subroutine name: not called CC=1 SCAR=2.6
Pod documented
60 6 ▶ field $columns :reader;
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
61 6 ▶ field $rows :reader;
Subroutine rows: called CC=1 SCAR=0.0
Pod documented
62 3 field $_is_dots;
63 3 field %_glyph_of_mask;
64 3 field @_candidates; # [ mask, glyph ] of every glyph, for approximations
65 3 field $_full_mask;
66  
67 ADJUST {
68 my $spec = $SPEC{$name} // croak "Term::Fabulous::Chart::Marker: unknown marker '$name' (known: " . join( ', ', sort keys %SPEC ) . ")";
69 ( $columns, $rows, $_is_dots ) = ( $spec->{columns}, $spec->{rows}, $spec->{dots} // 0 );
70 %_glyph_of_mask = %{ $spec->{glyphs} // {} };
71 @_candidates = map { [ $_, $_glyph_of_mask{$_} ] } sort { $a <=> $b } keys %_glyph_of_mask;
72 $_full_mask = ( 1 << ( $columns * $rows ) ) - 1;
73 }
74  
75 3 ▶ method names :common () {
Subroutine names: called CC=1 SCAR=0.0
Pod documented
76 3 my @names = sort keys %SPEC;
77 3 return @names;
78 3 }
79  
80 4 ▶ method is_name :common ($candidate) {
Subroutine is_name: called CC=4 SCAR=13.9
Pod documented
81 4 ▶ return defined $candidate && !ref $candidate && exists $SPEC{$candidate} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $candidate && !ref($candidate) && exists $SPEC{$candidate} ? : 1 3 4
Condition100%6/6
defined $candidate and not ref $candidate and exists $SPEC{$candidate}
!ll&&!rl&&r
211
defined $candidate and not ref $candidate
!ll&&!rl&&r
112
MC/DC100%3/3
defined $candidate and not ref $candidate and exists $SPEC{$candidate}
defined $candidate not ref $candidate exists $SPEC{$candidate}
Truth table100%4/4
A and not(B) and C
A= defined $candidateB= not ref $candidateC= exists $SPEC{$candidate}
ABCresult
0XX0
10X0
1100
1111
82 3 }
83  
84 # The shared instance of a marker; markers hold no state of their own.
85 22 ▶ method named :common ($wanted) {
Subroutine named: called CC=3 SCAR=11.2
Pod documented
86 22 ▶ return $INSTANCE{ $wanted // '' } //= $class->new( name => $wanted );
Condition60%3/5
$INSTANCE{$wanted // ""} //= $class->new("name", $wanted)
l!l&&r!l&&!r
1570
$wanted // ""
l!l
220
MC/DC0%0/2
$INSTANCE{$wanted // ""} //= $class->new("name", $wanted)
$INSTANCE{$wanted // ""} $class->new("name", $wanted)
MC/DC0%0/1
$wanted // ""
$wanted
Truth table66%2/3
A //= B
A= $INSTANCE{$wanted // ""}B= $class->new("name", $wanted)
ABresult
1X1
011
000
Truth table50%1/2
A
A= $wanted
Aresult
11
00
87 3 }
88  
89 68 ▶ method subpixels () {
Subroutine subpixels: called CC=1 SCAR=0.0
Pod documented
90 68 return $columns * $rows;
91 3 }
92  
93 # The glyph showing the subpixel colors of one cell (packed 0xRRGGBB,
94 # undef where nothing was drawn) over the cell's background $under
95 # (undef for none): ( $glyph, $fg, $bg ), where an undef $bg keeps the
96 # cell's background. The empty list when no subpixel was drawn.
97 73 ▶ method cell ( $colors, $under, $drawn = undef ) {
Subroutine cell: called CC=3 SCAR=11.0
Pod documented
98 73 ▶ return $_is_dots ? _dots_cell( $colors, $drawn ) : $self->_two_color_cell( $colors, $under );
Branch: 1 branch
BranchTrueFalseTotal
$_is_dots ? : 13 60 73
99 3 }
100  
101 # Braille: every drawn subpixel is a dot in one color: that of the dot
102 # drawn last, when the order is known (what is drawn on top wins), else
103 # the color most of them have.
104 13 ▶ sub _dots_cell ( $colors, $drawn ) {
Subroutine _dots_cell: called CC=11 SCAR=27.4
105 13 my ( $bits, %count, $color, $most, $latest ) = (0);
106 13 foreach my $index ( 0 .. 7 ) {
107 104 ▶ my $dot = $colors->[$index] // next;
Condition100%2/2
$colors->[$index] // (next)
l!l
2381
MC/DC100%1/1
$colors->[$index] // (next)
$colors->[$index]
Truth table100%2/2
A
A= $colors->[$index]
Aresult
11
00
108 23 $bits |= $BRAILLE_BIT[$index];
109 23 ▶ if ( $drawn && defined $drawn->[$index] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drawn and defined $drawn->[$index]) 0 23 23
Condition33%1/3
$drawn and defined $drawn->[$index]
!ll&&!rl&&r
2300
MC/DC0%0/2
$drawn and defined $drawn->[$index]
$drawn defined $drawn->[$index]
Truth table33%1/3
A and B
A= $drawnB= defined $drawn->[$index]
ABresult
0X0
100
111
110 0 ▶ ( $color, $latest ) = ( $dot, $drawn->[$index] ) if !defined $latest || $drawn->[$index] > $latest;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $latest or $drawn->[$index] > $latest 0 0 0
Condition0%0/3
not defined $latest or $drawn->[$index] > $latest
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $latest or $drawn->[$index] > $latest
not defined $latest $drawn->[$index] > $latest
Truth table0%0/3
not(A) or B
A= not defined $latestB= $drawn->[$index] > $latest
ABresult
1X1
011
000
111 0 next;
112 }
113 23 my $seen = ++$count{$dot};
114 23 ▶ ( $color, $most ) = ( $dot, $seen ) if !defined $latest && ( !defined $most || $seen > $most );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $latest and !defined($most) || $seen > $most 21 2 23
Condition83%5/6
not defined $latest and !defined($most) || $seen > $most
!ll&&!rl&&r
0221
not defined $most or $seen > $most
l!l&&r!l&&!r
1292
MC/DC66%2/3
not defined $latest and !defined($most) || $seen > $most
not defined $latest not defined $most $seen > $most
Truth table75%3/4
not(A) and not(B) or C
A= not defined $latestB= not defined $mostC= $seen > $most
ABCresult
0XX0
1000
11X1
1011
115 }
116 13 ▶ return () unless $bits;
Branch: 1 branch
BranchTrueFalseTotal
unless $bits 1 12 13
117 12 return ( chr( 0x2800 + $bits ), $color, undef );
118 }
119  
120 # Block characters show two colors: the glyph's shape in the
121 # foreground over the background. Subpixels of other colors take the
122 # nearer of the two most frequent ones.
123 60 ▶ method _two_color_cell ( $colors, $under ) {
Subroutine _two_color_cell: called CC=27 SCAR=33.0
124 60 my ( %count, @keys, %is_drawn );
125 60 $is_drawn{$_} = 1 foreach grep { defined } @$colors;
126 60 ▶ return () unless %is_drawn;
Branch: 1 branch
BranchTrueFalseTotal
unless %is_drawn 5 55 60
127 55 ▶ my @shown = map { $_ // $under } @$colors;
Condition100%3/3
$_ // $under
l!l&&r!l&&!r
16016150
MC/DC100%2/2
$_ // $under
$_ $under
Truth table100%3/3
A // B
A= $_B= $under
ABresult
1X1
011
000
128 55 foreach my $color (@shown) {
129 326 ▶ my $key = $color // 'none';
Condition100%2/2
$color // "none"
l!l
176150
MC/DC100%1/1
$color // "none"
$color
Truth table100%2/2
A
A= $color
Aresult
11
00
130 326 ▶ push @keys, $key unless $count{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $count{$key}++ 111 215 326
131 }
132 55 ▶ return ( ' ', undef, $shown[0], $shown[0] ) if @keys == 1 && defined $shown[0];
Branch: 1 branch
BranchTrueFalseTotal
if @keys == 1 and defined $shown[0] 2 53 55
Condition66%2/3
@keys == 1 and defined $shown[0]
!ll&&!rl&&r
5302
MC/DC50%1/2
@keys == 1 and defined $shown[0]
@keys == 1 defined $shown[0]
Truth table66%2/3
A and B
A= @keys == 1B= defined $shown[0]
ABresult
0X0
100
111
133 53 ▶ return () if @keys == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @keys == 1 0 53 53
134  
135 # The most frequent color is the foreground; on a tie, a drawn color
136 # rather than the background below.
137 53 my %first = map { $keys[$_] => $_ } 0 .. $#keys;
138 53 ▶ my ( $first, $second ) = ( sort { $count{$b} <=> $count{$a} || ( $is_drawn{$b} // 0 ) <=> ( $is_drawn{$a} // 0 ) || $first{$a} <=> $first{$b} } @keys )[ 0, 1 ];
Condition90%9/10
$count{$b} <=> $count{$a} || ($is_drawn{$b} // 0) <=> ($is_drawn{$a} // 0) || $first{$a} <=> $first{$b}
l!l&&r!l&&!r
5530
$count{$b} <=> $count{$a} or ($is_drawn{$b} // 0) <=> ($is_drawn{$a} // 0)
l!l&&r!l&&!r
38173
$is_drawn{$b} // 0
l!l
128
$is_drawn{$a} // 0
l!l
119
MC/DC0%0/3
$count{$b} <=> $count{$a} || ($is_drawn{$b} // 0) <=> ($is_drawn{$a} // 0) || $first{$a} <=> $first{$b}
$count{$b} <=> $count{$a} $is_drawn{$b} $first{$a} <=> $first{$b}
MC/DC100%1/1
$is_drawn{$a} // 0
$is_drawn{$a}
Truth table50%2/4
A or B || C
A= $count{$b} <=> $count{$a}B= $is_drawn{$b}C= $first{$a} <=> $first{$b}
ABCresult
1XX1
01X1
0011
0000
Truth table100%2/2
A
A= $is_drawn{$a}
Aresult
11
00
139 53 my $mask = 0;
140 53 foreach my $index ( 0 .. $#shown ) {
141 318 ▶ my $key = $shown[$index] // 'none';
Condition100%2/2
$shown[$index] // "none"
l!l
168150
MC/DC100%1/1
$shown[$index] // "none"
$shown[$index]
Truth table100%2/2
A
A= $shown[$index]
Aresult
11
00
142 318 ▶ $key = _nearer( $key, $first, $second, $under ) unless $key eq $first || $key eq $second;
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq $first or $key eq $second 3 315 318
Condition100%3/3
$key eq $first or $key eq $second
l!l&&r!l&&!r
216993
MC/DC100%2/2
$key eq $first or $key eq $second
$key eq $first $key eq $second
Truth table100%3/3
A or B
A= $key eq $firstB= $key eq $second
ABresult
1X1
011
000
143 318 ▶ $mask |= 1 << $index if $key eq $first;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $first 218 100 318
144 }
145 53 ▶ my ( $fg, $bg ) = map { $_ eq 'none' ? undef : $_ } $first, $second;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'none' ? : 44 62 106
146  
147 # What most of the cell shows; on a tie the background below, so a
148 # fill never looks larger than it is.
149 53 ▶ my $under_key = $under // 'none';
Condition100%2/2
$under // "none"
l!l
647
MC/DC100%1/1
$under // "none"
$under
Truth table100%2/2
A
A= $under
Aresult
11
00
150 53 ▶ my $dominant = $is_drawn{$first} && $count{$first} == ( $count{$under_key} // 0 ) && !$is_drawn{$under_key} ? $under : $fg // $under;
Branch: 1 branch
BranchTrueFalseTotal
$is_drawn{$first} && $count{$first} == ($count{$under_key} // 0) && !$is_drawn{$under_key} ? : 16 37 53
Condition81%9/11
$is_drawn{$first} and $count{$first} == ($count{$under_key} // 0) and not $is_drawn{$under_key}
!ll&&!rl&&r
37016
$is_drawn{$first} and $count{$first} == ($count{$under_key} // 0)
!ll&&!rl&&r
201716
$count{$under_key} // 0
l!l
312
$fg // $under
l!l&&r!l&&!r
20017
MC/DC66%2/3
$is_drawn{$first} and $count{$first} == ($count{$under_key} // 0) and not $is_drawn{$under_key}
$is_drawn{$first} $count{$first} == ($count{$under_key} // 0) not $is_drawn{$under_key}
MC/DC100%1/1
$count{$under_key} // 0
$count{$under_key}
MC/DC50%1/2
$fg // $under
$fg $under
Truth table75%3/4
A and B and not(C)
A= $is_drawn{$first}B= $count{$first} == ($count{$under_key} // 0)C= not $is_drawn{$under_key}
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $count{$under_key}
Aresult
11
00
Truth table66%2/3
A // B
A= $fgB= $under
ABresult
1X1
011
000
151 53 ▶ ( $mask, $fg, $bg ) = ( $_full_mask & ~$mask, $bg, $fg ) unless defined $fg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fg 17 36 53
152 53 return ( $self->_glyph_cell( $mask, $fg, $bg ), $dominant );
153 3 }
154  
155 3 ▶ sub _nearer ( $key, $first, $second, $under ) {
Subroutine _nearer: called CC=4 SCAR=13.9
156 3 ▶ my @rgb = map { $_ eq 'none' ? $under // 0 : $_ } $key, $first, $second;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'none' ? : 1 8 9
Condition50%1/2
$under // 0
l!l
01
MC/DC0%0/1
$under // 0
$under
Truth table50%1/2
A
A= $under
Aresult
11
00
157 3 ▶ return _distance( @rgb[ 0, 1 ] ) <= _distance( @rgb[ 0, 2 ] ) ? $first : $second;
Branch: 1 branch
BranchTrueFalseTotal
_distance(@rgb[0, 1]) <= _distance(@rgb[0, 2]) ? : 2 1 3
158 }
159  
160 6 ▶ sub _distance ( $one, $other ) {
Subroutine _distance: called CC=2 SCAR=6.9
161 6 my $sum = 0;
162 6 $sum += ( ( ( $one >> $_ ) & 0xFF ) - ( ( $other >> $_ ) & 0xFF ) )**2 foreach 16, 8, 0;
163 6 return $sum;
164 }
165  
166 # The cell for $mask drawn in $fg (defined) over $bg (undef for the
167 # cell's background). Markers without a glyph for every mask take the
168 # glyph that differs in the fewest subpixels, in either orientation.
169 53 ▶ method _glyph_cell ( $mask, $fg, $bg ) {
Subroutine _glyph_cell: called CC=12 SCAR=24.9
170 53 ▶ return ( ' ', undef, $fg ) if $mask == $_full_mask;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == $_full_mask 0 53 53
171 53 ▶ if ( defined( my $glyph = $_glyph_of_mask{$mask} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $glyph = $_glyph_of_mask{$mask})) 48 5 53
172 48 ▶ return $mask ? ( $glyph, $fg, $bg ) : ( ' ', undef, $bg );
Branch: 1 branch
BranchTrueFalseTotal
$mask ? : 48 0 48
173 }
174 5 my $inverse = $_full_mask & ~$mask;
175 5 ▶ return ( $_glyph_of_mask{$inverse}, $bg, $fg ) if defined $bg && defined $_glyph_of_mask{$inverse};
Branch: 1 branch
BranchTrueFalseTotal
if defined $bg and defined $_glyph_of_mask{$inverse} 2 3 5
Condition100%3/3
defined $bg and defined $_glyph_of_mask{$inverse}
!ll&&!rl&&r
212
MC/DC100%2/2
defined $bg and defined $_glyph_of_mask{$inverse}
defined $bg defined $_glyph_of_mask{$inverse}
Truth table100%3/3
A and B
A= defined $bgB= defined $_glyph_of_mask{$inverse}
ABresult
0X0
100
111
176  
177 3 my ( $best, $best_cost );
178 3 foreach my $candidate (@_candidates) {
179 33 my ( $shape, $glyph ) = @$candidate;
180 33 my @options = ( [ _bits( $shape ^ $mask ), $glyph, $fg, $bg ] );
181 33 ▶ push @options, [ _bits( $shape ^ $inverse ), $glyph, $bg, $fg ] if defined $bg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bg 11 22 33
182 33 foreach my $option (@options) {
183 44 ▶ ( $best, $best_cost ) = ( $option, $option->[0] ) if !defined $best_cost || $option->[0] < $best_cost;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $best_cost or $option->[0] < $best_cost 7 37 44
Condition100%3/3
not defined $best_cost or $option->[0] < $best_cost
l!l&&r!l&&!r
3437
MC/DC100%2/2
not defined $best_cost or $option->[0] < $best_cost
not defined $best_cost $option->[0] < $best_cost
Truth table100%3/3
not(A) or B
A= not defined $best_costB= $option->[0] < $best_cost
ABresult
1X1
011
000
184 }
185 }
186 3 my ( undef, $glyph, $glyph_fg, $glyph_bg ) = @$best;
187 3 ▶ return $glyph eq ' ' ? ( ' ', undef, $glyph_bg ) : ( $glyph, $glyph_fg, $glyph_bg );
Branch: 1 branch
BranchTrueFalseTotal
$glyph eq ' ' ? : 1 2 3
188 3 }
189  
190 44 ▶ sub _bits ($value) {
Subroutine _bits: called CC=2 SCAR=6.9
191 44 my $count = 0;
192 44 ▶ while ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 188 44 232
193 188 $value &= $value - 1;
194 188 $count++;
195 }
196 44 return $count;
197 }
198 }
199  
200 3 1;
201  
202 __END__
« blib/lib/Term/Fabulous/Widget/Table/Borders.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Scale/Time.pm »