blib/lib/Game/Terminal/Menu.pm

filter: Statementstmt 92.7% 281 / 303 Branchbran 74.0% 120 / 162 Conditioncond 52.7% 38 / 72 MC/DCmcdc 22.7% 10 / 44 Subroutinesub 97.5% 40 / 41 Podpod 96.5% 28 / 29 totaltotal 79.4% 517 / 651 CC 118 SCAR 26.0
CC
118
Cov
82%
choose
37.7
BUILD
28.1
prompt
15.1
CRAP 13.4
« bin/game ↑ Summary blib/lib/Game/Terminal.pm »
1 package Game::Terminal::Menu;
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 4 ▶use Game::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Game::Terminal::Kit::Form;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Game::Terminal::Kit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Text::ParseWords ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our $VERSION = '0.04';
17  
18 4 my %WORD = (0 => 'won or drew', 1 => 'lost', 2 => 'bad option');
19  
20 4 my $LEGEND = 'up and down to choose, enter to set up and play, a letter to jump, q to quit';
21 4 my $MORE = 'k and j move too, space plays, home and end jump to the ends';
22  
23 4 my %INK = (
24 title => '1',
25 count => '2',
26 key => '1',
27 board => '38;5;75',
28 card => '38;5;203',
29 tile => '38;5;220',
30 puzzle => '38;5;177',
31 arcade => '38;5;80',
32 detail => '38;5;245',
33 legend => '2',
34 set => '38;5;114',
35 own => '2',
36 play => '1;32',
37 good => '38;5;114',
38 bad => '38;5;203',
39 warn => '38;5;220',
40 );
41  
42 has [qw/in out/] => (
43 is => 'rw'
44 4 );
45  
46 has [qw/interactive colour picking/] => (
47 is => 'rw'
48 4 );
49  
50 has [qw/keyboard screen/] => (
51 is => 'rw'
52 4 );
53  
54 has keysource => (
55 is => 'rw',
56 isa => CodeRef
57 4 );
58  
59 has pending => (
60 is => 'rw',
61 isa => ArrayRef,
62 default => []
63 4 );
64  
65 has runner => (
66 is => 'rw',
67 isa => CodeRef
68 4 );
69  
70 has common => (
71 is => 'rw',
72 isa => HashRef,
73 default => {}
74 4 );
75  
76 has inc => (
77 is => 'rw',
78 isa => ArrayRef
79 4 );
80  
81 has games => (
82 is => 'rw',
83 isa => ArrayRef
84 4 );
85  
86 has at => (
87 is => 'rw',
88 isa => Int,
89 default => 0
90 4 );
91  
92 has status => (
93 is => 'rw',
94 isa => Str,
95 default => ''
96 4 );
97  
98 has tone => (
99 is => 'rw',
100 isa => Str,
101 default => ''
102 4 );
103  
104 has rows => (
105 is => 'rw',
106 isa => Int,
107 default => 0
108 4 );
109  
110 has values => (
111 is => 'rw',
112 isa => HashRef,
113 default => {}
114 4 );
115  
116 33 ▶sub BUILD {
Subroutine BUILD: called CC=19 SCAR=33.4
Pod undocumented
117 33 my ($self) = @_;
118 33 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 29 4 33
119 33 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 0 33 33
120 33 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 0 33 33
-t $self->in ? : 0 0 0
121 33 ▶ $self->colour($self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 0 33 33
$self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? : 0 0 0
Condition0%0/3
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
!ll&&!rl&&r
000
MC/DC0%0/2
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
$self->interactive not Game::Terminal::Kit::Screen::no_colour()
Truth table0%0/3
A and not(B)
A= $self->interactiveB= not Game::Terminal::Kit::Screen::no_colour()
ABresult
0X0
100
111
122 unless defined $self->colour;
123 33 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 33 0 33
$self->keysource ? : 31 2 33
124 in => $self->in,
125 pending => $self->pending,
126 ($self->keysource ? (keysource => $self->keysource) : ()),
127 )) unless $self->keyboard;
128 33 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->screen 33 0 33
129 out => $self->out,
130 interactive => $self->interactive,
131 colour => $self->colour,
132 ascii => 1,
133 )) unless $self->screen;
134 33 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 0 33 33
$self->interactive ? : 0 0 0
135 33 ▶ $self->picking(0) unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 2 31 33
136 33 ▶ $self->inc([ @INC ]) unless $self->inc;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->inc 33 0 33
137 33 ▶ $self->runner(\&Game::Terminal::launch) unless $self->runner;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->runner 4 29 33
138 33 ▶ unless ($self->games) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->games) 4 29 33
139 4 my @inc = @{$self->inc};
140 $self->games([
141 4 ▶ map { { %{$_}, installed => Game::Terminal::installed($_, @inc) ? 1 : 0 } }
Branch: 1 branch
BranchTrueFalseTotal
Game::Terminal::installed($_, @inc) ? : 96 0 96
142 Game::Terminal::games()
143 ]);
144 }
145 33 $self->at($self->first_installed);
146 33 ▶ $self->rows($self->height) unless $self->rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->rows 32 1 33
147 33 $self->screen->flush_on;
148 33 return $self;
149 }
150  
151 1 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
152 1 my ($self) = @_;
153 1 return $self->keyboard->raw;
154 }
155  
156 34 ▶sub keys_available {
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
157 34 my ($self) = @_;
158 34 return $self->keyboard->keys_available;
159 }
160  
161 47 ▶sub enter_raw {
Subroutine enter_raw: called CC=2 SCAR=7.2
Pod documented
162 47 my ($self) = @_;
163 47 ▶ return $self->keyboard->enter_raw ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->keyboard->enter_raw ? : 47 0 47
164 }
165  
166 27 ▶sub leave_raw {
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
167 27 my ($self) = @_;
168 27 $self->keyboard->leave_raw;
169 27 return $self;
170 }
171  
172 190 ▶sub read_key {
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
173 190 my ($self) = @_;
174 190 return $self->keyboard->read_key;
175 }
176  
177 32 ▶sub height {
Subroutine height: called CC=3 SCAR=16.5
Pod documented
178 32 my ($self) = @_;
179 32 ▶ return 0 unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 32 0 32
180 0 my (undef, $rows) = $self->screen->measured;
181 0 ▶ return $rows || 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
182 }
183  
184 3852 ▶sub say {
Subroutine say: called CC=1 SCAR=0.0
Pod documented
185 3852 my ($self, $line) = @_;
186 3852 $self->screen->say($line);
187 3852 return $self;
188 }
189  
190 196 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
191 196 my ($self) = @_;
192 196 $self->screen->clear;
193 196 return $self;
194 }
195  
196 4483 ▶sub paint {
Subroutine paint: called CC=1 SCAR=0.0
Pod documented
197 4483 my ($self, $text, $code) = @_;
198 4483 return $self->screen->paint($text, $code);
199 }
200  
201 33 ▶sub first_installed {
Subroutine first_installed: called CC=3 SCAR=11.4
Pod documented
202 33 my ($self) = @_;
203 33 my $games = $self->games;
204 33 for my $index (0 .. $#{$games}) {
205 33 ▶ return $index if $games->[$index]{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $games->[$index]{"installed"} 33 0 33
206 }
207 0 return 0;
208 }
209  
210 0 ▶sub last_installed {
Subroutine last_installed: not called CC=3 SCAR=24.8
Pod documented
211 0 my ($self) = @_;
212 0 my $games = $self->games;
213 0 for my $index (reverse 0 .. $#{$games}) {
214 0 ▶ return $index if $games->[$index]{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $games->[$index]{"installed"} 0 0 0
215 }
216 0 return 0;
217 }
218  
219 52 ▶sub note {
Subroutine note: called CC=3 SCAR=11.0
Pod documented
220 52 my ($self, $text, $tone) = @_;
221 52 ▶ $self->status(defined $text ? $text : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 52 0 52
222 52 ▶ $self->tone(defined $tone ? $tone : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $tone ? : 29 23 52
223 52 return $self;
224 }
225  
226 93 ▶sub status_line {
Subroutine status_line: called CC=3 SCAR=11.3
Pod documented
227 93 my ($self) = @_;
228 93 ▶ return () unless length $self->status;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->status 63 30 93
229 30 my $tone = $self->tone;
230 30 ▶ return $self->paint($self->status, $INK{$tone} || $INK{detail});
Condition33%1/3
$INK{$tone} || $INK{'detail'}
l!l&&r!l&&!r
3000
MC/DC0%0/2
$INK{$tone} || $INK{'detail'}
$INK{$tone} $INK{'detail'}
Truth table33%1/3
A || B
A= $INK{$tone}B= $INK{'detail'}
ABresult
1X1
011
000
231 }
232  
233 2028 ▶sub row_line {
Subroutine row_line: called CC=4 SCAR=14.0
Pod documented
234 2028 my ($self, $index) = @_;
235 2028 my $row = $self->games->[$index];
236 my $line = sprintf '%-11s %-6s %-4s %s',
237 2028 $row->{key}, $row->{family}, $row->{players}, $row->{blurb};
238 2028 $line =~ s/\s+$//;
239 2028 ▶ return $self->paint(" $line (not installed)", '2') unless $row->{installed};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"installed"} 50 1978 2028
240 1978 ▶ return ' ' . $self->paint("> $line", '1;7') if $index == $self->at;
Branch: 1 branch
BranchTrueFalseTotal
if $index == $self->at 84 1894 1978
241 return ' ' . $self->paint(sprintf('%-11s', $row->{key}), $INK{key}) . ' '
242 . $self->paint(sprintf('%-6s', $row->{family}), $INK{ $row->{family} } || $INK{detail}) . ' '
243 1894 ▶ . sprintf('%-4s %s', $row->{players}, $row->{blurb});
Condition33%1/3
$INK{$row->{'family'}} || $INK{'detail'}
l!l&&r!l&&!r
189400
MC/DC0%0/2
$INK{$row->{'family'}} || $INK{'detail'}
$INK{$row->{'family'}} $INK{'detail'}
Truth table33%1/3
A || B
A= $INK{$row->{'family'}}B= $INK{'detail'}
ABresult
1X1
011
000
244 }
245  
246 86 ▶sub list_lines {
Subroutine list_lines: called CC=8 SCAR=20.8
Pod documented
247 86 my ($self) = @_;
248 86 my $games = $self->games;
249 86 my $count = scalar @{$games};
250 86 ▶ my $room = $self->rows ? $self->rows - 7 : $count;
Branch: 1 branch
BranchTrueFalseTotal
$self->rows ? : 2 84 86
251 86 my ($first, $last) = (0, $count - 1);
252 86 ▶ if ($room < $count && $room >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($room < $count and $room >= 5) 2 84 86
Condition66%2/3
$room < $count and $room >= 5
!ll&&!rl&&r
8402
MC/DC50%1/2
$room < $count and $room >= 5
$room < $count $room >= 5
Truth table66%2/3
A and B
A= $room < $countB= $room >= 5
ABresult
0X0
100
111
253 2 my $window = $room - 2;
254 2 $first = $self->at - int($window / 2);
255 2 ▶ $first = 0 if $first < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $first < 0 1 1 2
256 2 ▶ $first = $count - $window if $first > $count - $window;
Branch: 1 branch
BranchTrueFalseTotal
if $first > $count - $window 1 1 2
257 2 $last = $first + $window - 1;
258 }
259 86 my @lines;
260 86 ▶ push @lines, sprintf ' %d further up', $first if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 1 85 86
261 86 push @lines, map { $self->row_line($_) } $first .. $last;
262 86 ▶ push @lines, sprintf ' %d further down', $count - 1 - $last if $last < $count - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $last < $count - 1 1 85 86
263 86 return \@lines;
264 }
265  
266 84 ▶sub detail_lines {
Subroutine detail_lines: called CC=3 SCAR=11.0
Pod documented
267 84 my ($self) = @_;
268 84 my $row = $self->games->[$self->at];
269 84 my @lines;
270 84 ▶ if ($row->{installed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'installed'}) { } 82 2 84
271 82 my $version = Game::Terminal::version_of($row, @{$self->inc});
272 82 ▶ push @lines, "$row->{key}: $row->{class}" . (defined $version ? " $version" : '')
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 82 0 82
273 . ", options: game $row->{key} --help";
274 } else {
275 2 push @lines, "$row->{key} is not installed: cpanm $row->{module}";
276 }
277 84 $lines[0] = $self->paint($lines[0], $INK{detail});
278 84 push @lines, $self->status_line;
279 84 return \@lines;
280 }
281  
282 84 ▶sub draw {
Subroutine draw: called CC=3 SCAR=11.0
Pod documented
283 84 my ($self) = @_;
284 84 $self->clear;
285 84 my $count = sprintf '%d games', scalar @{$self->games};
286 84 $self->say($self->paint('Game', $INK{title}) . $self->paint(sprintf('%*s', 72, $count), $INK{count}));
287 84 $self->say('');
288 84 $self->say($_) for @{$self->list_lines};
289 84 $self->say('');
290 84 $self->say($_) for @{$self->detail_lines};
291 84 $self->say('');
292 84 $self->say($self->paint($LEGEND, $INK{legend}));
293 84 return $self;
294 }
295  
296 25 ▶sub move {
Subroutine move: called CC=3 SCAR=11.0
Pod documented
297 25 my ($self, $step) = @_;
298 25 my $games = $self->games;
299 25 my $at = $self->at;
300 25 for (1 .. @{$games}) {
301 28 $at = ($at + $step) % @{$games};
302 28 ▶ last if $games->[$at]{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $games->[$at]{"installed"} 25 3 28
303 }
304 25 $self->at($at);
305 25 return $self;
306 }
307  
308 18 ▶sub jump {
Subroutine jump: called CC=4 SCAR=14.1
Pod documented
309 18 my ($self, $letter) = @_;
310 18 my $games = $self->games;
311 18 my $at = $self->at;
312 18 for (1 .. @{$games}) {
313 88 $at = ($at + 1) % @{$games};
314 88 ▶ next unless $games->[$at]{installed};
Branch: 1 branch
BranchTrueFalseTotal
unless $games->[$at]{"installed"} 0 88 88
315 88 ▶ next unless index($games->[$at]{key}, $letter) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($games->[$at]{"key"}, $letter) == 0 70 18 88
316 18 $self->at($at);
317 18 return $self;
318 }
319 0 $self->note("No game starts with $letter");
320 0 return $self;
321 }
322  
323 47 ▶sub choose {
Subroutine choose: called CC=20 SCAR=36.3
Pod documented
324 47 my ($self) = @_;
325 47 my $games = $self->games;
326 47 while (1) {
327 84 $self->draw;
328 84 my $key = $self->read_key;
329 84 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 84 84
330 84 ▶ return undef if $key eq 'q' || $key eq 'escape' || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" or $key eq "escape" or $key eq "eof" or $key eq "interrupt" 24 60 84
Condition66%6/9
$key eq "q" or $key eq "escape" or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
24060
$key eq "q" or $key eq "escape" or $key eq "eof"
l!l&&r!l&&!r
24060
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
24060
MC/DC25%1/4
$key eq "q" or $key eq "escape" or $key eq "eof" or $key eq "interrupt"
$key eq "q" $key eq "escape" $key eq "eof" $key eq "interrupt"
Truth table40%2/5
A or B or C or D
A= $key eq "q"B= $key eq "escape"C= $key eq "eof"D= $key eq "interrupt"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
331 60 ▶ if ($key eq 'up' || $key eq 'k') {
Branch: 7 branches
BranchTrueFalseTotal
if ($key eq 'up' or $key eq 'k') { } 0 60 60
elsif ($key eq 'down' or $key eq 'j' or $key eq 'tab') { } 23 37 60
elsif ($key eq 'home' or $key eq 'page_up') { } 0 37 37
elsif ($key eq 'end' or $key eq 'page_down') { } 0 37 37
elsif ($key eq 'enter' or $key eq ' ') { } 24 13 37
elsif ($key eq '?') { } 0 13 13
elsif ($key =~ /^[a-z0-9]$/u) { } 13 0 13
Condition33%1/3
$key eq "up" or $key eq "k"
l!l&&r!l&&!r
0060
MC/DC0%0/2
$key eq "up" or $key eq "k"
$key eq "up" $key eq "k"
Truth table33%1/3
A or B
A= $key eq "up"B= $key eq "k"
ABresult
1X1
011
000
332 0 $self->move(-1);
333 ▶ } elsif ($key eq 'down' || $key eq 'j' || $key eq 'tab') {
Condition66%4/6
$key eq "down" or $key eq "j" or $key eq "tab"
l!l&&r!l&&!r
23037
$key eq "down" or $key eq "j"
l!l&&r!l&&!r
23037
MC/DC33%1/3
$key eq "down" or $key eq "j" or $key eq "tab"
$key eq "down" $key eq "j" $key eq "tab"
Truth table50%2/4
A or B or C
A= $key eq "down"B= $key eq "j"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
334 23 $self->move(1);
335 ▶ } elsif ($key eq 'home' || $key eq 'page_up') {
Condition33%1/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
0037
MC/DC0%0/2
$key eq "home" or $key eq "page_up"
$key eq "home" $key eq "page_up"
Truth table33%1/3
A or B
A= $key eq "home"B= $key eq "page_up"
ABresult
1X1
011
000
336 0 $self->at($self->first_installed);
337 ▶ } elsif ($key eq 'end' || $key eq 'page_down') {
Condition33%1/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
0037
MC/DC0%0/2
$key eq "end" or $key eq "page_down"
$key eq "end" $key eq "page_down"
Truth table33%1/3
A or B
A= $key eq "end"B= $key eq "page_down"
ABresult
1X1
011
000
338 0 $self->at($self->last_installed);
339 ▶ } elsif ($key eq 'enter' || $key eq ' ') {
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
24013
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
340 24 my $row = $games->[$self->at];
341 24 ▶ return $row if $row->{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"installed"} 23 1 24
342 1 $self->note("$row->{key} is not installed: cpanm $row->{module}", 'warn');
343 } elsif ($key eq '?') {
344 0 $self->note($MORE);
345 } elsif ($key =~ m/^[a-z0-9]$/) {
346 13 $self->jump($key);
347 } else {
348 0 $self->note('That key does nothing here');
349 }
350 }
351 }
352  
353 6 ▶sub prompt {
Subroutine prompt: called CC=14 SCAR=27.2
Pod documented
354 6 my ($self) = @_;
355 6 my $games = $self->games;
356 6 my $in = $self->in;
357 6 my $out = $self->out;
358 6 while (1) {
359 7 $self->say('');
360 7 for my $index (0 .. $#{$games}) {
361 168 my $row = $games->[$index];
362 $self->say(sprintf '%2d. %-11s %s%s', $index + 1, $row->{key}, $row->{blurb},
363 168 ▶ $row->{installed} ? '' : ' (not installed)');
Branch: 1 branch
BranchTrueFalseTotal
$row->{'installed'} ? : 168 0 168
364 }
365 7 $self->say($_) for $self->status_line;
366 7 print {$out} 'Which game? (number, name, or q) ';
367 7 my $answer = readline $in;
368 7 ▶ return undef unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 7 7
369 7 $answer =~ s/^\s+|\s+$//g;
370 7 ▶ return undef if $answer eq '' || lc $answer eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "" or lc $answer eq "q" 2 5 7
Condition66%2/3
$answer eq "" or lc $answer eq "q"
l!l&&r!l&&!r
205
MC/DC50%1/2
$answer eq "" or lc $answer eq "q"
$answer eq "" lc $answer eq "q"
Truth table66%2/3
A or B
A= $answer eq ""B= lc $answer eq "q"
ABresult
1X1
011
000
371 5 my $row;
372 5 ▶ if ($answer =~ m/^\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($answer =~ /^\d+$/u) { } 3 2 5
elsif (my $found = Game::Terminal::find($answer)) { } 1 1 2
373 3 ▶ $row = $games->[$answer - 1] if $answer >= 1 && $answer <= @{$games};
Branch: 1 branch
BranchTrueFalseTotal
if $answer >= 1 and $answer <= @{$games;} 3 0 3
Condition33%1/3
$answer >= 1 and $answer <= @{$games;}
!ll&&!rl&&r
003
MC/DC0%0/2
$answer >= 1 and $answer <= @{$games;}
$answer >= 1 $answer <= @{$games;}
Truth table33%1/3
A and B
A= $answer >= 1B= $answer <= @{$games;}
ABresult
0X0
100
111
374 } elsif (my $found = Game::Terminal::find($answer)) {
375 1 ($row) = grep { $_->{key} eq $found->{key} } @{$games};
376 }
377 5 ▶ if (!$row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 1 4 5
378 1 my @near = Game::Terminal::nearest($answer);
379 1 ▶ $self->note("No game called '$answer'." . (@near ? " Did you mean $near[0]?" : ''), 'warn');
Branch: 1 branch
BranchTrueFalseTotal
@near ? : 1 0 1
380 1 next;
381 }
382 4 ▶ if (!$row->{installed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row->{"installed"}) 0 4 4
383 0 $self->note("$row->{key} is not installed: cpanm $row->{module}", 'warn');
384 0 next;
385 }
386 4 return $row;
387 }
388 }
389  
390 8 ▶sub read_line {
Subroutine read_line: called CC=1 SCAR=0.0
Pod documented
391 8 my ($self, $prompt) = @_;
392 8 return $self->keyboard->read_line($prompt, $self->out);
393 }
394  
395 23 ▶sub form {
Subroutine form: called CC=2 SCAR=6.9
Pod documented
396 23 my ($self, $row) = @_;
397 return Game::Terminal::Kit::Form->new(
398 title => $self->paint($row->{key}, $INK{title}) . ": $row->{blurb}",
399 fields => Game::Terminal::options_for($row),
400 23 ▶ values => ($self->values->{ $row->{key} } ||= {}),
Condition100%2/2
$self->values->{$row->{"key"}} ||= {}
l!l
122
MC/DC100%1/1
$self->values->{$row->{"key"}} ||= {}
$self->values->{$row->{"key"}}
Truth table100%2/2
A
A= $self->values->{$row->{"key"}}
Aresult
11
00
401 legend => [],
402 log => Game::Terminal::Kit::Log->new,
403 screen => $self->screen,
404 );
405 }
406  
407 23 ▶sub configure {
Subroutine configure: called CC=13 SCAR=25.8
Pod documented
408 23 my ($self, $row) = @_;
409 23 my $form = $self->form($row);
410 23 my $log = $form->log;
411 23 $self->note('');
412 23 while (1) {
413 86 $self->clear;
414 86 $self->say($_) for $form->lines;
415 86 ▶ $self->say($_) for length $self->status ? ('', $self->status_line) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $self->status ? : 2 84 86
416 86 $self->say($_) for '', $self->paint('left and right change a value, enter edits a text, backspace clears,', $INK{legend}),
417 $self->paint('enter on Play starts, escape goes back', $INK{legend});
418 86 my $key = $self->read_key;
419 86 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 86 86
420 86 my ($event, $value) = $form->step($key);
421 86 ▶ return undef if $event eq 'back' || $event eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "back" or $event eq "quit" 1 85 86
Condition66%2/3
$event eq "back" or $event eq "quit"
l!l&&r!l&&!r
1085
MC/DC50%1/2
$event eq "back" or $event eq "quit"
$event eq "back" $event eq "quit"
Truth table66%2/3
A or B
A= $event eq "back"B= $event eq "quit"
ABresult
1X1
011
000
422 85 ▶ return [ $form->argv ] if $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "chosen" 22 63 85
423 63 ▶ if ($event eq 'edit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "edit") 4 59 63
424 4 my $line = $self->read_line("$value->{name}: ");
425 4 ▶ $form->set($value->{name}, $line) if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 4 0 4
426 }
427 63 my @said = $log->lines;
428 63 $log->shown;
429 63 ▶ $self->note($said[0], $said[0] =~ m/^That key/ ? '' : 'warn') if @said;
Branch: 2 branches
BranchTrueFalseTotal
if @said 1 62 63
$said[0] =~ /^That key/u ? : 0 1 1
430 }
431 }
432  
433 4 ▶sub ask_options {
Subroutine ask_options: called CC=3 SCAR=11.0
Pod documented
434 4 my ($self, $row) = @_;
435 4 my $line = $self->read_line("Options for $row->{key}, as on the command line (blank for none): ");
436 4 ▶ return [] unless defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 3 1 4
Condition66%2/3
defined $line and length $line
!ll&&!rl&&r
031
MC/DC50%1/2
defined $line and length $line
defined $line length $line
Truth table66%2/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
437 1 return [ Text::ParseWords::shellwords($line) ];
438 }
439  
440 30 ▶sub describe {
Subroutine describe: called CC=5 SCAR=16.6
Pod documented
441 30 my ($self, $row, $status, $error) = @_;
442 30 ▶ return "$row->{key} died: $error" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 2 28 30
443 28 ▶ $status = 0 unless defined $status && $status =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status and $status =~ /^\d+$/u 0 28 28
Condition33%1/3
defined $status and $status =~ /^\d+$/u
!ll&&!rl&&r
0028
MC/DC0%0/2
defined $status and $status =~ /^\d+$/u
defined $status $status =~ /^\d+$/u
Truth table33%1/3
A and B
A= defined $statusB= $status =~ /^\d+$/u
ABresult
0X0
100
111
444 28 my $word = $WORD{$status};
445 28 ▶ return "$row->{key} finished with status $status" . ($word ? " ($word)" : '');
Branch: 1 branch
BranchTrueFalseTotal
$word ? : 27 1 28
446 }
447  
448 26 ▶sub launch {
Subroutine launch: called CC=11 SCAR=24.8
Pod documented
449 26 my ($self, $row, $argv) = @_;
450 26 $self->leave_raw;
451 26 $self->clear;
452 26 my $common = $self->common;
453 26 my @flags = Game::Terminal::flags_for($row, $common);
454 26 ▶ my ($status, $error) = $self->runner->($row, [ @flags, @{ $argv || [] } ],
Branch: 1 branch
BranchTrueFalseTotal
defined $common->{'colour'} && !$common->{'colour'} ? : 1 25 26
Condition60%3/5
$argv || []
l!l
260
defined $common->{"colour"} and not $common->{"colour"}
!ll&&!rl&&r
2501
MC/DC0%0/1
$argv || []
$argv
MC/DC50%1/2
defined $common->{"colour"} and not $common->{"colour"}
defined $common->{"colour"} not $common->{"colour"}
Truth table50%1/2
A
A= $argv
Aresult
11
00
Truth table66%2/3
A and not(B)
A= defined $common->{"colour"}B= not $common->{"colour"}
ABresult
0X0
100
111
455 out => $self->out,
456 26 no_colour => (defined $common->{colour} && !$common->{colour}) ? 1 : 0,
457 );
458 26 my $out = $self->out;
459 0 ▶ print {$out} "\e[0m\e[?25h" if $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->interactive 0 26 26
460 26 ▶ $self->picking(0) if $self->picking && !$self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->enter_raw 0 26 26
Condition66%2/3
$self->picking and not $self->enter_raw
!ll&&!rl&&r
4220
MC/DC0%0/2
$self->picking and not $self->enter_raw
$self->picking not $self->enter_raw
Truth table66%2/3
A and not(B)
A= $self->pickingB= not $self->enter_raw
ABresult
0X0
100
111
461 26 ▶ $self->note($self->describe($row, $status, $error),
Branch: 3 branches
BranchTrueFalseTotal
defined $error ? : 1 25 26
!defined($status) || $status eq '0' ? : 22 3 25
$status eq '1' ? : 2 1 3
Condition66%2/3
not defined $status or $status eq 0
l!l&&r!l&&!r
0223
MC/DC50%1/2
not defined $status or $status eq 0
not defined $status $status eq 0
Truth table66%2/3
not(A) or B
A= not defined $statusB= $status eq 0
ABresult
1X1
011
000
462 defined $error ? 'bad' : !defined $status || $status eq '0' ? 'good' : $status eq '1' ? 'bad' : 'warn');
463 26 return $self;
464 }
465  
466 26 ▶sub start {
Subroutine start: called CC=5 SCAR=16.5
Pod documented
467 26 my ($self) = @_;
468 26 ▶ $self->picking(0) if $self->picking && !$self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->enter_raw 0 26 26
Condition66%2/3
$self->picking and not $self->enter_raw
!ll&&!rl&&r
2240
MC/DC0%0/2
$self->picking and not $self->enter_raw
$self->picking not $self->enter_raw
Truth table66%2/3
A and not(B)
A= $self->pickingB= not $self->enter_raw
ABresult
0X0
100
111
469 26 my $ok = eval {
470 26 ▶ Game::Terminal::Kit::Run::guard(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
471 26 while (1) {
472 53 ▶ my $row = $self->picking ? $self->choose : $self->prompt;
Branch: 1 branch
BranchTrueFalseTotal
$self->picking ? : 47 6 53
473 53 ▶ last unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 26 27 53
474 27 ▶ my $argv = $self->picking ? $self->configure($row) : $self->ask_options($row);
Branch: 1 branch
BranchTrueFalseTotal
$self->picking ? : 23 4 27
475 27 ▶ next unless $argv;
Branch: 1 branch
BranchTrueFalseTotal
unless $argv 1 26 27
476 26 $self->launch($row, $argv);
477 }
478 26 }, $self->keyboard);
479 26 1;
480 };
481 26 my $error = $@;
482 26 ▶ if ($self->interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) 0 26 26
483 0 my $out = $self->out;
484 0 print {$out} "\e[0m\e[?25h\n";
485 }
486 26 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 26 26
487 26 return 0;
488 }
489  
490 4 1;
491  
492 __END__
« bin/game ↑ Summary blib/lib/Game/Terminal.pm »