blib/lib/Game/Terminal.pm

filter: Statementstmt 98.6% 149 / 151 Branchbran 90.9% 40 / 44 Conditioncond 70.0% 21 / 30 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 27 / 27 Podpod 100% 16 / 16 totaltotal 91.2% 260 / 285 CC 40 SCAR 16.3
CC
40
Cov
93%
launch
11.2
flags_for
7.3
distance
6.0
CRAP 5.1
« blib/lib/Game/Terminal/Menu.pm ↑ Summary
1 package Game::Terminal;
2  
3 19 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 19 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 19 ▶use Game::Terminal::Kit::Options ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 20 our $VERSION = '0.04';
11  
12 20 our @ROSTER;
13 20 my %BY_NAME;
14  
15 20 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
16 20 my %ALL = (ascii => '--ascii', pick => '--no-pick');
17  
18 20 @ROSTER = (
19 {
20 key => 'backgammon',
21 module => 'Game::Backgammon',
22 class => 'Game::Backgammon::Terminal',
23 blurb => 'backgammon',
24 family => 'board',
25 players => '2',
26 args => [],
27 aliases => [],
28 flags => { %ALL },
29 },
30 {
31 key => 'brandubh',
32 module => 'Game::Brandubh',
33 class => 'Game::Brandubh::Terminal',
34 blurb => 'the Irish 7 by 7 tafl game',
35 family => 'board',
36 players => '2',
37 args => [],
38 aliases => ['tafl'],
39 flags => { ascii => '--no-unicode', pick => '--no-pick' },
40 },
41 {
42 key => 'checkers',
43 module => 'Game::Checkers',
44 class => 'Game::Checkers::Terminal',
45 blurb => 'English draughts',
46 family => 'board',
47 players => '2',
48 args => [],
49 aliases => ['draughts'],
50 flags => { %ALL },
51 },
52 {
53 key => 'chess',
54 module => 'Term::Chess',
55 class => 'Term::Chess',
56 blurb => 'chess',
57 family => 'board',
58 players => '2',
59 args => [],
60 aliases => [],
61 flags => { %ALL },
62 },
63 {
64 key => 'cribbage',
65 module => 'Game::Cribbage',
66 class => 'Game::Cribbage::Terminal',
67 blurb => 'cribbage',
68 family => 'card',
69 players => '2',
70 args => [],
71 aliases => ['crib'],
72 flags => { pick => '--no-pick' },
73 },
74 {
75 key => 'dominoes',
76 module => 'Game::Dominoes',
77 class => 'Game::Dominoes::Terminal',
78 blurb => 'draw dominoes with All Fives scoring',
79 family => 'tile',
80 players => '2-4',
81 args => [],
82 aliases => [],
83 flags => { %ALL },
84 },
85 {
86 key => 'durak',
87 module => 'Game::Durak',
88 class => 'Game::Durak::Terminal',
89 blurb => 'Podkidnoy Durak, the Russian beating game',
90 family => 'card',
91 players => '2',
92 args => [],
93 aliases => [],
94 flags => { %ALL },
95 },
96 {
97 key => 'fanorona',
98 module => 'Game::Fanorona',
99 class => 'Game::Fanorona::Terminal',
100 blurb => 'the board game of Madagascar',
101 family => 'board',
102 players => '2',
103 args => [],
104 aliases => [],
105 flags => { ascii => '--no-unicode', pick => '--no-pick' },
106 },
107 {
108 key => 'floppy',
109 module => 'Game::Floppy',
110 class => 'Game::Floppy::Terminal',
111 blurb => 'a one-button bird through a field of tubes',
112 family => 'arcade',
113 players => '1',
114 args => [],
115 aliases => ['flappy'],
116 flags => { ascii => '--ascii' },
117 },
118 {
119 key => 'gin',
120 module => 'Game::Gin',
121 class => 'Game::Gin::Terminal',
122 blurb => 'gin rummy',
123 family => 'card',
124 players => '2',
125 args => [],
126 aliases => ['rummy'],
127 flags => { %ALL },
128 },
129 {
130 key => 'go',
131 module => 'Game::Go',
132 class => 'Game::Go::Terminal',
133 blurb => 'Go',
134 family => 'board',
135 players => '2',
136 args => [],
137 aliases => ['weiqi', 'baduk'],
138 flags => { %ALL },
139 },
140 {
141 key => 'mahjong',
142 module => 'Game::Mahjong',
143 class => 'Game::Mahjong::Terminal',
144 blurb => 'Mahjong, Chinese Official rules',
145 family => 'tile',
146 players => '4',
147 args => [],
148 aliases => [],
149 flags => { %ALL },
150 },
151 {
152 key => 'maze',
153 module => 'Term::Maze',
154 class => 'Term::Maze',
155 blurb => 'mazes in the terminal',
156 family => 'puzzle',
157 players => '1',
158 args => [],
159 aliases => [],
160 flags => {},
161 },
162 {
163 key => 'merrills',
164 module => 'Game::Merrills',
165 class => 'Game::Merrills::Terminal',
166 blurb => "Nine Men's Morris",
167 family => 'board',
168 players => '2',
169 args => [],
170 aliases => ['morris', 'mills', 'ninemensmorris'],
171 flags => { %ALL },
172 },
173 {
174 key => 'oware',
175 module => 'Game::Oware',
176 class => 'Game::Oware::Terminal',
177 blurb => 'the African sow and capture game, Abapa rules',
178 family => 'board',
179 players => '2',
180 args => [],
181 aliases => ['awale', 'awari'],
182 flags => { %ALL },
183 },
184 {
185 key => 'reversi',
186 module => 'Game::Reversi',
187 class => 'Game::Reversi::Terminal',
188 blurb => 'Reversi, with its 1883 opening',
189 family => 'board',
190 players => '2',
191 args => [],
192 aliases => ['othello'],
193 flags => { %ALL },
194 },
195 {
196 key => 'royalur',
197 module => 'Game::RoyalUr',
198 class => 'Game::RoyalUr::Terminal',
199 blurb => 'the Royal Game of Ur',
200 family => 'board',
201 players => '2',
202 args => [],
203 aliases => ['ur'],
204 flags => { %ALL },
205 },
206 {
207 key => 'schnapsen',
208 module => 'Game::Schnapsen',
209 class => 'Game::Schnapsen::Terminal',
210 blurb => 'Schnapsen',
211 family => 'card',
212 players => '2',
213 args => ['--variant', 'schnapsen'],
214 aliases => [],
215 flags => { %ALL },
216 },
217 {
218 key => 'senet',
219 module => 'Game::Senet',
220 class => 'Game::Senet::Terminal',
221 blurb => 'Senet, the race game of ancient Egypt',
222 family => 'board',
223 players => '2',
224 args => [],
225 aliases => [],
226 flags => { %ALL },
227 },
228 {
229 key => 'shogi',
230 module => 'Game::Shogi',
231 class => 'Game::Shogi::Terminal',
232 blurb => 'shogi, Japanese chess',
233 family => 'board',
234 players => '2',
235 args => [],
236 aliases => ['japanesechess'],
237 flags => { %ALL },
238 },
239 {
240 key => 'sixtysix',
241 module => 'Game::Schnapsen',
242 class => 'Game::Schnapsen::Terminal',
243 blurb => 'Sixty-Six, the German parent of Schnapsen',
244 family => 'card',
245 players => '2',
246 args => ['--variant', 'sixtysix'],
247 aliases => ['66'],
248 flags => { %ALL },
249 },
250 {
251 key => 'snake',
252 module => 'Game::Snake',
253 class => 'Game::Snake::Terminal',
254 blurb => 'the classic snake game',
255 family => 'arcade',
256 players => '1',
257 args => [],
258 aliases => [],
259 flags => { ascii => '--ascii' },
260 },
261 {
262 key => 'sternhalma',
263 module => 'Game::Sternhalma',
264 class => 'Game::Sternhalma::Terminal',
265 blurb => 'sternhalma, Chinese checkers, for two to six',
266 family => 'board',
267 players => '2-6',
268 args => [],
269 aliases => ['chinesecheckers'],
270 flags => { ascii => '--no-unicode', pick => '--no-pick' },
271 },
272 {
273 key => 'xiangqi',
274 module => 'Game::Xiangqi',
275 class => 'Game::Xiangqi::Terminal',
276 blurb => 'xiangqi, Chinese chess',
277 family => 'board',
278 players => '2',
279 args => [],
280 aliases => ['chinesechess'],
281 flags => { %ALL },
282 },
283 );
284  
285  
286 20 for my $row (@ROSTER) {
287 480 $BY_NAME{$row->{key}} = $row;
288 480 $BY_NAME{$_} = $row for @{$row->{aliases}};
289 }
290 }
291  
292 13 ▶sub games { return @ROSTER }
Subroutine games: called CC=1 SCAR=0.0
Pod documented
293  
294 11 ▶sub names { return map { $_->{key} } @ROSTER }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
295  
296 74 ▶sub normalise {
Subroutine normalise: called CC=2 SCAR=6.9
Pod documented
297 74 my ($name) = @_;
298 74 ▶ return '' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 72 74
299 72 my $clean = lc $name;
300 72 $clean =~ s/^game(?:::|-)//;
301 72 $clean =~ s/[^a-z0-9]//g;
302 72 return $clean;
303 }
304  
305 61 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
306 61 my ($name) = @_;
307 61 return $BY_NAME{normalise($name)};
308 }
309  
310 145 ▶sub distance {
Subroutine distance: called CC=6 SCAR=17.9
Pod documented
311 145 my ($a, $b) = @_;
312 145 my @a = split //, $a;
313 145 my @b = split //, $b;
314 145 my @previous = (0 .. @b);
315 145 for my $i (1 .. @a) {
316 1014 my @current = ($i);
317 1014 for my $j (1 .. @b) {
318 6678 ▶ my $cost = $a[$i - 1] eq $b[$j - 1] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$a[$i - 1] eq $b[$j - 1] ? : 364 6314 6678
319 6678 my $least = $previous[$j] + 1;
320 6678 ▶ $least = $current[$j - 1] + 1 if $current[$j - 1] + 1 < $least;
Branch: 1 branch
BranchTrueFalseTotal
if $current[$j - 1] + 1 < $least 2823 3855 6678
321 6678 ▶ $least = $previous[$j - 1] + $cost if $previous[$j - 1] + $cost < $least;
Branch: 1 branch
BranchTrueFalseTotal
if $previous[$j - 1] + $cost < $least 1093 5585 6678
322 6678 push @current, $least;
323 }
324 1014 @previous = @current;
325 }
326 145 return $previous[-1];
327 }
328  
329 8 ▶sub nearest {
Subroutine nearest: called CC=4 SCAR=13.9
Pod documented
330 8 my ($name) = @_;
331 8 my $clean = normalise($name);
332 8 ▶ return () unless length $clean;
Branch: 1 branch
BranchTrueFalseTotal
unless length $clean 1 7 8
333 7 my @keys = names();
334 7 my @prefix = grep { index($_, $clean) == 0 } @keys;
335 7 ▶ return @prefix if @prefix;
Branch: 1 branch
BranchTrueFalseTotal
if @prefix 1 6 7
336 6 my %score = map { $_ => distance($clean, $_) } @keys;
337 6 ▶ return grep { $score{$_} <= 3 } sort { $score{$a} <=> $score{$b} || $a cmp $b } @keys;
Condition66%2/3
$score{$a} <=> $score{$b} || $a cmp $b
l!l&&r!l&&!r
3131910
MC/DC0%0/2
$score{$a} <=> $score{$b} || $a cmp $b
$score{$a} <=> $score{$b} $a cmp $b
Truth table66%2/3
A || B
A= $score{$a} <=> $score{$b}B= $a cmp $b
ABresult
1X1
011
000
338 }
339  
340 252 ▶sub module_file {
Subroutine module_file: called CC=1 SCAR=0.0
Pod documented
341 252 my ($row) = @_;
342 252 (my $file = $row->{class} . '.pm') =~ s{::}{/}g;
343 252 return $file;
344 }
345  
346 236 ▶sub installed {
Subroutine installed: called CC=4 SCAR=13.9
Pod documented
347 236 my ($row, @inc) = @_;
348 236 ▶ @inc = @INC unless @inc;
Branch: 1 branch
BranchTrueFalseTotal
unless @inc 28 208 236
349 236 my $file = module_file($row);
350 236 for my $dir (grep { !ref } @inc) {
351 1064 ▶ return "$dir/$file" if -f "$dir/$file";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$dir/$file" 211 853 1064
352 }
353 25 return undef;
354 }
355  
356 108 ▶sub version_of {
Subroutine version_of: called CC=5 SCAR=16.5
Pod documented
357 108 my ($row, @inc) = @_;
358 108 my $path = installed($row, @inc);
359 108 ▶ return undef unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1 107 108
360 107 ▶ open my $fh, '<', $path or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return undef 107 0 107
361 107 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2103 0 2103
362 2103 ▶ return $1 if $line =~ m/^\s*our\s+\$VERSION\s*=\s*['"]([^'"]+)['"]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*our\s+\$VERSION\s*=\s*['"]([^'"]+)['"]/u 107 1996 2103
363 }
364 0 return undef;
365 }
366  
367 14 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
368 14 my ($row) = @_;
369 14 require(module_file($row));
370 13 return $row->{class};
371 }
372  
373 31 ▶sub flags_for {
Subroutine flags_for: called CC=7 SCAR=19.9
Pod documented
374 31 my ($row, $common) = @_;
375 31 ▶ $common ||= {};
Condition50%1/2
$common ||= {}
l!l
310
MC/DC0%0/1
$common ||= {}
$common
Truth table50%1/2
A
A= $common
Aresult
11
00
376 31 my @flags;
377 push @flags, $row->{flags}{ascii}
378 31 ▶ if $common->{ascii} && $row->{flags}{ascii};
Branch: 1 branch
BranchTrueFalseTotal
if $common->{"ascii"} and $row->{"flags"}{"ascii"} 3 28 31
Condition66%2/3
$common->{"ascii"} and $row->{"flags"}{"ascii"}
!ll&&!rl&&r
2803
MC/DC50%1/2
$common->{"ascii"} and $row->{"flags"}{"ascii"}
$common->{"ascii"} $row->{"flags"}{"ascii"}
Truth table66%2/3
A and B
A= $common->{"ascii"}B= $row->{"flags"}{"ascii"}
ABresult
0X0
100
111
379 push @flags, $row->{flags}{pick}
380 31 ▶ if defined $common->{pick} && !$common->{pick} && $row->{flags}{pick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $common->{"pick"} and not $common->{"pick"} and $row->{"flags"}{"pick"} 3 28 31
Condition66%4/6
defined $common->{"pick"} and not $common->{"pick"} and $row->{"flags"}{"pick"}
!ll&&!rl&&r
2803
defined $common->{"pick"} and not $common->{"pick"}
!ll&&!rl&&r
2803
MC/DC33%1/3
defined $common->{"pick"} and not $common->{"pick"} and $row->{"flags"}{"pick"}
defined $common->{"pick"} not $common->{"pick"} $row->{"flags"}{"pick"}
Truth table50%2/4
A and not(B) and C
A= defined $common->{"pick"}B= not $common->{"pick"}C= $row->{"flags"}{"pick"}
ABCresult
0XX0
10X0
1100
1111
381 31 return @flags;
382 }
383  
384 36 ▶sub options_for {
Subroutine options_for: called CC=6 SCAR=17.9
Pod documented
385 36 my ($row, %seam) = @_;
386 36 ▶ return $row->{options} if ref $row->{options} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row->{"options"} eq "ARRAY" 17 19 36
387 19 ▶ my $loader = $seam{loader} || \&load;
Condition100%2/2
$seam{'loader'} || \&Game::Terminal::load
l!l
109
MC/DC100%1/1
$seam{'loader'} || \&Game::Terminal::load
$seam{'loader'}
Truth table100%2/2
A
A= $seam{'loader'}
Aresult
11
00
388 19 my $class = eval { $loader->($row) };
389 19 ▶ return [] unless $class && $class->can('options');
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $class->can("options") 4 15 19
Condition100%3/3
$class and $class->can("options")
!ll&&!rl&&r
2215
MC/DC100%2/2
$class and $class->can("options")
$class $class->can("options")
Truth table100%3/3
A and B
A= $classB= $class->can("options")
ABresult
0X0
100
111
390 15 my $fields = eval { $class->options };
391 15 ▶ return ref $fields eq 'ARRAY' ? $fields : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'ARRAY' ? : 13 2 15
392 }
393  
394 1 ▶sub step_value {
Subroutine step_value: called CC=1 SCAR=0.0
Pod documented
395 1 return Game::Terminal::Kit::Options::step_value(@_);
396 }
397  
398 1 ▶sub check_value {
Subroutine check_value: called CC=1 SCAR=0.0
Pod documented
399 1 return Game::Terminal::Kit::Options::check_value(@_);
400 }
401  
402 5 ▶sub argv_for {
Subroutine argv_for: called CC=1 SCAR=0.0
Pod documented
403 5 my ($row, $values, %seam) = @_;
404 5 return Game::Terminal::Kit::Options::argv_for(options_for($row, %seam), $values);
405 }
406  
407 12 ▶sub launch {
Subroutine launch: called CC=11 SCAR=24.2
Pod documented
408 12 my ($row, $argv, %seam) = @_;
409 12 ▶ my $loader = $seam{loader} || \&load;
Condition100%2/2
$seam{'loader'} || \&Game::Terminal::load
l!l
75
MC/DC100%1/1
$seam{'loader'} || \&Game::Terminal::load
$seam{'loader'}
Truth table100%2/2
A
A= $seam{'loader'}
Aresult
11
00
410 12 ▶ my $out = $seam{out} || \*STDOUT;
Condition100%2/2
$seam{'out'} || \*STDOUT
l!l
111
MC/DC100%1/1
$seam{'out'} || \*STDOUT
$seam{'out'}
Truth table100%2/2
A
A= $seam{'out'}
Aresult
11
00
411 12 ▶ my @argv = (@{ $row->{args} || [] }, @{ $argv || [] });
Condition50%2/4
$row->{'args'} || []
l!l
120
$argv || []
l!l
120
MC/DC0%0/1
$row->{'args'} || []
$row->{'args'}
MC/DC0%0/1
$argv || []
$argv
Truth table50%1/2
A
A= $row->{'args'}
Aresult
11
00
Truth table50%1/2
A
A= $argv
Aresult
11
00
412  
413 12 my $getopt = Getopt::Long::Configure('default');
414 12 local @SIG{qw(INT TERM HUP)};
415 12 local $ENV{NO_COLOR} = $ENV{NO_COLOR};
416 12 ▶ $ENV{NO_COLOR} = 1 if $seam{no_colour};
Branch: 1 branch
BranchTrueFalseTotal
if $seam{"no_colour"} 1 11 12
417 12 my $depth = () = PerlIO::get_layers($out);
418 12 my $selected = select;
419 12 my $flush = do { select $out; my $was = $|; select $selected; $was };
420  
421 12 my ($status, $error);
422 12 my $ok = eval {
423 12 my $class = $loader->($row);
424 12 $status = $class->run(@argv);
425 9 1;
426 };
427 12 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 9 12
428 3 $error = $@;
429 3 ▶ $error = 'died without a message' unless defined $error && length $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error and length $error 0 3 3
Condition33%1/3
defined $error and length $error
!ll&&!rl&&r
003
MC/DC0%0/2
defined $error and length $error
defined $error length $error
Truth table33%1/3
A and B
A= defined $errorB= length $error
ABresult
0X0
100
111
430 3 $error =~ s/ at \S+ line \d+\.?\s*\z//;
431 3 $error =~ s/\s+\z//;
432 }
433  
434 12 select $selected;
435 12 { select $out; $| = $flush; select $selected }
436 12 ▶ binmode $out, ':pop' while (() = PerlIO::get_layers($out)) > $depth;
Branch: 1 branch
BranchTrueFalseTotal
(() = &PerlIO::get_layers($out)) > $depth and do { binmode $out, ":pop" } 1 12 13
437 12 Getopt::Long::Configure($getopt);
438 0 ▶ eval { Term::ReadKey::ReadMode(0) } if $INC{'Term/ReadKey.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Term/ReadKey.pm"} 0 12 12
439  
440 12 return ($status, $error);
441 }
442  
443 20 1;
444  
445 __END__
« blib/lib/Game/Terminal/Menu.pm ↑ Summary