blib/lib/Term/Fabulous/Enum/BorderStyle.pm

filter: Statementstmt 86.9% 107 / 123 Branchbran 59.0% 13 / 22 Conditioncond 63.1% 12 / 19 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 84.6% 22 / 26 Podpod 100% 16 / 16 totaltotal 79.8% 174 / 218 CC 20 SCAR 17.9
CC
20
Cov
80%
junction
13.4
_check_style
11.4
get_mixed_joint
3.0
CRAP 6.0
« blib/lib/Term/Fabulous/Unicode.pm ↑ Summary blib/lib/Term/Fabulous/Color.pm »
1 package Term::Fabulous::Enum::BorderStyle;
2  
3 4 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.05';
9  
10 4 ▶use Object::PadX::Enum;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 enum Term::Fabulous::Enum::BorderStyle {
13 4 ▶ use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 state $heavy_joints = [
17 "\x{2501}", # h_line
18 "\x{2503}", # v_line
19 "\x{254B}", # cross
20 "\x{2533}", # t_down
21 "\x{253B}", # t_up
22 "\x{2523}", # t_right
23 "\x{252B}", # t_left
24 ];
25 state $solid_joints = [
26 "\x{2500}", # h_line
27 "\x{2502}", # v_line
28 "\x{253C}", # cross
29 "\x{252C}", # t_down
30 "\x{2534}", # t_up
31 "\x{251C}", # t_right
32 "\x{2524}", # t_left
33 ];
34  
35 item Ascii(
36 glyphs => [ '+', '-', '+', '|', '|', '+', '-', '+' ],
37 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
38 joints => [ '-', '|', '+', '+', '+', '+', '+' ],
39 );
40 item Blank(
41 glyphs => [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ],
42 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
43 );
44 item Block(
45 glyphs => [ "\x{2584}", "\x{2584}", "\x{2584}", "\x{2588}", "\x{2588}", "\x{2580}", "\x{2580}", "\x{2580}" ],
46 locations => [ 1, 1, 1, 0, 0, 1, 1, 1 ],
47 );
48 item DarkShade(
49 glyphs => [ "\x{2593}", "\x{2593}", "\x{2593}", "\x{2593}", "\x{2593}", "\x{2593}", "\x{2593}", "\x{2593}" ],
50 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
51 );
52 item Dashed(
53 glyphs => [ "\x{250F}", "\x{254D}", "\x{2513}", "\x{254F}", "\x{254F}", "\x{2517}", "\x{254D}", "\x{251B}" ],
54 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
55 joints => $heavy_joints,
56 );
57 item Double(
58 glyphs => [ "\x{2554}", "\x{2550}", "\x{2557}", "\x{2551}", "\x{2551}", "\x{255A}", "\x{2550}", "\x{255D}" ],
59 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
60 joints => [
61 "\x{2550}", # h_line
62 "\x{2551}", # v_line
63 "\x{256C}", # cross
64 "\x{2566}", # t_down
65 "\x{2569}", # t_up
66 "\x{2560}", # t_right
67 "\x{2563}", # t_left
68 ],
69 mixed_joints => sub {
70 Term::Fabulous::Enum::BorderStyle->Solid, [
71 "\x{256A}", # cross horizontal double, vertical single
72 "\x{2564}", # t_down down single, horizontal double
73 "\x{2567}", # t_up up single, horizontal double
74 "\x{255E}", # t_right vertical single, right double
75 "\x{2561}", # t_left vertical single, left double
76 ],
77 ;
78 },
79 );
80 item Heavy(
81 glyphs => [ "\x{250F}", "\x{2501}", "\x{2513}", "\x{2503}", "\x{2503}", "\x{2517}", "\x{2501}", "\x{251B}" ],
82 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
83 joints => $heavy_joints,
84 mixed_joints => sub {
85 Term::Fabulous::Enum::BorderStyle->Solid, [
86 "\x{253F}", # cross horizontal heavy, vertical light
87 "\x{252F}", # t_down down light, horizontal heavy
88 "\x{2537}", # t_up up light, horizontal heavy
89 "\x{251D}", # t_right vertical light, right heavy
90 "\x{2525}", # t_left vertical light, left heavy
91 ],
92 ;
93 },
94 );
95  
96 # Hidden sides take no space and draw nothing: HasBorderStyle gives
97 # them a Clay border width of 0, so these glyphs are never painted.
98 item Hidden(
99 glyphs => [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ],
100 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
101 );
102 item Hkey(
103 glyphs => [ "\x{2594}", "\x{2594}", "\x{2594}", ' ', ' ', "\x{2581}", "\x{2581}", "\x{2581}" ],
104 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
105 );
106 item Inner(
107 glyphs => [ "\x{2597}", "\x{2584}", "\x{2596}", "\x{2590}", "\x{258C}", "\x{259D}", "\x{2580}", "\x{2598}" ],
108 locations => [ 1, 1, 1, 1, 1, 1, 1, 1 ],
109 );
110 item LightShade(
111 glyphs => [ "\x{2591}", "\x{2591}", "\x{2591}", "\x{2591}", "\x{2591}", "\x{2591}", "\x{2591}", "\x{2591}" ],
112 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
113 );
114 item MediumShade(
115 glyphs => [ "\x{2592}", "\x{2592}", "\x{2592}", "\x{2592}", "\x{2592}", "\x{2592}", "\x{2592}", "\x{2592}" ],
116 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
117 );
118 item Outer(
119 glyphs => [ "\x{259B}", "\x{2580}", "\x{259C}", "\x{258C}", "\x{2590}", "\x{2599}", "\x{2584}", "\x{259F}" ],
120 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
121 );
122 item Panel(
123 glyphs => [ "\x{258A}", "\x{2588}", "\x{258E}", "\x{258A}", "\x{258E}", "\x{258A}", "\x{2581}", "\x{258E}" ],
124 locations => [ 2, 0, 1, 2, 1, 2, 0, 1 ],
125 );
126 item Round(
127 glyphs => [ "\x{256D}", "\x{2500}", "\x{256E}", "\x{2502}", "\x{2502}", "\x{2570}", "\x{2500}", "\x{256F}" ],
128 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
129 joints => $solid_joints,
130 );
131  
132 item Solid(
133 glyphs => [ "\x{250C}", "\x{2500}", "\x{2510}", "\x{2502}", "\x{2502}", "\x{2514}", "\x{2500}", "\x{2518}" ],
134 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
135 joints => $solid_joints,
136 mixed_joints => sub {
137 Term::Fabulous::Enum::BorderStyle->Heavy, [
138 "\x{2542}", # cross vertical heavy, horizontal light
139 "\x{2530}", # t_down down heavy, horizontal light
140 "\x{2538}", # t_up up heavy, horizontal light
141 "\x{2520}", # t_right vertical heavy, right light
142 "\x{2528}", # t_left vertical heavy, left light
143 ],
144 Term::Fabulous::Enum::BorderStyle->Double, [
145 "\x{256B}", # cross vertical double, horizontal single
146 "\x{2565}", # t_down down double, horizontal single
147 "\x{2568}", # t_up up double, horizontal single
148 "\x{255F}", # t_right vertical double, right single
149 "\x{2562}", # t_left vertical double, left single
150 ],
151 ;
152 }
153 );
154  
155 item Tall(
156 glyphs => [ "\x{258A}", "\x{2594}", "\x{258E}", "\x{258A}", "\x{258E}", "\x{258A}", "\x{2581}", "\x{258E}" ],
157 locations => [ 2, 0, 1, 2, 1, 2, 0, 1 ],
158 );
159 item Thick(
160 glyphs => [ "\x{2588}", "\x{2580}", "\x{2588}", "\x{2588}", "\x{2588}", "\x{2588}", "\x{2584}", "\x{2588}" ],
161 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
162 );
163 item Vkey(
164 glyphs => [ "\x{258F}", ' ', "\x{2595}", "\x{258F}", "\x{2595}", "\x{258F}", ' ', "\x{2595}" ],
165 locations => [ 0, 0, 0, 0, 0, 0, 0, 0 ],
166 );
167 item Wide(
168 glyphs => [ "\x{2581}", "\x{2581}", "\x{2581}", "\x{258E}", "\x{258A}", "\x{2594}", "\x{2594}", "\x{2594}" ],
169 locations => [ 1, 1, 1, 0, 3, 1, 1, 1 ],
170 );
171  
172 # Renderer-side glyph tables for each border style item above.
173 # Sourced from Textualize/textual's `_border.py`:
174 # https://raw.githubusercontent.com/Textualize/textual/refs/heads/main/src/textual/_border.py
175 #
176 # Textual's BORDER_CHARS / BORDER_LOCATIONS are 3x3 tables:
177 #
178 # row 0 (top): (TL, T, TR )
179 # row 1 (middle): (L, mid, R )
180 # row 2 (bottom): (BL, B, BR )
181 #
182 # The middle-middle cell is always a space pad; we drop it. The remaining
183 # 8 cells are flattened in a fixed order and stored as 8-element arrayrefs:
184 #
185 # index: 0 1 2 3 4 5 6 7
186 # glyph: TL T TR L R BL B BR
187 # source: r0c0 r0c1 r0c2 r1c0 r1c2 r2c0 r2c1 r2c2
188 #
189 # `glyphs` carries the visible glyph (or a space for invisible / blank styles).
190 # 8-element arrayref of glyphs in `[TL, T, TR, L, R, BL, B, BR]` order.
191 24 ▶ field $glyphs :param :reader;
Subroutine glyphs: called CC=1 SCAR=0.0
Pod documented
192  
193 # `locations` carries one of {0,1,2,3} per Textual's location matrix:
194 # 0 = widget-side bg (inner)
195 # 1 = parent-side bg (outer)
196 # 2 = reverse(parent_bg, widget_fg) - Textual's styles[2]
197 # 3 = reverse(widget_bg, parent_fg) - Textual's styles[3]
198 # 8-element arrayref of LOCATIONS indices in the same order as `glyphs`.
199 0 ▶ field $locations :param :reader;
Subroutine locations: not called CC=1 SCAR=6.9
Pod documented
200  
201 # Joint glyph tables for grid-line rendering. Each entry is a
202 # 7-element arrayref in fixed order:
203 #
204 # index: 0 1 2 3 4 5 6
205 # glyph: h_line v_line cross t_down t_up t_right t_left
206 #
207 field $joints :param :reader = undef;
208  
209 # Mixed-style joint table for grid lines whose horizontal axis
210 # style differs from the vertical axis style (or whose perimeter
211 # style differs from the meeting interior axis style - same
212 # resolution rule).
213 #
214 # index: 0 1 2 3 4
215 # glyph: cross t_down t_up t_right t_left
216 #
217 # (h_line / v_line are not in this table - those are
218 # axis-uniform and live in `$joints`.)
219 #
220 # 5-element arrayref `[cross, t_down, t_up, t_right, t_left]` for h_style/v_style intersection.
221 266 ▶ field $mixed_joints :param :reader = undef;
Subroutine joints: called CC=1 SCAR=0.0
Pod documented
222  
223 # Helper method to get the subset of styles that support grid joints.
224 0 ▶ method get_grid_styles :common () {
Subroutine get_grid_styles: not called CC=1 SCAR=6.9
Subroutine mixed_joints: not called CC=1 SCAR=6.9
Pod documented
Pod documented
225 0 return grep { $_->joints } Term::Fabulous::Enum::BorderStyle->values;
226 }
227  
228 # `mixed_joints` is a code ref because its tables name items declared
229 # later in this enum; it is expanded into this map on first use.
230 field $_mixed_joint_by_style_name;
231  
232 142 ▶ sub _check_style ( $role, $style ) {
Subroutine _check_style: called CC=5 SCAR=24.4
233 142 ▶ die "Term::Fabulous::Enum::BorderStyle: the $role style must be a Term::Fabulous::Enum::BorderStyle, got " . ( defined $style ? ( ref $style || "'$style'" ) : 'undef' )
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($style) and $style->isa("Term::Fabulous::Enum::BorderStyle") 0 142 142
defined $style ? : 0 0 0
Condition20%1/5
builtin::blessed($style) and $style->isa("Term::Fabulous::Enum::BorderStyle")
!ll&&!rl&&r
00142
ref $style or "'${style}'"
l!l
00
MC/DC0%0/2
builtin::blessed($style) and $style->isa("Term::Fabulous::Enum::BorderStyle")
builtin::blessed($style) $style->isa("Term::Fabulous::Enum::BorderStyle")
MC/DC0%0/1
ref $style or "'${style}'"
ref $style
Truth table33%1/3
A and B
A= builtin::blessed($style)B= $style->isa("Term::Fabulous::Enum::BorderStyle")
ABresult
0X0
100
111
Truth table0%0/2
A
A= ref $style
Aresult
11
00
234 unless blessed $style && $style->isa('Term::Fabulous::Enum::BorderStyle');
235 142 return;
236 }
237  
238 # 5-element joint table for grid lines of this (horizontal) style
239 # crossing lines of the given vertical style, or undef if there is none.
240 11 ▶ method get_mixed_joint ($vertical_style) {
Subroutine get_mixed_joint: called CC=3 SCAR=11.0
Pod documented
241 11 _check_style( 'vertical', $vertical_style );
242 11 ▶ return undef unless defined $mixed_joints;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mixed_joints 0 11 11
243  
244 11 ▶ $_mixed_joint_by_style_name //= { pairmap { $a->name => $b } $mixed_joints->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
$_mixed_joint_by_style_name //= {&pairmap(sub { $a->name, $b; } , &$mixed_joints())}
l!l
83
MC/DC100%1/1
$_mixed_joint_by_style_name //= {&pairmap(sub { $a->name, $b; } , &$mixed_joints())}
$_mixed_joint_by_style_name
Truth table100%2/2
A
A= $_mixed_joint_by_style_name
Aresult
11
00
245 11 return $_mixed_joint_by_style_name->{ $vertical_style->name };
246 }
247  
248 # Class-method form of get_mixed_joint.
249 0 ▶ method get_mixed_joints :common ( $horizontal_style, $vertical_style ) {
Subroutine get_mixed_joints: not called CC=1 SCAR=6.9
Pod documented
250 0 _check_style( 'horizontal', $horizontal_style );
251 0 return $horizontal_style->get_mixed_joint($vertical_style);
252 }
253  
254 # Styles that draw their joints with the joints of another style (see
255 # joints) belong to that style's family for mixed joints.
256 state $joint_family = { Round => 'Solid', Dashed => 'Heavy' };
257  
258 118 ▶ sub _family_name ($style) {
Subroutine _family_name: called CC=2 SCAR=7.2
259 118 ▶ return $joint_family->{ $style->name } // $style->name;
Condition66%2/3
$$joint_family{$style->name} // $style->name
l!l&&r!l&&!r
69490
MC/DC0%0/2
$$joint_family{$style->name} // $style->name
$$joint_family{$style->name} $style->name
Truth table66%2/3
A // B
A= $$joint_family{$style->name}B= $style->name
ABresult
1X1
011
000
260 }
261  
262 # Which glyph a set of arms needs: [ joints index, mixed index ] for the
263 # T shapes and the cross; corners and straight lines are handled apart.
264 state $joint_of_arms = {
265 'up right down left' => [ 2, 0 ],
266 'right down left' => [ 3, 1 ],
267 'up right left' => [ 4, 2 ],
268 'up right down' => [ 5, 3 ],
269 'up down left' => [ 6, 4 ],
270 };
271 state $corner_of_arms = { 'right down' => 0, 'down left' => 2, 'up right' => 5, 'up left' => 7 };
272  
273 # The glyph where lines of grid styles meet: arms maps up, right, down
274 # and left to the style of the line leaving the cell that way, or undef.
275 49 ▶ method junction :common (%arms) {
Subroutine junction: called CC=13 SCAR=25.9
Pod documented
276 49 my @unknown = grep { !/\A(?:up|right|down|left)\z/ } sort keys %arms;
277 49 ▶ die "Term::Fabulous::Enum::BorderStyle: junction takes the arms up, right, down and left, got @unknown" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 49 49
278 49 _check_style( "$_ arm", $arms{$_} ) foreach grep { defined $arms{$_} } sort keys %arms;
279  
280 49 ▶ my @directions = grep { defined $arms{$_} && $arms{$_}->joints } qw(up right down left);
Condition66%2/3
defined $arms{$_} && $arms{$_}->joints
!ll&&!rl&&r
650131
MC/DC50%1/2
defined $arms{$_} && $arms{$_}->joints
defined $arms{$_} $arms{$_}->joints
Truth table66%2/3
A && B
A= defined $arms{$_}B= $arms{$_}->joints
ABresult
0X0
100
111
281 49 ▶ return undef unless @directions;
Branch: 1 branch
BranchTrueFalseTotal
unless @directions 0 49 49
282 49 my %joining = map { $_ => $arms{$_} } @directions; # an arm in a style without joints counts as no line
283 49 ▶ my $horizontal = $joining{right} // $joining{left};
Condition100%3/3
$joining{"right"} // $joining{"left"}
l!l&&r!l&&!r
32161
MC/DC100%2/2
$joining{"right"} // $joining{"left"}
$joining{"right"} $joining{"left"}
Truth table100%3/3
A // B
A= $joining{"right"}B= $joining{"left"}
ABresult
1X1
011
000
284 49 ▶ my $vertical = $joining{down} // $joining{up};
Condition66%2/3
$joining{"down"} // $joining{"up"}
l!l&&r!l&&!r
34150
MC/DC0%0/2
$joining{"down"} // $joining{"up"}
$joining{"down"} $joining{"up"}
Truth table66%2/3
A // B
A= $joining{"down"}B= $joining{"up"}
ABresult
1X1
011
000
285  
286 49 my $shape = join ' ', @directions;
287 49 ▶ return $horizontal->glyphs->[1] unless defined $vertical;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vertical 0 49 49
288 49 ▶ return $vertical->glyphs->[3] unless defined $horizontal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $horizontal 1 48 49
289 48 ▶ if ( exists $corner_of_arms->{$shape} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $corner_of_arms->{$shape}) 20 28 48
290 20 ▶ my $owner = _family_name($horizontal) eq _family_name($vertical) ? $vertical : $horizontal;
Branch: 1 branch
BranchTrueFalseTotal
_family_name($horizontal) eq _family_name($vertical) ? : 20 0 20
291 20 return $owner->glyphs->[ $corner_of_arms->{$shape} ];
292 }
293  
294 28 my ( $joint_index, $mixed_index ) = @{ $joint_of_arms->{$shape} };
295 28 ▶ return $horizontal->joints->[$joint_index] if _family_name($horizontal) eq _family_name($vertical);
Branch: 1 branch
BranchTrueFalseTotal
if _family_name($horizontal) eq _family_name($vertical) 17 11 28
296 11 my $family = Term::Fabulous::Enum::BorderStyle->from_name( _family_name($horizontal) );
297 11 my $mixed = $family->get_mixed_joint( Term::Fabulous::Enum::BorderStyle->from_name( _family_name($vertical) ) );
298 11 ▶ return defined $mixed ? $mixed->[$mixed_index] : $horizontal->joints->[$joint_index];
Branch: 1 branch
BranchTrueFalseTotal
defined $mixed ? : 11 0 11
299 }
300  
301 1 ▶ method get_top_glyphs () {
Subroutine get_top_glyphs: called CC=1 SCAR=0.0
Pod documented
302 1 return @{$glyphs}[ 0, 1, 2 ];
303 }
304  
305 1 ▶ method get_right_glyphs () {
Subroutine get_right_glyphs: called CC=1 SCAR=0.0
Pod documented
306 1 return @{ $self->glyphs }[4];
307 }
308  
309 1 ▶ method get_bottom_glyphs () {
Subroutine get_bottom_glyphs: called CC=1 SCAR=0.0
Pod documented
310 1 return @{ $self->glyphs }[ 5, 6, 7 ];
311 }
312  
313 1 ▶ method get_left_glyphs () {
Subroutine get_left_glyphs: called CC=1 SCAR=0.0
Pod documented
314 1 return @{ $self->glyphs }[3];
315 }
316  
317 3 ▶ method get_top_locations () {
Subroutine get_top_locations: called CC=1 SCAR=0.0
Pod documented
318 3 return @{$locations}[ 0, 1, 2 ];
319 }
320  
321 3 ▶ method get_right_locations () {
Subroutine get_right_locations: called CC=1 SCAR=0.0
Pod documented
322 3 return @{$locations}[4];
323 }
324  
325 3 ▶ method get_bottom_locations () {
Subroutine get_bottom_locations: called CC=1 SCAR=0.0
Pod documented
326 3 return @{$locations}[ 5, 6, 7 ];
327 }
328  
329 3 ▶ method get_left_locations () {
Subroutine get_left_locations: called CC=1 SCAR=0.0
Pod documented
330 3 return @{$locations}[3];
331 }
332 }
333  
334 4 1;
335  
336 __END__
« blib/lib/Term/Fabulous/Unicode.pm ↑ Summary blib/lib/Term/Fabulous/Color.pm »