blib/lib/Game/Terminal/Menu.pm

filter: Statementstmt 93.6% 324 / 346 Branchbran 75.0% 153 / 204 Conditioncond 54.0% 47 / 87 MC/DCmcdc 24.5% 13 / 53 Subroutinesub 97.7% 44 / 45 Podpod 96.7% 30 / 31 totaltotal 79.7% 611 / 766 CC 146 SCAR 26.4
CC
146
Cov
82%
choose
37.7
BUILD
28.1
choose_save
20.9
CRAP 14.0
« bin/game ↑ Summary blib/lib/Game/Terminal.pm »
1 package Game::Terminal::Menu;
2  
3 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Game::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Game::Terminal::Kit::Form;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Game::Terminal::Kit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Game::Terminal::Kit::Picker;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Text::ParseWords ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 our $VERSION = '0.05';
18  
19 5 my %WORD = (0 => 'won or drew', 1 => 'lost', 2 => 'bad option');
20  
21 5 my $LEGEND = 'up and down to choose, enter to set up and play, a letter to jump, q to quit';
22 5 my $MORE = 'k and j move too, space plays, home and end jump to the ends';
23  
24 5 my %INK = (
25 title => '1',
26 count => '2',
27 key => '1',
28 board => '38;5;75',
29 card => '38;5;203',
30 tile => '38;5;220',
31 puzzle => '38;5;177',
32 arcade => '38;5;80',
33 detail => '38;5;245',
34 legend => '2',
35 set => '38;5;114',
36 own => '2',
37 play => '1;32',
38 good => '38;5;114',
39 bad => '38;5;203',
40 warn => '38;5;220',
41 );
42  
43 has [qw/in out/] => (
44 is => 'rw'
45 5 );
46  
47 has [qw/interactive colour picking/] => (
48 is => 'rw'
49 5 );
50  
51 has [qw/keyboard screen/] => (
52 is => 'rw'
53 5 );
54  
55 has keysource => (
56 is => 'rw',
57 isa => CodeRef
58 5 );
59  
60 has pending => (
61 is => 'rw',
62 isa => ArrayRef,
63 default => []
64 5 );
65  
66 has runner => (
67 is => 'rw',
68 isa => CodeRef
69 5 );
70  
71 has common => (
72 is => 'rw',
73 isa => HashRef,
74 default => {}
75 5 );
76  
77 has inc => (
78 is => 'rw',
79 isa => ArrayRef
80 5 );
81  
82 has games => (
83 is => 'rw',
84 isa => ArrayRef
85 5 );
86  
87 has at => (
88 is => 'rw',
89 isa => Int,
90 default => 0
91 5 );
92  
93 has status => (
94 is => 'rw',
95 isa => Str,
96 default => ''
97 5 );
98  
99 has tone => (
100 is => 'rw',
101 isa => Str,
102 default => ''
103 5 );
104  
105 has rows => (
106 is => 'rw',
107 isa => Int,
108 default => 0
109 5 );
110  
111 has values => (
112 is => 'rw',
113 isa => HashRef,
114 default => {}
115 5 );
116  
117 has save_dir => (
118 is => 'rw',
119 isa => Str,
120 default => '.'
121 5 );
122  
123 5 my @SAVE_ACTIONS = (
124 { name => 'resume', label => 'Resume a saved game' },
125 { name => 'replay', label => 'Replay a saved game' },
126 );
127  
128 43 ▶sub BUILD {
Subroutine BUILD: called CC=19 SCAR=33.4
Pod undocumented
129 43 my ($self) = @_;
130 43 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 29 14 43
131 43 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 0 43 43
132 43 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 0 43 43
-t $self->in ? : 0 0 0
133 43 ▶ $self->colour($self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 0 43 43
$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
134 unless defined $self->colour;
135 43 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 43 0 43
$self->keysource ? : 41 2 43
136 in => $self->in,
137 pending => $self->pending,
138 ($self->keysource ? (keysource => $self->keysource) : ()),
139 )) unless $self->keyboard;
140 43 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->screen 43 0 43
141 out => $self->out,
142 interactive => $self->interactive,
143 colour => $self->colour,
144 ascii => 1,
145 )) unless $self->screen;
146 43 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 0 43 43
$self->interactive ? : 0 0 0
147 43 ▶ $self->picking(0) unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 2 41 43
148 43 ▶ $self->inc([ @INC ]) unless $self->inc;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->inc 43 0 43
149 43 ▶ $self->runner(\&Game::Terminal::launch) unless $self->runner;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->runner 4 39 43
150 43 ▶ unless ($self->games) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->games) 4 39 43
151 4 my @inc = @{$self->inc};
152 $self->games([
153 4 ▶ map { { %{$_}, installed => Game::Terminal::installed($_, @inc) ? 1 : 0 } }
Branch: 1 branch
BranchTrueFalseTotal
Game::Terminal::installed($_, @inc) ? : 96 0 96
154 Game::Terminal::games()
155 ]);
156 }
157 43 $self->at($self->first_installed);
158 43 ▶ $self->rows($self->height) unless $self->rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->rows 42 1 43
159 43 $self->screen->flush_on;
160 43 return $self;
161 }
162  
163 1 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
164 1 my ($self) = @_;
165 1 return $self->keyboard->raw;
166 }
167  
168 44 ▶sub keys_available {
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
169 44 my ($self) = @_;
170 44 return $self->keyboard->keys_available;
171 }
172  
173 61 ▶sub enter_raw {
Subroutine enter_raw: called CC=2 SCAR=7.2
Pod documented
174 61 my ($self) = @_;
175 61 ▶ return $self->keyboard->enter_raw ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->keyboard->enter_raw ? : 61 0 61
176 }
177  
178 33 ▶sub leave_raw {
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
179 33 my ($self) = @_;
180 33 $self->keyboard->leave_raw;
181 33 return $self;
182 }
183  
184 247 ▶sub read_key {
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
185 247 my ($self) = @_;
186 247 return $self->keyboard->read_key;
187 }
188  
189 42 ▶sub height {
Subroutine height: called CC=3 SCAR=16.5
Pod documented
190 42 my ($self) = @_;
191 42 ▶ return 0 unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 42 0 42
192 0 my (undef, $rows) = $self->screen->measured;
193 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
194 }
195  
196 4400 ▶sub say {
Subroutine say: called CC=1 SCAR=0.0
Pod documented
197 4400 my ($self, $line) = @_;
198 4400 $self->screen->say($line);
199 4400 return $self;
200 }
201  
202 259 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
203 259 my ($self) = @_;
204 259 $self->screen->clear;
205 259 return $self;
206 }
207  
208 4701 ▶sub paint {
Subroutine paint: called CC=1 SCAR=0.0
Pod documented
209 4701 my ($self, $text, $code) = @_;
210 4701 return $self->screen->paint($text, $code);
211 }
212  
213 43 ▶sub first_installed {
Subroutine first_installed: called CC=3 SCAR=11.4
Pod documented
214 43 my ($self) = @_;
215 43 my $games = $self->games;
216 43 for my $index (0 .. $#{$games}) {
217 43 ▶ return $index if $games->[$index]{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $games->[$index]{"installed"} 43 0 43
218 }
219 0 return 0;
220 }
221  
222 0 ▶sub last_installed {
Subroutine last_installed: not called CC=3 SCAR=24.8
Pod documented
223 0 my ($self) = @_;
224 0 my $games = $self->games;
225 0 for my $index (reverse 0 .. $#{$games}) {
226 0 ▶ return $index if $games->[$index]{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $games->[$index]{"installed"} 0 0 0
227 }
228 0 return 0;
229 }
230  
231 80 ▶sub note {
Subroutine note: called CC=3 SCAR=11.0
Pod documented
232 80 my ($self, $text, $tone) = @_;
233 80 ▶ $self->status(defined $text ? $text : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 80 0 80
234 80 ▶ $self->tone(defined $tone ? $tone : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $tone ? : 37 43 80
235 80 return $self;
236 }
237  
238 118 ▶sub status_line {
Subroutine status_line: called CC=3 SCAR=11.3
Pod documented
239 118 my ($self) = @_;
240 118 ▶ return () unless length $self->status;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->status 76 42 118
241 42 my $tone = $self->tone;
242 42 ▶ return $self->paint($self->status, $INK{$tone} || $INK{detail});
Condition33%1/3
$INK{$tone} || $INK{'detail'}
l!l&&r!l&&!r
4200
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
243 }
244  
245 2066 ▶sub row_line {
Subroutine row_line: called CC=4 SCAR=14.0
Pod documented
246 2066 my ($self, $index) = @_;
247 2066 my $row = $self->games->[$index];
248 my $line = sprintf '%-11s %-6s %-4s %s',
249 2066 $row->{key}, $row->{family}, $row->{players}, $row->{blurb};
250 2066 $line =~ s/\s+$//;
251 2066 ▶ return $self->paint(" $line (not installed)", '2') unless $row->{installed};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"installed"} 50 2016 2066
252 2016 ▶ return ' ' . $self->paint("> $line", '1;7') if $index == $self->at;
Branch: 1 branch
BranchTrueFalseTotal
if $index == $self->at 103 1913 2016
253 return ' ' . $self->paint(sprintf('%-11s', $row->{key}), $INK{key}) . ' '
254 . $self->paint(sprintf('%-6s', $row->{family}), $INK{ $row->{family} } || $INK{detail}) . ' '
255 1913 ▶ . sprintf('%-4s %s', $row->{players}, $row->{blurb});
Condition33%1/3
$INK{$row->{'family'}} || $INK{'detail'}
l!l&&r!l&&!r
191300
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
256 }
257  
258 105 ▶sub list_lines {
Subroutine list_lines: called CC=8 SCAR=20.8
Pod documented
259 105 my ($self) = @_;
260 105 my $games = $self->games;
261 105 my $count = scalar @{$games};
262 105 ▶ my $room = $self->rows ? $self->rows - 7 : $count;
Branch: 1 branch
BranchTrueFalseTotal
$self->rows ? : 2 103 105
263 105 my ($first, $last) = (0, $count - 1);
264 105 ▶ if ($room < $count && $room >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($room < $count and $room >= 5) 2 103 105
Condition66%2/3
$room < $count and $room >= 5
!ll&&!rl&&r
10302
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
265 2 my $window = $room - 2;
266 2 $first = $self->at - int($window / 2);
267 2 ▶ $first = 0 if $first < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $first < 0 1 1 2
268 2 ▶ $first = $count - $window if $first > $count - $window;
Branch: 1 branch
BranchTrueFalseTotal
if $first > $count - $window 1 1 2
269 2 $last = $first + $window - 1;
270 }
271 105 my @lines;
272 105 ▶ push @lines, sprintf ' %d further up', $first if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 1 104 105
273 105 push @lines, map { $self->row_line($_) } $first .. $last;
274 105 ▶ push @lines, sprintf ' %d further down', $count - 1 - $last if $last < $count - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $last < $count - 1 1 104 105
275 105 return \@lines;
276 }
277  
278 103 ▶sub detail_lines {
Subroutine detail_lines: called CC=3 SCAR=11.0
Pod documented
279 103 my ($self) = @_;
280 103 my $row = $self->games->[$self->at];
281 103 my @lines;
282 103 ▶ if ($row->{installed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'installed'}) { } 101 2 103
283 101 my $version = Game::Terminal::version_of($row, @{$self->inc});
284 101 ▶ push @lines, "$row->{key}: $row->{class}" . (defined $version ? " $version" : '')
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 101 0 101
285 . ", options: game $row->{key} --help";
286 } else {
287 2 push @lines, "$row->{key} is not installed: cpanm $row->{module}";
288 }
289 103 $lines[0] = $self->paint($lines[0], $INK{detail});
290 103 push @lines, $self->status_line;
291 103 return \@lines;
292 }
293  
294 103 ▶sub draw {
Subroutine draw: called CC=3 SCAR=11.0
Pod documented
295 103 my ($self) = @_;
296 103 $self->clear;
297 103 my $count = sprintf '%d games', scalar @{$self->games};
298 103 $self->say($self->paint('Game', $INK{title}) . $self->paint(sprintf('%*s', 72, $count), $INK{count}));
299 103 $self->say('');
300 103 $self->say($_) for @{$self->list_lines};
301 103 $self->say('');
302 103 $self->say($_) for @{$self->detail_lines};
303 103 $self->say('');
304 103 $self->say($self->paint($LEGEND, $INK{legend}));
305 103 return $self;
306 }
307  
308 26 ▶sub move {
Subroutine move: called CC=3 SCAR=11.0
Pod documented
309 26 my ($self, $step) = @_;
310 26 my $games = $self->games;
311 26 my $at = $self->at;
312 26 for (1 .. @{$games}) {
313 29 $at = ($at + $step) % @{$games};
314 29 ▶ last if $games->[$at]{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $games->[$at]{"installed"} 26 3 29
315 }
316 26 $self->at($at);
317 26 return $self;
318 }
319  
320 18 ▶sub jump {
Subroutine jump: called CC=4 SCAR=14.1
Pod documented
321 18 my ($self, $letter) = @_;
322 18 my $games = $self->games;
323 18 my $at = $self->at;
324 18 for (1 .. @{$games}) {
325 88 $at = ($at + 1) % @{$games};
326 88 ▶ next unless $games->[$at]{installed};
Branch: 1 branch
BranchTrueFalseTotal
unless $games->[$at]{"installed"} 0 88 88
327 88 ▶ next unless index($games->[$at]{key}, $letter) == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($games->[$at]{"key"}, $letter) == 0 70 18 88
328 18 $self->at($at);
329 18 return $self;
330 }
331 0 $self->note("No game starts with $letter");
332 0 return $self;
333 }
334  
335 65 ▶sub choose {
Subroutine choose: called CC=20 SCAR=36.3
Pod documented
336 65 my ($self) = @_;
337 65 my $games = $self->games;
338 65 while (1) {
339 103 $self->draw;
340 103 my $key = $self->read_key;
341 103 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 103 103
342 103 ▶ 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" 33 70 103
Condition66%6/9
$key eq "q" or $key eq "escape" or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
33070
$key eq "q" or $key eq "escape" or $key eq "eof"
l!l&&r!l&&!r
33070
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
33070
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
343 70 ▶ if ($key eq 'up' || $key eq 'k') {
Branch: 7 branches
BranchTrueFalseTotal
if ($key eq 'up' or $key eq 'k') { } 0 70 70
elsif ($key eq 'down' or $key eq 'j' or $key eq 'tab') { } 24 46 70
elsif ($key eq 'home' or $key eq 'page_up') { } 0 46 46
elsif ($key eq 'end' or $key eq 'page_down') { } 0 46 46
elsif ($key eq 'enter' or $key eq ' ') { } 33 13 46
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
0070
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
344 0 $self->move(-1);
345 ▶ } 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
24046
$key eq "down" or $key eq "j"
l!l&&r!l&&!r
24046
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
346 24 $self->move(1);
347 ▶ } elsif ($key eq 'home' || $key eq 'page_up') {
Condition33%1/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
0046
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
348 0 $self->at($self->first_installed);
349 ▶ } elsif ($key eq 'end' || $key eq 'page_down') {
Condition33%1/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
0046
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
350 0 $self->at($self->last_installed);
351 ▶ } elsif ($key eq 'enter' || $key eq ' ') {
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
33013
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
352 33 my $row = $games->[$self->at];
353 33 ▶ return $row if $row->{installed};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"installed"} 32 1 33
354 1 $self->note("$row->{key} is not installed: cpanm $row->{module}", 'warn');
355 } elsif ($key eq '?') {
356 0 $self->note($MORE);
357 } elsif ($key =~ m/^[a-z0-9]$/) {
358 13 $self->jump($key);
359 } else {
360 0 $self->note('That key does nothing here');
361 }
362 }
363 }
364  
365 8 ▶sub prompt {
Subroutine prompt: called CC=14 SCAR=27.0
Pod documented
366 8 my ($self) = @_;
367 8 my $games = $self->games;
368 8 my $in = $self->in;
369 8 my $out = $self->out;
370 8 while (1) {
371 9 $self->say('');
372 9 for my $index (0 .. $#{$games}) {
373 172 my $row = $games->[$index];
374 $self->say(sprintf '%2d. %-11s %s%s', $index + 1, $row->{key}, $row->{blurb},
375 172 ▶ $row->{installed} ? '' : ' (not installed)');
Branch: 1 branch
BranchTrueFalseTotal
$row->{'installed'} ? : 172 0 172
376 }
377 9 $self->say($_) for $self->status_line;
378 9 print {$out} 'Which game? (number, name, or q) ';
379 9 my $answer = readline $in;
380 9 ▶ return undef unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 9 9
381 9 $answer =~ s/^\s+|\s+$//g;
382 9 ▶ return undef if $answer eq '' || lc $answer eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "" or lc $answer eq "q" 3 6 9
Condition100%3/3
$answer eq "" or lc $answer eq "q"
l!l&&r!l&&!r
216
MC/DC100%2/2
$answer eq "" or lc $answer eq "q"
$answer eq "" lc $answer eq "q"
Truth table100%3/3
A or B
A= $answer eq ""B= lc $answer eq "q"
ABresult
1X1
011
000
383 6 my $row;
384 6 ▶ if ($answer =~ m/^\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($answer =~ /^\d+$/u) { } 4 2 6
elsif (my $found = Game::Terminal::find($answer)) { } 1 1 2
385 4 ▶ $row = $games->[$answer - 1] if $answer >= 1 && $answer <= @{$games};
Branch: 1 branch
BranchTrueFalseTotal
if $answer >= 1 and $answer <= @{$games;} 4 0 4
Condition33%1/3
$answer >= 1 and $answer <= @{$games;}
!ll&&!rl&&r
004
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
386 } elsif (my $found = Game::Terminal::find($answer)) {
387 1 ($row) = grep { $_->{key} eq $found->{key} } @{$games};
388 }
389 6 ▶ if (!$row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 1 5 6
390 1 my @near = Game::Terminal::nearest($answer);
391 1 ▶ $self->note("No game called '$answer'." . (@near ? " Did you mean $near[0]?" : ''), 'warn');
Branch: 1 branch
BranchTrueFalseTotal
@near ? : 1 0 1
392 1 next;
393 }
394 5 ▶ if (!$row->{installed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row->{"installed"}) 0 5 5
395 0 $self->note("$row->{key} is not installed: cpanm $row->{module}", 'warn');
396 0 next;
397 }
398 5 return $row;
399 }
400 }
401  
402 12 ▶sub read_line {
Subroutine read_line: called CC=1 SCAR=0.0
Pod documented
403 12 my ($self, $prompt) = @_;
404 12 return $self->keyboard->read_line($prompt, $self->out);
405 }
406  
407 32 ▶sub form {
Subroutine form: called CC=3 SCAR=11.0
Pod documented
408 32 my ($self, $row) = @_;
409 return Game::Terminal::Kit::Form->new(
410 title => $self->paint($row->{key}, $INK{title}) . ": $row->{blurb}",
411 fields => Game::Terminal::options_for($row),
412 32 ▶ values => ($self->values->{ $row->{key} } ||= {}),
Branch: 1 branch
BranchTrueFalseTotal
Game::Terminal::saves($row) ? : 13 19 32
Condition100%2/2
$self->values->{$row->{"key"}} ||= {}
l!l
131
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
413 legend => [],
414 log => Game::Terminal::Kit::Log->new,
415 screen => $self->screen,
416 actions => (Game::Terminal::saves($row) ? [ @SAVE_ACTIONS ] : []),
417 );
418 }
419  
420 14 ▶sub _when {
Subroutine _when: called CC=3 SCAR=12.5
421 14 my ($stamp) = @_;
422 14 ▶ return $stamp unless defined $stamp && $stamp =~ m/\A(\d{4}-\d\d-\d\d)T(\d\d:\d\d)/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stamp and $stamp =~ /\A(\d{4}-\d\d-\d\d)T(\d\d:\d\d)/u 0 14 14
Condition33%1/3
defined $stamp and $stamp =~ /\A(\d{4}-\d\d-\d\d)T(\d\d:\d\d)/u
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $stamp and $stamp =~ /\A(\d{4}-\d\d-\d\d)T(\d\d:\d\d)/u
defined $stamp $stamp =~ /\A(\d{4}-\d\d-\d\d)T(\d\d:\d\d)/u
Truth table33%1/3
A and B
A= defined $stampB= $stamp =~ /\A(\d{4}-\d\d-\d\d)T(\d\d:\d\d)/u
ABresult
0X0
100
111
423 14 return "$1 $2 UTC";
424 }
425  
426 8 ▶sub save_items {
Subroutine save_items: called CC=3 SCAR=11.0
Pod documented
427 8 my ($self, $row) = @_;
428 my @items = map {
429 8 my $save = $_;
430 14 my $count = $save->count;
431 14 (my $name = $save->file) =~ s{\A.*[/\\]}{};
432 +{
433 14 ▶ label => $name,
Branch: 2 branches
BranchTrueFalseTotal
defined $save->saved_at ? : 14 0 14
$count == 1 ? : 7 7 14
434 about => join(', ', (defined $save->saved_at ? 'saved ' . _when($save->saved_at) : ()),
435 $count . ($count == 1 ? ' turn' : ' turns')),
436 value => $save->file,
437 };
438 } Game::Terminal::saved_games($row, $self->save_dir);
439 8 push @items, { label => 'another file', about => 'type where it is', value => '' };
440 8 return \@items;
441 }
442  
443 8 ▶sub choose_save {
Subroutine choose_save: called CC=19 SCAR=30.4
Pod documented
444 8 my ($self, $row, $how) = @_;
445 8 my $items = $self->save_items($row);
446 8 my $found = @{$items} - 1;
447 8 ▶ my $dir = $self->save_dir eq '.' ? 'here' : 'in ' . $self->save_dir;
Branch: 1 branch
BranchTrueFalseTotal
$self->save_dir eq '.' ? : 0 8 8
448 my $picker = Game::Terminal::Kit::Picker->new(
449 items => $items,
450 window => $self->rows ? ($self->rows > 12 ? $self->rows - 10 : 2) : 0,
451 head => [
452 $self->paint($row->{key}, $INK{title}) . ': ' . ($how eq 'resume' ? 'carry on a saved game' : 'watch a saved game'),
453 '',
454 $found ? "$found saved " . ($found == 1 ? 'game' : 'games') . " of $row->{key} $dir, the last saved first:"
455 : "no saved games of $row->{key} $dir:",
456 '',
457 ],
458 legend => [ '', $self->paint('up and down to choose, enter to ' . ($how eq 'resume' ? 'carry it on' : 'watch it')
459 8 ▶ . ', escape goes back', $INK{legend}) ],
Branch: 6 branches
BranchTrueFalseTotal
$self->rows ? : 0 8 8
$self->rows > 12 ? : 0 0 0
$how eq 'resume' ? : 7 1 8
$found ? : 7 1 8
$found == 1 ? : 0 7 7
$how eq 'resume' ? : 7 1 8
460 screen => $self->screen,
461 );
462 8 $self->note('');
463 8 while (1) {
464 14 $self->clear;
465 14 $self->say($_) for $picker->lines;
466 14 ▶ $self->say($_) for length $self->status ? ('', $self->status_line) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $self->status ? : 2 12 14
467 14 my $key = $self->read_key;
468 14 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 14 14
469 14 my ($event, $value) = $picker->step($key);
470 14 ▶ return undef if $event eq 'back' || $event eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "back" or $event eq "quit" 5 9 14
Condition66%2/3
$event eq "back" or $event eq "quit"
l!l&&r!l&&!r
509
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
471 9 ▶ next unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 4 5 9
472 5 ▶ my $file = length $value ? $value : $self->read_line('the saved game: ');
Branch: 1 branch
BranchTrueFalseTotal
length $value ? : 2 3 5
473 5 ▶ next unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 5 5
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
005
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
474 5 my ($save, $why) = Game::Terminal::read_save($row, $file);
475 5 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) 3 2 5
476 3 $self->note('');
477 3 return $file;
478 }
479 2 $self->note(ucfirst($why) . '.', 'warn');
480 }
481 }
482  
483 32 ▶sub configure {
Subroutine configure: called CC=17 SCAR=28.6
Pod documented
484 32 my ($self, $row) = @_;
485 32 my $form = $self->form($row);
486 32 my $log = $form->log;
487 32 $self->note('');
488 32 while (1) {
489 110 $self->clear;
490 110 $self->say($_) for $form->lines;
491 110 ▶ $self->say($_) for length $self->status ? ('', $self->status_line) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $self->status ? : 4 106 110
492 110 $self->say($_) for '', $self->paint('left and right change a value, enter edits a text, backspace clears,', $INK{legend}),
493 $self->paint('enter on Play starts, escape goes back', $INK{legend});
494 110 my $key = $self->read_key;
495 110 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 110 110
496 110 my ($event, $value) = $form->step($key);
497 110 ▶ return undef if $event eq 'back' || $event eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "back" or $event eq "quit" 5 105 110
Condition66%2/3
$event eq "back" or $event eq "quit"
l!l&&r!l&&!r
50105
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
498 105 ▶ return [ $form->argv ] if $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
if $event eq "chosen" 24 81 105
499 81 ▶ if ($event eq 'key' && ($value eq 'resume' || $value eq 'replay')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "key" and $value eq "resume" || $value eq "replay") 8 73 81
Condition66%4/6
$event eq "key" and $value eq "resume" || $value eq "replay"
!ll&&!rl&&r
7308
$value eq "resume" or $value eq "replay"
l!l&&r!l&&!r
710
MC/DC33%1/3
$event eq "key" and $value eq "resume" || $value eq "replay"
$event eq "key" $value eq "resume" $value eq "replay"
Truth table75%3/4
A and B or C
A= $event eq "key"B= $value eq "resume"C= $value eq "replay"
ABCresult
0XX0
1000
11X1
1011
500 8 my $file = $self->choose_save($row, $value);
501 8 ▶ return [ "--$value", $file ] if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 3 5 8
502 5 next;
503 }
504 73 ▶ if ($event eq 'edit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "edit") 4 69 73
505 4 my $line = $self->read_line("$value->{name}: ");
506 4 ▶ $form->set($value->{name}, $line) if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 4 0 4
507 }
508 73 my @said = $log->lines;
509 73 $log->shown;
510 73 ▶ $self->note($said[0], $said[0] =~ m/^That key/ ? '' : 'warn') if @said;
Branch: 2 branches
BranchTrueFalseTotal
if @said 1 72 73
$said[0] =~ /^That key/u ? : 0 1 1
511 }
512 }
513  
514 5 ▶sub ask_options {
Subroutine ask_options: called CC=4 SCAR=13.9
Pod documented
515 5 my ($self, $row) = @_;
516 5 ▶ my $line = $self->read_line("Options for $row->{key}, as on the command line (blank for none"
Branch: 1 branch
BranchTrueFalseTotal
Game::Terminal::saves($row) ? : 1 4 5
517 . (Game::Terminal::saves($row) ? '; --resume FILE or --replay FILE for a saved game' : '') . '): ');
518 5 ▶ return [] unless defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 3 2 5
Condition66%2/3
defined $line and length $line
!ll&&!rl&&r
032
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
519 2 return [ Text::ParseWords::shellwords($line) ];
520 }
521  
522 36 ▶sub describe {
Subroutine describe: called CC=5 SCAR=16.6
Pod documented
523 36 my ($self, $row, $status, $error) = @_;
524 36 ▶ return "$row->{key} died: $error" if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 2 34 36
525 34 ▶ $status = 0 unless defined $status && $status =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status and $status =~ /^\d+$/u 0 34 34
Condition33%1/3
defined $status and $status =~ /^\d+$/u
!ll&&!rl&&r
0034
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
526 34 my $word = $WORD{$status};
527 34 ▶ return "$row->{key} finished with status $status" . ($word ? " ($word)" : '');
Branch: 1 branch
BranchTrueFalseTotal
$word ? : 33 1 34
528 }
529  
530 32 ▶sub launch {
Subroutine launch: called CC=11 SCAR=24.8
Pod documented
531 32 my ($self, $row, $argv) = @_;
532 32 $self->leave_raw;
533 32 $self->clear;
534 32 my $common = $self->common;
535 32 my @flags = Game::Terminal::flags_for($row, $common);
536 32 ▶ my ($status, $error) = $self->runner->($row, [ @flags, @{ $argv || [] } ],
Branch: 1 branch
BranchTrueFalseTotal
defined $common->{'colour'} && !$common->{'colour'} ? : 1 31 32
Condition60%3/5
$argv || []
l!l
320
defined $common->{"colour"} and not $common->{"colour"}
!ll&&!rl&&r
3101
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
537 out => $self->out,
538 32 no_colour => (defined $common->{colour} && !$common->{colour}) ? 1 : 0,
539 );
540 32 my $out = $self->out;
541 0 ▶ print {$out} "\e[0m\e[?25h" if $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->interactive 0 32 32
542 32 ▶ $self->picking(0) if $self->picking && !$self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->enter_raw 0 32 32
Condition66%2/3
$self->picking and not $self->enter_raw
!ll&&!rl&&r
5270
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
543 32 ▶ $self->note($self->describe($row, $status, $error),
Branch: 3 branches
BranchTrueFalseTotal
defined $error ? : 1 31 32
!defined($status) || $status eq '0' ? : 28 3 31
$status eq '1' ? : 2 1 3
Condition66%2/3
not defined $status or $status eq 0
l!l&&r!l&&!r
0283
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
544 defined $error ? 'bad' : !defined $status || $status eq '0' ? 'good' : $status eq '1' ? 'bad' : 'warn');
545 32 return $self;
546 }
547  
548 36 ▶sub start {
Subroutine start: called CC=5 SCAR=16.5
Pod documented
549 36 my ($self) = @_;
550 36 ▶ $self->picking(0) if $self->picking && !$self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->enter_raw 0 36 36
Condition66%2/3
$self->picking and not $self->enter_raw
!ll&&!rl&&r
3330
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
551 36 my $ok = eval {
552 36 ▶ Game::Terminal::Kit::Run::guard(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
553 36 while (1) {
554 73 ▶ my $row = $self->picking ? $self->choose : $self->prompt;
Branch: 1 branch
BranchTrueFalseTotal
$self->picking ? : 65 8 73
555 73 ▶ last unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 36 37 73
556 37 ▶ my $argv = $self->picking ? $self->configure($row) : $self->ask_options($row);
Branch: 1 branch
BranchTrueFalseTotal
$self->picking ? : 32 5 37
557 37 ▶ next unless $argv;
Branch: 1 branch
BranchTrueFalseTotal
unless $argv 5 32 37
558 32 $self->launch($row, $argv);
559 }
560 36 }, $self->keyboard);
561 36 1;
562 };
563 36 my $error = $@;
564 36 ▶ if ($self->interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) 0 36 36
565 0 my $out = $self->out;
566 0 print {$out} "\e[0m\e[?25h\n";
567 }
568 36 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 36 36
569 36 return 0;
570 }
571  
572 5 1;
573  
574 __END__
« bin/game ↑ Summary blib/lib/Game/Terminal.pm »