Kit/Keys.pm

filter: Statementstmt 89.5% 94 / 105 Branchbran 80.0% 48 / 60 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 93.7% 15 / 16 Podpod 90.0% 9 / 10 totaltotal 86.1% 180 / 209 CC 35 SCAR 16.5
CC
35
Cov
86%
read_char
8.4
read_sequence
8.0
enter_raw
6.9
CRAP 5.2
« Kit/Screen.pm ↑ Summary Kit/Run.pm »
1 package Game::Terminal::Kit::Keys;
2  
3 10 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 our $VERSION = '0.02';
10  
11 10 my %SEQUENCE = (
12 'A' => 'up',
13 'B' => 'down',
14 'C' => 'right',
15 'D' => 'left',
16 'H' => 'home',
17 'F' => 'end',
18 'Z' => 'back_tab',
19 '1~' => 'home',
20 '2~' => 'insert',
21 '3~' => 'delete',
22 '4~' => 'end',
23 '5~' => 'page_up',
24 '6~' => 'page_down',
25 '7~' => 'home',
26 '8~' => 'end',
27 );
28  
29 10 my %CONTROL = (
30 "\r" => 'enter',
31 "\n" => 'enter',
32 "\t" => 'tab',
33 "\x7f" => 'backspace',
34 "\x08" => 'backspace',
35 "\x03" => 'interrupt',
36 "\x04" => 'eof',
37 );
38  
39 has in => (
40 is => 'rw'
41 10 );
42  
43 has raw => (
44 is => 'rw',
45 isa => Bool,
46 default => 0
47 10 );
48  
49 has [qw/keysource sleeper/] => (
50 is => 'rw',
51 isa => CodeRef
52 10 );
53  
54 has pending => (
55 is => 'rw',
56 isa => ArrayRef,
57 default => []
58 10 );
59  
60 has sequence_wait => (
61 is => 'rw',
62 isa => Num
63 10 );
64  
65 73 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.3
Pod undocumented
66 73 my ($self) = @_;
67 73 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 44 29 73
68 73 ▶ $self->sequence_wait(0.05) unless defined $self->sequence_wait;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sequence_wait 73 0 73
69 0 ▶ $self->sleeper(sub { select undef, undef, undef, $_[0]; return })
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 73 ▶ unless $self->sleeper;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sleeper 72 1 73
71 73 return $self;
72 }
73  
74 1 ▶sub names {
Subroutine names: called CC=1 SCAR=0.0
Pod documented
75 1 my %seen;
76 1 return sort grep { !$seen{$_}++ }
77 values %SEQUENCE, values %CONTROL, 'escape', 'unknown';
78 }
79  
80 4 ▶sub _on_a_terminal {
Subroutine _on_a_terminal: called CC=3 SCAR=13.1
81 4 my ($self) = @_;
82 4 ▶ return defined $self->in && -t $self->in ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->in && -t $self->in ? : 0 4 4
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
040
MC/DC0%0/2
defined $self->in and -t $self->in
defined $self->in -t $self->in
Truth table33%1/3
A and B
A= defined $self->inB= -t $self->in
ABresult
0X0
100
111
83 }
84  
85 8 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=14.0
Pod documented
86 8 my ($self) = @_;
87 8 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 2 6 8
88 6 ▶ return 0 unless $self->_on_a_terminal;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_on_a_terminal 4 2 6
89 2 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 2 2
90 }
91  
92 11 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=19.3
Pod documented
93 11 my ($self) = @_;
94 11 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 1 10 11
95 10 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 8 2 10
96 8 $self->raw(1);
97 8 return $self;
98 }
99 2 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 2 0 2
100 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->in); 1 } } 0 0 0
101 0 $self->raw(1);
102 0 return $self;
103 }
104  
105 14 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.2
Pod documented
106 14 my ($self) = @_;
107 14 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 6 8 14
108 8 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 8 8
109 8 $self->raw(0);
110 8 return $self;
111 }
112  
113 4 ▶sub unread {
Subroutine unread: called CC=1 SCAR=0.0
Pod documented
114 4 my ($self, @chars) = @_;
115 4 unshift @{$self->pending}, @chars;
116 4 return $self;
117 }
118  
119 315 ▶sub read_char {
Subroutine read_char: called CC=7 SCAR=21.3
Pod documented
120 315 my ($self, $wait) = @_;
121 315 my $pending = $self->pending;
122 315 ▶ return shift @{$pending} if @{$pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pending;} 5 310 315
123 310 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 308 2 310
124 2 ▶ unless ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->raw) 2 0 2
125 2 ▶ $self->sleeper->($wait) if $wait;
Branch: 1 branch
BranchTrueFalseTotal
if $wait 1 1 2
126 2 return undef;
127 }
128 0 ▶ my $mode = !defined $wait ? 0 : $wait > 0 ? $wait : -1;
Branch: 2 branches
BranchTrueFalseTotal
!defined($wait) ? : 0 0 0
$wait > 0 ? : 0 0 0
129 0 return Term::ReadKey::ReadKey($mode, $self->in);
130 }
131  
132 184 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
133 184 my ($self, $wait) = @_;
134 184 my $char = $self->read_char($wait);
135 184 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 47 137 184
136 137 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CONTROL{$char} 37 100 137
137 100 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 59 41 100
138 41 return $char;
139 }
140  
141 59 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=20.8
Pod documented
142 59 my ($self) = @_;
143 59 my $opener = $self->read_char($self->sequence_wait);
144 59 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 4 55 59
145 55 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 53 55
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
4762
MC/DC100%2/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table100%3/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
146 2 $self->unread($opener);
147 2 return 'escape';
148 }
149 53 my $tail = '';
150 53 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 72 1 73
151 72 my $char = $self->read_char($self->sequence_wait);
152 72 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 71 72
153 71 $tail .= $char;
154 71 ▶ last if $char =~ m/[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/u 51 20 71
155 }
156 53 ▶ return $SEQUENCE{$tail} || 'unknown';
Condition100%2/2
$SEQUENCE{$tail} || 'unknown'
l!l
494
MC/DC100%1/1
$SEQUENCE{$tail} || 'unknown'
$SEQUENCE{$tail}
Truth table100%2/2
A
A= $SEQUENCE{$tail}
Aresult
11
00
157 }
158  
159 5 ▶sub read_line {
Subroutine read_line: called CC=5 SCAR=16.1
Pod documented
160 5 my ($self, $prompt, $out) = @_;
161 5 my $was_raw = $self->raw;
162 5 $self->leave_raw;
163 5 ▶ if (defined $prompt && $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prompt and $out) 1 4 5
Condition100%3/3
defined $prompt and $out
!ll&&!rl&&r
311
MC/DC100%2/2
defined $prompt and $out
defined $prompt $out
Truth table100%3/3
A and B
A= defined $promptB= $out
ABresult
0X0
100
111
164 1 my $previous = select $out;
165 1 my $buffered = $|;
166 1 print {$out} $prompt;
167 1 $| = 1;
168 1 $| = $buffered;
169 1 select $previous;
170 }
171 5 my $line = readline $self->in;
172 5 ▶ $self->enter_raw if $was_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $was_raw 1 4 5
173 5 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 4 5
174 4 $line =~ s/^\s+|\s+$//g;
175 4 return $line;
176 }
177  
178 10 1;
179  
180 __END__
« Kit/Screen.pm ↑ Summary Kit/Run.pm »