| 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
| |||||||||||||||||||||||||||||
| 68 | 73 | ▶ | $self->sequence_wait(0.05) unless defined $self->sequence_wait; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $self->in and -t $self->in
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 6 | ▶ | return 0 unless $self->_on_a_terminal; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | return eval { require Term::ReadKey; 1 } ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 95 | 10 | ▶ | if ($self->keysource) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 8 | $self->raw(1); | |||||||||||||||||||||||||||
| 97 | 8 | return $self; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | return undef unless $self->keys_available; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 108 | 8 | ▶ | eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 123 | 310 | ▶ | return $self->keysource->($wait) if $self->keysource; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | ▶ | unless ($self->raw) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | $self->sleeper->($wait) if $wait; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 2 | return undef; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $mode = !defined $wait ? 0 : $wait > 0 ? $wait : -1; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | 137 | ▶ | return $CONTROL{$char} if $CONTROL{$char}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 100 | ▶ | return $self->read_sequence if $char eq "\e"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 145 | 55 | ▶ | unless ($opener eq '[' || $opener eq 'O') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opener eq "[" or $opener eq "O"
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"
| |||||||||||||||||||||||||||||
| 146 | 2 | $self->unread($opener); | |||||||||||||||||||||||||||
| 147 | 2 | return 'escape'; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 53 | my $tail = ''; | |||||||||||||||||||||||||||
| 150 | 53 | ▶ | while (length $tail < 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 72 | my $char = $self->read_char($self->sequence_wait); | |||||||||||||||||||||||||||
| 152 | 72 | ▶ | last unless defined $char; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 71 | $tail .= $char; | |||||||||||||||||||||||||||
| 154 | 71 | ▶ | last if $char =~ m/[A-Za-z~]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 53 | ▶ | return $SEQUENCE{$tail} || 'unknown'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$SEQUENCE{$tail} || 'unknown'
MC/DC100%1/1
$SEQUENCE{$tail} || 'unknown'
$SEQUENCE{$tail}
Truth table100%2/2
A
A= $SEQUENCE{$tail}
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $prompt and $out
MC/DC100%2/2
defined $prompt and $out
defined $prompt
$out
Truth table100%3/3
A and B
A= defined $promptB= $out
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 173 | 5 | ▶ | return undef unless defined $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 4 | $line =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 175 | 4 | return $line; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 10 | 1; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||