blib/lib/Kubernetes/Comb/SVG.pm

filter: Statementstmt 99.7% 415 / 416 Branchbran 95.4% 187 / 196 Conditioncond 75.9% 41 / 54 MC/DCmcdc 60.6% 20 / 33 Subroutinesub 100% 70 / 70 Podpod 80.0% 4 / 5 totaltotal 95.2% 737 / 774 CC 126 SCAR 18.6
CC
126
Cov
97%
_tooltip
15.0
render
12.0
_cell_lines
12.0
CRAP 6.4
« blib/lib/Kubernetes/Comb/SVG/Layout.pm ↑ Summary blib/lib/Kubernetes/Comb/SVG/Cell.pm »
1 package Kubernetes::Comb::SVG;
2 # ABSTRACT: Render Kubernetes::Comb custom resources as an SVG honeycomb
3  
4  
5 99 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 99 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 99 ▶use Types::Common::Numeric qw( PositiveInt PositiveNum );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 99 ▶use Types::Standard qw( Any ArrayRef Bool CodeRef Enum HashRef Maybe Object Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 99 ▶use Kubernetes::Comb::SVG::Cell;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 99 ▶use Kubernetes::Comb::SVG::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 99 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 99 our $VERSION = '0.001';
14  
15 99 has combs => ( is => 'ro', isa => Any, required => 1 );
16  
17  
18 99 has title => ( is => 'ro', isa => Str, default => 'Combs' );
19  
20  
21 99 has group_label => ( is => 'ro', isa => Maybe[Str] );
22  
23  
24 99 has layout => ( is => 'ro', isa => Enum[qw( depth packed )], default => 'depth' );
25  
26  
27 99 has columns => ( is => 'ro', isa => PositiveInt, default => 6 );
28  
29  
30 # Whether columns came from the caller, see `columns` in the layout class.
31 99 has _columns_given => ( is => 'rwp', isa => Bool, init_arg => undef, default => 0 );
32  
33 361 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
34 361 my ( $self, $args ) = @_;
35 361 ▶ $self->_set__columns_given(1) if exists $args->{columns};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"columns"} 45 316 361
36 }
37  
38 99 has rows => ( is => 'ro', isa => PositiveInt, predicate => 'has_rows' );
39  
40  
41 99 has aspect => ( is => 'ro', isa => PositiveNum, default => 16 / 9 );
42  
43  
44 99 has size => ( is => 'ro', isa => PositiveNum, default => 56 );
45  
46  
47 99 has edges => ( is => 'lazy', isa => Bool );
48  
49 347 ▶sub _build_edges { $_[0]->layout eq 'packed' ? 0 : 1 }
Subroutine _build_edges: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->layout eq 'packed' ? : 70 277 347
50  
51  
52 99 has legend => ( is => 'ro', isa => Bool, default => 1 );
53  
54  
55 99 has link => ( is => 'ro', isa => Maybe[CodeRef] );
56  
57  
58 317 ▶has theme => ( is => 'ro', isa => HashRef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
59  
60  
61 329 ▶has blink => ( is => 'ro', isa => ArrayRef[Str], default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
62  
63  
64 99 has blink_seconds => ( is => 'ro', isa => PositiveNum, default => 1.2 );
65  
66  
67 99 has cells => ( is => 'lazy', isa => ArrayRef[Object], init_arg => undef );
68  
69  
70 361 ▶sub _build_cells {
Subroutine _build_cells: called CC=1 SCAR=0.0
71 361 my ( $self ) = @_;
72 361 return [ $self->cell_class->cells_from( $self->combs, group_label => $self->group_label ) ];
73 }
74  
75 99 has _layouter => ( is => 'lazy', isa => Object, init_arg => undef );
76  
77 361 ▶sub _build__layouter {
Subroutine _build__layouter: called CC=5 SCAR=16.1
78 361 my ( $self ) = @_;
79 361 ▶ return $self->layout_class->new(
Branch: 3 branches
BranchTrueFalseTotal
$self->legend ? : 353 5 358
$self->_columns_given || $self->layout eq 'depth' ? : 315 43 358
$self->has_rows ? : 10 348 358
Condition100%3/3
$self->_columns_given or $self->layout eq "depth"
l!l&&r!l&&!r
4527043
MC/DC100%2/2
$self->_columns_given or $self->layout eq "depth"
$self->_columns_given $self->layout eq "depth"
Truth table100%3/3
A or B
A= $self->_columns_givenB= $self->layout eq "depth"
ABresult
1X1
011
000
80 cells => $self->cells,
81 size => $self->size,
82 mode => $self->layout,
83 aspect => $self->aspect,
84 # What render puts around the honeycomb, the legend counted as one row.
85 frame_width => 2 * $self->_pad,
86 frame_height => 2 * $self->_pad + $self->_title_band
87 + ( $self->legend ? $self->_legend_gap + $self->_legend_row * 0.75 : 0 ),
88 $self->_columns_given || $self->layout eq 'depth' ? ( columns => $self->columns ) : (),
89 $self->has_rows ? ( rows => $self->rows ) : ()
90 );
91 }
92  
93 1799 ▶sub cell_class { 'Kubernetes::Comb::SVG::Cell' }
Subroutine cell_class: called CC=1 SCAR=0.0
Pod documented
94  
95  
96 361 ▶sub layout_class { 'Kubernetes::Comb::SVG::Layout' }
Subroutine layout_class: called CC=1 SCAR=0.0
Pod documented
97  
98  
99 #### Phases and colours
100  
101 1438 ▶sub phases {
Subroutine phases: called CC=1 SCAR=0.0
Pod documented
102 1438 my ( $self ) = @_;
103 1438 return ( $self->cell_class->known_phases, 'Unknown' );
104 }
105  
106  
107 # Per phase the stroke colour in light and in dark mode; the fill is the same
108 # colour at a low opacity over the panel, so text keeps its contrast whatever
109 # colour a theme brings.
110 359 ▶sub _default_colours {
Subroutine _default_colours: called CC=1 SCAR=0.0
111 return {
112 359 Running => [ '#1a7f37', '#3fb950' ],
113 Pending => [ '#bf8700', '#e3b341' ],
114 Blocked => [ '#bc4c00', '#fb8f44' ],
115 NeedsConfig => [ '#8250df', '#a371f7' ],
116 Disabled => [ '#8c959f', '#6e7681' ],
117 Error => [ '#cf222e', '#f85149' ],
118 Stopped => [ '#0891b2', '#39c5cf' ],
119 NotDeployed => [ '#0969da', '#58a6ff' ],
120 Unknown => [ '#475569', '#94a3b8' ]
121 };
122 }
123  
124 359 ▶sub _base_colours {
Subroutine _base_colours: called CC=1 SCAR=0.0
125 return (
126 359 [ bg => '#ffffff', '#0d1117' ],
127 [ border => '#d0d7de', '#30363d' ],
128 [ fg => '#1f2328', '#e6edf3' ],
129 [ muted => '#59636e', '#9198a1' ],
130 [ edge => '#57606a', '#9198a1' ]
131 );
132 }
133  
134 # A colour is used only when it is plain colour syntax: #hex, a colour name,
135 # or rgb()/hsl() over a strict character set. Anything else is ignored.
136 10052 ▶sub _colour {
Subroutine _colour: called CC=6 SCAR=17.9
137 10052 my ( $self, $value ) = @_;
138 10052 ▶ return if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 9947 105 10052
Condition66%2/3
not defined $value or ref $value
l!l&&r!l&&!r
99470105
MC/DC50%1/2
not defined $value or ref $value
not defined $value ref $value
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
139 105 ▶ return $value if $value =~ /\A#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\z/ 44 61 105
140 61 ▶ return $value if $value =~ /\A[a-zA-Z]{1,32}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A[a-zA-Z]{1,32}\z/ 14 47 61
141 47 ▶ return $value if $value =~ m{\A(?:rgb|hsl)a?\([0-9a-zA-Z%., /-]{1,64}\)\z}i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ m[\A(?:rgb|hsl)a?\([0-9a-zA-Z%., /-]{1,64}\)\z]i 17 30 47
142 30 return;
143 }
144  
145 # The light and the dark colour of a theme key (a phase or a surface): what
146 # the theme gives, one colour for both or one per mode, else the default.
147 5026 ▶sub _theme_colours {
Subroutine _theme_colours: called CC=4 SCAR=13.9
148 5026 my ( $self, $key, $light, $dark ) = @_;
149 5026 my $value = $self->theme->{$key};
150 10052 ▶ my ( $l, $d ) = map { scalar $self->_colour($_) }
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 27 4999 5026
151 5026 ref $value eq 'HASH' ? @{$value}{qw( light dark )} : ( $value, $value );
152 5026 ▶ return ( defined $l ? $l : $light, defined $d ? $d : $dark );
Branch: 2 branches
BranchTrueFalseTotal
defined $l ? : 37 4989 5026
defined $d ? : 38 4988 5026
153 }
154  
155 # The phases that blink, in the order of the legend: only known phases reach
156 # the style, whatever order and however often they were named.
157 359 ▶sub _blink_phases {
Subroutine _blink_phases: called CC=1 SCAR=0.0
158 359 my ( $self ) = @_;
159 359 my %blink = map { $_ => 1 } @{ $self->blink };
160 359 return grep { $blink{$_} } $self->phases;
161 }
162  
163 # The period as plain decimals; what does not print as such (inf, a number
164 # beyond %f) gives the default.
165 34 ▶sub _blink_period {
Subroutine _blink_period: called CC=3 SCAR=11.0
166 34 my ( $self ) = @_;
167 34 my $seconds = $self->_n( $self->blink_seconds );
168 34 ▶ return 1.2 unless $seconds =~ /\A[0-9]{1,9}(?:\.[0-9]{1,2})?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seconds =~ /\A[0-9]{1,9}(?:\.[0-9]{1,2})?\z/ 2 32 34
169 32 ▶ return $seconds < 0.01 ? 0.01 : $seconds;
Branch: 1 branch
BranchTrueFalseTotal
$seconds < 0.01 ? : 1 31 32
170 }
171  
172 # The pulse: fill and outline of the hexagon swell and settle, from and back
173 # to what the cell has anyway, so nothing moves and the texts stay readable.
174 # The phase line takes the text colour, to keep its contrast on the fuller
175 # fill. Without motion the outline is as thick as at the peak.
176 359 ▶sub _blink_css {
Subroutine _blink_css: called CC=2 SCAR=6.9
177 359 my ( $self ) = @_;
178 359 my @phases = $self->_blink_phases;
179 359 ▶ return unless @phases;
Branch: 1 branch
BranchTrueFalseTotal
unless @phases 325 34 359
180 34 my $s = '.comb-svg';
181 34 my $hex = join( ',', map { $s.' .comb.phase-'.$_.' .hex' } @phases );
182 34 my $width = 'stroke-width:'.$self->_n( $self->size * 0.07 );
183 return (
184 '@keyframes comb-blink{50%{fill-opacity:.4;'.$width.'}}',
185 $hex.'{animation:comb-blink '.$self->_blink_period.'s ease-in-out infinite}',
186 34 join( ',', map { $s.' .comb.phase-'.$_.' .phase' } @phases ).'{fill:var(--comb-fg)}',
187 '@media (prefers-reduced-motion:reduce){'.$hex.'{animation:none;'.$width.'}}'
188 );
189 }
190  
191 16514 ▶sub _var { '--comb-'.lc $_[1] }
Subroutine _var: called CC=1 SCAR=0.0
192  
193 # $small: some cell sets its name in the small two-line font. Its rule is
194 # written only then, so a picture without such a name keeps its bytes.
195 # $reason: some cell has a reason line; the same holds for its rule.
196 359 ▶sub _style {
Subroutine _style: called CC=4 SCAR=13.9
197 359 my ( $self, $small, $reason ) = @_;
198 359 my $r = $self->size;
199 359 my $colours = $self->_default_colours;
200 359 my ( @light, @dark );
201 359 for my $colour ( $self->_base_colours, map { [ $_, @{ $colours->{$_} } ] } $self->phases ) {
202 5026 my ( $light, $dark ) = $self->_theme_colours(@$colour);
203 5026 push @light, $self->_var( $colour->[0] ).':'.$light;
204 5026 push @dark, $self->_var( $colour->[0] ).':'.$dark;
205 }
206 359 push @light, '--comb-tint:.13', '--comb-tint-muted:.06';
207 359 push @dark, '--comb-tint:.2', '--comb-tint-muted:.09';
208  
209 359 my $s = '.comb-svg';
210 my @css = (
211 $s.'{'.join( ';', @light )
212 .';font-family:system-ui,-apple-system,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif}',
213 '@media (prefers-color-scheme:dark){'.$s.'{'.join( ';', @dark ).'}}',
214 $s.' .panel{fill:var(--comb-bg);stroke:var(--comb-border);stroke-width:1}',
215 $s.' text{fill:var(--comb-fg)}',
216 $s.' .heading{font-size:'.$self->_n( $self->_title_font ).'px;font-weight:600}',
217 $s.' .group-name{font-size:'.$self->_n( $self->_group_font )
218 .'px;font-weight:600;fill:var(--comb-muted)}',
219 $s.' .group-rule{stroke:var(--comb-border);stroke-width:1}',
220 $s.' .hex{fill:var(--comb-unknown);fill-opacity:var(--comb-tint);stroke:var(--comb-unknown);stroke-width:'
221 .$self->_n( $r * 0.03 ).';stroke-linejoin:round}',
222 ( map {
223 359 ▶ $s.' .phase-'.$_.' .hex{fill:var('.$self->_var($_).');stroke:var('.$self->_var($_).')}'
Branch: 2 branches
BranchTrueFalseTotal
$small ? : 19 340 359
$reason ? : 31 328 359
224 } $self->phases ),
225 $s.' .borrowed .hex{stroke-dasharray:'.$self->_n( $r * 0.11 ).' '.$self->_n( $r * 0.08 ).'}',
226 $s.' .disabled .hex{fill-opacity:var(--comb-tint-muted);stroke-opacity:.55}',
227 $s.' .comb text{text-anchor:middle}',
228 $s.' .name{font-size:'.$self->_n( $self->_name_font ).'px;font-weight:500}',
229 $small ? $s.' .name-small{font-size:'.$self->_n( $self->_name_small_font ).'px}' : (),
230 $s.' .phase{font-size:'.$self->_n( $self->_phase_font ).'px;fill:var(--comb-muted)}',
231 $s.' .upstream{font-size:'.$self->_n( $self->_upstream_font )
232 .'px;font-style:italic;fill:var(--comb-muted)}',
233 $reason ? $s.' .reason{font-size:'.$self->_n( $self->_reason_font )
234 .'px;fill:var(--comb-muted)}' : (),
235 $s.' .disabled text{fill:var(--comb-muted);fill-opacity:.8}',
236 $s.' .dep{fill:none;stroke:var(--comb-edge);stroke-opacity:.5;stroke-width:'
237 .$self->_n( $r * 0.025 ).';stroke-linecap:round;pointer-events:none}',
238 $s.' .arrow{fill:var(--comb-edge);fill-opacity:.5}',
239 $s.' .dep-start{fill:var(--comb-edge);fill-opacity:.5;pointer-events:none}',
240 $s.' a{cursor:pointer;text-decoration:none}',
241 $s.' .legend text{font-size:'.$self->_n( $self->_legend_font ).'px;fill:var(--comb-muted)}',
242 $s.' .legend .hex{stroke-width:'.$self->_n( $r * 0.02 ).'}',
243 $s.' .legend .count{font-weight:600;fill:var(--comb-fg)}',
244 $self->_blink_css
245 );
246 359 return $self->_el( 'style', [], $self->_text( join( "\n", @css ) ) );
247 }
248  
249 #### Measures, all derived from size
250  
251 1075 ▶sub _pad { $_[0]->size * 0.45 }
Subroutine _pad: called CC=1 SCAR=0.0
252  
253 1077 ▶sub _title_font { $_[0]->size * 0.34 }
Subroutine _title_font: called CC=1 SCAR=0.0
254  
255 717 ▶sub _title_band { $_[0]->size * 0.75 }
Subroutine _title_band: called CC=1 SCAR=0.0
256  
257 421 ▶sub _group_font { $_[0]->size * 0.2 }
Subroutine _group_font: called CC=1 SCAR=0.0
258  
259 6245 ▶sub _name_font { $_[0]->size * 0.22 }
Subroutine _name_font: called CC=1 SCAR=0.0
260  
261 # A name on two lines that is too wide at _name_font: 13 characters a line
262 # instead of 11.
263 679 ▶sub _name_small_font { $_[0]->size * 0.185 }
Subroutine _name_small_font: called CC=1 SCAR=0.0
264  
265 359 ▶sub _phase_font { $_[0]->size * 0.17 }
Subroutine _phase_font: called CC=1 SCAR=0.0
266  
267 4432 ▶sub _upstream_font { $_[0]->size * 0.15 }
Subroutine _upstream_font: called CC=1 SCAR=0.0
268  
269 # The reason line is as small as the upstream line: 16 characters.
270 3795 ▶sub _reason_font { $_[0]->_upstream_font }
Subroutine _reason_font: called CC=1 SCAR=0.0
271  
272 2771 ▶sub _legend_font { $_[0]->size * 0.19 }
Subroutine _legend_font: called CC=1 SCAR=0.0
273  
274 1555 ▶sub _legend_swatch { $_[0]->size * 0.15 }
Subroutine _legend_swatch: called CC=1 SCAR=0.0
275  
276 1522 ▶sub _legend_row { $_[0]->size * 0.44 }
Subroutine _legend_row: called CC=1 SCAR=0.0
277  
278 712 ▶sub _legend_gap { $_[0]->size * 0.5 }
Subroutine _legend_gap: called CC=1 SCAR=0.0
279  
280 # Air kept between a name and the two sides of its hexagon.
281 6998 ▶sub _name_pad { $_[0]->size * 0.14 }
Subroutine _name_pad: called CC=1 SCAR=0.0
282  
283 643 ▶sub _arrow { $_[0]->size * 0.16 }
Subroutine _arrow: called CC=1 SCAR=0.0
284  
285 # Radius of the dot that marks where an edge starts.
286 426 ▶sub _start_dot { $_[0]->size * 0.04 }
Subroutine _start_dot: called CC=1 SCAR=0.0
287  
288 # Text is not measured, it is estimated: this share of the font size per
289 # character. Good enough for a system sans, and the same on every machine.
290 10321 ▶sub _char_width { 0.59 }
Subroutine _char_width: called CC=1 SCAR=0.0
291  
292 1615 ▶sub _text_width {
Subroutine _text_width: called CC=1 SCAR=0.0
293 1615 my ( $self, $text, $font ) = @_;
294 1615 return length( $text ) * $font * $self->_char_width;
295 }
296  
297 # Cuts a text to what fits into $width at $font, with an ellipsis.
298 1849 ▶sub _fit {
Subroutine _fit: called CC=3 SCAR=11.0
299 1849 my ( $self, $text, $font, $width ) = @_;
300 1849 my $max = int( $width / ( $font * $self->_char_width ) );
301 1849 ▶ $max = 1 if $max < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 1 0 1849 1849
302 1849 ▶ return $text if length $text <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $text <= $max 973 876 1849
303 876 return substr( $text, 0, $max - 1 )."\x{2026}";
304 }
305  
306 # How many characters fit into $width at $font, one at the least.
307 6857 ▶sub _chars {
Subroutine _chars: called CC=2 SCAR=7.1
308 6857 my ( $self, $font, $width ) = @_;
309 6857 my $max = int( $width / ( $font * $self->_char_width ) );
310 6857 ▶ return $max < 1 ? 1 : $max;
Branch: 1 branch
BranchTrueFalseTotal
$max < 1 ? : 0 6857 6857
311 }
312  
313 # The name of a cell as it is drawn: { lines => [ one or two ], small => bool }.
314 # A name that fits stays one line. Otherwise it is broken after a hyphen, a
315 # dot or an underscore, at the break that leaves the shortest longer line (the
316 # earlier of two equal ones); when that is too wide for the name font the two
317 # lines take the small one, and what is still too wide is cut there. A name
318 # too long without such a character is cut on its one line.
319 5116 ▶sub _name_lines {
Subroutine _name_lines: called CC=8 SCAR=20.8
320 5116 my ( $self, $name ) = @_;
321 5116 my $width = $self->_layouter->hex_width - 2 * $self->_name_pad;
322 5116 my $length = length $name;
323 5116 ▶ return { lines => [$name], small => 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $length <= $self->_chars($self->_name_font, $width) 4346 770 5116
324 if $length <= $self->_chars( $self->_name_font, $width );
325  
326 770 my ( $at, $longer );
327 770 for my $break ( grep { substr( $name, $_ - 1, 1 ) =~ /\A[-._]\z/ } 1 .. $length - 1 ) {
328 1386 my $rest = $length - $break;
329 1386 ▶ my $long = $break > $rest ? $break : $rest;
Branch: 1 branch
BranchTrueFalseTotal
$break > $rest ? : 636 750 1386
330 1386 ▶ ( $at, $longer ) = ( $break, $long ) if !defined $longer || $long < $longer;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $longer or $long < $longer 770 616 1386
Condition100%3/3
not defined $longer or $long < $longer
l!l&&r!l&&!r
616154616
MC/DC100%2/2
not defined $longer or $long < $longer
not defined $longer $long < $longer
Truth table100%3/3
not(A) or B
A= not defined $longerB= $long < $longer
ABresult
1X1
011
000
331 }
332 770 ▶ return { lines => [ $self->_fit( $name, $self->_name_font, $width ) ], small => 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 154 616 770
333 unless defined $at;
334  
335 616 my @lines = ( substr( $name, 0, $at ), substr( $name, $at ) );
336 616 ▶ return { lines => \@lines, small => 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $longer <= $self->_chars($self->_name_font, $width) 286 330 616
337 if $longer <= $self->_chars( $self->_name_font, $width );
338 return {
339 330 lines => [ map { $self->_fit( $_, $self->_name_small_font, $width ) } @lines ],
340 small => 1
341 };
342 }
343  
344 # The text lines of a cell, top to bottom, as text elements: { class, rows },
345 # a row being [ text, baseline as an offset from the centre of the cell ].
346 # Where the lines sit is decided by _cell_baselines. A reason too long for
347 # its line takes a second one where the cell has the room, see _reason_break.
348 2558 ▶sub _cell_lines {
Subroutine _cell_lines: called CC=12 SCAR=24.8
349 2558 my ( $self, $cell ) = @_;
350 2558 my $name = $self->_name_lines( $cell->name );
351 2558 my @names = @{ $name->{lines} };
352 2558 ▶ my $step = $self->size * ( $name->{small} ? 0.21 : 0.24 );
Branch: 1 branch
BranchTrueFalseTotal
$name->{'small'} ? : 165 2393 2558
353 2558 my $reason = $self->_shows_reason($cell);
354  
355 # The small lines under the phase, by class.
356 2558 ▶ my @below = ( $reason ? 'reason' : (), $cell->borrowed ? 'upstream' : () );
Branch: 2 branches
BranchTrueFalseTotal
$reason ? : 527 2031 2558
$cell->borrowed ? : 278 2280 2558
357 2558 my @at = $self->_cell_baselines( scalar @names, $step, @below );
358 2558 my @reason;
359 2558 ▶ if ($reason) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason) 527 2031 2558
360 527 my $text = $cell->reason;
361 527 my $one = $at[ @names + 1 ];
362 527 @reason = ( $self->_fit( $text, $self->_reason_font, $self->_reason_width($one) ) );
363 527 ▶ my @two = length $text > $self->_reason_chars($one)
Branch: 1 branch
BranchTrueFalseTotal
length $text > $self->_reason_chars($one) ? : 410 117 527
364 ? $self->_cell_baselines( scalar @names, $step, 'reason', @below )
365 : ();
366 527 ▶ if (@two) {
Branch: 1 branch
BranchTrueFalseTotal
if (@two) 299 228 527
367 299 my @under = @two[ @names + 1, @names + 2 ];
368 299 ▶ if ( my @broken = $self->_reason_break( $text, map { $self->_reason_chars($_) } @under ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@broken) = $self->_reason_break($text, map({$self->_reason_chars($_);} @under))) 230 69 299
369 230 $broken[1] = $self->_fit( $broken[1], $self->_reason_font, $self->_reason_width( $under[1] ) );
370 230 @reason = @broken;
371 230 @at = @two;
372 }
373 }
374 }
375  
376 my @lines = (
377 2558 ▶ { class => $name->{small} ? 'name name-small' : 'name', rows => [ map { [ $_, shift @at ] } @names ] },
Branch: 1 branch
BranchTrueFalseTotal
$name->{'small'} ? : 165 2393 2558
378 { class => 'phase', rows => [ [ $cell->phase, shift @at ] ] }
379 );
380 2558 ▶ push @lines, { class => 'reason', rows => [ map { [ $_, shift @at ] } @reason ] } if $reason;
Branch: 1 branch
BranchTrueFalseTotal
if $reason 527 2031 2558
381 2558 ▶ push @lines, { class => 'upstream', rows => [ [
Branch: 2 branches
BranchTrueFalseTotal
if $cell->borrowed 278 2280 2558
defined $cell->upstream_context ? : 274 4 278
382 $self->_fit(
383 defined $cell->upstream_context ? 'from '.$cell->upstream_context : 'borrowed',
384 $self->_upstream_font, $self->_layouter->hex_width * 0.8
385 ),
386 shift @at
387 ] ] } if $cell->borrowed;
388 2558 return @lines;
389 }
390  
391 # The baselines of the lines of a cell, top to bottom, as offsets from its
392 # centre: $names name lines $step apart, the phase, then one line per class in
393 # @below ('reason', once or twice, and 'upstream'). The lines are stacked by
394 # their advance, the distance of a baseline from the one above.
395 #
396 # A cell without a reason line, and one with nothing but one name line, the
397 # phase and one reason line, starts where a cell with one name line and its
398 # phase sits centred, every further line moving that start up.
399 #
400 # Every other cell with a reason line is tight: the small lines sit closer
401 # together and the block is centred as a whole, to stay inside the hexagon.
402 # The advances of such a block may add up to 0.89 of the size; what the block
403 # leaves of that goes to the phase, up to 0.28 of the size from the name
404 # above it, so that it does not read as one more name line. A block that is
405 # too high even with the phase at 0.24 gives an empty list when it has two
406 # reason lines -- there is no room for the second -- and is drawn as it is
407 # otherwise.
408 2968 ▶sub _cell_baselines {
Subroutine _cell_baselines: called CC=11 SCAR=24.0
409 2968 my ( $self, $names, $step, @below ) = @_;
410 2968 my $r = $self->size;
411 2968 my $reason = grep { $_ eq 'reason' } @below;
412 2968 ▶ my $tight = $reason && $names + @below > 2;
Condition100%3/3
$reason && $names + @below > 2
!ll&&!rl&&r
2031151786
MC/DC100%2/2
$reason && $names + @below > 2
$reason $names + @below > 2
Truth table100%3/3
A && B
A= $reasonB= $names + @below > 2
ABresult
0X0
100
111
413 2968 ▶ my ( $to_phase, $to_below ) = $tight ? ( 0.24 * $r, 0.2 * $r ) : ( 0.3 * $r, 0.25 * $r );
Branch: 1 branch
BranchTrueFalseTotal
$tight ? : 786 2182 2968
414 2968 my @advance = ( $to_below ) x @below;
415 2968 ▶ $advance[1] = 0.17 * $r if $reason > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $reason > 1 410 2558 2968
416  
417 2968 my $at;
418 2968 ▶ if ($tight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tight) { } 786 2182 2968
419 786 my $sum = $step * ( $names - 1 ) + $to_phase;
420 786 $sum += $_ for @advance;
421 786 my $spare = 0.89 * $r - $sum;
422 786 ▶ return if $reason > 1 && $spare < -$r * 1e-9;
Branch: 1 branch
BranchTrueFalseTotal
if $reason > 1 and $spare < -$r * "1e-09" 111 675 786
Condition100%3/3
$reason > 1 and $spare < -$r * "1e-09"
!ll&&!rl&&r
376299111
MC/DC100%2/2
$reason > 1 and $spare < -$r * "1e-09"
$reason > 1 $spare < -$r * "1e-09"
Truth table100%3/3
A and B
A= $reason > 1B= $spare < -$r * "1e-09"
ABresult
0X0
100
111
423 675 ▶ $spare = $spare < 0 ? 0 : $spare > 0.04 * $r ? 0.04 * $r : $spare;
Branch: 2 branches
BranchTrueFalseTotal
$spare < 0 ? : 0 675 675
$spare > 0.04 * $r ? : 587 88 675
424 675 $to_phase += $spare;
425 675 $at = 0.06 * $r - ( $sum + $spare ) / 2;
426 }
427 else {
428 2182 $at = -0.07 * $r - $step * ( $names - 1 ) / 2 - 0.1 * $r * @below;
429 }
430 2857 my @at = ( $at );
431 2857 push @at, $at += $_ for ( $step ) x ( $names - 1 ), $to_phase, @advance;
432 2857 return @at;
433 }
434  
435 # The width a reason line with its baseline at $at may take, and the number
436 # of characters that is: sixteen inside the full-width band of the hexagon,
437 # fewer for a line that reaches below it.
438 1882 ▶sub _reason_width {
Subroutine _reason_width: called CC=1 SCAR=0.0
439 1882 my ( $self, $at ) = @_;
440 1882 my $font = $self->_reason_font;
441 1882 return $self->_line_width( $at - 0.75 * $font, $at + 0.25 * $font );
442 }
443  
444 1125 ▶sub _reason_chars {
Subroutine _reason_chars: called CC=1 SCAR=0.0
445 1125 my ( $self, $at ) = @_;
446 1125 return $self->_chars( $self->_reason_font, $self->_reason_width($at) );
447 }
448  
449 # A reason on two lines of at most $first and $second characters, or nothing
450 # when it cannot be broken. It breaks at a run of whitespace, which is
451 # dropped, and before an upper-case letter that follows a lower-case letter
452 # or a digit. Of the breaks that make both lines fit it takes the one with the
453 # shortest longer line (the earlier of two equal ones); when there is none,
454 # the last break whose first line fits, the second line then being too long
455 # and left to the caller to cut.
456 299 ▶sub _reason_break {
Subroutine _reason_break: called CC=9 SCAR=22.1
457 299 my ( $self, $text, $first, $second ) = @_;
458 299 my ( @best, $longer, @last );
459 299 ▶ while ( $text =~ /\s+|(?<=[\p{Ll}0-9])(?=\p{Lu})/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\s+|(?<=[\p{Ll}0-9])(?=\p{Lu})/g) 611 299 910
460 611 my @lines = ( substr( $text, 0, $-[0] ), substr( $text, $+[0] ) );
461 611 ▶ next if !length $lines[0] || !length $lines[1] || length $lines[0] > $first;
Branch: 1 branch
BranchTrueFalseTotal
if not length $lines[0] && length $lines[1] or length $lines[0] > $first 287 324 611
Condition50%3/6
not length $lines[0] && length $lines[1] or length $lines[0] > $first
l!l&&r!l&&!r
0287324
length $lines[0] && length $lines[1]
!ll&&!rl&&r
00611
MC/DC33%1/3
not length $lines[0] && length $lines[1] or length $lines[0] > $first
length $lines[0] length $lines[1] length $lines[0] > $first
Truth table50%2/4
not(A && B) or C
A= length $lines[0]B= length $lines[1]C= length $lines[0] > $first
ABCresult
0XX1
10X1
1111
1100
462 324 @last = @lines;
463 324 ▶ next if length $lines[1] > $second;
Branch: 1 branch
BranchTrueFalseTotal
if length $lines[1] > $second 144 180 324
464 180 my ( $long ) = sort { $b <=> $a } map { length } @lines;
465 180 ▶ ( $longer, @best ) = ( $long, @lines ) if !defined $longer || $long < $longer;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $longer or $long < $longer 168 12 180
Condition100%3/3
not defined $longer or $long < $longer
l!l&&r!l&&!r
1551312
MC/DC100%2/2
not defined $longer or $long < $longer
not defined $longer $long < $longer
Truth table100%3/3
not(A) or B
A= not defined $longerB= $long < $longer
ABresult
1X1
011
000
466 }
467 299 ▶ return @best ? @best : @last;
Branch: 1 branch
BranchTrueFalseTotal
@best ? : 155 144 299
468 }
469  
470 # Whether a cell gets a reason line: it has a reason and is not Running.
471 5116 ▶sub _shows_reason {
Subroutine _shows_reason: called CC=5 SCAR=16.6
472 5116 my ( $self, $cell ) = @_;
473 5116 my $reason = $cell->reason;
474 5116 ▶ return 0 if !defined $reason || ref $reason || !length $reason;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $reason or ref $reason or not length $reason 4062 1054 5116
Condition66%4/6
not defined $reason or ref $reason or not length $reason
l!l&&r!l&&!r
406201054
not defined $reason or ref $reason
l!l&&r!l&&!r
406201054
MC/DC33%1/3
not defined $reason or ref $reason or not length $reason
not defined $reason ref $reason not length $reason
Truth table50%2/4
not(A) or B or not(C)
A= not defined $reasonB= ref $reasonC= not length $reason
ABCresult
1XX1
01X1
0011
0000
475 1054 ▶ return $cell->phase eq 'Running' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$cell->phase eq 'Running' ? : 0 1054 1054
476 }
477  
478 # The width a line of text may take when it reaches from $top to $bottom,
479 # both offsets from the centre of the cell: the width of the hexagon where it
480 # is narrowest in that span -- full inside the middle band of half a radius
481 # up and down, less towards the corners -- minus the air on both sides.
482 1882 ▶sub _line_width {
Subroutine _line_width: called CC=3 SCAR=11.0
483 1882 my ( $self, $top, $bottom ) = @_;
484 1882 my $r = $self->size;
485 1882 my ( $far ) = sort { $b <=> $a } abs $top, abs $bottom;
486 1882 ▶ my $share = $far <= $r / 2 ? 1 : $far >= $r ? 0 : 2 * ( $r - $far ) / $r;
Branch: 2 branches
BranchTrueFalseTotal
$far <= $r / 2 ? : 1708 174 1882
$far >= $r ? : 0 174 174
487 1882 return $self->_layouter->hex_width * $share - 2 * $self->_name_pad;
488 }
489  
490 #### Escaping
491  
492 # Text content. Drops what XML 1.0 cannot carry, escapes the five special
493 # characters, and writes everything outside ASCII as a character reference,
494 # so the document is plain ASCII whatever the input was.
495 68081 ▶sub _text {
Subroutine _text: called CC=3 SCAR=11.1
496 68081 my ( $self, $value ) = @_;
497 68081 ▶ return '' if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 68081 68081
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
0068081
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
498 68081 $value = ''.$value;
499 68081 $value =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g;
500 68081 $value =~ s/&/&amp;/g;
501 68081 $value =~ s/</&lt;/g;
502 68081 $value =~ s/>/&gt;/g;
503 68081 $value =~ s/"/&quot;/g;
504 68081 $value =~ s/'/&#39;/g;
505 68081 $value =~ s/([\x0D\x7F-\x{10FFFF}])/'&#'.ord($1).';'/ge;
506 68081 return $value;
507 }
508  
509 # Attribute value: as text, and tab and newline as references too, so they
510 # survive attribute value normalisation.
511 55191 ▶sub _attr {
Subroutine _attr: called CC=1 SCAR=0.1
512 55191 my ( $self, $value ) = @_;
513 55191 $value = $self->_text($value);
514 55191 $value =~ s/([\x09\x0A])/'&#'.ord($1).';'/ge;
515 55191 return $value;
516 }
517  
518 # One element. Attributes are name/value pairs in the order given, every
519 # value escaped here; $content is markup already and undef closes the tag.
520 23531 ▶sub _el {
Subroutine _el: called CC=3 SCAR=11.0
521 23531 my ( $self, $name, $attrs, $content ) = @_;
522 23531 my @pairs = @$attrs;
523 23531 my $tag = '<'.$name;
524 23531 ▶ while (@pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 55191 23531 78722
525 55191 my ( $key, $value ) = splice @pairs, 0, 2;
526 55191 $tag .= ' '.$key.'="'.$self->_attr($value).'"';
527 }
528 23531 ▶ return defined $content ? $tag.'>'.$content.'</'.$name.'>' : $tag.'/>';
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 18728 4803 23531
529 }
530  
531 # Two decimals, as a number: the same on every platform, and no '-0'.
532 68677 ▶sub _n {
Subroutine _n: called CC=1 SCAR=0.0
533 68677 my ( $self, $value ) = @_;
534 68677 return sprintf( '%.2f', $value ) + 0;
535 }
536  
537 #### Render
538  
539 362 ▶sub render {
Subroutine render: called CC=12 SCAR=24.9
Pod documented
540 362 my ( $self ) = @_;
541 362 my $layout = $self->_layouter->layout;
542 359 my $pad = $self->_pad;
543 359 my %cell = map { $_->id => $_ } @{ $self->cells };
544 359 my @placed = grep { $cell{ $_->{id} } } @{ $layout->{cells} };
545  
546 359 my %count;
547 359 $count{ $cell{ $_->{id} }->phase }++ for @placed;
548 359 my @occurring = grep { $count{$_} } $self->phases;
549  
550 my @legend = $self->legend
551 359 ▶ ? map { { phase => $_, count => $count{$_}, width => $self->_legend_item_width( $_, $count{$_} ) } }
Branch: 1 branch
BranchTrueFalseTotal
$self->legend ? : 354 5 359
552 @occurring
553 : ();
554 359 my $content = $layout->{width};
555 359 for my $width (
556 $self->_text_width( $self->title, $self->_title_font ),
557 1206 ( map { $_->{width} } @legend ),
558 25 ( map { $self->_text_width( $_->{name}, $self->_group_font ) }
559 377 grep { defined $_->{name} } @{ $layout->{groups} } )
560 ) {
561 1590 ▶ $content = $width if $width > $content;
Branch: 1 branch
BranchTrueFalseTotal
if $width > $content 5 1585 1590
562 }
563 359 my @legend_rows = $self->_legend_rows( \@legend, $content );
564  
565 359 my $ox = $pad;
566 359 my $oy = $pad + $self->_title_band;
567 359 my $bottom = $oy + $layout->{height};
568 359 my $legend_top = $bottom + $self->_legend_gap;
569 359 ▶ $bottom = $legend_top + @legend_rows * $self->_legend_row - $self->_legend_row / 4
Branch: 1 branch
BranchTrueFalseTotal
if @legend_rows 349 10 359
570 if @legend_rows;
571 359 my $width = $content + 2 * $pad;
572 359 my $height = $bottom + $pad;
573  
574 my $summary = ( @placed == 1 ? '1 Comb' : scalar(@placed).' Combs' )
575 359 ▶ .( @occurring ? ': '.join( ', ', map { $count{$_}.' '.$_ } @occurring ) : '' );
Branch: 2 branches
BranchTrueFalseTotal
@placed == 1 ? : 36 323 359
@occurring ? : 354 5 359
576  
577 359 my %at = map { $_->{id} => [ $_->{x} + $ox, $_->{y} + $oy ] } @placed;
578  
579 my @out = (
580 $self->_el( 'title', [ id => 'comb-title' ], $self->_text( $self->title ) ),
581 $self->_el( 'desc', [ id => 'comb-desc' ], $self->_text($summary) ),
582 $self->_style(
583 2558 scalar( grep { $self->_name_lines( $cell{ $_->{id} }->name )->{small} } @placed ),
584 359 scalar( grep { $self->_shows_reason( $cell{ $_->{id} } ) } @placed )
585 ),
586 $self->_el( 'defs', [], $self->_marker ),
587 $self->_el( 'rect', [
588 class => 'panel',
589 x => 0.5,
590 y => 0.5,
591 width => $self->_n( $width - 1 ),
592 height => $self->_n( $height - 1 ),
593 rx => $self->_n( $self->size * 0.22 )
594 ] ),
595 $self->_el( 'text', [
596 class => 'heading',
597 x => $self->_n($ox),
598 y => $self->_n( $pad + $self->_title_font * 0.8 )
599 ], $self->_text( $self->title ) )
600 );
601  
602 37 push @out, map { $self->_group( $_, $ox, $oy, $content ) }
603 359 grep { $_->{heading} } @{ $layout->{groups} };
604  
605 359 ▶ if ( $self->edges ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->edges) 282 77 359
606 142 my @paths = map { $self->_edge( $_, $at{ $_->{from} }, $at{ $_->{to} } ) }
607 282 ▶ grep { $at{ $_->{from} } && $at{ $_->{to} } } @{ $layout->{edges} };
Condition33%1/3
$at{$_->{'from'}} && $at{$_->{'to'}}
!ll&&!rl&&r
00142
MC/DC0%0/2
$at{$_->{'from'}} && $at{$_->{'to'}}
$at{$_->{'from'}} $at{$_->{'to'}}
Truth table33%1/3
A && B
A= $at{$_->{'from'}}B= $at{$_->{'to'}}
ABresult
0X0
100
111
608 282 ▶ push @out, $self->_el( 'g', [ class => 'deps' ], join( '', @paths ) ) if @paths;
Branch: 1 branch
BranchTrueFalseTotal
if @paths 56 226 282
609 }
610  
611 359 push @out, map { $self->_comb( $cell{ $_->{id} }, @{ $at{ $_->{id} } } ) } @placed;
612  
613 359 ▶ push @out, $self->_legend( \@legend_rows, $ox, $legend_top ) if @legend_rows;
Branch: 1 branch
BranchTrueFalseTotal
if @legend_rows 349 10 359
614  
615 359 return $self->_el( 'svg', [
616 xmlns => 'http://www.w3.org/2000/svg',
617 viewBox => '0 0 '.$self->_n($width).' '.$self->_n($height),
618 role => 'img',
619 class => 'comb-svg',
620 'aria-labelledby' => 'comb-title comb-desc'
621 ], "\n".join( "\n", @out )."\n" )."\n";
622 }
623  
624  
625 359 ▶sub _marker {
Subroutine _marker: called CC=1 SCAR=0.0
626 359 my ( $self ) = @_;
627 359 my $arrow = $self->_n( $self->_arrow );
628 359 return $self->_el( 'marker', [
629 id => 'comb-arrow',
630 viewBox => '0 0 10 10',
631 refX => 0,
632 refY => 5,
633 markerWidth => $arrow,
634 markerHeight => $arrow,
635 markerUnits => 'userSpaceOnUse',
636 orient => 'auto'
637 ], $self->_el( 'path', [ class => 'arrow', d => 'M0 1L10 5L0 9z' ] ) );
638 }
639  
640 # The heading of a group: its name and a faint rule to the right of it. The
641 # unnamed group among named ones gets the rule alone.
642 37 ▶sub _group {
Subroutine _group: called CC=4 SCAR=13.9
643 37 my ( $self, $group, $ox, $oy, $content ) = @_;
644 37 my $x = $group->{heading}{x} + $ox;
645 37 my $y = $group->{heading}{y} + $oy;
646 37 my $font = $self->_group_font;
647 37 my ( $text, $from ) = ( '', $x );
648 37 ▶ if ( defined $group->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group->{"name"}) 25 12 37
649 $text = $self->_el( 'text', [
650 class => 'group-name',
651 x => $self->_n($x),
652 y => $self->_n($y)
653 25 ], $self->_text( $group->{name} ) );
654 25 $from = $x + $self->_text_width( $group->{name}, $font ) + $font * 0.8;
655 }
656 37 my $to = $ox + $content;
657 37 ▶ my $rule = $to - $from > $font
Branch: 1 branch
BranchTrueFalseTotal
$to - $from > $font ? : 37 0 37
658 ? $self->_el( 'line', [
659 class => 'group-rule',
660 x1 => $self->_n($from),
661 y1 => $self->_n( $y - $font * 0.35 ),
662 x2 => $self->_n($to),
663 y2 => $self->_n( $y - $font * 0.35 )
664 ] )
665 : '';
666 return $self->_el( 'g', [
667 class => 'group',
668 37 ▶ defined $group->{name} ? ( 'data-group' => $group->{name} ) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $group->{'name'} ? : 25 12 37
669 ], $text.$rule );
670 }
671  
672 3764 ▶sub _hexagon {
Subroutine _hexagon: called CC=1 SCAR=0.0
673 3764 my ( $self, $x, $y, $radius ) = @_;
674 3764 my $half = $radius * sqrt(3) / 2;
675 3764 my @points = (
676 [ $x, $y - $radius ],
677 [ $x + $half, $y - $radius / 2 ],
678 [ $x + $half, $y + $radius / 2 ],
679 [ $x, $y + $radius ],
680 [ $x - $half, $y + $radius / 2 ],
681 [ $x - $half, $y - $radius / 2 ]
682 );
683 return $self->_el( 'polygon', [
684 class => 'hex',
685 3764 points => join( ' ', map { $self->_n( $_->[0] ).','.$self->_n( $_->[1] ) } @points )
686 ] );
687 }
688  
689 2558 ▶sub _tooltip {
Subroutine _tooltip: called CC=15 SCAR=27.1
690 2558 my ( $self, $cell ) = @_;
691 2558 my @lines = ( $cell->id );
692 2558 ▶ push @lines, 'namespace: '.$cell->namespace if defined $cell->namespace;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->namespace 540 2018 2558
693 2558 ▶ push @lines, 'class: '.$cell->class if defined $cell->class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->class 40 2518 2558
694 2558 ▶ push @lines, 'phase: '.$cell->phase
Branch: 1 branch
BranchTrueFalseTotal
$cell->phase eq 'Unknown' && defined $cell->raw_phase ? : 9 2549 2558
Condition100%3/3
$cell->phase eq "Unknown" and defined $cell->raw_phase
!ll&&!rl&&r
154510049
MC/DC100%2/2
$cell->phase eq "Unknown" and defined $cell->raw_phase
$cell->phase eq "Unknown" defined $cell->raw_phase
Truth table100%3/3
A and B
A= $cell->phase eq "Unknown"B= defined $cell->raw_phase
ABresult
0X0
100
111
695 .( $cell->phase eq 'Unknown' && defined $cell->raw_phase ? ' ('.$cell->raw_phase.')' : '' );
696 2558 ▶ push @lines, 'message: '.$cell->message
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->message and $cell->phase ne "Running" 25 2533 2558
Condition66%2/3
defined $cell->message and $cell->phase ne "Running"
!ll&&!rl&&r
2533025
MC/DC50%1/2
defined $cell->message and $cell->phase ne "Running"
defined $cell->message $cell->phase ne "Running"
Truth table66%2/3
A and B
A= defined $cell->messageB= $cell->phase ne "Running"
ABresult
0X0
100
111
697 if defined $cell->message && $cell->phase ne 'Running';
698 40 ▶ push @lines, map { 'endpoint: '.$_->{name}.( defined $_->{port} ? ' '.$_->{port} : '' ) }
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'port'} ? : 34 6 40
699 2558 @{ $cell->endpoints };
700 2558 ▶ if ( $cell->upstream_recorded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell->upstream_recorded) 291 2267 2558
701 291 ▶ my @upstream = (
Branch: 2 branches
BranchTrueFalseTotal
defined $cell->upstream_class ? : 14 277 291
defined $cell->upstream_context ? : 285 6 291
702 defined $cell->upstream_class ? $cell->upstream_class : (),
703 defined $cell->upstream_context ? 'context '.$cell->upstream_context : ()
704 );
705 291 ▶ push @lines, 'upstream: '.( @upstream ? join( ', ', @upstream ) : 'recorded' )
Branch: 2 branches
BranchTrueFalseTotal
@upstream ? : 287 4 291
$cell->borrowed ? : 278 13 291
706 .( $cell->borrowed ? '' : ' (not borrowing)' );
707 291 ▶ push @lines, 'via: '.join( ', ', @{ $cell->upstream_via } ) if @{ $cell->upstream_via };
Branch: 1 branch
BranchTrueFalseTotal
if @{$cell->upstream_via;} 14 277 291
708 }
709 2558 ▶ push @lines, 'missing: '.join( ', ', @{ $cell->missing } ) if @{ $cell->missing };
Branch: 1 branch
BranchTrueFalseTotal
if @{$cell->missing;} 7 2551 2558
710 2558 return join( "\n", @lines );
711 }
712  
713 # The href for a cell, or undef: what the link callback answers, when it is
714 # relative or http(s) and carries neither whitespace nor control characters.
715 2558 ▶sub _href {
Subroutine _href: called CC=9 SCAR=22.0
716 2558 my ( $self, $cell ) = @_;
717 2558 ▶ return unless $self->link;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->link 2522 36 2558
718 36 my $href = eval { $self->link->($cell) };
719 36 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 35 36
720 1 carp __PACKAGE__.'->render: link callback died for '.$cell->id.': '.$error;
721 1 return;
722 }
723 35 ▶ return if !defined $href || ref $href || !length $href;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $href or ref $href or not length $href 5 30 35
Condition83%5/6
not defined $href or ref $href or not length $href
l!l&&r!l&&!r
4130
not defined $href or ref $href
l!l&&r!l&&!r
4031
MC/DC66%2/3
not defined $href or ref $href or not length $href
not defined $href ref $href not length $href
Truth table75%3/4
not(A) or B or not(C)
A= not defined $hrefB= ref $hrefC= not length $href
ABCresult
1XX1
01X1
0011
0000
724 30 ▶ return if $href =~ /[\x00-\x20\x7F]/;
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ /[\x00-\x20\x7F]/ 6 24 30
725 24 ▶ return $href if $href =~ m{\Ahttps?://}i;
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ m[\Ahttps?://]i 6 18 24
726 18 ▶ return if $href =~ m{\A[^/?#]*:};
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ m[\A[^/?#]*:] 7 11 18
727 11 return $href;
728 }
729  
730 # One text element of a cell. A single row is the text itself; several rows
731 # are one <tspan> each, placed on its own, so the text content of the element
732 # is still the whole of what is shown.
733 5921 ▶sub _cell_text {
Subroutine _cell_text: called CC=2 SCAR=6.9
734 5921 my ( $self, $line, $x, $y ) = @_;
735 6459 my @rows = map { [ $self->_text( $_->[0] ), x => $self->_n($x), y => $self->_n( $y + $_->[1] ) ] }
736 5921 @{ $line->{rows} };
737 5921 ▶ return $self->_el( 'text', [ class => $line->{class}, @{ $rows[0] }[ 1 .. 4 ] ], $rows[0][0] )
Branch: 1 branch
BranchTrueFalseTotal
if @rows == 1 5383 538 5921
738 if @rows == 1;
739 return $self->_el( 'text', [ class => $line->{class} ],
740 538 join( '', map { $self->_el( 'tspan', [ @{$_}[ 1 .. 4 ] ], $_->[0] ) } @rows ) );
741 }
742  
743 2558 ▶sub _comb {
Subroutine _comb: called CC=4 SCAR=13.9
744 2558 my ( $self, $cell, $x, $y ) = @_;
745 2558 my $r = $self->size;
746 2558 my $borrowed = $cell->borrowed;
747 2558 my $disabled = $cell->phase eq 'Disabled';
748  
749 my @parts = (
750 $self->_el( 'title', [], $self->_text( $self->_tooltip($cell) ) ),
751 $self->_hexagon( $x, $y, $r ),
752 2558 map { $self->_cell_text( $_, $x, $y ) } $self->_cell_lines($cell)
753 );
754  
755 2558 ▶ my $group = $self->_el( 'g', [
Branch: 2 branches
BranchTrueFalseTotal
$borrowed ? : 278 2280 2558
$disabled ? : 121 2437 2558
756 class => join( ' ', 'comb', 'phase-'.$cell->phase,
757 $borrowed ? 'borrowed' : (), $disabled ? 'disabled' : () ),
758 'data-name' => $cell->name,
759 'data-id' => $cell->id,
760 'data-phase' => $cell->phase
761 ], join( '', @parts ) );
762  
763 2558 my $href = $self->_href($cell);
764 2558 ▶ return defined $href ? $self->_el( 'a', [ href => $href ], $group ) : $group;
Branch: 1 branch
BranchTrueFalseTotal
defined $href ? : 17 2541 2558
765 }
766  
767 # One dependency, dependent to dependency, drawn between the label-free
768 # corner regions of its two ends: the name, phase and upstream lines fill the
769 # middle band of a hexagon, the regions towards its top and bottom corner are
770 # free. The line starts at a dot just inside the outline of the dependent and
771 # its arrowhead ends inside the dependency.
772 #
773 # Across rows it is a straight line from the corner region facing the
774 # dependency into the corner region facing the dependent. Inside one row it
775 # is a bow through the corner regions of the cells it passes: above the row
776 # when it runs left to right, below when it runs right to left, so the two
777 # edges of a cycle never share a line.
778 142 ▶sub _edge {
Subroutine _edge: called CC=10 SCAR=23.1
779 142 my ( $self, $edge, $from, $to ) = @_;
780 142 my $r = $self->size;
781 142 my ( $dx, $dy ) = ( $to->[0] - $from->[0], $to->[1] - $from->[1] );
782 142 ▶ return () if abs($dx) < 0.01 && abs($dy) < 0.01;
Branch: 1 branch
BranchTrueFalseTotal
if abs $dx < "0.01" and abs $dy < "0.01" 0 142 142
Condition66%2/3
abs $dx < "0.01" and abs $dy < "0.01"
!ll&&!rl&&r
130120
MC/DC0%0/2
abs $dx < "0.01" and abs $dy < "0.01"
abs $dx < "0.01" abs $dy < "0.01"
Truth table66%2/3
A and B
A= abs $dx < "0.01"B= abs $dy < "0.01"
ABresult
0X0
100
111
783  
784 142 my ( @a, @b, @via );
785 142 ▶ if ( abs($dy) < 0.01 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $dy < 0.01) { } 24 118 142
786 24 ▶ my $along = $dx > 0 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$dx > 0 ? : 13 11 24
787 24 my $side = -$along;
788 24 my $steps = abs($dx) / $self->_layouter->step_x;
789 24 ▶ $steps = 3 if $steps > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $steps > 3 1 23 24
790 24 @a = ( $from->[0] + $along * $r * 0.3, $from->[1] + $side * $r * 0.62 );
791 24 @b = ( $to->[0] - $along * $r * 0.35, $to->[1] + $side * $r * 0.55 );
792 24 @via = ( ( $a[0] + $b[0] ) / 2, $from->[1] + $side * $r * ( 0.9 + 0.15 * $steps ) );
793 }
794 else {
795 118 ▶ my $side = $dy > 0 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$dy > 0 ? : 0 118 118
796 118 @a = ( $from->[0] + $self->_clamp( $dx * 0.3, $r * 0.2 ), $from->[1] + $side * $r * 0.62 );
797 118 @b = ( $to->[0] - $self->_clamp( $dx * 0.3, $r * 0.3 ), $to->[1] - $side * $r * 0.6 );
798 }
799  
800 # @b is the tip of the arrowhead; the marker draws it beyond the end of the
801 # path, so the path stops one arrowhead short, along its last direction.
802 142 ▶ my @last = @via ? @via : @a;
Branch: 1 branch
BranchTrueFalseTotal
@via ? : 24 118 142
803 142 my $angle = atan2( $b[1] - $last[1], $b[0] - $last[0] );
804 142 my @end = ( $b[0] - cos($angle) * $self->_arrow, $b[1] - sin($angle) * $self->_arrow );
805  
806 # @a is the centre of the start dot; the line leaves it at its rim, so the
807 # two translucent shapes do not overlap.
808 142 ▶ my @first = @via ? @via : @end;
Branch: 1 branch
BranchTrueFalseTotal
@via ? : 24 118 142
809 142 my $out = atan2( $first[1] - $a[1], $first[0] - $a[0] );
810 142 my @start = ( $a[0] + cos($out) * $self->_start_dot, $a[1] + sin($out) * $self->_start_dot );
811  
812 142 ▶ my $d = 'M'.$self->_n( $start[0] ).' '.$self->_n( $start[1] )
Branch: 1 branch
BranchTrueFalseTotal
@via ? : 24 118 142
813 .( @via ? 'Q'.$self->_n( $via[0] ).' '.$self->_n( $via[1] ).' ' : 'L' )
814 .$self->_n( $end[0] ).' '.$self->_n( $end[1] );
815 return $self->_el( 'path', [
816 class => 'dep',
817 'data-from' => $edge->{from},
818 'data-to' => $edge->{to},
819 142 d => $d,
820 'marker-end' => 'url(#comb-arrow)'
821 ] ).$self->_el( 'circle', [
822 class => 'dep-start',
823 cx => $self->_n( $a[0] ),
824 cy => $self->_n( $a[1] ),
825 r => $self->_n( $self->_start_dot )
826 ] );
827 }
828  
829 236 ▶sub _clamp {
Subroutine _clamp: called CC=3 SCAR=11.0
830 236 my ( $self, $value, $limit ) = @_;
831 236 ▶ return $value > $limit ? $limit : $value < -$limit ? -$limit : $value;
Branch: 2 branches
BranchTrueFalseTotal
$value > $limit ? : 61 175 236
$value < -$limit ? : 61 114 175
832 }
833  
834 #### Legend
835  
836 1206 ▶sub _legend_item_width {
Subroutine _legend_item_width: called CC=1 SCAR=0.0
837 1206 my ( $self, $phase, $count ) = @_;
838 1206 return $self->_legend_swatch * 2.6
839 + $self->_text_width( $phase.' '.$count, $self->_legend_font );
840 }
841  
842 # Fills rows of at most $width, at least one item each.
843 359 ▶sub _legend_rows {
Subroutine _legend_rows: called CC=4 SCAR=13.9
844 359 my ( $self, $items, $width ) = @_;
845 359 my $gap = $self->size * 0.35;
846 359 my ( @rows, $used );
847 359 for my $item (@$items) {
848 1206 ▶ if ( !@rows || $used + $gap + $item->{width} > $width ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @rows or $used + $gap + $item->{"width"} > $width) 471 735 1206
Condition100%3/3
not @rows or $used + $gap + $item->{"width"} > $width
l!l&&r!l&&!r
349122735
MC/DC100%2/2
not @rows or $used + $gap + $item->{"width"} > $width
not @rows $used + $gap + $item->{"width"} > $width
Truth table100%3/3
not(A) or B
A= not @rowsB= $used + $gap + $item->{"width"} > $width
ABresult
1X1
011
000
849 471 push @rows, [];
850 471 $used = -$gap;
851 }
852 1206 $item->{x} = $used + $gap;
853 1206 $used = $item->{x} + $item->{width};
854 1206 push @{ $rows[-1] }, $item;
855 }
856 359 return @rows;
857 }
858  
859 349 ▶sub _legend {
Subroutine _legend: called CC=3 SCAR=11.0
860 349 my ( $self, $rows, $ox, $top ) = @_;
861 349 my $swatch = $self->_legend_swatch;
862 349 my @items;
863 349 for my $row ( 0 .. $#$rows ) {
864 471 my $y = $top + $row * $self->_legend_row + $swatch;
865 471 for my $item ( @{ $rows->[$row] } ) {
866 1206 my $x = $ox + $item->{x} + $swatch;
867 push @items, $self->_el( 'g', [
868 class => 'legend-item phase-'.$item->{phase},
869 'data-phase' => $item->{phase},
870 'data-count' => $item->{count}
871 ], $self->_hexagon( $x, $y, $swatch )
872 .$self->_el( 'text', [
873 x => $self->_n( $x + $swatch * 1.6 ),
874 y => $self->_n( $y + $self->_legend_font * 0.35 )
875 ], $self->_text( $item->{phase} ).' '
876 1206 .$self->_el( 'tspan', [ class => 'count' ], $self->_text( $item->{count} ) ) ) );
877 }
878 }
879 349 return $self->_el( 'g', [ class => 'legend' ], join( '', @items ) );
880 }
881  
882 99 1;
883  
884 __END__
« blib/lib/Kubernetes/Comb/SVG/Layout.pm ↑ Summary blib/lib/Kubernetes/Comb/SVG/Cell.pm »