2048/Input.pm

filter: Statementstmt 45.6% 21 / 46 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 5 totaltotal 36.0% 27 / 75 SCAR 32.0
CC
8
Cov
36%
read_key
20.0
key_vector
12.0
poll_key
6.0
CRAP 24.6
« 2048/Game.pm ↑ Summary 2048/Grid.pm »
1 package Games::2048::Input;
2 4 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use if $^O eq "MSWin32", "Win32::Console::ANSI";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶eval 'END { ReadMode "normal" }';
Subroutine END: called CC=1 SCAR=0.0
10 4 ReadMode "cbreak";
11  
12 # manual and automatic window size updating
13 4 my $_window_size;
14 4 eval { $SIG{WINCH} = \&update_window_size };
15  
16 sub read_key {
17 0 ▶ state @keys;
Subroutine read_key: not called CC=4 SCAR=30.0
Pod undocumented
18  
19 0 ▶ if (@keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) 0 0 0
20 0 return shift @keys;
21 }
22  
23 0 my $char;
24 0 my $packet = '';
25 0 ▶ while (defined($char = ReadKey -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = ReadKey(-1))) 0 0 0
26 0 $packet .= $char;
27 }
28  
29 0 ▶ while ($packet =~ m(
Branch: 1 branch
BranchTrueFalseTotal
if ($packet =~ / \G( \e \[ # CSI [\x30-\x3f]* # Parameter Bytes [\x20-\x2f]* # Intermediate Bytes [\x40-\x7e] # Final Byte | . # Otherwise just any character ) /gsux) 0 0 0
30 \G(
31 \e \[ # CSI
32 [\x30-\x3f]* # Parameter Bytes
33 [\x20-\x2f]* # Intermediate Bytes
34 [\x40-\x7e] # Final Byte
35 |
36 . # Otherwise just any character
37 )
38 )gsx) {
39 0 push @keys, $1;
40 }
41  
42 0 return shift @keys;
43 }
44  
45 sub poll_key {
46 0 ▶ while (1) {
Subroutine poll_key: not called CC=2 SCAR=17.9
Pod undocumented
47 0 my $key = read_key;
48 0 ▶ return $key if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
49 0 Time::HiRes::sleep(Games::2048::FRAME_TIME);
50 }
51 0 return;
52 }
53  
54 sub key_vector {
55 0 ▶ my ($key) = @_;
Subroutine key_vector: not called CC=3 SCAR=24.8
Pod undocumented
56 0 state $vectors = [ [0, -1], [0, 1], [1, 0], [-1, 0] ];
57 0 state $keys = [ map "\e[$_", "A".."D" ];
58 0 for (0..3) {
59 0 ▶ return $vectors->[$_] if $key eq $keys->[$_];
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $keys->[$_] 0 0 0
60 }
61 0 return;
62 }
63  
64 sub update_window_size {
65 0 ▶ ($_window_size) = eval { GetTerminalSize *STDOUT };
Subroutine update_window_size: not called CC=2 SCAR=17.9
Pod undocumented
66 0 ▶ $_window_size //= 80;
Condition0%0/2
$_window_size //= 80
l!l
00
MC/DC0%0/1
$_window_size //= 80
$_window_size
Truth table0%0/2
A
A= $_window_size
Aresult
11
00
67 }
68  
69 sub window_size {
70 0 ▶ $_window_size;
Subroutine window_size: not called CC=1 SCAR=6.9
Pod undocumented
71 }
72  
73 4 1;
« 2048/Game.pm ↑ Summary 2048/Grid.pm »