Kit/Keys.pm

filter: Statementstmt 92.1% 129 / 140 Branchbran 87.2% 82 / 94 Conditioncond 85.0% 17 / 20 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 94.7% 18 / 19 Podpod 90.0% 9 / 10 totaltotal 89.5% 265 / 296 CC 56 SCAR 19.8
CC
56
Cov
90%
read_sequence
13.0
_mouse_event
12.0
read_char
8.4
CRAP 7.3
« Kit/Canvas.pm ↑ Summary Kit/Line.pm »
1 package Game::Terminal::Kit::Keys;
2  
3 12 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 our $VERSION = '0.06';
10  
11 12 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 12 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 12 );
42  
43 has raw => (
44 is => 'rw',
45 isa => Bool,
46 default => 0
47 12 );
48  
49 has [qw/keysource sleeper/] => (
50 is => 'rw',
51 isa => CodeRef
52 12 );
53  
54 has pending => (
55 is => 'rw',
56 isa => ArrayRef,
57 default => []
58 12 );
59  
60 has sequence_wait => (
61 is => 'rw',
62 isa => Num
63 12 );
64  
65 has [qw/mouse held/] => (
66 is => 'rw'
67 12 );
68  
69 110 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.3
Pod undocumented
70 110 my ($self) = @_;
71 110 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 69 41 110
72 110 ▶ $self->sequence_wait(0.05) unless defined $self->sequence_wait;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sequence_wait 110 0 110
73 0 ▶ $self->sleeper(sub { select undef, undef, undef, $_[0]; return })
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 110 ▶ unless $self->sleeper;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sleeper 109 1 110
75 110 return $self;
76 }
77  
78 1 ▶sub names {
Subroutine names: called CC=1 SCAR=0.0
Pod documented
79 1 my %seen;
80 1 return sort grep { !$seen{$_}++ }
81 values %SEQUENCE, values %CONTROL, 'escape', 'mouse', 'unknown';
82 }
83  
84 5 ▶sub _on_a_terminal {
Subroutine _on_a_terminal: called CC=3 SCAR=13.1
85 5 my ($self) = @_;
86 5 ▶ return defined $self->in && -t $self->in ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->in && -t $self->in ? : 0 5 5
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
050
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
87 }
88  
89 9 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=14.0
Pod documented
90 9 my ($self) = @_;
91 9 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 2 7 9
92 7 ▶ return 0 unless $self->_on_a_terminal;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_on_a_terminal 5 2 7
93 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 2 2
94 }
95  
96 17 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=19.3
Pod documented
97 17 my ($self) = @_;
98 17 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 1 16 17
99 16 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 13 3 16
100 13 $self->raw(1);
101 13 return $self;
102 }
103 3 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 3 0 3
104 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
105 0 $self->raw(1);
106 0 return $self;
107 }
108  
109 18 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.2
Pod documented
110 18 my ($self) = @_;
111 18 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 9 9 18
112 0 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 9 9
113 9 $self->raw(0);
114 9 return $self;
115 }
116  
117 5 ▶sub unread {
Subroutine unread: called CC=1 SCAR=0.0
Pod documented
118 5 my ($self, @chars) = @_;
119 5 unshift @{$self->pending}, @chars;
120 5 return $self;
121 }
122  
123 679 ▶sub read_char {
Subroutine read_char: called CC=7 SCAR=21.3
Pod documented
124 679 my ($self, $wait) = @_;
125 679 my $pending = $self->pending;
126 679 ▶ return shift @{$pending} if @{$pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pending;} 7 672 679
127 672 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 670 2 672
128 2 ▶ unless ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->raw) 2 0 2
129 2 ▶ $self->sleeper->($wait) if $wait;
Branch: 1 branch
BranchTrueFalseTotal
if $wait 1 1 2
130 2 return undef;
131 }
132 0 ▶ my $mode = !defined $wait ? 0 : $wait > 0 ? $wait : -1;
Branch: 2 branches
BranchTrueFalseTotal
!defined($wait) ? : 0 0 0
$wait > 0 ? : 0 0 0
133 0 return Term::ReadKey::ReadKey($mode, $self->in);
134 }
135  
136 271 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
137 271 my ($self, $wait) = @_;
138 271 my $char = $self->read_char($wait);
139 271 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 51 220 271
140 220 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CONTROL{$char} 53 167 220
141 167 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 97 70 167
142 70 return $char;
143 }
144  
145 97 ▶sub read_sequence {
Subroutine read_sequence: called CC=13 SCAR=25.6
Pod documented
146 97 my ($self) = @_;
147 97 my $opener = $self->read_char($self->sequence_wait);
148 97 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 5 92 97
149 92 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 90 92
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
8462
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
150 2 $self->unread($opener);
151 2 return 'escape';
152 }
153 90 my $tail = '';
154 90 my $limit = 8;
155 90 ▶ while (length $tail < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < $limit) 293 1 294
156 293 my $char = $self->read_char($self->sequence_wait);
157 293 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 2 291 293
158 291 $tail .= $char;
159 291 ▶ $limit = 32 if $opener eq '[' && $tail eq '<';
Branch: 1 branch
BranchTrueFalseTotal
if $opener eq "[" and $tail eq "<" 26 265 291
Condition100%3/3
$opener eq "[" and $tail eq "<"
!ll&&!rl&&r
625926
MC/DC100%2/2
$opener eq "[" and $tail eq "<"
$opener eq "[" $tail eq "<"
Truth table100%3/3
A and B
A= $opener eq "["B= $tail eq "<"
ABresult
0X0
100
111
160 291 ▶ last if $char =~ m/[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/u 87 204 291
161 }
162 90 ▶ if ($opener eq '[') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opener eq "[") 84 6 90
163 84 ▶ return $self->_sgr_mouse($tail) if $tail =~ m/\A</;
Branch: 1 branch
BranchTrueFalseTotal
if $tail =~ /\A</u 26 58 84
164 58 ▶ return $self->_legacy_mouse if $tail eq 'M';
Branch: 1 branch
BranchTrueFalseTotal
if $tail eq "M" 6 52 58
165 }
166 58 ▶ return $SEQUENCE{$tail} || 'unknown';
Condition100%2/2
$SEQUENCE{$tail} || 'unknown'
l!l
544
MC/DC100%1/1
$SEQUENCE{$tail} || 'unknown'
$SEQUENCE{$tail}
Truth table100%2/2
A
A= $SEQUENCE{$tail}
Aresult
11
00
167 }
168  
169 26 ▶sub _sgr_mouse {
Subroutine _sgr_mouse: called CC=2 SCAR=6.9
170 26 my ($self, $tail) = @_;
171 26 ▶ return 'unknown' unless $tail =~ m/\A<(\d+);(\d+);(\d+)([Mm])\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tail =~ /\A<(\d+);(\d+);(\d+)([Mm])\z/u 5 21 26
172 21 return $self->_mouse_event($1, $2, $3, $4 eq 'm');
173 }
174  
175 6 ▶sub _legacy_mouse {
Subroutine _legacy_mouse: called CC=5 SCAR=16.1
176 6 my ($self) = @_;
177 6 my @chars;
178 6 for (1 .. 3) {
179 18 my $char = $self->read_char($self->sequence_wait);
180 18 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 17 18
181 17 push @chars, $char;
182 }
183 6 ▶ if (@chars < 3 || grep { ord $_ < 32 } @chars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chars < 3 or grep {ord $_ < 32;} @chars) 1 5 6
Condition66%2/3
@chars < 3 or grep {ord $_ < 32;} @chars
l!l&&r!l&&!r
105
MC/DC50%1/2
@chars < 3 or grep {ord $_ < 32;} @chars
@chars < 3 grep {ord $_ < 32;} @chars
Truth table66%2/3
A or B
A= @chars < 3B= grep {ord $_ < 32;} @chars
ABresult
1X1
011
000
184 1 $self->unread(@chars);
185 1 return 'unknown';
186 }
187 5 my ($code, $column, $row) = map { ord($_) - 32 } @chars;
188 5 return $self->_mouse_event($code, $column, $row, ($code & 99) == 3);
189 }
190  
191 26 ▶sub _mouse_event {
Subroutine _mouse_event: called CC=12 SCAR=24.8
192 26 my ($self, $code, $column, $row, $released) = @_;
193 26 ▶ return 'unknown' if $code & 128;
Branch: 1 branch
BranchTrueFalseTotal
if $code & 128 1 25 26
194 25 my $button = $code & 3;
195 25 my ($type, $name);
196 25 ▶ if ($code & 64) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code & 64) { } 6 19 25
elsif ($code & 32) { } 7 12 19
197 6 ($type, $name) = ('scroll', (qw/up down left right/)[$button]);
198 } elsif ($code & 32) {
199 7 ▶ ($type, $name) = $button == 3 ? ('move', undef) : ('drag', (qw/left middle right/)[$button]);
Branch: 1 branch
BranchTrueFalseTotal
$button == 3 ? : 3 4 7
200 } else {
201 12 ▶ ($type, $name) = ($released ? 'release' : 'press', (qw/left middle right/)[$button]);
Branch: 1 branch
BranchTrueFalseTotal
$released ? : 4 8 12
202 }
203 25 ▶ $self->mouse({
Branch: 3 branches
BranchTrueFalseTotal
$code & 4 ? : 1 24 25
$code & 8 ? : 1 24 25
$code & 16 ? : 1 24 25
204 type => $type,
205 button => $name,
206 column => $column + 0,
207 row => $row + 0,
208 shift => $code & 4 ? 1 : 0,
209 alt => $code & 8 ? 1 : 0,
210 control => $code & 16 ? 1 : 0,
211 });
212 25 ▶ if ($type eq 'press' || $type eq 'drag') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'press' or $type eq 'drag') { } 12 13 25
elsif ($type eq 'release') { } 4 9 13
Condition100%3/3
$type eq "press" or $type eq "drag"
l!l&&r!l&&!r
8413
MC/DC100%2/2
$type eq "press" or $type eq "drag"
$type eq "press" $type eq "drag"
Truth table100%3/3
A or B
A= $type eq "press"B= $type eq "drag"
ABresult
1X1
011
000
213 12 $self->held({ button => $name, column => $column + 0, row => $row + 0 });
214 } elsif ($type eq 'release') {
215 4 $self->held(undef);
216 }
217 25 return 'mouse';
218 }
219  
220 8 ▶sub read_line {
Subroutine read_line: called CC=5 SCAR=16.1
Pod documented
221 8 my ($self, $prompt, $out) = @_;
222 8 my $was_raw = $self->raw;
223 8 $self->leave_raw;
224 8 ▶ if (defined $prompt && $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prompt and $out) 4 4 8
Condition100%3/3
defined $prompt and $out
!ll&&!rl&&r
314
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
225 4 my $previous = select $out;
226 4 my $buffered = $|;
227 4 print {$out} $prompt;
228 4 $| = 1;
229 4 $| = $buffered;
230 4 select $previous;
231 }
232 8 my $line = readline $self->in;
233 8 ▶ $self->enter_raw if $was_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $was_raw 1 7 8
234 8 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 6 8
235 6 $line =~ s/^\s+|\s+$//g;
236 6 return $line;
237 }
238  
239 12 1;
240  
241 __END__
« Kit/Canvas.pm ↑ Summary Kit/Line.pm »