blib/lib/Term/Fabulous/Event/KeyPress.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 210 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Event::KeyPress;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.05';
9  
10 use Object::Pad 0.825;
11  
12 use Clay::UI::Events::Event;
13  
14 class Term::Fabulous::Event::KeyPress :isa(Clay::UI::Events::Event) :strict(params) {
15 use Term::Fabulous::Termbox qw(:keys TB_MOD_ALT TB_MOD_CTRL TB_MOD_SHIFT TF_MOD_SUPER TF_MOD_HYPER TF_MOD_META);
16  
17 field $key :param :reader;
18 field $char :param :reader;
19 field $modifiers :param :reader;
20  
21 # termbox2 reports the keys of the ASCII control range with their byte as
22 # the key code.
23 my %NAME_BY_KEY = (
24 TB_KEY_BACKSPACE() => 'Backspace', # Ctrl+H, sent by some terminals for Backspace
25 TB_KEY_TAB() => 'Tab',
26 TB_KEY_ENTER() => 'Enter',
27 TB_KEY_ESC() => 'Escape',
28 TB_KEY_SPACE() => 'Space',
29 TB_KEY_BACKSPACE2() => 'Backspace',
30 TB_KEY_ARROW_LEFT() => 'Left',
31 TB_KEY_ARROW_RIGHT() => 'Right',
32 TB_KEY_ARROW_UP() => 'Up',
33 TB_KEY_ARROW_DOWN() => 'Down',
34 TB_KEY_HOME() => 'Home',
35 TB_KEY_END() => 'End',
36 TB_KEY_PGUP() => 'PageUp',
37 TB_KEY_PGDN() => 'PageDown',
38 TB_KEY_INSERT() => 'Insert',
39 TB_KEY_DELETE() => 'Delete',
40 TB_KEY_BACK_TAB() => 'BackTab',
41 TB_KEY_F1() => 'F1',
42 TB_KEY_F2() => 'F2',
43 TB_KEY_F3() => 'F3',
44 TB_KEY_F4() => 'F4',
45 TB_KEY_F5() => 'F5',
46 TB_KEY_F6() => 'F6',
47 TB_KEY_F7() => 'F7',
48 TB_KEY_F8() => 'F8',
49 TB_KEY_F9() => 'F9',
50 TB_KEY_F10() => 'F10',
51 TB_KEY_F11() => 'F11',
52 TB_KEY_F12() => 'F12',
53  
54 # Keys only the kitty keyboard protocol reports.
55 TF_KEY_CAPS_LOCK() => 'CapsLock',
56 TF_KEY_SCROLL_LOCK() => 'ScrollLock',
57 TF_KEY_NUM_LOCK() => 'NumLock',
58 TF_KEY_PRINT_SCREEN() => 'PrintScreen',
59 TF_KEY_PAUSE() => 'Pause',
60 TF_KEY_MENU() => 'Menu',
61 TF_KEY_F13() => 'F13',
62 TF_KEY_F14() => 'F14',
63 TF_KEY_F15() => 'F15',
64 TF_KEY_F16() => 'F16',
65 TF_KEY_F17() => 'F17',
66 TF_KEY_F18() => 'F18',
67 TF_KEY_F19() => 'F19',
68 TF_KEY_F20() => 'F20',
69 TF_KEY_F21() => 'F21',
70 TF_KEY_F22() => 'F22',
71 TF_KEY_F23() => 'F23',
72 TF_KEY_F24() => 'F24',
73 TF_KEY_F25() => 'F25',
74 TF_KEY_F26() => 'F26',
75 TF_KEY_F27() => 'F27',
76 TF_KEY_F28() => 'F28',
77 TF_KEY_F29() => 'F29',
78 TF_KEY_F30() => 'F30',
79 TF_KEY_F31() => 'F31',
80 TF_KEY_F32() => 'F32',
81 TF_KEY_F33() => 'F33',
82 TF_KEY_F34() => 'F34',
83 TF_KEY_F35() => 'F35',
84 TF_KEY_KP_0() => 'Keypad0',
85 TF_KEY_KP_1() => 'Keypad1',
86 TF_KEY_KP_2() => 'Keypad2',
87 TF_KEY_KP_3() => 'Keypad3',
88 TF_KEY_KP_4() => 'Keypad4',
89 TF_KEY_KP_5() => 'Keypad5',
90 TF_KEY_KP_6() => 'Keypad6',
91 TF_KEY_KP_7() => 'Keypad7',
92 TF_KEY_KP_8() => 'Keypad8',
93 TF_KEY_KP_9() => 'Keypad9',
94 TF_KEY_KP_DECIMAL() => 'KeypadDecimal',
95 TF_KEY_KP_DIVIDE() => 'KeypadDivide',
96 TF_KEY_KP_MULTIPLY() => 'KeypadMultiply',
97 TF_KEY_KP_SUBTRACT() => 'KeypadSubtract',
98 TF_KEY_KP_ADD() => 'KeypadAdd',
99 TF_KEY_KP_ENTER() => 'KeypadEnter',
100 TF_KEY_KP_EQUAL() => 'KeypadEqual',
101 TF_KEY_KP_SEPARATOR() => 'KeypadSeparator',
102 TF_KEY_KP_LEFT() => 'KeypadLeft',
103 TF_KEY_KP_RIGHT() => 'KeypadRight',
104 TF_KEY_KP_UP() => 'KeypadUp',
105 TF_KEY_KP_DOWN() => 'KeypadDown',
106 TF_KEY_KP_PAGE_UP() => 'KeypadPageUp',
107 TF_KEY_KP_PAGE_DOWN() => 'KeypadPageDown',
108 TF_KEY_KP_HOME() => 'KeypadHome',
109 TF_KEY_KP_END() => 'KeypadEnd',
110 TF_KEY_KP_INSERT() => 'KeypadInsert',
111 TF_KEY_KP_DELETE() => 'KeypadDelete',
112 TF_KEY_KP_BEGIN() => 'KeypadBegin',
113 TF_KEY_MEDIA_PLAY() => 'MediaPlay',
114 TF_KEY_MEDIA_PAUSE() => 'MediaPause',
115 TF_KEY_MEDIA_PLAY_PAUSE() => 'MediaPlayPause',
116 TF_KEY_MEDIA_REVERSE() => 'MediaReverse',
117 TF_KEY_MEDIA_STOP() => 'MediaStop',
118 TF_KEY_MEDIA_FAST_FORWARD() => 'MediaFastForward',
119 TF_KEY_MEDIA_REWIND() => 'MediaRewind',
120 TF_KEY_MEDIA_TRACK_NEXT() => 'MediaTrackNext',
121 TF_KEY_MEDIA_TRACK_PREVIOUS() => 'MediaTrackPrevious',
122 TF_KEY_MEDIA_RECORD() => 'MediaRecord',
123 TF_KEY_LOWER_VOLUME() => 'LowerVolume',
124 TF_KEY_RAISE_VOLUME() => 'RaiseVolume',
125 TF_KEY_MUTE_VOLUME() => 'MuteVolume',
126 );
127  
128 # The keypad keys the kitty keyboard protocol tells apart, and the
129 # main keyboard keys legacy terminals report for them instead.
130 my %MAIN_NAME_BY_KEYPAD_NAME = (
131 Keypad0 => '0',
132 Keypad1 => '1',
133 Keypad2 => '2',
134 Keypad3 => '3',
135 Keypad4 => '4',
136 Keypad5 => '5',
137 Keypad6 => '6',
138 Keypad7 => '7',
139 Keypad8 => '8',
140 Keypad9 => '9',
141 KeypadDecimal => '.',
142 KeypadDivide => '/',
143 KeypadMultiply => '*',
144 KeypadSubtract => '-',
145 KeypadAdd => '+',
146 KeypadEnter => 'Enter',
147 KeypadEqual => '=',
148 KeypadSeparator => ',',
149 KeypadLeft => 'Left',
150 KeypadRight => 'Right',
151 KeypadUp => 'Up',
152 KeypadDown => 'Down',
153 KeypadPageUp => 'PageUp',
154 KeypadPageDown => 'PageDown',
155 KeypadHome => 'Home',
156 KeypadEnd => 'End',
157 KeypadInsert => 'Insert',
158 KeypadDelete => 'Delete',
159 );
160  
161 # The key codes by name; Backspace is the code terminals send for it.
162 my %KEY_BY_NAME = ( reverse(%NAME_BY_KEY), Backspace => TB_KEY_BACKSPACE2 );
163  
164 # The modifiers in the order key_name writes them.
165 my @MODIFIER_NAMES = qw(Ctrl Alt Shift Super Hyper Meta);
166 my %MODIFIER_BY_NAME = (
167 Ctrl => TB_MOD_CTRL,
168 Alt => TB_MOD_ALT,
169 Shift => TB_MOD_SHIFT,
170 Super => TF_MOD_SUPER,
171 Hyper => TF_MOD_HYPER,
172 Meta => TF_MOD_META,
173 );
174 my $MODIFIER_PREFIX = join '|', @MODIFIER_NAMES;
175  
176 method event_name :common { 'KeyPress' }
177  
178 method of :common ($ev) {
179 return $class->new(
180 key => $ev->key,
181 char => $ev->ch,
182 modifiers => $ev->mod,
183 );
184 }
185  
186 sub _is_control_byte ($code) {
187 return $code < 0x20 || $code == 0x7F;
188 }
189  
190 method fields_for_name :common ($name) {
191 my ( $prefix, $base ) = ( $name // '' ) =~ /\A((?:(?:$MODIFIER_PREFIX)\+)*)(.+)\z/s;
192 my $modifiers = 0;
193 $modifiers |= $MODIFIER_BY_NAME{$_} foreach split /\+/, $prefix // '';
194 my %fields = defined $base ? _fields_of_base( $base, $modifiers ) : ();
195  
196 # The tables decide: a name is valid when the event reads back as it.
197 my $event = %fields ? $class->new( key => $fields{key}, char => $fields{ch}, modifiers => $fields{mod} ) : undef;
198 die "Term::Fabulous::Event::KeyPress: no key is named " . ( defined $name ? "'$name'" : 'undef' ) . "; use the names key_name returns, such as 'Ctrl+Shift+Left' or 'a'\n"
199 unless defined $event && ( $event->key_name // '' ) eq $name;
200 return %fields;
201 }
202  
203 # What termbox2 reports for a key: a named key by its code, Ctrl plus a
204 # letter or symbol by its control byte, any other character by itself.
205 # termbox2 sets the Ctrl bit on every control byte, and the kitty
206 # keyboard protocol puts a Ctrl combination the control range cannot
207 # carry (Ctrl+I, Ctrl+Enter) into the character.
208 sub _fields_of_base ( $base, $modifiers ) {
209 my $ctrl = $modifiers & TB_MOD_CTRL;
210 if ( $base eq 'Space' ) {
211 return ( key => 0, ch => $ctrl ? 0 : 0x20, mod => $modifiers );
212 }
213 if ( exists $KEY_BY_NAME{$base} ) {
214 my $code = $KEY_BY_NAME{$base};
215 return ( key => 0, ch => $code, mod => $modifiers ) if $ctrl && _is_control_byte($code);
216 return ( key => $code, ch => 0, mod => _is_control_byte($code) ? $modifiers | TB_MOD_CTRL : $modifiers );
217 }
218 return () unless length $base == 1;
219  
220 my $control_byte = ord($base) - 0x40;
221 return ( key => $control_byte, ch => 0, mod => $modifiers )
222 if $ctrl && $control_byte > 0 && $control_byte < 0x20 && !exists $NAME_BY_KEY{$control_byte};
223 return ( key => 0, ch => ord( $ctrl ? lc $base : $base ), mod => $modifiers );
224 }
225  
226 # The key without its modifiers, and whether it implies Ctrl: the
227 # unnamed bytes of the control range are Ctrl plus a letter or symbol.
228 method _base_key () {
229 return ( 'Space', 0 ) if $char == 0x20;
230 return ( undef, 0 ) if $char >= 0x80 && $char <= 0x9F; # C1 control characters have no name
231 return ( $self->_base_of_char, 0 ) if $char != 0;
232 return ( $NAME_BY_KEY{$key}, 0 ) if exists $NAME_BY_KEY{$key};
233 return ( 'Space', 1 ) if $key == 0x00;
234 return ( chr( $key + 0x40 ), 1 ) if $key < 0x20;
235 return ( undef, 0 );
236 }
237  
238 # The kitty keyboard protocol puts the key of a Ctrl combination the
239 # legacy encoding cannot carry into the char: a named control key
240 # (Ctrl+Enter), or a character, named in upper case like Ctrl+W.
241 method _base_of_char () {
242 return $NAME_BY_KEY{$char} if _is_control_byte($char);
243 my $character = chr $char;
244 return $character unless $modifiers & TB_MOD_CTRL;
245 my $upper = uc $character;
246 return length($upper) == 1 ? $upper : $character; # 'ß' would become 'SS'
247 }
248  
249 # termbox2 sets TB_MOD_CTRL on every byte of the control range, Enter
250 # and Tab included; there the byte alone says whether Ctrl was held.
251 method _with_modifiers ( $base, $implies_ctrl ) {
252 my $ctrl = $char == 0 && _is_control_byte($key) ? $implies_ctrl : $modifiers & TB_MOD_CTRL;
253 my @names = grep { $_ eq 'Ctrl' ? $ctrl : $modifiers & $MODIFIER_BY_NAME{$_} } @MODIFIER_NAMES;
254 return join '+', @names, $base;
255 }
256  
257 method key_name () {
258 my ( $base, $implies_ctrl ) = $self->_base_key;
259 return undef unless defined $base;
260 return $self->_with_modifiers( $base, $implies_ctrl );
261 }
262  
263 method main_key_name () {
264 my ( $base, $implies_ctrl ) = $self->_base_key;
265 return undef unless defined $base;
266 return $self->_with_modifiers( $MAIN_NAME_BY_KEYPAD_NAME{$base} // $base, $implies_ctrl );
267 }
268  
269 method text () {
270 return undef if $modifiers & ( TB_MOD_CTRL | TB_MOD_ALT | TF_MOD_SUPER | TF_MOD_HYPER | TF_MOD_META );
271 return chr($char) if $char >= 0x20 && ( $char < 0x7F || $char > 0x9F );
272 return ' ' if $char == 0 && $key == 0x20;
273 return undef;
274 }
275 }
276  
277 1;
278  
279 __END__