Kit/Canvas.pm

filter: Statementstmt 100% 85 / 85 Branchbran 100% 28 / 28 Conditioncond 91.6% 44 / 48 MC/DCmcdc 84.6% 22 / 26 Subroutinesub 100% 13 / 13 Podpod 85.7% 6 / 7 totaltotal 95.6% 198 / 207 CC 36 SCAR 21.6
CC
36
Cov
98%
lines
16.0
_set
9.0
put
6.0
CRAP 8.7
« Kit/Steer.pm ↑ Summary Kit/Line.pm »
1 package Game::Terminal::Kit::Canvas;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '0.02';
10  
11 has [qw/width height/] => (
12 is => 'rw',
13 isa => Int,
14 default => 0
15 4 );
16  
17 has cells => (
18 is => 'rw',
19 isa => ArrayRef
20 4 );
21  
22 49 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
23 49 my ($self) = @_;
24 49 $self->wipe;
25 49 return $self;
26 }
27  
28 50 ▶sub wipe {
Subroutine wipe: called CC=1 SCAR=0.0
Pod documented
29 50 my ($self) = @_;
30 50 my ($width, $height) = ($self->width, $self->height);
31 50 $self->cells([ map { [ map { [' '] } 1 .. $width ] } 1 .. $height ]);
32 50 return $self;
33 }
34  
35 307 ▶sub _style {
Subroutine _style: called CC=2 SCAR=6.9
36 307 my (%style) = @_;
37 307 ▶ return join ';', grep { defined $_ && length $_ } $style{ink}, $style{ground};
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
595118
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
38 }
39  
40 883 ▶sub _set {
Subroutine _set: called CC=9 SCAR=22.0
41 883 my ($self, $x, $y, $glyph, $style, $half) = @_;
42 883 ▶ return if $y < 0 || $y >= $self->height || $x < 0 || $x >= $self->width;
Branch: 1 branch
BranchTrueFalseTotal
if $y < 0 or $y >= $self->height or $x < 0 or $x >= $self->width 91 792 883
Condition100%9/9
$y < 0 or $y >= $self->height or $x < 0 or $x >= $self->width
l!l&&r!l&&!r
7219792
$y < 0 or $y >= $self->height or $x < 0
l!l&&r!l&&!r
675811
$y < 0 or $y >= $self->height
l!l&&r!l&&!r
265816
MC/DC100%4/4
$y < 0 or $y >= $self->height or $x < 0 or $x >= $self->width
$y < 0 $y >= $self->height $x < 0 $x >= $self->width
Truth table100%5/5
A or B or C or D
A= $y < 0B= $y >= $self->heightC= $x < 0D= $x >= $self->width
ABCDresult
1XXX1
01XX1
001X1
00011
00000
43 792 my $row = $self->cells->[$y];
44 792 ▶ if ($row->[$x][2] && $x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[$x][2] and $x > 0) 2 790 792
Condition66%2/3
$row->[$x][2] and $x > 0
!ll&&!rl&&r
79002
MC/DC50%1/2
$row->[$x][2] and $x > 0
$row->[$x][2] $x > 0
Truth table66%2/3
A and B
A= $row->[$x][2]B= $x > 0
ABresult
0X0
100
111
45 2 $row->[ $x - 1 ] = [ ' ', $row->[ $x - 1 ][1] ];
46 }
47 792 ▶ if ($x + 1 < $self->width && $row->[ $x + 1 ][2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x + 1 < $self->width and $$row[$x + 1][2]) 2 790 792
Condition100%3/3
$x + 1 < $self->width and $$row[$x + 1][2]
!ll&&!rl&&r
697212
MC/DC100%2/2
$x + 1 < $self->width and $$row[$x + 1][2]
$x + 1 < $self->width $$row[$x + 1][2]
Truth table100%3/3
A and B
A= $x + 1 < $self->widthB= $$row[$x + 1][2]
ABresult
0X0
100
111
48 2 $row->[ $x + 1 ] = [ ' ', $row->[ $x + 1 ][1] ];
49 }
50 792 $row->[$x] = [ $glyph, $style, $half ];
51 792 return;
52 }
53  
54 308 ▶sub put {
Subroutine put: called CC=6 SCAR=17.9
Pod documented
55 308 my ($self, $x, $y, $text, %style) = @_;
56 308 ▶ return $self unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 307 308
57 307 my $style = _style(%style);
58 307 for my $glyph (split //, $text) {
59 872 ▶ if ($style{wide}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style{"wide"}) 11 861 872
60 11 ▶ if ($x >= 0 && $x + 1 < $self->width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= 0 and $x + 1 < $self->width) { } 9 2 11
Condition100%3/3
$x >= 0 and $x + 1 < $self->width
!ll&&!rl&&r
119
MC/DC100%2/2
$x >= 0 and $x + 1 < $self->width
$x >= 0 $x + 1 < $self->width
Truth table100%3/3
A and B
A= $x >= 0B= $x + 1 < $self->width
ABresult
0X0
100
111
61 9 $self->_set($x, $y, $glyph, $style);
62 9 $self->_set($x + 1, $y, '', $style, 1);
63 } else {
64 2 $self->_set($x, $y, ' ', $style);
65 2 $self->_set($x + 1, $y, ' ', $style);
66 }
67 11 $x += 2;
68 11 next;
69 }
70 861 $self->_set($x, $y, $glyph, $style);
71 861 $x++;
72 }
73 307 return $self;
74 }
75  
76 6 ▶sub fill {
Subroutine fill: called CC=4 SCAR=13.9
Pod documented
77 6 my ($self, $x, $y, $width, $height, $glyph, %style) = @_;
78 6 ▶ $glyph = ' ' unless defined $glyph && length $glyph;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glyph and length $glyph 1 5 6
Condition66%2/3
defined $glyph and length $glyph
!ll&&!rl&&r
105
MC/DC50%1/2
defined $glyph and length $glyph
defined $glyph length $glyph
Truth table66%2/3
A and B
A= defined $glyphB= length $glyph
ABresult
0X0
100
111
79 6 for my $row ($y .. $y + $height - 1) {
80 17 $self->put($x, $row, $glyph x $width, %style);
81 }
82 6 return $self;
83 }
84  
85 54 ▶sub box {
Subroutine box: called CC=4 SCAR=13.9
Pod documented
86 54 my ($self, $x, $y, $width, $height, $glyphs, %style) = @_;
87 54 ▶ return $self if $width < 2 || $height < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $width < 2 or $height < 2 2 52 54
Condition100%3/3
$width < 2 or $height < 2
l!l&&r!l&&!r
1152
MC/DC100%2/2
$width < 2 or $height < 2
$width < 2 $height < 2
Truth table100%3/3
A or B
A= $width < 2B= $height < 2
ABresult
1X1
011
000
88 52 my $inner = $width - 2;
89 52 $self->put($x, $y, $glyphs->{tl} . ($glyphs->{t} x $inner) . $glyphs->{tr}, %style);
90 52 for my $row ($y + 1 .. $y + $height - 2) {
91 52 $self->put($x, $row, $glyphs->{l}, %style);
92 52 $self->put($x + $width - 1, $row, $glyphs->{r}, %style);
93 }
94 52 $self->put($x, $y + $height - 1, $glyphs->{bl} . ($glyphs->{b} x $inner) . $glyphs->{br}, %style);
95 52 return $self;
96 }
97  
98 58 ▶sub lines {
Subroutine lines: called CC=16 SCAR=27.7
Pod documented
99 58 my ($self, $screen) = @_;
100 58 ▶ my $colour = $screen && $screen->colour;
Condition100%3/3
$screen && $screen->colour
!ll&&!rl&&r
4927
MC/DC100%2/2
$screen && $screen->colour
$screen $screen->colour
Truth table100%3/3
A && B
A= $screenB= $screen->colour
ABresult
0X0
100
111
101 58 my @lines;
102 58 for my $row (@{ $self->cells }) {
103 133 my @cells = @{$row};
104 133 ▶ pop @cells while @cells
Branch: 1 branch
BranchTrueFalseTotal
@cells and $cells[-1][0] eq " " || $cells[-1][0] eq "" and not $colour && defined $cells[-1][1] && length $cells[-1][1] and do { pop @cells } 223 133 356
Condition93%14/15
@cells and $cells[-1][0] eq " " || $cells[-1][0] eq "" and not $colour && defined $cells[-1][1] && length $cells[-1][1]
!ll&&!rl&&r
1321223
@cells and $cells[-1][0] eq " " || $cells[-1][0] eq ""
!ll&&!rl&&r
3399224
$cells[-1][0] eq " " or $cells[-1][0] eq ""
l!l&&r!l&&!r
222299
$colour && defined $cells[-1][1] && length $cells[-1][1]
!ll&&!rl&&r
22301
$colour and defined $cells[-1][1]
!ll&&!rl&&r
205181
MC/DC83%5/6
@cells and $cells[-1][0] eq " " || $cells[-1][0] eq "" and not $colour && defined $cells[-1][1] && length $cells[-1][1]
@cells $cells[-1][0] eq " " $cells[-1][0] eq "" $colour defined $cells[-1][1] length $cells[-1][1]
Truth table60%6/10
A and B or C and not(D and E && F)
A= @cellsB= $cells[-1][0] eq " "C= $cells[-1][0] eq ""D= $colourE= defined $cells[-1][1]F= length $cells[-1][1]
ABCDEFresult
0XXXXX0
100XXX0
11X1110
1011110
11X0XX1
11X10X1
11X1101
1010XX1
10110X1
1011101
105 && ($cells[-1][0] eq ' ' || $cells[-1][0] eq '')
106 && !($colour && defined $cells[-1][1] && length $cells[-1][1]);
107 133 my ($line, $held) = ('', '');
108 133 for my $cell (@cells) {
109 1066 ▶ my $want = $colour && defined $cell->[1] ? $cell->[1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$colour && defined $cell->[1] ? : 66 1000 1066
Condition66%2/3
$colour and defined $cell->[1]
!ll&&!rl&&r
1000066
MC/DC50%1/2
$colour and defined $cell->[1]
$colour defined $cell->[1]
Truth table66%2/3
A and B
A= $colourB= defined $cell->[1]
ABresult
0X0
100
111
110 1066 ▶ if ($want ne $held) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want ne $held) 19 1047 1066
111 19 ▶ $line .= length $want ? (length $held ? "\e[0;${want}m" : "\e[${want}m") : "\e[0m";
Branch: 2 branches
BranchTrueFalseTotal
length $want ? : 18 1 19
length $held ? : 10 8 18
112 19 $held = $want;
113 }
114 1066 $line .= $cell->[0];
115 }
116 133 ▶ $line .= "\e[0m" if length $held;
Branch: 1 branch
BranchTrueFalseTotal
if length $held 7 126 133
117 133 push @lines, $line;
118 }
119 58 return @lines;
120 }
121  
122 48 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
123 48 my ($self) = @_;
124 48 return $self->lines;
125 }
126  
127 4 1;
128  
129 __END__
« Kit/Steer.pm ↑ Summary Kit/Line.pm »