Kit/Screen.pm

filter: Statementstmt 97.1% 137 / 141 Branchbran 94.1% 81 / 86 Conditioncond 90.7% 59 / 65 MC/DCmcdc 83.3% 30 / 36 Subroutinesub 100% 29 / 29 Podpod 95.6% 22 / 23 totaltotal 94.2% 358 / 380 CC 69 SCAR 18.2
CC
69
Cov
95%
measured
15.5
BUILD
11.0
too_small
8.0
CRAP 6.2
« Kit/Line.pm ↑ Summary Kit/Zones.pm »
1 package Game::Terminal::Kit::Screen;
2  
3 14 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 our $VERSION = '0.06';
10  
11 has out => (
12 is => 'rw'
13 14 );
14  
15 has [qw/interactive colour depth columns rows mouse_reporting/] => (
16 is => 'rw'
17 14 );
18  
19 has [qw/ascii cursor_hidden/] => (
20 is => 'rw',
21 isa => Bool,
22 default => 0
23 14 );
24  
25 82 ▶sub BUILD {
Subroutine BUILD: called CC=11 SCAR=24.0
Pod undocumented
26 82 my ($self) = @_;
27 82 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 3 79 82
28 82 ▶ $self->interactive(-t $self->out ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 16 66 82
-t $self->out ? : 0 16 16
29 82 ▶ $self->colour($self->interactive && !no_colour() ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 17 65 82
$self->interactive && !no_colour() ? : 3 14 17
Condition100%3/3
$self->interactive and not no_colour()
!ll&&!rl&&r
1133
MC/DC100%2/2
$self->interactive and not no_colour()
$self->interactive not no_colour()
Truth table100%3/3
A and not(B)
A= $self->interactiveB= not no_colour()
ABresult
0X0
100
111
30 unless defined $self->colour;
31 82 ▶ $self->depth(!$self->colour ? 0 : ($ENV{COLORTERM} // '') =~ m/truecolor|24bit/i ? 24 : 8)
Branch: 3 branches
BranchTrueFalseTotal
unless defined $self->depth 71 11 82
!$self->colour ? : 58 13 71
($ENV{'COLORTERM'} // '') =~ /truecolor|24bit/iu ? : 2 11 13
Condition100%2/2
$ENV{"COLORTERM"} // ""
l!l
211
MC/DC100%1/1
$ENV{"COLORTERM"} // ""
$ENV{"COLORTERM"}
Truth table100%2/2
A
A= $ENV{"COLORTERM"}
Aresult
11
00
32 unless defined $self->depth;
33 82 return $self;
34 }
35  
36 6 ▶sub no_colour {
Subroutine no_colour: called CC=3 SCAR=11.0
Pod documented
37 6 ▶ return defined $ENV{NO_COLOR} && length $ENV{NO_COLOR} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'NO_COLOR'} && length $ENV{'NO_COLOR'} ? : 3 3 6
Condition100%3/3
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
!ll&&!rl&&r
213
MC/DC100%2/2
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
defined $ENV{"NO_COLOR"} length $ENV{"NO_COLOR"}
Truth table100%3/3
A and B
A= defined $ENV{"NO_COLOR"}B= length $ENV{"NO_COLOR"}
ABresult
0X0
100
111
38 }
39  
40 162 ▶sub _print {
Subroutine _print: called CC=1 SCAR=0.0
41 162 my ($self, @text) = @_;
42 162 my $out = $self->out;
43 162 print {$out} @text;
44 162 return $self;
45 }
46  
47 1 ▶sub flush_on {
Subroutine flush_on: called CC=1 SCAR=0.0
Pod documented
48 1 my ($self) = @_;
49 1 my $previous = select $self->out;
50 1 $| = 1;
51 1 select $previous;
52 1 return $self;
53 }
54  
55 3 ▶sub utf8 {
Subroutine utf8: called CC=4 SCAR=13.9
Pod documented
56 3 my ($self) = @_;
57 3 ▶ return $self if $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ascii 1 2 3
58 2 my @layers = eval { PerlIO::get_layers($self->out) };
59 2 ▶ return $self if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
60 2 ▶ return $self if grep { m/\A(?:utf8|encoding)/ } @layers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\A(?:utf8|encoding)/u;} @layers 1 1 2
61 1 binmode $self->out, ':encoding(UTF-8)';
62 1 return $self;
63 }
64  
65 9 ▶sub say {
Subroutine say: called CC=3 SCAR=11.0
Pod documented
66 9 my ($self, @lines) = @_;
67 9 ▶ @lines = ('') unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 1 8 9
68 9 ▶ return $self->_print(map { (defined $_ ? $_ : '') . "\n" } @lines);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 22 1 23
69 }
70  
71 2 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
72 2 my ($self, @text) = @_;
73 2 return $self->_print(@text);
74 }
75  
76 5 ▶sub draw {
Subroutine draw: called CC=3 SCAR=11.0
Pod documented
77 5 my ($self, @text) = @_;
78 5 my $text = join '', grep { defined } @text;
79 5 ▶ return $self unless $self->interactive && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and length $text 4 1 5
Condition100%3/3
$self->interactive and length $text
!ll&&!rl&&r
221
MC/DC100%2/2
$self->interactive and length $text
$self->interactive length $text
Truth table100%3/3
A and B
A= $self->interactiveB= length $text
ABresult
0X0
100
111
80 1 return $self->_print("\e[?2026h", $text, "\e[?2026l");
81 }
82  
83 5 ▶sub clear {
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
84 5 my ($self, %option) = @_;
85 5 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 3 2 5
86 2 ▶ return $self->_print($option{scrollback} ? "\e[2J\e[3J\e[H" : "\e[2J\e[H");
Branch: 1 branch
BranchTrueFalseTotal
$option{'scrollback'} ? : 1 1 2
87 }
88  
89 125 ▶sub show {
Subroutine show: called CC=2 SCAR=6.9
Pod documented
90 125 my ($self, $lines) = @_;
91 125 ▶ return $self->_print("\e[H", (map { "$_\e[K\n" } @{$lines}), "\e[J")
Branch: 1 branch
BranchTrueFalseTotal
if $self->interactive 5 120 125
92 if $self->interactive;
93 120 return $self->_print(map { "$_\n" } @{$lines});
94 }
95  
96 6 ▶sub rewind {
Subroutine rewind: called CC=4 SCAR=13.9
Pod documented
97 6 my ($self, $count) = @_;
98 6 ▶ return $self unless $self->interactive && $count && $count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and $count and $count > 0 5 1 6
Condition100%6/6
$self->interactive and $count and $count > 0
!ll&&!rl&&r
411
$self->interactive and $count
!ll&&!rl&&r
222
MC/DC100%3/3
$self->interactive and $count and $count > 0
$self->interactive $count $count > 0
Truth table100%4/4
A and B and C
A= $self->interactiveB= $countC= $count > 0
ABCresult
0XX0
10X0
1100
1111
99 1 return $self->_print("\e[${count}A\r");
100 }
101  
102 3 ▶sub at {
Subroutine at: called CC=2 SCAR=6.9
Pod documented
103 3 my ($self, $row, $column) = @_;
104 3 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 2 1 3
105 1 return $self->_print("\e[${row};${column}H");
106 }
107  
108 10 ▶sub paint {
Subroutine paint: called CC=4 SCAR=13.9
Pod documented
109 10 my ($self, $text, $code) = @_;
110 10 ▶ return $text unless $self->colour && defined $code && length $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour and defined $code and length $code 6 4 10
Condition100%6/6
$self->colour and defined $code and length $code
!ll&&!rl&&r
514
$self->colour and defined $code
!ll&&!rl&&r
415
MC/DC100%3/3
$self->colour and defined $code and length $code
$self->colour defined $code length $code
Truth table100%4/4
A and B and C
A= $self->colourB= defined $codeC= length $code
ABCresult
0XX0
10X0
1100
1111
111 4 return "\e[${code}m" . $text . "\e[0m";
112 }
113  
114 15 ▶sub palette {
Subroutine palette: called CC=8 SCAR=20.8
Pod documented
115 15 my ($colour) = @_;
116 15 my @part = ($colour >> 16 & 255, $colour >> 8 & 255, $colour & 255);
117 15 ▶ my @step = map { $_ < 48 ? 0 : $_ < 115 ? 1 : int(($_ - 35) / 40) } @part;
Branch: 2 branches
BranchTrueFalseTotal
$_ < 48 ? : 19 26 45
$_ < 115 ? : 3 23 26
118 15 ▶ my @cube = map { $_ ? 55 + 40 * $_ : 0 } @step;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 26 19 45
119 15 my $mean = int(($part[0] + $part[1] + $part[2]) / 3);
120 15 ▶ my $ramp = $mean < 8 ? 0 : $mean > 238 ? 23 : int(($mean - 8) / 10 + 0.5);
Branch: 2 branches
BranchTrueFalseTotal
$mean < 8 ? : 2 13 15
$mean > 238 ? : 2 11 13
121 15 my $grey = 8 + 10 * $ramp;
122 15 my ($to_cube, $to_grey) = (0, 0);
123 15 for my $index (0 .. 2) {
124 45 $to_cube += ($part[$index] - $cube[$index]) ** 2;
125 45 $to_grey += ($part[$index] - $grey) ** 2;
126 }
127 15 ▶ return 232 + $ramp if $to_grey < $to_cube;
Branch: 1 branch
BranchTrueFalseTotal
if $to_grey < $to_cube 5 10 15
128 10 return 16 + 36 * $step[0] + 6 * $step[1] + $step[2];
129 }
130  
131 11 ▶sub _colour_code {
Subroutine _colour_code: called CC=5 SCAR=16.1
132 11 my ($self, $lead, $colour) = @_;
133 11 ▶ return '' unless defined $colour && $self->colour && $self->depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colour and $self->colour and $self->depth 6 5 11
Condition100%6/6
defined $colour and $self->colour and $self->depth
!ll&&!rl&&r
425
defined $colour and $self->colour
!ll&&!rl&&r
137
MC/DC100%3/3
defined $colour and $self->colour and $self->depth
defined $colour $self->colour $self->depth
Truth table100%4/4
A and B and C
A= defined $colourB= $self->colourC= $self->depth
ABCresult
0XX0
10X0
1100
1111
134 5 ▶ return join ';', $lead, 2, $colour >> 16 & 255, $colour >> 8 & 255, $colour & 255
Branch: 1 branch
BranchTrueFalseTotal
if $self->depth >= 24 3 2 5
135 if $self->depth >= 24;
136 2 return "$lead;5;" . palette($colour);
137 }
138  
139 7 ▶sub ink {
Subroutine ink: called CC=1 SCAR=0.0
Pod documented
140 7 my ($self, $colour) = @_;
141 7 return $self->_colour_code(38, $colour);
142 }
143  
144 4 ▶sub ground {
Subroutine ground: called CC=1 SCAR=0.0
Pod documented
145 4 my ($self, $colour) = @_;
146 4 return $self->_colour_code(48, $colour);
147 }
148  
149 14 ▶sub hide_cursor {
Subroutine hide_cursor: called CC=3 SCAR=11.0
Pod documented
150 14 my ($self) = @_;
151 14 ▶ return $self unless $self->interactive && !$self->cursor_hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and not $self->cursor_hidden 8 6 14
Condition100%3/3
$self->interactive and not $self->cursor_hidden
!ll&&!rl&&r
716
MC/DC100%2/2
$self->interactive and not $self->cursor_hidden
$self->interactive not $self->cursor_hidden
Truth table100%3/3
A and not(B)
A= $self->interactiveB= not $self->cursor_hidden
ABresult
0X0
100
111
152 6 $self->cursor_hidden(1);
153 6 return $self->_print("\e[?25l");
154 }
155  
156 11 ▶sub show_cursor {
Subroutine show_cursor: called CC=2 SCAR=6.9
Pod documented
157 11 my ($self) = @_;
158 11 ▶ return $self unless $self->cursor_hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cursor_hidden 5 6 11
159 6 $self->cursor_hidden(0);
160 6 return $self->_print("\e[?25h");
161 }
162  
163 6 ▶sub mouse_on {
Subroutine mouse_on: called CC=4 SCAR=13.9
Pod documented
164 6 my ($self, %option) = @_;
165 6 ▶ my $motion = $option{motion} // 'drag';
Condition100%2/2
$option{"motion"} // "drag"
l!l
33
MC/DC100%1/1
$option{"motion"} // "drag"
$option{"motion"}
Truth table100%2/2
A
A= $option{"motion"}
Aresult
11
00
166 6 ▶ my $mode = { click => 1000, drag => 1002, all => 1003 }->{$motion}
Branch: 1 branch
BranchTrueFalseTotal
my $mode = {"click", 1000, "drag", 1002, "all", 1003}->{$motion} or die "mouse_on: motion is click, drag or all, not $motion\n" 5 1 6
167 or die "mouse_on: motion is click, drag or all, not $motion\n";
168 5 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 1 4 5
169 4 $self->mouse_off;
170 4 $self->mouse_reporting($mode);
171 4 return $self->_print("\e[?${mode}h\e[?1006h");
172 }
173  
174 24 ▶sub mouse_off {
Subroutine mouse_off: called CC=2 SCAR=6.9
Pod documented
175 24 my ($self) = @_;
176 24 ▶ my $mode = $self->mouse_reporting or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $mode = $self->mouse_reporting or return $self 4 20 24
177 4 $self->mouse_reporting(0);
178 4 return $self->_print("\e[?1006l\e[?${mode}l");
179 }
180  
181 16 ▶sub restore {
Subroutine restore: called CC=3 SCAR=11.0
Pod documented
182 16 my ($self) = @_;
183 16 $self->mouse_off;
184 16 ▶ return $self unless $self->cursor_hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cursor_hidden 11 5 16
185 5 $self->show_cursor;
186 5 ▶ $self->_print("\e[0m") if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 1 4 5
187 5 return $self;
188 }
189  
190 18 ▶sub measured {
Subroutine measured: called CC=11 SCAR=27.4
Pod documented
191 18 my ($self) = @_;
192 18 ▶ return ($self->columns, $self->rows || 0) if $self->columns;
Branch: 1 branch
BranchTrueFalseTotal
if $self->columns 7 11 18
Condition100%2/2
$self->rows || 0
l!l
61
MC/DC100%1/1
$self->rows || 0
$self->rows
Truth table100%2/2
A
A= $self->rows
Aresult
11
00
193 0 ▶ if ($self->interactive && eval { require Term::ReadKey; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive and eval { do { require Term::ReadKey; 1 } }) 0 11 11
Condition66%2/3
$self->interactive and eval { do { require Term::ReadKey; 1 } }
!ll&&!rl&&r
920
MC/DC0%0/2
$self->interactive and eval { do { require Term::ReadKey; 1 } }
$self->interactive eval { do { require Term::ReadKey; 1 } }
Truth table66%2/3
A and B
A= $self->interactiveB= eval { do { require Term::ReadKey; 1 } }
ABresult
0X0
100
111
194 0 my ($columns, $rows) = eval { Term::ReadKey::GetTerminalSize($self->out) };
195 0 ▶ return ($columns, $rows || 0) if $columns;
Branch: 1 branch
BranchTrueFalseTotal
if $columns 0 0 0
Condition0%0/2
$rows || 0
l!l
00
MC/DC0%0/1
$rows || 0
$rows
Truth table0%0/2
A
A= $rows
Aresult
11
00
196 }
197 return ($ENV{COLUMNS} + 0, ($ENV{LINES} || 0) + 0)
198 11 ▶ if defined $ENV{COLUMNS} && $ENV{COLUMNS} =~ m/^\d+$/ && $ENV{COLUMNS} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u and $ENV{"COLUMNS"} > 0 3 8 11
Condition87%7/8
defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u and $ENV{"COLUMNS"} > 0
!ll&&!rl&&r
803
defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u
!ll&&!rl&&r
713
$ENV{'LINES'} || 0
l!l
12
MC/DC66%2/3
defined $ENV{"COLUMNS"} and $ENV{"COLUMNS"} =~ /^\d+$/u and $ENV{"COLUMNS"} > 0
defined $ENV{"COLUMNS"} $ENV{"COLUMNS"} =~ /^\d+$/u $ENV{"COLUMNS"} > 0
MC/DC100%1/1
$ENV{'LINES'} || 0
$ENV{'LINES'}
Truth table75%3/4
A and B and C
A= defined $ENV{"COLUMNS"}B= $ENV{"COLUMNS"} =~ /^\d+$/uC= $ENV{"COLUMNS"} > 0
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $ENV{'LINES'}
Aresult
11
00
199 8 return ();
200 }
201  
202 8 ▶sub size {
Subroutine size: called CC=3 SCAR=11.0
Pod documented
203 8 my ($self) = @_;
204 8 my ($columns, $rows) = $self->measured;
205 8 ▶ return ($columns || 80, $rows || 24);
Condition100%4/4
$columns || 80
l!l
44
$rows || 24
l!l
26
MC/DC100%1/1
$columns || 80
$columns
MC/DC100%1/1
$rows || 24
$rows
Truth table100%2/2
A
A= $columns
Aresult
11
00
Truth table100%2/2
A
A= $rows
Aresult
11
00
206 }
207  
208 7 ▶sub too_small {
Subroutine too_small: called CC=8 SCAR=20.8
Pod documented
209 7 my ($self, $need_columns, $need_rows) = @_;
210 7 my ($columns, $rows) = $self->measured;
211 7 ▶ return undef unless $columns;
Branch: 1 branch
BranchTrueFalseTotal
unless $columns 2 5 7
212 5 my $narrow = $columns < $need_columns;
213 5 ▶ my $short = $need_rows && $rows && $rows < $need_rows;
Condition83%5/6
$need_rows && $rows && $rows < $need_rows
!ll&&!rl&&r
221
$need_rows and $rows
!ll&&!rl&&r
203
MC/DC66%2/3
$need_rows && $rows && $rows < $need_rows
$need_rows $rows $rows < $need_rows
Truth table75%3/4
A and B && C
A= $need_rowsB= $rowsC= $rows < $need_rows
ABCresult
0XX0
10X0
1100
1111
214 5 ▶ return undef unless $narrow || $short;
Branch: 1 branch
BranchTrueFalseTotal
unless $narrow or $short 2 3 5
Condition100%3/3
$narrow or $short
l!l&&r!l&&!r
212
MC/DC100%2/2
$narrow or $short
$narrow $short
Truth table100%3/3
A or B
A= $narrowB= $short
ABresult
1X1
011
000
215 3 ▶ return "This window is $columns columns by $rows lines and $need_columns by $need_rows are needed."
Branch: 1 branch
BranchTrueFalseTotal
if $need_rows and $rows 2 1 3
Condition66%2/3
$need_rows and $rows
!ll&&!rl&&r
102
MC/DC50%1/2
$need_rows and $rows
$need_rows $rows
Truth table66%2/3
A and B
A= $need_rowsB= $rows
ABresult
0X0
100
111
216 if $need_rows && $rows;
217 1 return "This window is $columns columns wide and $need_columns are needed.";
218 }
219  
220 14 1;
221  
222 __END__
« Kit/Line.pm ↑ Summary Kit/Zones.pm »