lib/Termbox/PP.pm

filter: Statementstmt 76.5% 1399 / 1828 Branchbran 46.6% 403 / 864 Conditioncond 31.0% 129 / 415 MC/DCmcdc 9.2% 20 / 216 Subroutinesub 91.3% 212 / 232 Podpod 0.0% 0 / 106 totaltotal 59.0% 2163 / 3661 CC 604 SCAR 53.4
CC
604
Cov
62%
wcwidth
2450.0
send_attr
953.3
send_sgr
140.3
CRAP 208.6
↑ Summary lib/Terminal/WCWidth.pm »
1 # ------------------------------------------------------------------------
2 #
3 # Termbox (Perl port)
4 #
5 # Implementation based on termbox2 v2.7.0-dev, 8. Feb 2026
6 #
7 # Copyright (C) 2015-2026 Adam Saponara <as@php.net>
8 # 2010-2020 nsf <no.smile.face@gmail.com>
9 #
10 # ------------------------------------------------------------------------
11 # Author: 2024-2026 J. Schneider
12 # ------------------------------------------------------------------------
13
14 package Termbox::PP;
15 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶return 1 if $INC{"Termbox.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Termbox.pm"} 0 39 39
18 package # hide from PAUSE
19 Termbox; ## no_index
20
21 # ------------------------------------------------------------------------
22 # Boilerplate ------------------------------------------------------------
23 # ------------------------------------------------------------------------
24
25 39 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28
29 # version '...'
30 39 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 39 our $version = version->declare('v2.7.0_0');
32 39 our $VERSION = version->declare('v0.6.1');
33
34 # authority '...'
35 39 our $authority = 'github:adsr';
36 39 our $AUTHORITY = 'github:brickpool';
37
38 # ------------------------------------------------------------------------
39 # Imports ----------------------------------------------------------------
40 # ------------------------------------------------------------------------
41
42 39 require bytes;
43 39 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 39 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 39 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 39 ▶use Params::Check ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 39 ▶use POSIX qw( :termios_h :fcntl_h );
Subroutine BEGIN: called CC=1 SCAR=0.0
48 39 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
49 39 ▶use Unicode::UCD ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 39 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
51
52 # ------------------------------------------------------------------------
53 # Compile-time options ---------------------------------------------------
54 # ------------------------------------------------------------------------
55
56 # Check whether the module is running on a 32-bit or 64-bit version of Windows
57 39 ▶use constant _WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
58
59 # use constant TB_VERSION_STR => $version->normal;
60 2 ▶BEGIN { sub TB_VERSION_STR () { state $qv = $version->normal } }
Subroutine TB_VERSION_STR: called CC=1 SCAR=0.0
Pod undocumented
61
62 # STRICT is a global flag that enables strict argument checking.
63 39 ▶use constant STRICT => !!grep { exists $ENV{$_} && $ENV{$_} } qw(
Subroutine BEGIN: called CC=2 SCAR=7.0
Condition66%2/3
exists $ENV{$_} && $ENV{$_}
!ll&&!rl&&r
117039
MC/DC50%1/2
exists $ENV{$_} && $ENV{$_}
exists $ENV{$_} $ENV{$_}
Truth table66%2/3
A && B
A= exists $ENV{$_}B= $ENV{$_}
ABresult
0X0
100
111
64 PERL_STRICT
65 EXTENDED_TESTING
66 AUTHOR_TESTING
67 RELEASE_TESTING
68 39 );
69
70 # The following compile-time options are available for conditional code.
71 # Their defaults are derived from the original C implementation and the
72 # current Perl configuration, but may be overridden through environment
73 # variables before loading this module.
74
75 # Ensure consistent compile-time options
76 39 ▶use constant TB_LIB_OPTS => $ENV{TB_LIB_OPTS} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TB_LIB_OPTS'} ? : 0 39 39
77
78 # Deprecated. Sets TB_OPT_ATTR_W to 32 if not already set.
79 39 ▶use constant TB_OPT_TRUECOLOR => $ENV{TB_OPT_TRUECOLOR} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TB_OPT_TRUECOLOR'} ? : 0 39 39
80
81 # Integer width of fg and bg attributes. Valid values (assuming system support)
82 # are 16, 32, and 64. 32 or 64 enables output mode TB_OUTPUT_TRUECOLOR.
83 # 64 enables additional style attributes. (See tb_set_output_mode.) Larger
84 # values consume more memory in exchange for more features. Defaults to 16.
85 39 ▶use constant TB_OPT_ATTR_W =>
Subroutine BEGIN: called CC=3 SCAR=12.5
86 ( TB_LIB_OPTS ) ? $Config{ivsize} * 8 :
87 ( exists $ENV{TB_OPT_ATTR_W}
88 && $ENV{TB_OPT_ATTR_W} =~ /^(16|32|64)$/ ) ? $ENV{TB_OPT_ATTR_W} :
89 39 ▶ ( TB_OPT_TRUECOLOR ) ? 32 :
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'TB_OPT_ATTR_W'} && $ENV{'TB_OPT_ATTR_W'} =~ /^(16|32|64)$/ ? : 0 39 39
Condition33%1/3
exists $ENV{"TB_OPT_ATTR_W"} and $ENV{"TB_OPT_ATTR_W"} =~ /^(16|32|64)$/
!ll&&!rl&&r
3900
MC/DC0%0/2
exists $ENV{"TB_OPT_ATTR_W"} and $ENV{"TB_OPT_ATTR_W"} =~ /^(16|32|64)$/
exists $ENV{"TB_OPT_ATTR_W"} $ENV{"TB_OPT_ATTR_W"} =~ /^(16|32|64)$/
Truth table33%1/3
A and B
A= exists $ENV{"TB_OPT_ATTR_W"}B= $ENV{"TB_OPT_ATTR_W"} =~ /^(16|32|64)$/
ABresult
0X0
100
111
90 39 16 ;
91
92 # If set, enable extended grapheme cluster support (tb_extend_cell,
93 # tb_set_cell_ex). Consumes more memory. Defaults off.
94 39 ▶use constant TB_OPT_EGC => TB_LIB_OPTS || ($ENV{TB_OPT_EGC} ? 1 : 0);
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TB_OPT_EGC'} ? : 2 37 39
95
96 # Write buffer size for printf operations. Represents the largest string that
97 # can be sent in one call to tb_print and tb_send functions. Defaults to 4096.
98 39 ▶use constant TB_OPT_PRINTF_BUF => !TB_LIB_OPTS
Subroutine BEGIN: called CC=3 SCAR=13.1
99 39 ▶ && exists $ENV{TB_OPT_PRINTF_BUF} ? ($ENV{TB_OPT_PRINTF_BUF} || 4096): 4096;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'TB_OPT_PRINTF_BUF'} ? : 0 39 39
Condition0%0/2
$ENV{"TB_OPT_PRINTF_BUF"} or 4096
l!l
00
MC/DC0%0/1
$ENV{"TB_OPT_PRINTF_BUF"} or 4096
$ENV{"TB_OPT_PRINTF_BUF"}
Truth table0%0/2
A
A= $ENV{"TB_OPT_PRINTF_BUF"}
Aresult
11
00
100
101 # Read buffer size for tty reads. Defaults to 64.
102 39 ▶use constant TB_OPT_READ_BUF => !TB_LIB_OPTS
Subroutine BEGIN: called CC=3 SCAR=13.1
103 39 ▶ && exists $ENV{TB_OPT_READ_BUF} ? ($ENV{TB_OPT_READ_BUF} || 64): 64;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'TB_OPT_READ_BUF'} ? : 0 39 39
Condition0%0/2
$ENV{"TB_OPT_READ_BUF"} or 64
l!l
00
MC/DC0%0/1
$ENV{"TB_OPT_READ_BUF"} or 64
$ENV{"TB_OPT_READ_BUF"}
Truth table0%0/2
A
A= $ENV{"TB_OPT_READ_BUF"}
Aresult
11
00
104
105 # If set, use Perl's core Unicode::UCD module instead of the built-in
106 # Unicode-aware versions. Note, Unicode::UCD are version-dependent and must
107 # support prop_invmap and search_invlist. Defaults to built-in.
108 39 ▶use constant TB_OPT_LIBC_WCHAR => !TB_LIB_OPTS
Subroutine BEGIN: called CC=4 SCAR=17.1
109 && $ENV{TB_OPT_LIBC_WCHAR}
110 39 ▶ && Unicode::UCD->can('prop_invmap')
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TB_OPT_LIBC_WCHAR'} && 'Unicode::UCD'->can('prop_invmap') && 'Unicode::UCD'->can('search_invlist') ? : 0 39 39
Condition33%2/6
$ENV{"TB_OPT_LIBC_WCHAR"} and "Unicode::UCD"->can("prop_invmap") and "Unicode::UCD"->can("search_invlist")
!ll&&!rl&&r
3900
$ENV{"TB_OPT_LIBC_WCHAR"} and "Unicode::UCD"->can("prop_invmap")
!ll&&!rl&&r
3900
MC/DC0%0/3
$ENV{"TB_OPT_LIBC_WCHAR"} and "Unicode::UCD"->can("prop_invmap") and "Unicode::UCD"->can("search_invlist")
$ENV{"TB_OPT_LIBC_WCHAR"} "Unicode::UCD"->can("prop_invmap") "Unicode::UCD"->can("search_invlist")
Truth table25%1/4
A and B and C
A= $ENV{"TB_OPT_LIBC_WCHAR"}B= "Unicode::UCD"->can("prop_invmap")C= "Unicode::UCD"->can("search_invlist")
ABCresult
0XX0
10X0
1100
1111
111 39 && Unicode::UCD->can('search_invlist') ? 1 : 0;
112
113 39 ▶use constant TB_PATH_MAX => exists $ENV{PATH_MAX} ? 0+$ENV{PATH_MAX} : 4096;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'PATH_MAX'} ? : 0 39 39
114 39 ▶use constant TB_TERMINFO_DIR => exists $ENV{TB_TERMINFO_DIR}
Subroutine BEGIN: called CC=2 SCAR=7.1
115 ? ''.$ENV{TB_TERMINFO_DIR}
116 39 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'TB_TERMINFO_DIR'} ? : 0 39 39
117
118 39 ▶use constant TB_RESIZE_FALLBACK_MS => exists $ENV{TB_RESIZE_FALLBACK_MS}
Subroutine BEGIN: called CC=3 SCAR=13.1
119 39 ▶ ? ($ENV{TB_RESIZE_FALLBACK_MS} || 1000)
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'TB_RESIZE_FALLBACK_MS'} ? : 0 39 39
Condition0%0/2
$ENV{"TB_RESIZE_FALLBACK_MS"} or 1000
l!l
00
MC/DC0%0/1
$ENV{"TB_RESIZE_FALLBACK_MS"} or 1000
$ENV{"TB_RESIZE_FALLBACK_MS"}
Truth table0%0/2
A
A= $ENV{"TB_RESIZE_FALLBACK_MS"}
Aresult
11
00
120 39 : 1000;
121
122 # Retrieve the debug level from the environment variable TB_DEBUG_LEVEL.
123 39 ▶use constant _DEBUG => $ENV{TB_DEBUG_LEVEL} && !$ENV{PERL_NDEBUG} && !$ENV{NDEBUG}
Subroutine BEGIN: called CC=5 SCAR=21.9
124 39 ▶ ? ($ENV{TB_DEBUG_LEVEL} || 0)
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TB_DEBUG_LEVEL'} && !$ENV{'PERL_NDEBUG'} && !$ENV{'NDEBUG'} ? : 0 39 39
Condition25%2/8
$ENV{"TB_DEBUG_LEVEL"} and not $ENV{"PERL_NDEBUG"} and not $ENV{"NDEBUG"}
!ll&&!rl&&r
3900
$ENV{"TB_DEBUG_LEVEL"} and not $ENV{"PERL_NDEBUG"}
!ll&&!rl&&r
3900
$ENV{"TB_DEBUG_LEVEL"} or 0
l!l
00
MC/DC0%0/3
$ENV{"TB_DEBUG_LEVEL"} and not $ENV{"PERL_NDEBUG"} and not $ENV{"NDEBUG"}
$ENV{"TB_DEBUG_LEVEL"} not $ENV{"PERL_NDEBUG"} not $ENV{"NDEBUG"}
MC/DC0%0/1
$ENV{"TB_DEBUG_LEVEL"} or 0
$ENV{"TB_DEBUG_LEVEL"}
Truth table25%1/4
A and not(B) and not(C)
A= $ENV{"TB_DEBUG_LEVEL"}B= not $ENV{"PERL_NDEBUG"}C= not $ENV{"NDEBUG"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $ENV{"TB_DEBUG_LEVEL"}
Aresult
11
00
125 39 : 0;
126
127 # ------------------------------------------------------------------------
128 # Exports ----------------------------------------------------------------
129 # ------------------------------------------------------------------------
130
131 39 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
132
133 39 our @EXPORT_OK = qw(
134 );
135
136 39 our %EXPORT_TAGS = (
137
138 api => [qw(
139 tb_init
140 tb_init_file
141 tb_init_fd
142 tb_init_rwfd
143 tb_shutdown
144
145 tb_width
146 tb_height
147
148 tb_clear
149 tb_set_clear_attrs
150
151 tb_present
152 tb_invalidate
153
154 tb_set_cursor
155 tb_hide_cursor
156
157 tb_set_cell
158 tb_set_cell_ex
159 tb_extend_cell
160
161 tb_get_cell
162
163 tb_set_input_mode
164 tb_set_output_mode
165
166 tb_peek_event
167 tb_poll_event
168
169 tb_get_fds
170
171 tb_print
172 tb_printf
173 tb_print_ex
174 tb_printf_ex
175
176 tb_send
177 tb_sendf
178
179 tb_set_func
180
181 tb_utf8_char_length
182 tb_utf8_char_to_unicode
183 tb_utf8_unicode_to_char
184
185 tb_last_errno
186 tb_strerror
187 tb_cell_buffer
188 tb_has_truecolor
189 tb_has_egc
190 tb_attr_width
191 tb_version
192 )],
193
194 keys => [qw(
195 TB_KEY_CTRL_TILDE
196 TB_KEY_CTRL_2
197 TB_KEY_CTRL_A
198 TB_KEY_CTRL_B
199 TB_KEY_CTRL_C
200 TB_KEY_CTRL_D
201 TB_KEY_CTRL_E
202 TB_KEY_CTRL_F
203 TB_KEY_CTRL_G
204 TB_KEY_BACKSPACE
205 TB_KEY_CTRL_H
206 TB_KEY_TAB
207 TB_KEY_CTRL_I
208 TB_KEY_CTRL_J
209 TB_KEY_CTRL_K
210 TB_KEY_CTRL_L
211 TB_KEY_ENTER
212 TB_KEY_CTRL_M
213 TB_KEY_CTRL_N
214 TB_KEY_CTRL_O
215 TB_KEY_CTRL_P
216 TB_KEY_CTRL_Q
217 TB_KEY_CTRL_R
218 TB_KEY_CTRL_S
219 TB_KEY_CTRL_T
220 TB_KEY_CTRL_U
221 TB_KEY_CTRL_V
222 TB_KEY_CTRL_W
223 TB_KEY_CTRL_X
224 TB_KEY_CTRL_Y
225 TB_KEY_CTRL_Z
226 TB_KEY_ESC
227 TB_KEY_CTRL_LSQ_BRACKET
228 TB_KEY_CTRL_3
229 TB_KEY_CTRL_4
230 TB_KEY_CTRL_BACKSLASH
231 TB_KEY_CTRL_5
232 TB_KEY_CTRL_RSQ_BRACKET
233 TB_KEY_CTRL_6
234 TB_KEY_CTRL_7
235 TB_KEY_CTRL_SLASH
236 TB_KEY_CTRL_UNDERSCORE
237 TB_KEY_SPACE
238 TB_KEY_BACKSPACE2
239 TB_KEY_CTRL_8
240
241 TB_KEY_F1
242 TB_KEY_F2
243 TB_KEY_F3
244 TB_KEY_F4
245 TB_KEY_F5
246 TB_KEY_F6
247 TB_KEY_F7
248 TB_KEY_F8
249 TB_KEY_F9
250 TB_KEY_F10
251 TB_KEY_F11
252 TB_KEY_F12
253 TB_KEY_INSERT
254 TB_KEY_DELETE
255 TB_KEY_HOME
256 TB_KEY_END
257 TB_KEY_PGUP
258 TB_KEY_PGDN
259 TB_KEY_ARROW_UP
260 TB_KEY_ARROW_DOWN
261 TB_KEY_ARROW_LEFT
262 TB_KEY_ARROW_RIGHT
263 TB_KEY_BACK_TAB
264 TB_KEY_MOUSE_LEFT
265 TB_KEY_MOUSE_RIGHT
266 TB_KEY_MOUSE_MIDDLE
267 TB_KEY_MOUSE_RELEASE
268 TB_KEY_MOUSE_WHEEL_UP
269 TB_KEY_MOUSE_WHEEL_DOWN
270
271 TB_CAP_F1
272 TB_CAP_F2
273 TB_CAP_F3
274 TB_CAP_F4
275 TB_CAP_F5
276 TB_CAP_F6
277 TB_CAP_F7
278 TB_CAP_F8
279 TB_CAP_F9
280 TB_CAP_F10
281 TB_CAP_F11
282 TB_CAP_F12
283 TB_CAP_INSERT
284 TB_CAP_DELETE
285 TB_CAP_HOME
286 TB_CAP_END
287 TB_CAP_PGUP
288 TB_CAP_PGDN
289 TB_CAP_ARROW_UP
290 TB_CAP_ARROW_DOWN
291 TB_CAP_ARROW_LEFT
292 TB_CAP_ARROW_RIGHT
293 TB_CAP_BACK_TAB
294 TB_CAP__COUNT_KEYS
295 TB_CAP_ENTER_CA
296 TB_CAP_EXIT_CA
297 TB_CAP_SHOW_CURSOR
298 TB_CAP_HIDE_CURSOR
299 TB_CAP_CLEAR_SCREEN
300 TB_CAP_SGR0
301 TB_CAP_UNDERLINE
302 TB_CAP_BOLD
303 TB_CAP_BLINK
304 TB_CAP_ITALIC
305 TB_CAP_REVERSE
306 TB_CAP_ENTER_KEYPAD
307 TB_CAP_EXIT_KEYPAD
308 TB_CAP_DIM
309 TB_CAP_INVISIBLE
310 TB_CAP__COUNT
311 )],
312
313 colors => [qw(
314 TB_DEFAULT
315 TB_BLACK
316 TB_RED
317 TB_GREEN
318 TB_YELLOW
319 TB_BLUE
320 TB_MAGENTA
321 TB_CYAN
322 TB_WHITE
323
324 TB_BOLD
325 TB_UNDERLINE
326 TB_REVERSE
327 TB_ITALIC
328 TB_BLINK
329 TB_HI_BLACK
330 TB_BRIGHT
331 TB_DIM
332 ),
333 TB_OPT_ATTR_W == 16
334 ? qw( TB_256_BLACK )
335 : qw( TB_TRUECOLOR_BOLD
336 TB_TRUECOLOR_UNDERLINE
337 TB_TRUECOLOR_REVERSE
338 TB_TRUECOLOR_ITALIC
339 TB_TRUECOLOR_BLINK
340 TB_TRUECOLOR_BLACK ),
341 TB_OPT_ATTR_W == 64
342 ? qw( TB_STRIKEOUT
343 TB_UNDERLINE_2
344 TB_OVERLINE
345 TB_INVISIBLE )
346 : ()
347 ],
348
349 event => [qw(
350 TB_EVENT_KEY
351 TB_EVENT_RESIZE
352 TB_EVENT_MOUSE
353
354 TB_MOD_ALT
355 TB_MOD_CTRL
356 TB_MOD_SHIFT
357 TB_MOD_MOTION
358
359 TB_INPUT_CURRENT
360 TB_INPUT_ESC
361 TB_INPUT_ALT
362 TB_INPUT_MOUSE
363
364 TB_OUTPUT_CURRENT
365 TB_OUTPUT_NORMAL
366 TB_OUTPUT_256
367 TB_OUTPUT_216
368 TB_OUTPUT_GRAYSCALE
369 ),
370 TB_OPT_ATTR_W >= 32
371 ? qw( TB_OUTPUT_TRUECOLOR )
372 : ()
373 ],
374
375 return => [qw(
376 TB_OK
377 TB_ERR
378 TB_ERR_NEED_MORE
379 TB_ERR_INIT_ALREADY
380 TB_ERR_INIT_OPEN
381 TB_ERR_MEM
382 TB_ERR_NO_EVENT
383 TB_ERR_NO_TERM
384 TB_ERR_NOT_INIT
385 TB_ERR_OUT_OF_BOUNDS
386 TB_ERR_READ
387 TB_ERR_RESIZE_IOCTL
388 TB_ERR_RESIZE_PIPE
389 TB_ERR_RESIZE_SIGACTION
390 TB_ERR_POLL
391 TB_ERR_TCGETATTR
392 TB_ERR_TCSETATTR
393 TB_ERR_UNSUPPORTED_TERM
394 TB_ERR_RESIZE_WRITE
395 TB_ERR_RESIZE_POLL
396 TB_ERR_RESIZE_READ
397 TB_ERR_RESIZE_SSCANF
398 TB_ERR_CAP_COLLISION
399
400 TB_ERR_SELECT
401 TB_ERR_RESIZE_SELECT
402 ),
403 _WIN32
404 ? qw( TB_ERR_WIN_RESIZE
405 TB_ERR_WIN_GET_CONMODE
406 TB_ERR_WIN_SET_CONMODE
407 TB_ERR_WIN_NO_STDIO
408 TB_ERR_WIN_UNSUPPORTED )
409 : ()
410 ],
411
412 func => [qw(
413 TB_FUNC_EXTRACT_PRE
414 TB_FUNC_EXTRACT_POST
415 )],
416
417 );
418
419 # add all the other %EXPORT_TAGS ":class" tags to the ":all" class and
420 # @EXPORT_OK, deleting duplicates
421 {
422 39 my %seen;
423 push
424 @EXPORT_OK,
425 8346 grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}}
426 39 foreach keys %EXPORT_TAGS;
427 push
428 39 @{$EXPORT_TAGS{all}},
429 @EXPORT_OK;
430 }
431
432 # ------------------------------------------------------------------------
433 # Constants --------------------------------------------------------------
434 # ------------------------------------------------------------------------
435
436 39 ▶use constant INT16_SIZE => $Config{shortsize};
Subroutine BEGIN: called CC=1 SCAR=0.0
437
438 # ASCII key constants ($tb_event->key)
439 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
440 39 TB_KEY_CTRL_TILDE => 0x00,
441 TB_KEY_CTRL_2 => 0x00, # clash with 'CTRL_TILDE'
442 TB_KEY_CTRL_A => 0x01,
443 TB_KEY_CTRL_B => 0x02,
444 TB_KEY_CTRL_C => 0x03,
445 TB_KEY_CTRL_D => 0x04,
446 TB_KEY_CTRL_E => 0x05,
447 TB_KEY_CTRL_F => 0x06,
448 TB_KEY_CTRL_G => 0x07,
449 TB_KEY_BACKSPACE => 0x08,
450 TB_KEY_CTRL_H => 0x08, # clash with 'BACKSPACE'
451 TB_KEY_TAB => 0x09,
452 TB_KEY_CTRL_I => 0x09, # clash with 'TAB'
453 TB_KEY_CTRL_J => 0x0a,
454 TB_KEY_CTRL_K => 0x0b,
455 TB_KEY_CTRL_L => 0x0c,
456 TB_KEY_ENTER => 0x0d,
457 TB_KEY_CTRL_M => 0x0d, # clash with 'ENTER'
458 TB_KEY_CTRL_N => 0x0e,
459 TB_KEY_CTRL_O => 0x0f,
460 TB_KEY_CTRL_P => 0x10,
461 TB_KEY_CTRL_Q => 0x11,
462 TB_KEY_CTRL_R => 0x12,
463 TB_KEY_CTRL_S => 0x13,
464 TB_KEY_CTRL_T => 0x14,
465 TB_KEY_CTRL_U => 0x15,
466 TB_KEY_CTRL_V => 0x16,
467 TB_KEY_CTRL_W => 0x17,
468 TB_KEY_CTRL_X => 0x18,
469 TB_KEY_CTRL_Y => 0x19,
470 TB_KEY_CTRL_Z => 0x1a,
471 TB_KEY_ESC => 0x1b,
472 TB_KEY_CTRL_LSQ_BRACKET => 0x1b, # clash with 'ESC'
473 TB_KEY_CTRL_3 => 0x1b, # clash with 'ESC'
474 TB_KEY_CTRL_4 => 0x1c,
475 TB_KEY_CTRL_BACKSLASH => 0x1c, # clash with 'CTRL_4'
476 TB_KEY_CTRL_5 => 0x1d,
477 TB_KEY_CTRL_RSQ_BRACKET => 0x1d, # clash with 'CTRL_5'
478 TB_KEY_CTRL_6 => 0x1e,
479 TB_KEY_CTRL_7 => 0x1f,
480 TB_KEY_CTRL_SLASH => 0x1f, # clash with 'CTRL_7'
481 TB_KEY_CTRL_UNDERSCORE => 0x1f, # clash with 'CTRL_7'
482 TB_KEY_SPACE => 0x20,
483 TB_KEY_BACKSPACE2 => 0x7f,
484 TB_KEY_CTRL_8 => 0x7f, # clash with 'BACKSPACE2'
485 39 };
486
487 0 ▶sub tb_key_i($) { 0xFFFF - $_[0] }
Subroutine tb_key_i: not called CC=1 SCAR=6.9
Pod undocumented
488
489 # Terminal-dependent key constants ($tb_event->key) and terminfo caps
490 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
491 39 TB_KEY_F1 => 0xFFFF - 0,
492 TB_KEY_F2 => 0xFFFF - 1,
493 TB_KEY_F3 => 0xFFFF - 2,
494 TB_KEY_F4 => 0xFFFF - 3,
495 TB_KEY_F5 => 0xFFFF - 4,
496 TB_KEY_F6 => 0xFFFF - 5,
497 TB_KEY_F7 => 0xFFFF - 6,
498 TB_KEY_F8 => 0xFFFF - 7,
499 TB_KEY_F9 => 0xFFFF - 8,
500 TB_KEY_F10 => 0xFFFF - 9,
501 TB_KEY_F11 => 0xFFFF - 10,
502 TB_KEY_F12 => 0xFFFF - 11,
503 TB_KEY_INSERT => 0xFFFF - 12,
504 TB_KEY_DELETE => 0xFFFF - 13,
505 TB_KEY_HOME => 0xFFFF - 14,
506 TB_KEY_END => 0xFFFF - 15,
507 TB_KEY_PGUP => 0xFFFF - 16,
508 TB_KEY_PGDN => 0xFFFF - 17,
509 TB_KEY_ARROW_UP => 0xFFFF - 18,
510 TB_KEY_ARROW_DOWN => 0xFFFF - 19,
511 TB_KEY_ARROW_LEFT => 0xFFFF - 20,
512 TB_KEY_ARROW_RIGHT => 0xFFFF - 21,
513 TB_KEY_BACK_TAB => 0xFFFF - 22,
514 TB_KEY_MOUSE_LEFT => 0xFFFF - 23,
515 TB_KEY_MOUSE_RIGHT => 0xFFFF - 24,
516 TB_KEY_MOUSE_MIDDLE => 0xFFFF - 25,
517 TB_KEY_MOUSE_RELEASE => 0xFFFF - 26,
518 TB_KEY_MOUSE_WHEEL_UP => 0xFFFF - 27,
519 TB_KEY_MOUSE_WHEEL_DOWN => 0xFFFF - 28,
520 39 };
521
522 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
523 39 TB_CAP_F1 => 0,
524 TB_CAP_F2 => 1,
525 TB_CAP_F3 => 2,
526 TB_CAP_F4 => 3,
527 TB_CAP_F5 => 4,
528 TB_CAP_F6 => 5,
529 TB_CAP_F7 => 6,
530 TB_CAP_F8 => 7,
531 TB_CAP_F9 => 8,
532 TB_CAP_F10 => 9,
533 TB_CAP_F11 => 10,
534 TB_CAP_F12 => 11,
535 TB_CAP_INSERT => 12,
536 TB_CAP_DELETE => 13,
537 TB_CAP_HOME => 14,
538 TB_CAP_END => 15,
539 TB_CAP_PGUP => 16,
540 TB_CAP_PGDN => 17,
541 TB_CAP_ARROW_UP => 18,
542 TB_CAP_ARROW_DOWN => 19,
543 TB_CAP_ARROW_LEFT => 20,
544 TB_CAP_ARROW_RIGHT => 21,
545 TB_CAP_BACK_TAB => 22,
546 TB_CAP__COUNT_KEYS => 23,
547 TB_CAP_ENTER_CA => 23,
548 TB_CAP_EXIT_CA => 24,
549 TB_CAP_SHOW_CURSOR => 25,
550 TB_CAP_HIDE_CURSOR => 26,
551 TB_CAP_CLEAR_SCREEN => 27,
552 TB_CAP_SGR0 => 28,
553 TB_CAP_UNDERLINE => 29,
554 TB_CAP_BOLD => 30,
555 TB_CAP_BLINK => 31,
556 TB_CAP_ITALIC => 32,
557 TB_CAP_REVERSE => 33,
558 TB_CAP_ENTER_KEYPAD => 34,
559 TB_CAP_EXIT_KEYPAD => 35,
560 TB_CAP_DIM => 36,
561 TB_CAP_INVISIBLE => 37,
562 TB_CAP__COUNT => 38,
563 39 };
564
565 # Some hard-coded caps
566 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
567 39 TB_HARDCAP_ENTER_MOUSE => "\x1b[?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h",
568 TB_HARDCAP_EXIT_MOUSE => "\x1b[?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l",
569 TB_HARDCAP_STRIKEOUT => "\x1b[9m",
570 TB_HARDCAP_UNDERLINE_2 => "\x1b[21m",
571 TB_HARDCAP_OVERLINE => "\x1b[53m",
572 39 };
573
574 # Colors (numeric) and attributes (bitwise) ($tb_cell->fg, $tb_cell->bg)
575 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
576 39 TB_DEFAULT => 0x0000,
577 TB_BLACK => 0x0001,
578 TB_RED => 0x0002,
579 TB_GREEN => 0x0003,
580 TB_YELLOW => 0x0004,
581 TB_BLUE => 0x0005,
582 TB_MAGENTA => 0x0006,
583 TB_CYAN => 0x0007,
584 TB_WHITE => 0x0008,
585 39 };
586
587 39 ▶BEGIN { if (TB_OPT_ATTR_W == 16) {
Subroutine BEGIN: called CC=1 SCAR=0.0
588 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
589 39 *TB_BOLD = sub () { 0x0100 };
590 39 *TB_UNDERLINE = sub () { 0x0200 };
591 39 *TB_REVERSE = sub () { 0x0400 };
592 39 *TB_ITALIC = sub () { 0x0800 };
593 39 *TB_BLINK = sub () { 0x1000 };
594 39 *TB_HI_BLACK = sub () { 0x2000 };
595 39 *TB_BRIGHT = sub () { 0x4000 };
596 39 *TB_DIM = sub () { 0x8000 };
597 39 *TB_256_BLACK = &TB_HI_BLACK; # TB_256_BLACK is deprecated
598 }
599 else { # TB_OPT_ATTR_W is 32 or 64
600 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
601 *TB_BOLD = sub () { 0x01000000 };
602 *TB_UNDERLINE = sub () { 0x02000000 };
603 *TB_REVERSE = sub () { 0x04000000 };
604 *TB_ITALIC = sub () { 0x08000000 };
605 *TB_BLINK = sub () { 0x10000000 };
606 *TB_HI_BLACK = sub () { 0x20000000 };
607 *TB_BRIGHT = sub () { 0x40000000 };
608 *TB_DIM = sub () { 0x80000000 };
609 *TB_TRUECOLOR_BOLD = &TB_BOLD; # TB_TRUECOLOR_BOLD is deprecated
610 *TB_TRUECOLOR_UNDERLINE = &TB_UNDERLINE;
611 *TB_TRUECOLOR_REVERSE = &TB_REVERSE;
612 *TB_TRUECOLOR_ITALIC = &TB_ITALIC;
613 *TB_TRUECOLOR_BLINK = &TB_BLINK;
614 *TB_TRUECOLOR_BLACK = &TB_HI_BLACK;
615 }}
616
617 39 ▶BEGIN { if (TB_OPT_ATTR_W == 64) {
Subroutine BEGIN: called CC=1 SCAR=0.0
618 39 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
619 *TB_STRIKEOUT = sub () { 0x010000000000 };
620 *TB_UNDERLINE_2 = sub () { 0x020000000000 };
621 *TB_OVERLINE = sub () { 0x040000000000 };
622 *TB_INVISIBLE = sub () { 0x080000000000 };
623 }}
624
625 # Event types ($tb_event->type)
626 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
627 39 TB_EVENT_KEY => 1,
628 TB_EVENT_RESIZE => 2,
629 TB_EVENT_MOUSE => 3,
630 39 };
631
632 # Key modifiers (bitwise) ($tb_event->mod)
633 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
634 39 TB_MOD_ALT => 1,
635 TB_MOD_CTRL => 2,
636 TB_MOD_SHIFT => 4,
637 TB_MOD_MOTION => 8,
638 39 };
639
640 # Input modes (bitwise) (tb_set_input_mode)
641 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
642 39 TB_INPUT_CURRENT => 0,
643 TB_INPUT_ESC => 1,
644 TB_INPUT_ALT => 2,
645 TB_INPUT_MOUSE => 4,
646 39 };
647
648 # Output modes (bitwise) (tb_set_output_mode)
649 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
650 39 TB_OUTPUT_CURRENT => 0,
651 TB_OUTPUT_NORMAL => 1,
652 TB_OUTPUT_256 => 2,
653 TB_OUTPUT_216 => 4,
654 TB_OUTPUT_GRAYSCALE => 8,
655 39 };
656 39 ▶BEGIN { if (TB_OPT_ATTR_W >= 32) {
Subroutine BEGIN: called CC=1 SCAR=0.0
657 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
658 *TB_OUTPUT_TRUECOLOR = sub () { 5 };
659 }}
660
661 # Common function return values unless otherwise noted.
662 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
663 39 TB_OK => 0,
664 TB_ERR => -1,
665 TB_ERR_NEED_MORE => -2,
666 TB_ERR_INIT_ALREADY => -3,
667 TB_ERR_INIT_OPEN => -4,
668 TB_ERR_MEM => -5,
669 TB_ERR_NO_EVENT => -6,
670 TB_ERR_NO_TERM => -7,
671 TB_ERR_NOT_INIT => -8,
672 TB_ERR_OUT_OF_BOUNDS => -9,
673 TB_ERR_READ => -10,
674 TB_ERR_RESIZE_IOCTL => -11,
675 TB_ERR_RESIZE_PIPE => -12,
676 TB_ERR_RESIZE_SIGACTION => -13,
677 TB_ERR_POLL => -14,
678 TB_ERR_TCGETATTR => -15,
679 TB_ERR_TCSETATTR => -16,
680 TB_ERR_UNSUPPORTED_TERM => -17,
681 TB_ERR_RESIZE_WRITE => -18,
682 TB_ERR_RESIZE_POLL => -19,
683 TB_ERR_RESIZE_READ => -20,
684 TB_ERR_RESIZE_SSCANF => -21,
685 TB_ERR_CAP_COLLISION => -22,
686 39 };
687 39 ▶BEGIN { if (_WIN32) {
Subroutine BEGIN: called CC=1 SCAR=0.0
688 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
689 *TB_ERR_WIN_RESIZE = sub () { -23 };
690 *TB_ERR_WIN_GET_CONMODE = sub () { -24 };
691 *TB_ERR_WIN_SET_CONMODE = sub () { -25 };
692 *TB_ERR_WIN_NO_STDIO = sub () { -26 };
693 *TB_ERR_WIN_UNSUPPORTED = sub () { -27 };
694 }}
695 39 ▶use constant TB_ERR_SELECT => TB_ERR_POLL;
Subroutine BEGIN: called CC=1 SCAR=0.0
696 39 ▶use constant TB_ERR_RESIZE_SELECT => TB_ERR_RESIZE_POLL;
Subroutine BEGIN: called CC=1 SCAR=0.0
697
698 # Deprecated. Function types to be used with 'tb_set_func'.
699 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
700 39 TB_FUNC_EXTRACT_PRE => 0,
701 TB_FUNC_EXTRACT_POST => 1,
702 39 };
703
704 # Mouse escape parser type tags (enum equivalent)
705 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
706 39 TYPE_VT200 => 0,
707 TYPE_1006 => 1,
708 TYPE_1015 => 2,
709 TYPE_MAX => 3,
710 39 };
711
712 # Standard error codes (POSIX errors)
713 39 ▶use constant {
Subroutine BEGIN: called CC=9 SCAR=28.7
714 39 ▶ EIO => defined(&POSIX::EIO) ? POSIX::EIO() : 5,
Branch: 8 branches
BranchTrueFalseTotal
defined &POSIX::EIO ? : 39 0 39
defined &POSIX::EBADF ? : 39 0 39
defined &POSIX::EACCES ? : 39 0 39
defined &POSIX::EINVAL ? : 39 0 39
defined &POSIX::ENOTTY ? : 39 0 39
defined &POSIX::EPIPE ? : 39 0 39
defined &POSIX::ENOTSUP ? : 39 0 39
defined &POSIX::EOPNOTSUPP ? : 0 0 0
715 EBADF => defined(&POSIX::EBADF) ? POSIX::EBADF() : 9,
716 EACCES => defined(&POSIX::EACCES) ? POSIX::EACCES() : 13,
717 EINVAL => defined(&POSIX::EINVAL) ? POSIX::EINVAL() : 22,
718 ENOTTY => defined(&POSIX::ENOTTY) ? POSIX::ENOTTY() : 25,
719 EPIPE => defined(&POSIX::EPIPE) ? POSIX::EPIPE() : 32,
720 ENOTSUP => defined(&POSIX::ENOTSUP) ? POSIX::ENOTSUP() :
721 defined(&POSIX::EOPNOTSUPP) ? POSIX::EOPNOTSUPP() :
722 95,
723 39 };
724
725 # ------------------------------------------------------------------------
726 # Globals ----------------------------------------------------------------
727 # ------------------------------------------------------------------------
728
729 # Holds the current state of the terminal
730 39 ▶use vars qw( $global );
Subroutine BEGIN: called CC=1 SCAR=0.0
731
732 # ------------------------------------------------------------------------
733 # Utility functions ------------------------------------------------------
734 # ------------------------------------------------------------------------
735
736 39 ▶BEGIN { require Terminal::WCWidth unless TB_OPT_LIBC_WCHAR }
Subroutine BEGIN: called CC=1 SCAR=0.0
737
738 # Determine if the current locale is CJK, which affects the width of
739 # 'A' (Ambiguous) characters.
740 39 ▶our $is_cjk = eval {
Condition50%1/2
eval { do { require Unicode::EastAsianWidth::Detect; Unicode::EastAsianWidth::Detect::is_cjk_lang() ? 1 : 0 } } || 0
l!l
039
MC/DC0%0/1
eval { do { require Unicode::EastAsianWidth::Detect; Unicode::EastAsianWidth::Detect::is_cjk_lang() ? 1 : 0 } } || 0
eval { do { require Unicode::EastAsianWidth::Detect; Unicode::EastAsianWidth::Detect::is_cjk_lang() ? 1 : 0 } }
Truth table50%1/2
A
A= eval { do { require Unicode::EastAsianWidth::Detect; Unicode::EastAsianWidth::Detect::is_cjk_lang() ? 1 : 0 } }
Aresult
11
00
741 39 require Unicode::EastAsianWidth::Detect;
742 39 ▶ Unicode::EastAsianWidth::Detect::is_cjk_lang() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
Unicode::EastAsianWidth::Detect::is_cjk_lang() ? : 0 39 39
743 } || 0;
744
745 0 ▶sub wcwidth {
Subroutine wcwidth: not called CC=49 SCAR=78.0
Pod undocumented
746 0 my ($cp) = @_;
747
748 0 ▶ return -1 if $cp < 0 || $cp > 0x10FFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $cp < 0 or $cp > 1114111 0 0 0
Condition0%0/3
$cp < 0 or $cp > 1114111
l!l&&r!l&&!r
000
MC/DC0%0/2
$cp < 0 or $cp > 1114111
$cp < 0 $cp > 1114111
Truth table0%0/3
A or B
A= $cp < 0B= $cp > 1114111
ABresult
1X1
011
000
749 0 ▶ return -1 if $cp >= 0xD800 && $cp <= 0xDFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $cp >= 55296 and $cp <= 57343 0 0 0
Condition0%0/3
$cp >= 55296 and $cp <= 57343
!ll&&!rl&&r
000
MC/DC0%0/2
$cp >= 55296 and $cp <= 57343
$cp >= 55296 $cp <= 57343
Truth table0%0/3
A and B
A= $cp >= 55296B= $cp <= 57343
ABresult
0X0
100
111
750 0 ▶ return 0 if $cp == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cp == 0 0 0 0
751 0 ▶ return -1 if $cp < 0x20 || ($cp >= 0x7F && $cp < 0xA0);
Branch: 1 branch
BranchTrueFalseTotal
if $cp < 32 or $cp >= 127 and $cp < 160 0 0 0
Condition0%0/6
$cp < 32 or $cp >= 127 and $cp < 160
l!l&&r!l&&!r
000
$cp >= 127 and $cp < 160
!ll&&!rl&&r
000
MC/DC0%0/3
$cp < 32 or $cp >= 127 and $cp < 160
$cp < 32 $cp >= 127 $cp < 160
Truth table0%0/4
A or B and C
A= $cp < 32B= $cp >= 127C= $cp < 160
ABCresult
1XX1
0111
00X0
0100
752 0 ▶ return 1 if $cp <= 0x7E || ($cp >= 0xA0 && $cp <= 0xFF);
Branch: 1 branch
BranchTrueFalseTotal
if $cp <= 126 or $cp >= 160 and $cp <= 255 0 0 0
Condition0%0/6
$cp <= 126 or $cp >= 160 and $cp <= 255
l!l&&r!l&&!r
000
$cp >= 160 and $cp <= 255
!ll&&!rl&&r
000
MC/DC0%0/3
$cp <= 126 or $cp >= 160 and $cp <= 255
$cp <= 126 $cp >= 160 $cp <= 255
Truth table0%0/4
A or B and C
A= $cp <= 126B= $cp >= 160C= $cp <= 255
ABCresult
1XX1
0111
00X0
0100
753
754 0 state $unicode_version = Unicode::UCD::UnicodeVersion();
755 0 state $cat = [ Unicode::UCD::prop_invmap('General_Category') ];
756 0 state $eaw = [ Unicode::UCD::prop_invmap('East_Asian_Width') ];
757 0 state $dig = [ Unicode::UCD::prop_invmap('Default_Ignorable_Code_Point') ];
758
759 0 my $i = Unicode::UCD::search_invlist($cat->[0], $cp);
760 0 ▶ if (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 0 0 0
761 0 ▶ my $v = $cat->[1]->[$i] // '';
Condition0%0/2
$cat->[1][$i] // ""
l!l
00
MC/DC0%0/1
$cat->[1][$i] // ""
$cat->[1][$i]
Truth table0%0/2
A
A= $cat->[1][$i]
Aresult
11
00
762 0 ▶ return -1
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "Cc" or $v eq "Cs" or $v eq "Cn" or $v eq "Control" or $v eq "Surrogate" or $v eq "Unassigned" 0 0 0
Condition0%0/15
$v eq "Cc" or $v eq "Cs" or $v eq "Cn" or $v eq "Control" or $v eq "Surrogate" or $v eq "Unassigned"
l!l&&r!l&&!r
000
$v eq "Cc" or $v eq "Cs" or $v eq "Cn" or $v eq "Control" or $v eq "Surrogate"
l!l&&r!l&&!r
000
$v eq "Cc" or $v eq "Cs" or $v eq "Cn" or $v eq "Control"
l!l&&r!l&&!r
000
$v eq "Cc" or $v eq "Cs" or $v eq "Cn"
l!l&&r!l&&!r
000
$v eq "Cc" or $v eq "Cs"
l!l&&r!l&&!r
000
MC/DC0%0/6
$v eq "Cc" or $v eq "Cs" or $v eq "Cn" or $v eq "Control" or $v eq "Surrogate" or $v eq "Unassigned"
$v eq "Cc" $v eq "Cs" $v eq "Cn" $v eq "Control" $v eq "Surrogate" $v eq "Unassigned"
Truth table0%0/7
A or B or C or D or E or F
A= $v eq "Cc"B= $v eq "Cs"C= $v eq "Cn"D= $v eq "Control"E= $v eq "Surrogate"F= $v eq "Unassigned"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
763 if $v eq 'Cc' || $v eq 'Cs' || $v eq 'Cn'
764 || $v eq 'Control' || $v eq 'Surrogate' || $v eq 'Unassigned';
765 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp" or $v eq "Nonspacing_Mark" or $v eq "Enclosing_Mark" or $v eq "Format" or $v eq "Line_Separator" or $v eq "Paragraph_Separator" 0 0 0
Condition0%0/27
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp" or $v eq "Nonspacing_Mark" or $v eq "Enclosing_Mark" or $v eq "Format" or $v eq "Line_Separator" or $v eq "Paragraph_Separator"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp" or $v eq "Nonspacing_Mark" or $v eq "Enclosing_Mark" or $v eq "Format" or $v eq "Line_Separator"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp" or $v eq "Nonspacing_Mark" or $v eq "Enclosing_Mark" or $v eq "Format"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp" or $v eq "Nonspacing_Mark" or $v eq "Enclosing_Mark"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp" or $v eq "Nonspacing_Mark"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me" or $v eq "Cf"
l!l&&r!l&&!r
000
$v eq "Mn" or $v eq "Me"
l!l&&r!l&&!r
000
MC/DC0%0/10
$v eq "Mn" or $v eq "Me" or $v eq "Cf" or $v eq "Zl" or $v eq "Zp" or $v eq "Nonspacing_Mark" or $v eq "Enclosing_Mark" or $v eq "Format" or $v eq "Line_Separator" or $v eq "Paragraph_Separator"
$v eq "Mn" $v eq "Me" $v eq "Cf" $v eq "Zl" $v eq "Zp" $v eq "Nonspacing_Mark" $v eq "Enclosing_Mark" $v eq "Format" $v eq "Line_Separator" $v eq "Paragraph_Separator"
Truth table0%0/11
A or B or C or D or E or F or G or H or I or J
A= $v eq "Mn"B= $v eq "Me"C= $v eq "Cf"D= $v eq "Zl"E= $v eq "Zp"F= $v eq "Nonspacing_Mark"G= $v eq "Enclosing_Mark"H= $v eq "Format"I= $v eq "Line_Separator"J= $v eq "Paragraph_Separator"
ABCDEFGHIJresult
1XXXXXXXXX1
01XXXXXXXX1
001XXXXXXX1
0001XXXXXX1
00001XXXXX1
000001XXXX1
0000001XXX1
00000001XX1
000000001X1
00000000011
00000000000
766 if $v eq 'Mn' || $v eq 'Me' || $v eq 'Cf' || $v eq 'Zl' || $v eq 'Zp'
767 || $v eq 'Nonspacing_Mark' || $v eq 'Enclosing_Mark'
768 || $v eq 'Format' || $v eq 'Line_Separator'
769 || $v eq 'Paragraph_Separator';
770 }
771
772 0 $i = Unicode::UCD::search_invlist($dig->[0], $cp);
773 0 ▶ if (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 0 0 0
774 0 ▶ my $v = $dig->[1]->[$i] // '';
Condition0%0/2
$dig->[1][$i] // ""
l!l
00
MC/DC0%0/1
$dig->[1][$i] // ""
$dig->[1][$i]
Truth table0%0/2
A
A= $dig->[1][$i]
Aresult
11
00
775 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "Y" or $v eq "Yes" or $v eq "T" or $v eq "True" or $v eq 1 0 0 0
Condition0%0/12
$v eq "Y" or $v eq "Yes" or $v eq "T" or $v eq "True" or $v eq 1
l!l&&r!l&&!r
000
$v eq "Y" or $v eq "Yes" or $v eq "T" or $v eq "True"
l!l&&r!l&&!r
000
$v eq "Y" or $v eq "Yes" or $v eq "T"
l!l&&r!l&&!r
000
$v eq "Y" or $v eq "Yes"
l!l&&r!l&&!r
000
MC/DC0%0/5
$v eq "Y" or $v eq "Yes" or $v eq "T" or $v eq "True" or $v eq 1
$v eq "Y" $v eq "Yes" $v eq "T" $v eq "True" $v eq 1
Truth table0%0/6
A or B or C or D or E
A= $v eq "Y"B= $v eq "Yes"C= $v eq "T"D= $v eq "True"E= $v eq 1
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
776 if $v eq 'Y' || $v eq 'Yes' || $v eq 'T' || $v eq 'True' || $v eq '1';
777 }
778
779 0 $i = Unicode::UCD::search_invlist($eaw->[0], $cp);
780 0 ▶ if (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 0 0 0
781 0 ▶ my $v = $eaw->[1]->[$i] // '';
Condition0%0/2
$eaw->[1][$i] // ""
l!l
00
MC/DC0%0/1
$eaw->[1][$i] // ""
$eaw->[1][$i]
Truth table0%0/2
A
A= $eaw->[1][$i]
Aresult
11
00
782 0 ▶ return 2 if $v eq 'W' || $v eq 'F' || $v eq 'Wide' || $v eq 'Fullwidth';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "W" or $v eq "F" or $v eq "Wide" or $v eq "Fullwidth" 0 0 0
Condition0%0/9
$v eq "W" or $v eq "F" or $v eq "Wide" or $v eq "Fullwidth"
l!l&&r!l&&!r
000
$v eq "W" or $v eq "F" or $v eq "Wide"
l!l&&r!l&&!r
000
$v eq "W" or $v eq "F"
l!l&&r!l&&!r
000
MC/DC0%0/4
$v eq "W" or $v eq "F" or $v eq "Wide" or $v eq "Fullwidth"
$v eq "W" $v eq "F" $v eq "Wide" $v eq "Fullwidth"
Truth table0%0/5
A or B or C or D
A= $v eq "W"B= $v eq "F"C= $v eq "Wide"D= $v eq "Fullwidth"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
783 0 ▶ return $is_cjk ? 2 : 1 if $v eq 'A' || $v eq 'Ambiguous';
Branch: 2 branches
BranchTrueFalseTotal
if $v eq "A" or $v eq "Ambiguous" 0 0 0
$Termbox::is_cjk ? : 0 0 0
Condition0%0/3
$v eq "A" or $v eq "Ambiguous"
l!l&&r!l&&!r
000
MC/DC0%0/2
$v eq "A" or $v eq "Ambiguous"
$v eq "A" $v eq "Ambiguous"
Truth table0%0/3
A or B
A= $v eq "A"B= $v eq "Ambiguous"
ABresult
1X1
011
000
784 }
785
786 # Emoji fallback for old runtime Unicode databases
787 0 ▶ return 2
Branch: 1 branch
BranchTrueFalseTotal
if $unicode_version lt "9.0.0" and $cp >= 127744 and $cp <= 129791 0 0 0
Condition0%0/6
$unicode_version lt "9.0.0" and $cp >= 127744 and $cp <= 129791
!ll&&!rl&&r
000
$unicode_version lt "9.0.0" and $cp >= 127744
!ll&&!rl&&r
000
MC/DC0%0/3
$unicode_version lt "9.0.0" and $cp >= 127744 and $cp <= 129791
$unicode_version lt "9.0.0" $cp >= 127744 $cp <= 129791
Truth table0%0/4
A and B and C
A= $unicode_version lt "9.0.0"B= $cp >= 127744C= $cp <= 129791
ABCresult
0XX0
10X0
1100
1111
788 if $unicode_version lt '9.0.0' && $cp >= 0x1F300 && $cp <= 0x1FAFF;
789
790 0 return 1;
791 }
792
793 # Use NCCS (if supported); otherwise, read at least 32 fields.
794 39 ▶BEGIN { if (eval { &POSIX::NCCS }) {
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { &POSIX::NCCS } }) { } 39 0 39
795 39 *NCCS = \&POSIX::NCCS;
796 } else {
797 0 *NCCS = sub () { 32 };
798 }}
799
800 0 ▶sub cfmakeraw { # void (\$tios)
Subroutine cfmakeraw: not called CC=3 SCAR=24.8
Pod undocumented
801 0 my ($tios) = @_;
802 0 ▶ return unless blessed($tios) && $tios->isa('POSIX::Termios');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($tios) and $tios->isa("POSIX::Termios") 0 0 0
Condition0%0/3
builtin::blessed($tios) and $tios->isa("POSIX::Termios")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($tios) and $tios->isa("POSIX::Termios")
builtin::blessed($tios) $tios->isa("POSIX::Termios")
Truth table0%0/3
A and B
A= builtin::blessed($tios)B= $tios->isa("POSIX::Termios")
ABresult
0X0
100
111
803 0 $tios->setiflag(
804 $tios->getiflag()
805 & ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON)
806 );
807 0 $tios->setoflag(
808 $tios->getoflag()
809 & ~OPOST
810 );
811 0 $tios->setlflag(
812 $tios->getlflag()
813 & ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN)
814 );
815 0 $tios->setcflag(
816 ($tios->getcflag() & ~(CSIZE | PARENB)) | CS8
817 );
818 0 return;
819 }
820
821 0 ▶my $clone = sub { # $clone ()
Subroutine __ANON__: not called CC=4 SCAR=30.0
822 0 my ($src) = @_;
823 0 ▶ return undef unless blessed($src) && $src->isa('POSIX::Termios');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($src) and $src->isa("POSIX::Termios") 0 0 0
Condition0%0/3
builtin::blessed($src) and $src->isa("POSIX::Termios")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($src) and $src->isa("POSIX::Termios")
builtin::blessed($src) $src->isa("POSIX::Termios")
Truth table0%0/3
A and B
A= builtin::blessed($src)B= $src->isa("POSIX::Termios")
ABresult
0X0
100
111
824 0 my $dst = POSIX::Termios->new();
825 0 $dst->setiflag($src->getiflag());
826 0 $dst->setoflag($src->getoflag());
827 0 $dst->setcflag($src->getcflag());
828 0 $dst->setlflag($src->getlflag());
829 0 $dst->setispeed($src->getispeed());
830 0 $dst->setospeed($src->getospeed());
831 0 for my $i (0 .. NCCS - 1) {
832 0 $dst->setcc($i, $src->getcc($i));
833 }
834 0 return $dst;
835 39 };
836
837 # ------------------------------------------------------------------------
838 # Params Check -----------------------------------------------------------
839 # ------------------------------------------------------------------------
840
841 # This sub is a wrapper around Params::Check::check that provides positional
842 # argument checking. It returns a coderef that can be used to check the
843 # arguments of a function. The coderef will return the original arguments if
844 # they pass the checks, or will die with an error message if they fail.
845 # See Type::Params::compile for the inspiration for this function.
846 270 ▶sub compile {
Subroutine compile: called CC=2 SCAR=6.9
Pod undocumented
847 0 ▶ return sub { @_ } unless STRICT;
Subroutine __ANON__: not called CC=1 SCAR=1.2
848
849 270 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 41 229 270
850 61 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
851 61 ▶ Carp::croak "Expected no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 61 61
852 61 return @_;
853 41 };
854 }
855
856 229 my %tmpl;
857 229 @tmpl{1 .. @_} = @_;
858
859 31742 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
860 31742 my %in;
861 31742 @in{1 .. @_} = @_;
862
863 31742 local $Params::Check::STRIP_LEADING_DASHES = 0;
864 31742 local $Params::Check::PRESERVE_CASE = 1;
865 31742 local $Params::Check::CALLER_DEPTH
866 = $Params::Check::CALLER_DEPTH + 1;
867
868 31742 ▶ Params::Check::check(\%tmpl, \%in, 0) or do {
Branch: 1 branch
BranchTrueFalseTotal
Params::Check::check(\%tmpl, \%in, 0) or do { local $_ = Params::Check::last_error(); s/\bKey '(\d+)'/Argument #$1/g; Carp::croak($_) } 31733 9 31742
869 9 local $_ = Params::Check::last_error();
870 9 s/\bKey '(\d+)'/Argument #$1/g;
871 9 Carp::croak $_;
872 };
873
874 31733 return @_;
875 229 };
876 }
877
878 # Types::Standard like templates for Params::Check.
879 39 ▶use constant {
Subroutine BEGIN: called CC=2 SCAR=7.6
880 _Bool => { required => 1, allow => qr/\A[01]?\z/ },
881 _Str => {
882 required => 1,
883 defined => 1, default => '', strict_type => 1
884 },
885 _Int => { required => 1, defined => 1, allow => qr/\A[-]?\d+\z/ },
886 _ClassName => { required => 1, defined => 1, allow => qr/\A\w+(::\w+)*\z/ },
887 _Ref => {
888 required => 1,
889 0 ▶ allow => ($] >= 5.016) ? \&CORE::ref : sub { ref($_[0]) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
890 },
891 _ScalarRef => {
892 required => 1, defined => 1, default => \undef,
893 strict_type => 1
894 },
895 _ArrayRef => { required => 1, defined => 1, default => [], strict_type => 1 },
896 _CodeRef => {
897 0 ▶ required => 1, defined => 1, default => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
898 39 ▶ strict_type => 1
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.016 ? : 39 0 39
899 },
900 _Object => { required => 1, allow => \&blessed },
901 39 };
902
903 10 ▶sub _Maybe ($) {
Subroutine _Maybe: called CC=4 SCAR=14.4
904 10 my ($aref) = @_;
905
906 10 ▶ Carp::croak("_Maybe['a] expects exactly one template hashref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aref eq "ARRAY" and @$aref == 1 and ref $aref->[0] eq "HASH" 0 10 10
Condition33%2/6
ref $aref eq "ARRAY" and @$aref == 1 and ref $aref->[0] eq "HASH"
!ll&&!rl&&r
0010
ref $aref eq "ARRAY" and @$aref == 1
!ll&&!rl&&r
0010
MC/DC0%0/3
ref $aref eq "ARRAY" and @$aref == 1 and ref $aref->[0] eq "HASH"
ref $aref eq "ARRAY" @$aref == 1 ref $aref->[0] eq "HASH"
Truth table25%1/4
A and B and C
A= ref $aref eq "ARRAY"B= @$aref == 1C= ref $aref->[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
907 unless ref($aref) eq 'ARRAY' && @$aref == 1 && ref($aref->[0]) eq 'HASH';
908
909 10 my ($t) = @$aref;
910 10 my %u = %$t;
911
912 10 my $allow = delete $u{allow};
913 10 my $strict_type = delete $u{strict_type};
914 10 my $default = $u{default};
915
916 10 $u{defined} = 0;
917 64 ▶ $u{allow} = sub {
Subroutine __ANON__: called CC=6 SCAR=18.3
918 64 my ($v) = @_;
919 64 ▶ return 1 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 24 40 64
920 40 ▶ return 0 if $strict_type && ref($v) ne ref($default);
Branch: 1 branch
BranchTrueFalseTotal
if $strict_type and ref $v ne ref $default 0 40 40
Condition66%2/3
$strict_type and ref $v ne ref $default
!ll&&!rl&&r
18220
MC/DC0%0/2
$strict_type and ref $v ne ref $default
$strict_type ref $v ne ref $default
Truth table66%2/3
A and B
A= $strict_typeB= ref $v ne ref $default
ABresult
0X0
100
111
921 40 ▶ return 1 unless defined $allow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow 22 18 40
922 18 ▶ return Params::Check::allow(
Branch: 1 branch
BranchTrueFalseTotal
ref $allow eq 'ARRAY' ? : 0 18 18
923 $v,
924 ref($allow) eq 'ARRAY' ? $allow : [ $allow ],
925 );
926 10 };
927 10 return \%u;
928 }
929
930 # Types::Common::Numeric like templates for Params::Check.
931 39 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
932 39 _PositiveInt => { required => 1, defined => 1, allow => qr/\A[1-9]\d*\z/ },
933 _PositiveOrZeroInt => { required => 1, defined => 1, allow => qr/\A\d+\z/ },
934 39 };
935
936 # ------------------------------------------------------------------------
937 # Debug helpers ----------------------------------------------------------
938 # ------------------------------------------------------------------------
939
940 # The tracing and debugging helpers were inspired by the debug macros
941 # used by libserialport.
942
943 # Internal debugging helper
944 10 ▶sub DEBUG($@) { # void ($fmt, @args)
Subroutine DEBUG: called CC=2 SCAR=7.0
Pod undocumented
945 10 ▶ return unless defined &Termbox::tb_debug_handler;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Termbox::tb_debug_handler 0 10 10
946 10 my $fmt = shift;
947 # suppress warnings and exceptions from the debug handler
948 10 try: local $@; eval {
949 10 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
950 10 Termbox::tb_debug_handler("$fmt.\n", @_);
951 };
952 10 return;
953 }
954
955 # Internal tracing helper
956 4 ▶sub TRACE($@) { # void ($fmt, @args)
Subroutine TRACE: called CC=2 SCAR=7.0
Pod undocumented
957 4 my $fmt = shift;
958 4 ▶ my $sub = (caller(1))[3] // '__ANON__';
Condition50%1/2
(caller 1)[3] // "__ANON__"
l!l
40
MC/DC0%0/1
(caller 1)[3] // "__ANON__"
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
959 4 (my $func = $sub) =~ s/^.*:://;
960 4 DEBUG("%s($fmt) called", $func, @_);
961 4 return;
962 }
963
964 4 ▶sub Termbox::OnLeavingScope::DESTROY {
Subroutine DESTROY: called CC=3 SCAR=12.3
965 4 my $self = shift;
966 4 my $func = $self->{func};
967 4 my $rv_ref = $self->{rv};
968 4 ▶ if (defined $rv_ref && defined $$rv_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv_ref and defined $$rv_ref) { } 4 0 4
Condition33%1/3
defined $rv_ref and defined $$rv_ref
!ll&&!rl&&r
004
MC/DC0%0/2
defined $rv_ref and defined $$rv_ref
defined $rv_ref defined $$rv_ref
Truth table33%1/3
A and B
A= defined $rv_refB= defined $$rv_ref
ABresult
0X0
100
111
969 4 DEBUG("\&%s returning %d", $func, int($$rv_ref));
970 } else {
971 0 DEBUG("\&%s returning", $func);
972 }
973 }
974
975 # Internal scope guard used to trace subroutine return values
976 4 ▶sub TRACE_LEAVE($) { # $guard (\$rv)
Subroutine TRACE_LEAVE: called CC=2 SCAR=7.0
Pod undocumented
977 4 my $rv_ref = shift;
978 4 ▶ my $sub = (caller(1))[3] // '__ANON__';
Condition50%1/2
(caller 1)[3] // "__ANON__"
l!l
40
MC/DC0%0/1
(caller 1)[3] // "__ANON__"
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
979 4 (my $func = $sub) =~ s/^.*:://;
980 4 return bless {
981 func => $func,
982 rv => $rv_ref
983 } => 'Termbox::OnLeavingScope';
984 }
985
986 # ------------------------------------------------------------------------
987 # WinVT ------------------------------------------------------------------
988 # ------------------------------------------------------------------------
989
990 39 ▶use if _WIN32, 'Time::HiRes';
Subroutine BEGIN: called CC=1 SCAR=0.0
991 39 ▶use if _WIN32, 'Win32::Console';
Subroutine BEGIN: called CC=1 SCAR=0.0
992 39 ▶use if _WIN32, 'Win32API::File', qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
993 :Misc
994 :Func
995 :FILE_TYPE_
996 39 );
997
998 # Windows Error Codes
999 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1000 ERROR_ACCESS_DENIED => 0x5,
1001 ERROR_INVALID_HANDLE => 0x6,
1002 ERROR_INVALID_PARAMETER => 0x57,
1003 ERROR_BROKEN_PIPE => 0x6d,
1004 WSAEINVAL => 0x2726,
1005 39 };
1006
1007 # Windows INPUT_RECORD EventType
1008 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1009 KEY_EVENT => 0x0001,
1010 MOUSE_EVENT => 0x0002,
1011 WINDOW_BUFFER_SIZE_EVENT => 0x0004,
1012 MENU_EVENT => 0x0008,
1013 FOCUS_EVENT => 0x0010,
1014 39 };
1015
1016 # Windows Input mode flags
1017 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1018 ENABLE_INSERT_MODE => 0x0020,
1019 ENABLE_QUICK_EDIT_MODE => 0x0040,
1020 ENABLE_VIRTUAL_TERMINAL_INPUT => 0x0200,
1021 ENABLE_EXTENDED_FLAGS => 0x0080,
1022 39 };
1023
1024 # Windows Output mode flags
1025 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1026 ENABLE_VIRTUAL_TERMINAL_PROCESSING => 0x0004,
1027 DISABLE_NEWLINE_AUTO_RETURN => 0x0008,
1028 ENABLE_LVB_GRID_WORLDWIDE => 0x0010,
1029 39 };
1030
1031 # Windows codepage's
1032 # https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
1033 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1034 CP_UTF8 => 65001,
1035 39 };
1036
1037 # Windows PeekConsoleInput/ReadConsoleInput constants
1038 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1039 # INPUT_RECORD
1040 wEventType => 0,
1041 # KEY_EVENT_RECORD
1042 bKeyDown => 1,
1043 wRepeatCount => 2,
1044 wVirtualKeyCode => 3,
1045 wVirtualScanCode => 4,
1046 UnicodeChar => 5,
1047 AsciiChar => 5,
1048 dwControlKeyState => 6,
1049 39 };
1050
1051 # Windows Virtual-Key Codes
1052 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1053 VK_SHIFT => 0x10, # SHIFT key
1054 VK_CONTROL => 0x11, # CTRL key
1055 VK_MENU => 0x12, # ALT key
1056 VK_CAPITAL => 0x14, # CAPS LOCK key
1057 VK_NUMLOCK => 0x90, # NUM LOCK key
1058 VK_SCROLL => 0x91, # SCROLL LOCK key
1059 39 };
1060
1061 # Windows wait event constants
1062 39 ▶use if _WIN32, constant => {
Subroutine BEGIN: called CC=1 SCAR=0.0
1063 INFINITE => 0xffffffff,
1064 WAIT_OBJECT_0 => 0x00000000,
1065 WAIT_TIMEOUT => 0x00000102,
1066 39 };
1067
1068 # Windows ReadConsoleInputW/WaitForSingleObject imports
1069 39 ▶BEGIN { if (_WIN32) {
Subroutine BEGIN: called CC=1 SCAR=0.0
1070 require Win32::API;
1071
1072 Win32::API::More->Import('kernel32',
1073 'DWORD WINAPI WaitForSingleObject(
1074 HANDLE hHandle,
1075 DWORD dwMilliseconds
1076 )'
1077 ) or die "Import WaitForSingleObject: $^E";
1078
1079 my $ReadConsoleInputW = Win32::API::More->new('kernel32',
1080 'BOOL WINAPI ReadConsoleInputW(
1081 HANDLE hConsoleInput,
1082 LPVOID lpBuffer,
1083 DWORD nLength,
1084 LPDWORD lpNumberOfEventsRead
1085 )'
1086 ) or die "Import ReadConsoleInputW: $^E";
1087
1088 # Win32::Console::_ReadConsoleInput like wrapper for ReadConsoleInputW
1089 0 ▶ *ReadConsoleInputW = sub { # @events ($hConsoleInput)
Subroutine __ANON__: not called CC=11 SCAR=48.8
1090 0 state $sig = compile(
1091 _Int,
1092 );
1093 0 my ($hInput) = $sig->(@_);
1094
1095 0 my $lpBuffer = "\0" x 20;
1096 0 my $read = 0;
1097 return ()
1098 0 ▶ unless $ReadConsoleInputW->Call($hInput, $lpBuffer, 1, $read) && $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $ReadConsoleInputW->Call($hInput, $lpBuffer, 1, $read) and $read 0 0 0
Condition0%0/3
$ReadConsoleInputW->Call($hInput, $lpBuffer, 1, $read) and $read
!ll&&!rl&&r
000
MC/DC0%0/2
$ReadConsoleInputW->Call($hInput, $lpBuffer, 1, $read) and $read
$ReadConsoleInputW->Call($hInput, $lpBuffer, 1, $read) $read
Truth table0%0/3
A and B
A= $ReadConsoleInputW->Call($hInput, $lpBuffer, 1, $read)B= $read
ABresult
0X0
100
111
1099
1100 0 my @ir = unpack('S', $lpBuffer);
1101 0 ▶ my $type = $ir[0] // 0;
Condition0%0/2
$ir[0] // 0
l!l
00
MC/DC0%0/1
$ir[0] // 0
$ir[0]
Truth table0%0/2
A
A= $ir[0]
Aresult
11
00
1102 0 switch: for ($type) {
1103 0 ▶ case: KEY_EVENT() == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
KEY_EVENT() == $_ and do { @ir = (@ir, unpack("x4 L S S S S L", $lpBuffer)); last } 0 0 0
1104 0 @ir = ( @ir, unpack('x4 L S S S S L', $lpBuffer) );
1105 0 last;
1106 };
1107 0 ▶ case: MOUSE_EVENT() == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
MOUSE_EVENT() == $_ and do { @ir = (@ir, unpack("x4 s2 L L L", $lpBuffer)); last } 0 0 0
1108 0 @ir = ( @ir, unpack('x4 s2 L L L', $lpBuffer) );
1109 0 last;
1110 };
1111 0 ▶ case: WINDOW_BUFFER_SIZE_EVENT() == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
WINDOW_BUFFER_SIZE_EVENT() == $_ and do { @ir = (@ir, unpack("x4 s2", $lpBuffer)); last } 0 0 0
1112 0 @ir = ( @ir, unpack('x4 s2', $lpBuffer) );
1113 0 last;
1114 };
1115 case: MENU_EVENT() == $_ ||
1116 FOCUS_EVENT() == $_
1117 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
MENU_EVENT() == $_ || FOCUS_EVENT() == $_ and do { @ir = (@ir, unpack("x4 L", $lpBuffer)); last } 0 0 0
Condition0%0/3
MENU_EVENT() == $_ or FOCUS_EVENT() == $_
l!l&&r!l&&!r
000
MC/DC0%0/2
MENU_EVENT() == $_ or FOCUS_EVENT() == $_
MENU_EVENT() == $_ FOCUS_EVENT() == $_
Truth table0%0/3
A or B
A= MENU_EVENT() == $_B= FOCUS_EVENT() == $_
ABresult
1X1
011
000
1118 0 @ir = ( @ir, unpack('x4 L', $lpBuffer) );
1119 0 last;
1120 };
1121 default: {
1122 0 return ();
1123 }
1124 }
1125 0 ▶ return @ir > 1 ? @ir : ();
Branch: 1 branch
BranchTrueFalseTotal
@ir > 1 ? : 0 0 0
1126 };
1127 }}
1128
1129 # ------------------------------------------------------------------------
1130 # Helper classes ---------------------------------------------------------
1131 # ------------------------------------------------------------------------
1132
1133 #
1134 # The 'Termbox::Cell' class represents a single cell in the terminal.
1135 #
1136
1137 11609 ▶sub Termbox::Cell::new { # $cell ()
Subroutine new: called CC=1 SCAR=0.0
1138 11609 state $sig = compile(
1139 _ClassName,
1140 );
1141 11609 my ($class) = $sig->(@_);
1142 11609 return bless [
1143 "\0", # UFT8 string
1144 0, # bitwise foreground attributes
1145 0, # bitwise background attributes
1146 ], $class;
1147 }
1148
1149 8 ▶sub Termbox::Cell::ch { ord $_[0]->[0] }
Subroutine ch: called CC=1 SCAR=0.0
1150 5 ▶sub Termbox::Cell::fg { 0+ $_[0]->[1] }
Subroutine fg: called CC=1 SCAR=0.0
1151 4 ▶sub Termbox::Cell::bg { 0+ $_[0]->[2] }
Subroutine bg: called CC=1 SCAR=0.0
1152 39 if (TB_OPT_EGC) {
1153 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
1154 2 ▶ *Termbox::Cell::ech = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1155 2 my $ch = $_[0]->[0];
1156 2 ▶ length($ch) > 1 ? [ unpack('U*', $ch) ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $ch > 1 ? : 2 0 2
1157 };
1158 0 ▶ *Termbox::Cell::nech = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1159 0 my $ch = $_[0]->[0];
1160 0 my $n = length($ch);
1161 0 ▶ return ($n && $ch =~ /\A\X\z/) ? $n : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n && $ch =~ /\A\X\z/ ? : 0 0 0
Condition0%0/3
$n and $ch =~ /\A\X\z/
!ll&&!rl&&r
000
MC/DC0%0/2
$n and $ch =~ /\A\X\z/
$n $ch =~ /\A\X\z/
Truth table0%0/3
A and B
A= $nB= $ch =~ /\A\X\z/
ABresult
0X0
100
111
1162 };
1163 0 ▶ *Termbox::Cell::cech = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1164 0 my $ech = $_[0]->ech;
1165 0 ▶ $ech ? scalar(@$ech) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ech ? : 0 0 0
1166 };
1167 }
1168
1169 11583 ▶sub Termbox::Cell::set { # $int ($ch, $fg, $bg)
Subroutine set: called CC=2 SCAR=6.9
1170 11583 state $sig = compile(
1171 _Object,
1172 _Str,
1173 _PositiveOrZeroInt,
1174 _PositiveOrZeroInt,
1175 );
1176 11583 my ($cell, $ch, $fg, $bg) = $sig->(@_);
1177
1178 11583 ▶ $ch = "\0" unless length($ch);
Branch: 1 branch
BranchTrueFalseTotal
unless length $ch 2 11581 11583
1179 11583 $cell->[0] = TB_OPT_EGC ? $ch : substr($ch, 0, 1);
1180 11583 $cell->[1] = $fg;
1181 11583 $cell->[2] = $bg;
1182 11583 return TB_OK;
1183 }
1184
1185 6 ▶sub Termbox::Cell::equal { # $bool ($other)
Subroutine equal: called CC=6 SCAR=18.2
1186 6 state $sig = compile(
1187 _Object,
1188 _Object,
1189 );
1190 6 my ($a, $b) = $sig->(@_);
1191 6 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if not ref $a && ref $b or $a->[0] ne $b->[0] or $a->[1] != $b->[1] or $a->[2] != $b->[2] 3 3 6
Condition75%9/12
not ref $a && ref $b or $a->[0] ne $b->[0] or $a->[1] != $b->[1] or $a->[2] != $b->[2]
l!l&&r!l&&!r
213
not ref $a && ref $b or $a->[0] ne $b->[0] or $a->[1] != $b->[1]
l!l&&r!l&&!r
114
not ref $a && ref $b or $a->[0] ne $b->[0]
l!l&&r!l&&!r
015
ref $a && ref $b
!ll&&!rl&&r
006
MC/DC60%3/5
not ref $a && ref $b or $a->[0] ne $b->[0] or $a->[1] != $b->[1] or $a->[2] != $b->[2]
ref $a ref $b $a->[0] ne $b->[0] $a->[1] != $b->[1] $a->[2] != $b->[2]
Truth table66%4/6
not(A && B) or C or D or E
A= ref $aB= ref $bC= $a->[0] ne $b->[0]D= $a->[1] != $b->[1]E= $a->[2] != $b->[2]
ABCDEresult
0XXXX1
10XXX1
111XX1
1101X1
110011
110000
1192 if !(ref $a && ref $b)
1193 || $a->[0] ne $b->[0] # ch
1194 || $a->[1] != $b->[1] # fg
1195 || $a->[2] != $b->[2]; # bg
1196 3 return 1;
1197 }
1198
1199 21 ▶sub Termbox::Cell::copy { # $int ($src)
Subroutine copy: called CC=1 SCAR=0.0
1200 21 state $sig = compile(
1201 _Object,
1202 _Object,
1203 );
1204 21 my ($dst, $src) = $sig->(@_);
1205 21 @$dst = @$src;
1206 21 return TB_OK;
1207 }
1208
1209 #
1210 # The 'Termbox::Event' class represents an input event from the terminal.
1211 #
1212
1213 17 ▶sub Termbox::Event::new { # $event ()
Subroutine new: called CC=1 SCAR=0.0
1214 17 state $sig = compile(
1215 _ClassName,
1216 );
1217 17 my ($class) = $sig->(@_);
1218 17 return bless {
1219 type => 0, # one of 'TB_EVENT_*' constants
1220 mod => 0, # bitwise 'TB_MOD_*' constants
1221 key => 0, # one of 'TB_KEY_*' constants
1222 ch => 0, # a Unicode codepoint
1223 w => 0, # resize height
1224 h => 0, # resize height
1225 x => 0, # mouse x
1226 y => 0, # mouse y
1227 }, $class;
1228 };
1229
1230 1 ▶sub Termbox::Event::type { $_[0]->{type} }
Subroutine type: called CC=1 SCAR=0.0
1231 1 ▶sub Termbox::Event::mod { $_[0]->{mod} }
Subroutine mod: called CC=1 SCAR=0.0
1232 1 ▶sub Termbox::Event::key { $_[0]->{key} }
Subroutine key: called CC=1 SCAR=0.0
1233 1 ▶sub Termbox::Event::ch { $_[0]->{ch} }
Subroutine ch: called CC=1 SCAR=0.0
1234 1 ▶sub Termbox::Event::w { $_[0]->{w} }
Subroutine w: called CC=1 SCAR=0.0
1235 1 ▶sub Termbox::Event::h { $_[0]->{h} }
Subroutine h: called CC=1 SCAR=0.0
1236 1 ▶sub Termbox::Event::x { $_[0]->{x} }
Subroutine x: called CC=1 SCAR=0.0
1237 1 ▶sub Termbox::Event::y { $_[0]->{y} }
Subroutine y: called CC=1 SCAR=0.0
1238
1239 #
1240 # The 'cellbuf' class represents the cell buffer of the terminal,
1241 # which holds the state of each cell on the screen.
1242 #
1243
1244 118 ▶sub cellbuf::new { # $cellbuf ()
Subroutine new: called CC=1 SCAR=0.0
1245 118 state $sig = compile(
1246 _ClassName,
1247 );
1248 118 my ($class) = $sig->(@_);
1249 118 return bless {
1250 width => 0,
1251 height => 0,
1252 cells => [],
1253 }, $class;
1254 };
1255
1256 21 ▶sub cellbuf::init { # $int ($width, $height)
Subroutine init: called CC=1 SCAR=0.0
1257 21 state $sig = compile(
1258 _Object,
1259 _PositiveInt,
1260 _PositiveInt,
1261 );
1262 21 my ($c, $w, $h) = $sig->(@_);
1263 21 $c->{width} = $w;
1264 21 $c->{height} = $h;
1265 21 $c->{cells} = [ map { Termbox::Cell->new() } 0 .. $w*$h-1 ];
1266 21 return TB_OK;
1267 }
1268
1269 14 ▶sub cellbuf::clear { # $int ()
Subroutine clear: called CC=3 SCAR=11.0
1270 14 state $sig = compile(
1271 _Object,
1272 );
1273 14 my ($c) = $sig->(@_);
1274 14 my $rv;
1275 14 for my $i (0 .. $c->{width}*$c->{height}-1) {
1276 11557 $c->{cells}[$i]->set(' ', $global->{fg}, $global->{bg});
1277 }
1278 14 ▶ $global->{dirty_lines} = [ (1) x ($c->{height} || 0) ];
Condition100%2/2
$c->{'height'} || 0
l!l
131
MC/DC100%1/1
$c->{'height'} || 0
$c->{'height'}
Truth table100%2/2
A
A= $c->{'height'}
Aresult
11
00
1279 14 return TB_OK;
1280 }
1281
1282 32 ▶sub cellbuf::get { # $cell ($x, $y)
Subroutine get: called CC=1 SCAR=0.0
1283 32 state $sig = compile(
1284 _Object,
1285 _PositiveOrZeroInt,
1286 _PositiveOrZeroInt,
1287 );
1288 32 my ($c, $x, $y) = $sig->(@_);
1289 32 return $c->{cells}[ $x + $y * $c->{width} ];
1290 }
1291
1292 47 ▶sub cellbuf::in_bounds { # $bool ($x, $y)
Subroutine in_bounds: called CC=5 SCAR=16.4
1293 47 state $sig = compile(
1294 _Object,
1295 _PositiveOrZeroInt,
1296 _PositiveOrZeroInt,
1297 );
1298 47 my ($c, $x, $y) = $sig->(@_);
1299 46 ▶ if ($x < 0 || $x >= $c->{width} || $y < 0 || $y >= $c->{height}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $x >= $c->{"width"} or $y < 0 or $y >= $c->{"height"}) 3 43 46
Condition66%6/9
$x < 0 or $x >= $c->{"width"} or $y < 0 or $y >= $c->{"height"}
l!l&&r!l&&!r
3043
$x < 0 or $x >= $c->{"width"} or $y < 0
l!l&&r!l&&!r
3043
$x < 0 or $x >= $c->{"width"}
l!l&&r!l&&!r
0343
MC/DC25%1/4
$x < 0 or $x >= $c->{"width"} or $y < 0 or $y >= $c->{"height"}
$x < 0 $x >= $c->{"width"} $y < 0 $y >= $c->{"height"}
Truth table40%2/5
A or B or C or D
A= $x < 0B= $x >= $c->{"width"}C= $y < 0D= $y >= $c->{"height"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1300 3 return 0;
1301 }
1302 43 return 1;
1303 }
1304
1305 5 ▶sub cellbuf::resize { # $int ($width, $height)
Subroutine resize: called CC=14 SCAR=27.5
1306 5 state $sig = compile(
1307 _Object,
1308 _PositiveInt,
1309 _PositiveInt,
1310 );
1311 5 my ($c, $w, $h) = $sig->(@_);
1312 5 my $rv;
1313
1314 5 my $ow = $c->{width};
1315 5 my $oh = $c->{height};
1316
1317 5 ▶ if ($ow == $w && $oh == $h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ow == $w and $oh == $h) 0 5 5
Condition33%1/3
$ow == $w and $oh == $h
!ll&&!rl&&r
500
MC/DC0%0/2
$ow == $w and $oh == $h
$ow == $w $oh == $h
Truth table33%1/3
A and B
A= $ow == $wB= $oh == $h
ABresult
0X0
100
111
1318 0 return TB_OK;
1319 }
1320
1321 5 ▶ $w = $w < 1 ? 1 : $w;
Branch: 1 branch
BranchTrueFalseTotal
$w < 1 ? : 0 5 5
1322 5 ▶ $h = $h < 1 ? 1 : $h;
Branch: 1 branch
BranchTrueFalseTotal
$h < 1 ? : 0 5 5
1323
1324 5 ▶ my $minw = ($w < $ow) ? $w : $ow;
Branch: 1 branch
BranchTrueFalseTotal
$w < $ow ? : 2 3 5
1325 5 ▶ my $minh = ($h < $oh) ? $h : $oh;
Branch: 1 branch
BranchTrueFalseTotal
$h < $oh ? : 1 4 5
1326
1327 5 my $prev = $c->{cells};
1328
1329 5 $rv = $c->init($w, $h);
1330 5 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 5 5
1331 5 $rv = $c->clear();
1332 5 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 5 5
1333
1334 5 my ($x, $y);
1335 5 ▶ for ($x = 0; $x < $minw; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $minw) 8 5 13
1336 8 ▶ for ($y = 0; $y < $minh; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $minh) 19 8 27
1337 19 my ($src, $dst);
1338 19 $src = $prev->[($y * $ow) + $x];
1339 19 ▶ return TB_ERR_OUT_OF_BOUNDS unless $c->in_bounds($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $c->in_bounds($x, $y) 0 19 19
1340 19 $dst = $c->get($x, $y);
1341 19 ▶ return TB_ERR unless defined $dst;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dst 0 19 19
1342 19 $rv = $dst->copy($src);
1343 19 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 19 19
1344 }
1345 }
1346
1347 5 return TB_OK;
1348 }
1349
1350 #
1351 # captrie class for terminal capability matching
1352 #
1353
1354 57 ▶sub captrie::new { # $captrie ()
Subroutine new: called CC=1 SCAR=0.0
1355 57 state $sig = compile(
1356 _ClassName,
1357 );
1358 57 my ($class) = $sig->(@_);
1359 57 return bless {
1360 exact => {},
1361 prefixes => {},
1362 alt => undef,
1363 }, $class;
1364 }
1365
1366 6 ▶sub captrie::clear { # $int ()
Subroutine clear: called CC=1 SCAR=0.0
1367 6 state $sig = compile(
1368 _Object,
1369 );
1370 6 my ($self) = $sig->(@_);
1371 6 $self->{exact} = {};
1372 6 $self->{prefixes} = {};
1373 6 $self->{alt} = undef;
1374 6 return TB_OK;
1375 }
1376
1377 3 ▶sub captrie::rebuild_alt { # $int ()
Subroutine rebuild_alt: called CC=3 SCAR=12.6
1378 3 state $sig = compile(
1379 _Object,
1380 );
1381 3 my ($self) = $sig->(@_);
1382 my @ordered = sort {
1383 0 ▶ length($b) <=> length($a)
Condition0%0/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table0%0/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
1384 || $a cmp $b
1385 3 } keys %{ $self->{exact} };
1386 3 ▶ $self->{alt} = @ordered ? join('|', map { quotemeta($_) } @ordered) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@ordered ? : 3 0 3
1387 3 return TB_OK;
1388 }
1389
1390 3 ▶sub captrie::has_prefix { # $bool ($s)
Subroutine has_prefix: called CC=3 SCAR=11.0
1391 3 state $sig = compile(
1392 _Object,
1393 _Str,
1394 );
1395 3 my ($self, $s) = $sig->(@_);
1396 3 ▶ return 0 unless length($s);
Branch: 1 branch
BranchTrueFalseTotal
unless length $s 0 3 3
1397 3 ▶ return exists $self->{prefixes}{$s} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'prefixes'}{$s} ? : 1 2 3
1398 }
1399
1400 3 ▶sub captrie::best_match { # $s|undef ($buf)
Subroutine best_match: called CC=4 SCAR=14.0
1401 3 state $sig = compile(
1402 _Object,
1403 _Str,
1404 );
1405 3 my ($self, $buf) = $sig->(@_);
1406 3 ▶ return undef unless length($buf);
Branch: 1 branch
BranchTrueFalseTotal
unless length $buf 0 3 3
1407 3 my $alt = $self->{alt};
1408 3 ▶ return undef unless defined $alt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alt 0 3 3
1409 3 ▶ return $1 if $buf =~ /\A($alt)/s;
Branch: 1 branch
BranchTrueFalseTotal
if $buf =~ /\A($alt)/s 2 1 3
1410 1 return undef;
1411 }
1412
1413 3 ▶sub captrie::add { # $int ($cap, $key, $mod)
Subroutine add: called CC=4 SCAR=14.0
1414 3 state $sig = compile(
1415 _Object,
1416 _Str,
1417 _PositiveOrZeroInt,
1418 _PositiveOrZeroInt,
1419 );
1420 3 my ($self, $cap, $key, $mod) = $sig->(@_);
1421 # Nothing to do for empty caps
1422 3 ▶ return TB_OK unless length($cap);
Branch: 1 branch
BranchTrueFalseTotal
unless length $cap 0 3 3
1423 3 ▶ return TB_ERR_CAP_COLLISION if exists $self->{exact}{$cap};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"exact"}{$cap} 0 3 3
1424
1425 3 $self->{exact}{$cap} = {
1426 key => 0 + $key,
1427 mod => 0 + $mod,
1428 };
1429
1430 3 for my $i (1 .. length($cap) - 1) {
1431 8 my $p = substr($cap, 0, $i);
1432 8 $self->{prefixes}{$p} = 1;
1433 }
1434
1435 3 return $self->rebuild_alt();
1436 }
1437
1438 3 ▶sub captrie::find { # $int ($buf, \$last, \$depth)
Subroutine find: called CC=5 SCAR=16.4
1439 3 state $sig = compile(
1440 _Object,
1441 _Str,
1442 _Ref,
1443 _Ref,
1444 );
1445 3 my ($self, $buf, $last, $depth) = $sig->(@_);
1446 my $node = {
1447 is_leaf => 0,
1448 key => 0,
1449 mod => 0,
1450 3 ▶ nchildren => scalar(keys %{ $self->{exact} }) ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$$self{'exact'};} ? : 3 0 3
1451 cap => '',
1452 };
1453
1454 3 ▶ if (defined(my $m = $self->best_match($buf))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $m = $self->best_match($buf))) { } 2 1 3
elsif ($self->has_prefix($buf)) { } 1 0 1
1455 2 my $leaf = $self->{exact}{$m};
1456 $node = {
1457 is_leaf => 1,
1458 key => $leaf->{key},
1459 mod => $leaf->{mod},
1460 # Keep C-shape node contract; derive child-ness from prefix table.
1461 2 ▶ nchildren => ($self->has_prefix($m) ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$self->has_prefix($m) ? : 0 2 2
1462 cap => $m,
1463 };
1464 2 $$depth = length($m);
1465 }
1466 elsif ($self->has_prefix($buf)) {
1467 1 $node->{nchildren} = 1;
1468 1 $node->{cap} = $buf;
1469 1 $$depth = length($buf);
1470 }
1471 else {
1472 0 $$depth = 0;
1473 }
1474
1475 3 $$last = $node;
1476 3 return TB_OK;
1477 }
1478
1479 # ------------------------------------------------------------------------
1480 # Global state and initialization ----------------------------------------
1481 # ------------------------------------------------------------------------
1482
1483 #
1484 # The 'tb_global' struct holds the global state of the terminal, including
1485 # filehandles, terminal state, colors, modes, parser state, buffers,
1486 #
1487
1488 39 $global = {
1489 # Filehandles
1490 ttyfd => _WIN32 ? INVALID_HANDLE_VALUE : -1,
1491 rfd => _WIN32 ? INVALID_HANDLE_VALUE : -1,
1492 wfd => _WIN32 ? INVALID_HANDLE_VALUE : -1,
1493 ttyfd_open => 0,
1494 resize_pipefd => [-1, -1],
1495
1496 # Terminal state
1497 width => -1,
1498 height => -1,
1499 cursor_x => -1,
1500 cursor_y => -1,
1501 last_x => -1,
1502 last_y => -1,
1503
1504 # Colors
1505 fg => TB_DEFAULT,
1506 bg => TB_DEFAULT,
1507 last_fg => ~TB_DEFAULT,
1508 last_bg => ~TB_DEFAULT,
1509
1510 # Modes
1511 input_mode => TB_INPUT_ESC,
1512 output_mode => TB_OUTPUT_NORMAL,
1513
1514 # Terminfo
1515 terminfo => '',
1516 # ->{nterminfo} is not needed
1517
1518 # Parser
1519 caps => [ (undef) x TB_CAP__COUNT ],
1520 cap_trie => captrie->new(),
1521
1522 # Buffers
1523 inbuf => '',
1524 outbuf => '',
1525 back => cellbuf->new(),
1526 front => cellbuf->new(),
1527
1528 # Termios
1529 orig_tios => undef,
1530 has_orig_tios => 0,
1531
1532 # (Error) state
1533 last_errno => 0,
1534 initialized => 0,
1535 # ->{errbuf} is not needed
1536
1537 # Custom callbacks for escape sequence parsing
1538 fn_extract_esc_pre => undef,
1539 fn_extract_esc_post => undef,
1540
1541 # Dirty lines tracking for efficient rendering
1542 full_repaint => 0,
1543 dirty_lines => [],
1544 };
1545
1546 # ------------------------------------------------------------------------
1547 # Implementation of the termbox2 API incl. helpers ----------------------
1548 # ------------------------------------------------------------------------
1549
1550 #
1551 # Forwarding declarations of the public termbox API.
1552 #
1553
1554 # Initialize the termbox library
1555 sub tb_init;
1556 sub tb_init_file;
1557 sub tb_init_fd;
1558 sub tb_init_rwfd;
1559 sub tb_shutdown;
1560
1561 # Return the size of the internal back buffer
1562 sub tb_width;
1563 sub tb_height;
1564
1565 # Clear the internal back buffer
1566 sub tb_clear;
1567 sub tb_set_clear_attrs;
1568
1569 # Synchronize the internal back buffer with the terminal by writing to tty
1570 sub tb_present;
1571
1572 # Clear the internal front buffer and force a complete re-render
1573 sub tb_invalidate;
1574
1575 # Set the position of the cursor
1576 sub tb_set_cursor;
1577 sub tb_hide_cursor;
1578
1579 # Set cell contents in the internal back buffer at the specified position
1580 sub tb_set_cell;
1581 sub tb_set_cell_ex;
1582 sub tb_extend_cell;
1583
1584 # Return a reference to the cell at the specified position
1585 sub tb_get_cell;
1586
1587 # Set the input and output mode
1588 sub tb_set_input_mode;
1589 sub tb_set_output_mode;
1590
1591 # Wait for an event
1592 sub tb_peek_event;
1593 sub tb_poll_event;
1594
1595 # Internal termbox fds that can be used with 'poll', 'select'
1596 sub tb_get_fds;
1597
1598 # Print and printf functions
1599 sub tb_print;
1600 sub tb_printf;
1601 sub tb_print_ex;
1602 sub tb_printf_ex;
1603
1604 # Send raw bytes to terminal
1605 sub tb_send;
1606 sub tb_sendf;
1607
1608 # Set custom callbacks for escape sequence parsing
1609 sub tb_set_func; # Deprecated
1610
1611 # UTF-8 utility functions
1612 sub tb_utf8_char_length;
1613 sub tb_utf8_char_to_unicode;
1614 sub tb_utf8_unicode_to_char;
1615
1616 # Library utility functions
1617 sub tb_last_errno;
1618 sub tb_strerror;
1619 sub tb_cell_buffer; # Deprecated
1620 sub tb_has_truecolor;
1621 sub tb_has_egc;
1622 sub tb_attr_width;
1623 sub tb_version;
1624 sub tb_iswprint;
1625 sub tb_wcwidth;
1626
1627 #
1628 # Forwarding declarations of internal helper functions.
1629 #
1630
1631 # Process management helpers
1632 sub tb_reset;
1633 sub tb_printf_inner;
1634 sub tb_deinit;
1635 sub tb_iswprint_ex;
1636 sub tb_cluster_width;
1637
1638 # Terminal initialization helpers
1639 sub init_term_attrs;
1640 sub init_term_caps;
1641
1642 # Terminfo parsing helpers
1643 sub load_terminfo;
1644 sub load_terminfo_from_path;
1645 sub read_terminfo_path;
1646 sub parse_terminfo_caps;
1647 sub load_builtin_caps;
1648 sub get_terminfo_string;
1649 sub get_terminfo_int16;
1650
1651 # Resize handling helpers
1652 sub init_resize_handler;
1653 sub resize_cellbufs;
1654 sub handle_resize;
1655 sub update_term_size;
1656 sub update_term_size_via_esc;
1657
1658 # Escape-cap parser helpers
1659 sub init_cap_trie;
1660 sub cap_trie_add;
1661 sub cap_trie_find;
1662 sub cap_trie_deinit;
1663
1664 # Event extraction helpers
1665 sub wait_event;
1666 sub extract_event;
1667 sub extract_esc;
1668 sub extract_esc_user;
1669 sub extract_esc_cap;
1670 sub extract_esc_mouse;
1671
1672 # Cell related helpers
1673 sub cell_cmp;
1674 sub cell_copy;
1675 sub cell_set;
1676 sub cell_reserve_ech;
1677 sub cell_free;
1678
1679 # Cell buffer related helpers
1680 sub init_cellbuf;
1681 sub cellbuf_init;
1682 sub cellbuf_free;
1683 sub cellbuf_clear;
1684 sub cellbuf_get;
1685 sub cellbuf_in_bounds;
1686 sub cellbuf_resize;
1687
1688 # Sending output helpers
1689 sub send_literal;
1690 sub send_num;
1691 sub send_init_escape_codes;
1692 sub send_clear;
1693 sub send_attr;
1694 sub send_sgr;
1695 sub send_cursor_if;
1696 sub send_char;
1697 sub send_cluster;
1698 sub convert_num;
1699
1700 # Byte buffer related helpers
1701 sub bytebuf_puts;
1702 sub bytebuf_nputs;
1703 sub bytebuf_shift;
1704 sub bytebuf_flush;
1705 sub bytebuf_reserve;
1706 sub bytebuf_free;
1707
1708 # ------------------------------------------------------------------------
1709 # Public API implementation ----------------------------------------------
1710 # ------------------------------------------------------------------------
1711
1712 #
1713 # Initialize the termbox library
1714 #
1715
1716 0 ▶sub tb_init { # $int ()
Subroutine tb_init: not called CC=1 SCAR=2.9
Pod undocumented
1717 0 TRACE('') if _DEBUG;
1718 0 state $sig = compile();
1719 0 $sig->(@_);
1720 0 my $rv;
1721 0 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1722 0 if (_WIN32) {
1723 # Windows Terminal (WT) does not support the traditional termios interface,
1724 # so we need to use the Windows API to open the console handles directly.
1725 39 ▶ use open IO => ':raw'; # https://github.com/Perl/perl5/issues/17665
Subroutine BEGIN: called CC=1 SCAR=0.0
1726 my $err = sysopen(TB_OUT, 'CONOUT$', O_RDWR) ? 0 : $!+0;
1727 if ($err != 0) {
1728 $global->{last_errno} = $err;
1729 return $rv = TB_ERR_WIN_NO_STDIO;
1730 }
1731 $err = sysopen(TB_IN, 'CONIN$', O_RDWR) ? 0 : $!+0;
1732 if ($err != 0) {
1733 $global->{last_errno} = $err;
1734 return $rv = TB_ERR_WIN_NO_STDIO;
1735 }
1736 $global->{ttyfd_open} = 1;
1737 return $rv = tb_init_rwfd(fileno(TB_IN), fileno(TB_OUT));
1738 } #endif
1739 0 return $rv = tb_init_file('/dev/tty');
1740 }
1741
1742 1 ▶sub tb_init_file { # $int ($path)
Subroutine tb_init_file: called CC=5 SCAR=25.2
Pod undocumented
1743 1 TRACE('"%s"', @_) if _DEBUG;
1744 1 state $sig = compile(
1745 _Str,
1746 );
1747 1 my ($path) = $sig->(@_);
1748 1 my $rv;
1749 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1750 1 ▶ return $rv = TB_ERR_INIT_ALREADY if $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
if $Termbox::global->{"initialized"} 1 0 1
1751 0 if (_WIN32) {
1752 return $rv = TB_ERR_WIN_UNSUPPORTED;
1753 } #endif
1754 0 ▶ my $ttyfd = sysopen(TB_OUT, $path, O_RDWR) ? (fileno(TB_OUT) // -1) : -1;
Branch: 1 branch
BranchTrueFalseTotal
sysopen(TB_OUT, $path, 2) ? : 0 0 0
Condition0%0/2
fileno TB_OUT // -1
l!l
00
MC/DC0%0/1
fileno TB_OUT // -1
fileno TB_OUT
Truth table0%0/2
A
A= fileno TB_OUT
Aresult
11
00
1755 0 ▶ if ($ttyfd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttyfd < 0) 0 0 0
1756 0 $global->{last_errno} = 0+ $!;
1757 0 return $rv = TB_ERR_INIT_OPEN;
1758 }
1759 0 $global->{ttyfd_open} = 1;
1760 0 return $rv = tb_init_fd($ttyfd);
1761 }
1762
1763 1 ▶sub tb_init_fd { # $int ($ttyfd)
Subroutine tb_init_fd: called CC=1 SCAR=0.0
Pod undocumented
1764 1 TRACE('%d', @_) if _DEBUG;
1765 1 state $sig = compile(
1766 _Int,
1767 );
1768 1 my ($ttyfd) = $sig->(@_);
1769 1 my $rv;
1770 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1771 1 if (_WIN32) {
1772 return $rv = TB_ERR_WIN_UNSUPPORTED;
1773 } #endif
1774 1 return $rv = tb_init_rwfd($ttyfd, $ttyfd);
1775 }
1776
1777 2 ▶sub tb_init_rwfd { # $int ($rfd, $wfd)
Subroutine tb_init_rwfd: called CC=13 SCAR=27.1
Pod undocumented
1778 2 TRACE('%d, %d', @_) if _DEBUG;
1779 2 state $sig = compile(
1780 _Int,
1781 _Int,
1782 );
1783 2 my ($rfd, $wfd) = $sig->(@_);
1784 2 my $rv;
1785 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1786
1787 2 tb_reset();
1788 2 if (_WIN32) {
1789 my ($hInput, $hOutput);
1790
1791 for (1) {
1792 # Check if the file descriptors are windows handles
1793 $hInput = FdGetOsFHandle($rfd) // INVALID_HANDLE_VALUE;
1794 if ($hInput == INVALID_HANDLE_VALUE) { $rv = TB_ERR_WIN_NO_STDIO; last }
1795 $hOutput = FdGetOsFHandle($wfd) // INVALID_HANDLE_VALUE;
1796 if ($hOutput == INVALID_HANDLE_VALUE) { $rv = TB_ERR_WIN_NO_STDIO; last }
1797
1798 # Ensure the input and output handle are valid console handles
1799 $^E = 0;
1800 Win32::Console::_GetConsoleMode($hInput);
1801 if ($^E) { $rv = TB_ERR_WIN_GET_CONMODE; last }
1802 my $mode_out = Win32::Console::_GetConsoleMode($hOutput);
1803 if ($^E) { $rv = TB_ERR_WIN_GET_CONMODE; last }
1804
1805 # Check if the console handle supports virtual terminal processing
1806 Win32::Console::_SetConsoleMode($hOutput,
1807 $mode_out | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
1808 if ($^E) { $rv = TB_ERR_WIN_UNSUPPORTED; last }
1809 my $supportsVT = Win32::Console::_GetConsoleMode($hOutput)
1810 & ENABLE_VIRTUAL_TERMINAL_PROCESSING ? 1 : 0;
1811 if ($^E) { $rv = TB_ERR_WIN_GET_CONMODE; last }
1812
1813 # Restore the original console mode after checking for VT support
1814 Win32::Console::_SetConsoleMode($hOutput, $mode_out);
1815 if ($^E) { $rv = TB_ERR_WIN_SET_CONMODE; last }
1816
1817 # If the console does not support VT processing, return an error
1818 if (!$supportsVT) { $rv = TB_ERR_WIN_UNSUPPORTED; last }
1819 };
1820
1821 switch: for ($rv // 0) {
1822 case: TB_ERR_WIN_NO_STDIO() == $_ and do {
1823 $global->{last_errno} = $! = EBADF;
1824 last;
1825 };
1826 case: TB_ERR_WIN_GET_CONMODE() == $_ ||
1827 TB_ERR_WIN_SET_CONMODE() == $_
1828 and do {
1829 $global->{last_errno} = $! = ENOTTY;
1830 last;
1831 };
1832 case: TB_ERR_WIN_UNSUPPORTED() == $_ and do {
1833 $global->{last_errno} = $! = ENOTSUP;
1834 last;
1835 };
1836 default: {
1837 $rv ||= TB_OK;
1838 }
1839 }
1840 return $rv if $rv != TB_OK;
1841
1842 $global->{ttyfd} = $hOutput;
1843 $global->{rfd} = $hInput;
1844 $global->{wfd} = $hOutput;
1845 } else {
1846 $global->{ttyfd} =
1847 2 ▶ POSIX::isatty($rfd) ? $rfd :
Branch: 2 branches
BranchTrueFalseTotal
POSIX::isatty($rfd) ? : 2 0 2
POSIX::isatty($wfd) ? : 0 0 0
1848 POSIX::isatty($wfd) ? $wfd :
1849 -1;
1850 2 $global->{rfd} = $rfd;
1851 2 $global->{wfd} = $wfd;
1852 } #endif
1853
1854 2 for (1) {
1855 2 ▶ $rv = init_term_attrs(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1856 2 ▶ $rv = init_term_caps(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1857 2 ▶ $rv = init_cap_trie(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1858 2 ▶ $rv = init_resize_handler(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1859 2 ▶ $rv = send_init_escape_codes(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1860 2 ▶ $rv = send_clear(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1861 2 ▶ $rv = update_term_size(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1862 2 ▶ $rv = init_cellbuf(); last if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1863
1864 2 $global->{initialized} = 1;
1865 };
1866
1867 2 ▶ tb_deinit() if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 2 2
1868
1869 2 return $rv;
1870 }
1871
1872 2 ▶sub tb_shutdown { # $int ()
Subroutine tb_shutdown: called CC=2 SCAR=6.9
Pod undocumented
1873 2 TRACE('') if _DEBUG;
1874 2 state $sig = compile();
1875 2 $sig->(@_);
1876 2 my $rv;
1877 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1878 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 1 2
1879 1 tb_deinit();
1880 1 return $rv = TB_OK;
1881 }
1882
1883 #
1884 # Return the size of the internal back buffer
1885 #
1886
1887 1 ▶sub tb_width { # $int ()
Subroutine tb_width: called CC=2 SCAR=7.0
Pod undocumented
1888 1 TRACE('') if _DEBUG;
1889 1 state $sig = compile();
1890 1 $sig->(@_);
1891 1 my $rv;
1892 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1893 1 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 0 1 1
1894 1 return $rv = $global->{width};
1895 }
1896
1897 1 ▶sub tb_height { # $int ()
Subroutine tb_height: called CC=2 SCAR=7.0
Pod undocumented
1898 1 TRACE('') if _DEBUG;
1899 1 state $sig = compile();
1900 1 $sig->(@_);
1901 1 my $rv;
1902 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1903 1 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 0 1 1
1904 1 return $rv = $global->{height};
1905 }
1906
1907 #
1908 # Clear the internal back buffer
1909 #
1910
1911 2 ▶sub tb_clear { # $int ()
Subroutine tb_clear: called CC=2 SCAR=6.9
Pod undocumented
1912 2 TRACE('') if _DEBUG;
1913 2 state $sig = compile();
1914 2 $sig->(@_);
1915 2 my $rv;
1916 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1917 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 1 2
1918 1 return $rv = cellbuf_clear($global->{back});
1919 }
1920
1921 2 ▶sub tb_set_clear_attrs { # $int ($fg, $bg)
Subroutine tb_set_clear_attrs: called CC=2 SCAR=6.9
Pod undocumented
1922 2 TRACE('%d, %d', @_) if _DEBUG;
1923 2 state $sig = compile(
1924 _PositiveOrZeroInt,
1925 _PositiveOrZeroInt,
1926 );
1927 2 my ($fg, $bg) = $sig->(@_);
1928 2 my $rv;
1929 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
1930 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 1 2
1931 1 $global->{fg} = $fg;
1932 1 $global->{bg} = $bg;
1933 1 return $rv = TB_OK;
1934 }
1935
1936 #
1937 # Synchronize the internal back buffer with the terminal by writing to tty
1938 #
1939
1940 1 ▶sub tb_present { # $int ()
Subroutine tb_present: called CC=17 SCAR=46.2
Pod undocumented
1941 1 TRACE('') if _DEBUG > 1;
1942 1 state $sig = compile();
1943 1 $sig->(@_);
1944 1 my $rv;
1945 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 1;
1946 1 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 0 1 1
1947
1948 # TODO: assert $global->{back}[width,height] == $global->{front}[width,height]
1949
1950 1 my $full_repaint = $global->{full_repaint};
1951 1 my $dirty_lines = $global->{dirty_lines};
1952 1 my $front = $global->{front};
1953 1 my $back = $global->{back};
1954 1 my $front_cells = $front->{cells};
1955 1 my $back_cells = $back->{cells};
1956 1 my $width = $front->{width};
1957 1 my $height = $front->{height};
1958
1959 1 $global->{last_x} = -1;
1960 1 $global->{last_y} = -1;
1961
1962 1 ▶ for (my $y = 0; $y < $height; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $height) 1 1 2
1963 # Skip lines that are not dirty unless we are doing a full repaint
1964 1 ▶ next unless $full_repaint || $dirty_lines->[$y];
Branch: 1 branch
BranchTrueFalseTotal
unless $full_repaint or $dirty_lines->[$y] 1 0 1
Condition33%1/3
$full_repaint or $dirty_lines->[$y]
l!l&&r!l&&!r
001
MC/DC0%0/2
$full_repaint or $dirty_lines->[$y]
$full_repaint $dirty_lines->[$y]
Truth table33%1/3
A or B
A= $full_repaintB= $dirty_lines->[$y]
ABresult
1X1
011
000
1965 0 my $line_offset = $y * $width;
1966
1967 0 ▶ for (my $x = 0; $x < $width; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $width) 0 0 0
1968 0 my $cell_offset = $line_offset + $x;
1969
1970 0 my $back_cell = $back_cells->[$cell_offset];
1971 0 my $front_cell = $front_cells->[$cell_offset];
1972
1973 0 my $ch = $back_cell->[0];
1974 0 my $is_cluster = 0;
1975 0 my $ech;
1976 my $cp;
1977 0 my $w;
1978
1979 0 if (TB_OPT_EGC &&
1980 # if (
1981 length($ch) > 1 && $ch =~ /\A\X\z/
1982 ) {
1983 $is_cluster = 1;
1984 $ech = [ unpack 'U*', $ch ];
1985 $w = tb_cluster_width($ech, scalar @$ech);
1986 } else {
1987 #endif
1988 0 state %wcwidth_cache;
1989 0 $cp = ord($ch);
1990 0 ▶ $w = $wcwidth_cache{$cp} //= tb_wcwidth($cp);
Condition0%0/3
$wcwidth_cache{$cp} //= tb_wcwidth($cp)
l!l&&r!l&&!r
000
MC/DC0%0/2
$wcwidth_cache{$cp} //= tb_wcwidth($cp)
$wcwidth_cache{$cp} tb_wcwidth($cp)
Truth table0%0/3
A //= B
A= $wcwidth_cache{$cp}B= tb_wcwidth($cp)
ABresult
1X1
011
000
1991 }
1992 0 ▶ $w = 1 if $w < 1; # wcwidth returns -1 for invalid codepoints
Branch: 1 branch
BranchTrueFalseTotal
if $w < 1 0 0 0
1993
1994 0 ▶ if ( $back_cell->[0] ne $front_cell->[0] # ch
Branch: 1 branch
BranchTrueFalseTotal
if ($back_cell->[0] ne $front_cell->[0] or $back_cell->[1] != $front_cell->[1] or $back_cell->[2] != $front_cell->[2]) 0 0 0
Condition0%0/6
$back_cell->[0] ne $front_cell->[0] or $back_cell->[1] != $front_cell->[1] or $back_cell->[2] != $front_cell->[2]
l!l&&r!l&&!r
000
$back_cell->[0] ne $front_cell->[0] or $back_cell->[1] != $front_cell->[1]
l!l&&r!l&&!r
000
MC/DC0%0/3
$back_cell->[0] ne $front_cell->[0] or $back_cell->[1] != $front_cell->[1] or $back_cell->[2] != $front_cell->[2]
$back_cell->[0] ne $front_cell->[0] $back_cell->[1] != $front_cell->[1] $back_cell->[2] != $front_cell->[2]
Truth table0%0/4
A or B or C
A= $back_cell->[0] ne $front_cell->[0]B= $back_cell->[1] != $front_cell->[1]C= $back_cell->[2] != $front_cell->[2]
ABCresult
1XX1
01X1
0011
0000
1995 || $back_cell->[1] != $front_cell->[1] # fg
1996 || $back_cell->[2] != $front_cell->[2] # bg
1997 ) {
1998 0 @$front_cell = @$back_cell;
1999
2000 0 send_attr($back_cell->[1], $back_cell->[2]);
2001 0 ▶ if ($w > 1 && $x >= $width - ($w - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w > 1 and $x >= $width - ($w - 1)) { } 0 0 0
Condition0%0/3
$w > 1 and $x >= $width - ($w - 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$w > 1 and $x >= $width - ($w - 1)
$w > 1 $x >= $width - ($w - 1)
Truth table0%0/3
A and B
A= $w > 1B= $x >= $width - ($w - 1)
ABresult
0X0
100
111
2002 # Not enough room for wide char, send spaces
2003 0 ▶ for (my $i = $x; $i < $width; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $width) 0 0 0
2004 0 send_char($i, $y, ord(' '));
2005 }
2006 } else {
2007 0 if (TB_OPT_EGC &&
2008 # if (
2009 $is_cluster
2010 ) {
2011 send_cluster($x, $y, $ech, scalar @$ech);
2012 } else {
2013 #endif
2014 0 send_char($x, $y, $cp);
2015 }
2016
2017 # When wcwidth>1, we need to advance the cursor by more
2018 # than 1, thereby skipping some cells. Set these skipped
2019 # cells to an invalid codepoint in the front buffer, so
2020 # that if this cell is later replaced by a wcwidth==1
2021 # char, we'll get a cell_cmp diff for the skipped cells
2022 # and properly re-render.
2023 0 ▶ for (my $i = 1; $i < $w; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $w) 0 0 0
2024 0 my $front_wide = $front_cells->[$cell_offset + $i];
2025 0 $front_wide->[0] = "\x{fffd}"; # ch
2026 0 $front_wide->[1] = ~0; # fg
2027 0 $front_wide->[2] = ~0; # bg
2028 }
2029 }
2030 }
2031 0 $x += $w;
2032 }
2033 0 $dirty_lines->[$y] = 0;
2034 }
2035
2036 1 $rv = send_cursor_if($global->{cursor_x}, $global->{cursor_y});
2037 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
2038 1 $rv = bytebuf_flush(\$global->{outbuf}, $global->{wfd});
2039 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
2040
2041 1 return $rv = TB_OK;
2042 }
2043
2044 #
2045 # Clear the internal front buffer and force a complete re-render
2046 #
2047
2048 3 ▶sub tb_invalidate { # $int ()
Subroutine tb_invalidate: called CC=3 SCAR=11.0
Pod undocumented
2049 3 TRACE('') if _DEBUG;
2050 3 state $sig = compile();
2051 3 $sig->(@_);
2052 3 my $rv;
2053 3 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2054 3 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 2 3
2055 2 $rv = resize_cellbufs();
2056 2 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 1 1 2
2057 1 return $rv = TB_OK;
2058 }
2059
2060 #
2061 # Set the position of the cursor
2062 #
2063
2064 2 ▶sub tb_set_cursor { # $int ($cx, $cy)
Subroutine tb_set_cursor: called CC=7 SCAR=19.8
Pod undocumented
2065 2 TRACE('%d, %d', @_) if _DEBUG;
2066 2 state $sig = compile(
2067 _Int,
2068 _Int,
2069 );
2070 2 my ($cx, $cy) = $sig->(@_);
2071 2 my $rv;
2072 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2073 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 1 2
2074
2075 1 ▶ $cx = 0 if $cx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cx < 0 0 1 1
2076 1 ▶ $cy = 0 if $cy < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cy < 0 0 1 1
2077
2078 1 ▶ if ($global->{cursor_x} == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"cursor_x"} == -1) 1 0 1
2079 1 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_SHOW_CURSOR]);
2080 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
2081 }
2082 1 $rv = send_cursor_if($cx, $cy);
2083 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
2084
2085 1 $global->{cursor_x} = $cx;
2086 1 $global->{cursor_y} = $cy;
2087
2088 1 return $rv = TB_OK;
2089 }
2090
2091 1 ▶sub tb_hide_cursor { # $int ()
Subroutine tb_hide_cursor: called CC=4 SCAR=14.0
Pod undocumented
2092 1 TRACE('') if _DEBUG;
2093 1 state $sig = compile();
2094 1 $sig->(@_);
2095 1 my $rv;
2096 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2097 1 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 0 1 1
2098
2099 1 ▶ if ($global->{cursor_x} >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"cursor_x"} >= 0) 1 0 1
2100 1 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_HIDE_CURSOR]);
2101 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
2102 }
2103
2104 1 $global->{cursor_x} = -1;
2105 1 $global->{cursor_y} = -1;
2106
2107 1 return $rv = TB_OK;
2108 }
2109
2110 #
2111 # Set cell contents in the internal back buffer at the specified position
2112 #
2113
2114 8 ▶sub tb_set_cell { # $int ($x, $y, $ch, $fg, $bg)
Subroutine tb_set_cell: called CC=1 SCAR=0.0
Pod undocumented
2115 8 TRACE('%d, %d, "%s", %d, %d', @_) if _DEBUG > 1;
2116 8 state $sig = compile(
2117 _Int,
2118 _Int,
2119 _Str,
2120 _PositiveOrZeroInt,
2121 _PositiveOrZeroInt,
2122 );
2123 8 my ($x, $y, $ch, $fg, $bg) = $sig->(@_);
2124 8 my $rv;
2125 8 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 1;
2126 # Note: tb_set_cell stores only the first Perl character
2127 8 return $rv = tb_set_cell_ex($x, $y, substr($ch, 0, 1), 1, $fg, $bg);
2128 }
2129
2130 12 ▶sub tb_set_cell_ex { # $int ($x, $y, $ch, $nch, $fg, $bg)
Subroutine tb_set_cell_ex: called CC=4 SCAR=13.9
Pod undocumented
2131 12 TRACE('%d, %d, "%s", %d, %d, %d', @_) if _DEBUG > 2;
2132 12 state $sig = compile(
2133 _Int,
2134 _Int,
2135 _Str,
2136 _PositiveOrZeroInt,
2137 _PositiveOrZeroInt,
2138 _PositiveOrZeroInt,
2139 );
2140 12 my ($x, $y, $ch, $nch, $fg, $bg) = $sig->(@_);
2141 12 my $rv;
2142 12 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 2;
2143 # Note: ch is a Perl string, not an array of codepoints
2144 # and nch is accepted for API compatibility but ignored in the Perl port
2145 12 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 3 9 12
2146 9 my $cell;
2147 9 $rv = cellbuf_get($global->{back}, $x, $y, \$cell);
2148 9 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 9 9
2149 9 $global->{dirty_lines}[$y] = 1;
2150 9 $rv = $cell->set($ch, $fg, $bg);
2151 9 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 9 9
2152 9 return $rv = TB_OK;
2153 }
2154
2155 3 ▶sub tb_extend_cell { # $int ($x, $y, $ch)
Subroutine tb_extend_cell: called CC=2 SCAR=6.9
Pod undocumented
2156 3 TRACE('%d, %d, "%s"', @_) if _DEBUG > 1;
2157 3 state $sig = compile(
2158 _Int,
2159 _Int,
2160 _Str,
2161 );
2162 3 my ($x, $y, $ch) = $sig->(@_);
2163 3 my $rv;
2164 3 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 1;
2165 3 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 2 3
2166 2 if (TB_OPT_EGC) {
2167 # TODO: iswprint ch?
2168 my $cell;
2169 $rv = cellbuf_get($global->{back}, $x, $y, \$cell);
2170 return $rv if $rv != TB_OK;
2171 $global->{dirty_lines}[$y] = 1;
2172 # Note: tb_extend_cell appends only the first Perl character
2173 $cell->[0] .= substr($ch, 0, 1);
2174 return $rv = TB_OK;
2175 } else {
2176 2 return $rv = TB_ERR;
2177 } #endif
2178 }
2179
2180 #
2181 # Return a reference to the cell at the specified position
2182 #
2183
2184 3 ▶sub tb_get_cell { # $int ($x, $y, $back, \$cell)
Subroutine tb_get_cell: called CC=3 SCAR=11.0
Pod undocumented
2185 5 TRACE('%d, %d, %d, %s', @_) if _DEBUG;
2186 5 state $sig = compile(
2187 _Int,
2188 _Int,
2189 _Bool,
2190 _Ref,
2191 );
2192 5 my ($x, $y, $back, $cell) = $sig->(@_);
2193 5 my $rv;
2194 5 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2195 3 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 4 5
2196 2 ▶ return $rv = cellbuf_get($back ? $global->{back} : $global->{front}, $x, $y,
Branch: 1 branch
BranchTrueFalseTotal
$back ? : 0 2 2
2197 $cell);
2198 }
2199
2200 #
2201 # Set the input and output mode
2202 #
2203
2204 2 ▶sub tb_set_input_mode { # $int ($mode)
Subroutine tb_set_input_mode: called CC=6 SCAR=19.1
Pod undocumented
2205 2 TRACE('%d', @_) if _DEBUG;
2206 2 state $sig = compile(
2207 _Int,
2208 );
2209 2 my ($mode) = $sig->(@_);
2210 2 my $rv;
2211 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2212 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 0 2 2
2213
2214 2 ▶ if ($mode == TB_INPUT_CURRENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 0) 1 1 2
2215 1 return $rv = $global->{input_mode};
2216 }
2217
2218 1 my $esc_or_alt = TB_INPUT_ESC | TB_INPUT_ALT;
2219 1 ▶ if (($mode & $esc_or_alt) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (($mode & $esc_or_alt) == 0) { } 1 0 1
elsif (($mode & $esc_or_alt) == $esc_or_alt) { } 0 0 0
2220 # neither specified; flip on ESC
2221 1 $mode |= TB_INPUT_ESC;
2222 } elsif (($mode & $esc_or_alt) == $esc_or_alt) {
2223 # both specified; flip off ALT
2224 0 $mode &= ~TB_INPUT_ALT;
2225 }
2226
2227 1 ▶ if ($mode & TB_INPUT_MOUSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode & 4) { } 1 0 1
2228 1 bytebuf_puts(\$global->{outbuf}, TB_HARDCAP_ENTER_MOUSE);
2229 1 bytebuf_flush(\$global->{outbuf}, $global->{wfd});
2230 } else {
2231 0 bytebuf_puts(\$global->{outbuf}, TB_HARDCAP_EXIT_MOUSE);
2232 0 bytebuf_flush(\$global->{outbuf}, $global->{wfd});
2233 }
2234
2235 1 $global->{input_mode} = $mode;
2236 1 return $rv = TB_OK;
2237 }
2238
2239 3 ▶sub tb_set_output_mode { # $int ($mode)
Subroutine tb_set_output_mode: called CC=9 SCAR=22.3
Pod undocumented
2240 3 TRACE('%d', @_) if _DEBUG;
2241 3 state $sig = compile(
2242 _Int,
2243 );
2244 3 my ($mode) = $sig->(@_);
2245 3 my $rv;
2246 3 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2247 3 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 0 3 3
2248 3 switch: for ($mode) {
2249 case: TB_OUTPUT_CURRENT == $_ and
2250 3 ▶ return $rv = $global->{output_mode};
Branch: 1 branch
BranchTrueFalseTotal
0 == $_ and return $rv = $Termbox::global->{"output_mode"} 1 2 3
2251 case: TB_OUTPUT_NORMAL == $_ ||
2252 TB_OUTPUT_256 == $_ ||
2253 TB_OUTPUT_216 == $_ ||
2254 TB_OUTPUT_GRAYSCALE == $_ ||
2255 #if
2256 (TB_OPT_ATTR_W >= 32 &&
2257 TB_OUTPUT_TRUECOLOR() == $_)
2258 #endif
2259 2 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
and do { $Termbox::global->{"last_fg"} = ~$Termbox::global->{"fg"}; $Termbox::global->{"last_bg"} = ~$Termbox::global->{"bg"}; $Termbox::global->{"output_mode"} = $mode; return $rv = 0 } 1 1 2
Condition66%8/12
1 == $_ or 2 == $_ or 4 == $_ or 8 == $_ or
l!l
110
1 == $_ or 2 == $_ or 4 == $_ or 8 == $_
l!l&&r!l&&!r
101
1 == $_ or 2 == $_ or 4 == $_
l!l&&r!l&&!r
101
1 == $_ or 2 == $_
l!l&&r!l&&!r
011
MC/DC25%1/4
1 == $_ or 2 == $_ or 4 == $_ or 8 == $_ or
1 == $_ 2 == $_ 4 == $_ 8 == $_
Truth table40%2/5
A or B or C or D
A= 1 == $_B= 2 == $_C= 4 == $_D= 8 == $_
ABCDresult
1XXX1
01XX1
001X1
00011
00000
2260 1 $global->{last_fg} = ~$global->{fg};
2261 1 $global->{last_bg} = ~$global->{bg};
2262 1 $global->{output_mode} = $mode;
2263 1 return $rv = TB_OK;
2264 }
2265 }
2266 1 return $rv = TB_ERR;
2267 }
2268
2269 #
2270 # Wait for an event
2271 #
2272
2273 2 ▶sub tb_peek_event { # $int ($event, $timeout_ms)
Subroutine tb_peek_event: called CC=2 SCAR=6.9
Pod undocumented
2274 2 TRACE('%s, %d', @_) if _DEBUG > 2;
2275 2 state $sig = compile(
2276 _Object,
2277 _Int,
2278 );
2279 2 my ($event, $timeout_ms) = $sig->(@_);
2280 2 my $rv;
2281 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 2;
2282 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 1 2
2283 1 return $rv = wait_event($event, $timeout_ms);
2284 }
2285
2286 2 ▶sub tb_poll_event { # $int ($event)
Subroutine tb_poll_event: called CC=2 SCAR=6.9
Pod undocumented
2287 2 TRACE('%s', @_) if _DEBUG > 1;
2288 2 state $sig = compile(
2289 _Object,
2290 );
2291 2 my ($event) = $sig->(@_);
2292 2 my $rv;
2293 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 1;
2294 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 1 2
2295 1 return $rv = wait_event($event, -1);
2296 }
2297
2298 #
2299 # Internal termbox fds that can be used with 'poll', 'select'
2300 #
2301
2302 2 ▶sub tb_get_fds { # $int (\$ttyfd, \$resizefd)
Subroutine tb_get_fds: called CC=2 SCAR=6.9
Pod undocumented
2303 2 TRACE('%s, %s', @_) if _DEBUG;
2304 2 state $sig = compile(
2305 _ScalarRef,
2306 _ScalarRef,
2307 );
2308 2 my ($ttyfd, $resizefd) = $sig->(@_);
2309 2 my $rv;
2310 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2311 2 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 1 1 2
2312 1 if (_WIN32) {
2313 return $rv = TB_ERR_WIN_UNSUPPORTED;
2314 } #endif
2315 1 $$ttyfd = $global->{rfd};
2316 1 $$resizefd = $global->{resize_pipefd}[0];
2317
2318 1 return $rv = TB_OK;
2319 }
2320
2321 #
2322 # Print and printf functions
2323 #
2324
2325 1 ▶sub tb_print { # $int ($x, $y, $fg, $bg, $str)
Subroutine tb_print: called CC=1 SCAR=0.0
Pod undocumented
2326 1 TRACE('%d, %d, %d, %d, "%s"', @_) if _DEBUG;
2327 1 state $sig = compile(
2328 _Int,
2329 _Int,
2330 _PositiveOrZeroInt,
2331 _PositiveOrZeroInt,
2332 _Str,
2333 );
2334 1 my ($x, $y, $fg, $bg, $str) = $sig->(@_);
2335 1 my $rv;
2336 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2337 1 return $rv = tb_print_ex($x, $y, $fg, $bg, undef, $str);
2338 }
2339
2340 1 ▶sub tb_printf { # $int ($x, $y, $fg, $bg, $fmt, @args)
Subroutine tb_printf: called CC=1 SCAR=0.0
Pod undocumented
2341 1 TRACE('%d, %d, %d, %d, "%s", %s', @_[0..4], join(', ', @_[5..$#_]))
2342 if _DEBUG;
2343 1 state $sig = compile(
2344 _Int,
2345 _Int,
2346 _PositiveOrZeroInt,
2347 _PositiveOrZeroInt,
2348 _Str,
2349 );
2350 1 my ($x, $y, $fg, $bg, $fmt, @args) = ($sig->(@_[0..4]), @_[5..$#_]);
2351 1 my $rv;
2352 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2353 1 return $rv = tb_printf_inner($x, $y, $fg, $bg, undef, $fmt, @args);
2354 }
2355
2356 12 ▶sub tb_print_ex { # $int ($x, $y, $fg, $bg, \$out_w|undef, $str)
Subroutine tb_print_ex: called CC=16 SCAR=31.8
Pod undocumented
2357 12 TRACE('%d, %d, %d, %d, %s, "%s"', @_[0..3], $_[4] // 'undef', $_[5])
2358 if _DEBUG > 1;
2359 12 state $sig = compile(
2360 _Int,
2361 _Int,
2362 _PositiveOrZeroInt,
2363 _PositiveOrZeroInt,
2364 _Maybe[_ScalarRef],
2365 _Str,
2366 );
2367 12 my ($x, $y, $fg, $bg, $out_w, $str) = $sig->(@_);
2368 12 my $rv;
2369 12 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 1;
2370 12 ▶ return $rv = TB_ERR_NOT_INIT unless $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global->{"initialized"} 8 4 12
2371
2372 4 my $back = $global->{back};
2373 4 ▶ return $rv = TB_ERR_OUT_OF_BOUNDS unless $back->in_bounds($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $back->in_bounds($x, $y) 0 3 3
2374
2375 3 ▶ $$out_w = 0 if defined $out_w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_w 3 0 3
2376
2377 3 my $w;
2378 3 my $ix = $x;
2379 3 my $x_prev = $x;
2380 3 my $uni;
2381
2382 3 ▶ while ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 7 3 10
2383 7 $rv = tb_utf8_char_to_unicode(\$uni, substr($str, 0, 1));
2384 7 ▶ if ($rv < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv < 0) { } 0 7 7
elsif ($rv > 0) { } 7 0 7
2385 0 $uni = 0xfffd; # replace invalid UTF-8 char with U+FFFD
2386 0 bytes::substr($str, 0, -$rv, '');
2387 } elsif ($rv > 0) {
2388 7 bytes::substr($str, 0, $rv, '');
2389 } else {
2390 0 last; # shouldn't get here
2391 }
2392
2393 7 ▶ if ($uni eq ord("\n")) { # TODO: \r, \t, \v, \f, etc?
Branch: 1 branch
BranchTrueFalseTotal
if ($uni eq 10) 0 7 7
2394 0 $x = $ix;
2395 0 $x_prev = $x;
2396 0 $y++;
2397 0 next;
2398 }
2399
2400 7 ▶ if (!tb_iswprint_ex($uni, \$w)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (tb_iswprint_ex($uni, \$w)) 0 7 7
2401 0 $uni = 0xfffd; # replace non-printable with U+FFFD
2402 0 $w = 1;
2403 }
2404
2405 7 ▶ if ($w < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($w < 0) { } 0 7 7
elsif ($w == 0) { } 1 6 7
2406 0 return TB_ERR; # shouldn't happen if iswprint
2407 }
2408 elsif ($w == 0) { # combining character
2409 1 ▶ if ($back->in_bounds($x_prev, $y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($back->in_bounds($x_prev, $y)) 1 0 1
2410 1 $rv = tb_extend_cell($x_prev, $y, chr $uni);
2411 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
2412 }
2413 }
2414 else {
2415 6 ▶ if ($back->in_bounds($x, $y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($back->in_bounds($x, $y)) 6 0 6
2416 6 $rv = tb_set_cell($x, $y, chr $uni, $fg, $bg);
2417 6 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 6 6
2418 }
2419 6 $x_prev = $x;
2420 6 $x += $w;
2421 6 ▶ $$out_w += $w if defined $out_w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out_w 6 0 6
2422 }
2423 }
2424
2425 3 return $rv = TB_OK;
2426 }
2427
2428 2 ▶sub tb_printf_ex { # $int ($x, $y, $fg, $bg, \$out_w|undef, $fmt, @args)
Subroutine tb_printf_ex: called CC=1 SCAR=0.0
Pod undocumented
2429 2 TRACE('%d, %d, %d, %d, %s, "%s", %s', @_[0..3], $_[4] // 'undef', $_[5],
2430 join(', ', @_[6..$#_])) if _DEBUG;
2431 2 DEBUG("goto &tb_printf_inner") if _DEBUG;
2432 2 goto &tb_printf_inner;
2433 }
2434
2435 #
2436 # Send raw bytes to terminal
2437 #
2438
2439 2 ▶sub tb_send { # $int ($buf, $nbuf)
Subroutine tb_send: called CC=1 SCAR=0.0
Pod undocumented
2440 2 TRACE('"%s", %d', @_) if _DEBUG > 1;
2441 2 state $sig = compile(
2442 _Str,
2443 _PositiveOrZeroInt,
2444 );
2445 2 my ($buf, $nbuf) = $sig->(@_);
2446 2 my $rv;
2447 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 1;
2448 2 return $rv = bytebuf_nputs(\$global->{outbuf}, $buf, $nbuf);
2449 }
2450
2451 1 ▶sub tb_sendf { # $int ($fmt, @args)
Subroutine tb_sendf: called CC=3 SCAR=11.1
Pod undocumented
2452 1 TRACE('"%s", %s', $_[0], join(', ', @_[1..$#_])) if _DEBUG > 1;
2453 1 state $sig = compile(
2454 _Str,
2455 );
2456 1 my ($fmt, @args) = ($sig->(shift), @_);
2457 1 my $rv;
2458 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 1;
2459 1 ▶ my $buf = @args ? sprintf($fmt, @args) : $fmt;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 1 0 1
2460 1 my $len = bytes::length($buf);
2461 1 ▶ return $rv = TB_ERR if $len >= TB_OPT_PRINTF_BUF;
Branch: 1 branch
BranchTrueFalseTotal
if $len >= 4096 0 1 1
2462 1 return $rv = tb_send($buf, $len);
2463 }
2464
2465 #
2466 # Set custom callbacks for escape sequence parsing
2467 #
2468
2469 3 ▶sub tb_set_func { # $int ($fn_type, $fn|undef)
Subroutine tb_set_func: called CC=5 SCAR=16.1
Pod undocumented
2470 3 TRACE('%d, %s', @_) if _DEBUG;
2471 3 state $sig = compile(
2472 _Int,
2473 _Maybe[_CodeRef],
2474 );
2475 3 my ($fn_type, $fn) = $sig->(@_);
2476 3 my $rv;
2477 3 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2478
2479 3 state $warned = 0;
2480 3 ▶ warnings::warnif(
Branch: 1 branch
BranchTrueFalseTotal
unless $warned++ 1 2 3
2481 deprecated =>
2482 "tb_set_func() is deprecated and may be removed in a future release"
2483 ) unless $warned++;
2484
2485 3 switch: for ($fn_type) {
2486 3 ▶ case: TB_FUNC_EXTRACT_PRE == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
0 == $_ and do { $Termbox::global->{"fn_extract_esc_pre"} = $fn; return $rv = 0 } 1 2 3
2487 1 $global->{fn_extract_esc_pre} = $fn;
2488 1 return $rv = TB_OK;
2489 };
2490 2 ▶ case: TB_FUNC_EXTRACT_POST == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { $Termbox::global->{"fn_extract_esc_post"} = $fn; return $rv = 0 } 1 1 2
2491 1 $global->{fn_extract_esc_post} = $fn;
2492 1 return $rv = TB_OK;
2493 };
2494 }
2495
2496 1 return $rv = TB_ERR;
2497 }
2498
2499 #
2500 # UTF-8 utility functions
2501 #
2502
2503 24 ▶sub tb_utf8_char_length { # $length ($c)
Subroutine tb_utf8_char_length: called CC=9 SCAR=22.3
Pod undocumented
2504 24 TRACE('"%s"', @_) if _DEBUG > 2;
2505 24 state $sig = compile(
2506 _Str,
2507 );
2508 24 my ($c) = $sig->(@_);
2509 24 my $guard = TRACE_LEAVE(undef) if _DEBUG > 2;
2510 24 ▶ return 0 if $c eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "" 0 24 24
2511 24 $c = bytes::substr($c, 0, 1);
2512 24 state $utf8_length = {};
2513 24 ▶ $utf8_length->{$c} //= do {
Condition66%2/3
$utf8_length->{$c} //= do { my $b = unpack("C", $c); $b < 192 ? 1 : ($b < 224 ? 2 : ($b < 240 ? 3 : ($b < 248 ? 4 : ($b < 252 ? 5 : ($b < 254 ? 6 : 1))))) }
l!l&&r!l&&!r
7170
MC/DC0%0/2
$utf8_length->{$c} //= do { my $b = unpack("C", $c); $b < 192 ? 1 : ($b < 224 ? 2 : ($b < 240 ? 3 : ($b < 248 ? 4 : ($b < 252 ? 5 : ($b < 254 ? 6 : 1))))) }
$utf8_length->{$c} do { my $b = unpack("C", $c); $b < 192 ? 1 : ($b < 224 ? 2 : ($b < 240 ? 3 : ($b < 248 ? 4 : ($b < 252 ? 5 : ($b < 254 ? 6 : 1))))) }
Truth table66%2/3
A //= B
A= $utf8_length->{$c}B= do { my $b = unpack("C", $c); $b < 192 ? 1 : ($b < 224 ? 2 : ($b < 240 ? 3 : ($b < 248 ? 4 : ($b < 252 ? 5 : ($b < 254 ? 6 : 1))))) }
ABresult
1X1
011
000
2514 17 my $b = unpack('C', $c);
2515 17 ▶ ($b < 0xC0) ? 1
Branch: 6 branches
BranchTrueFalseTotal
$b < 192 ? : 10 7 17
$b < 224 ? : 2 5 7
$b < 240 ? : 1 4 5
$b < 248 ? : 2 2 4
$b < 252 ? : 0 2 2
$b < 254 ? : 0 2 2
2516 : ($b < 0xE0) ? 2
2517 : ($b < 0xF0) ? 3
2518 : ($b < 0xF8) ? 4
2519 : ($b < 0xFC) ? 5
2520 : ($b < 0xFE) ? 6
2521 : 1;
2522 };
2523 }
2524
2525 19 ▶sub tb_utf8_char_to_unicode { # $length (\$out, $c)
Subroutine tb_utf8_char_to_unicode: called CC=7 SCAR=19.5
Pod undocumented
2526 19 TRACE('%s, "%s"', @_) if _DEBUG > 2;
2527 19 state $sig = compile(
2528 _ScalarRef,
2529 _Str,
2530 );
2531 19 my ($out, $c) = $sig->(@_);
2532 19 my $guard = TRACE_LEAVE(undef) if _DEBUG > 2;
2533 39 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
2534
2535 19 ▶ return 0 if $c eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "" 0 19 19
2536 19 my $b0 = substr($c, 0, 1);
2537 19 ▶ return 0 if $b0 eq "\0";
Branch: 1 branch
BranchTrueFalseTotal
if $b0 eq "\0" 1 18 19
2538 18 my $len = tb_utf8_char_length($b0);
2539
2540 18 my @c = unpack('C*', substr($c, 0, $len));
2541
2542 18 state $utf8_mask = [0x7F, 0x1F, 0x0F, 0x07, 0x03, 0x01];
2543 18 ▶ my $mask = $utf8_mask->[$len - 1] // 0x7F;
Condition50%1/2
$$utf8_mask[$len - 1] // 127
l!l
180
MC/DC0%0/1
$$utf8_mask[$len - 1] // 127
$$utf8_mask[$len - 1]
Truth table50%1/2
A
A= $$utf8_mask[$len - 1]
Aresult
11
00
2544
2545 18 my $result = $c[0] & $mask;
2546
2547 18 my $i;
2548 18 ▶ for ($i = 1; $i < @c && $c[$i] != 0; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @c and $c[$i] != 0) 14 18 32
Condition100%3/3
$i < @c and $c[$i] != 0
!ll&&!rl&&r
15314
MC/DC100%2/2
$i < @c and $c[$i] != 0
$i < @c $c[$i] != 0
Truth table100%3/3
A and B
A= $i < @cB= $c[$i] != 0
ABresult
0X0
100
111
2549 14 $result <<= 6;
2550 14 $result |= $c[$i] & 0x3F;
2551 }
2552
2553 18 ▶ return -$i if $i != $len;
Branch: 1 branch
BranchTrueFalseTotal
if $i != $len 6 12 18
2554
2555 12 $$out = $result;
2556 12 return $len;
2557 }
2558
2559 5 ▶sub tb_utf8_unicode_to_char { # $length (\$out, $c)
Subroutine tb_utf8_unicode_to_char: called CC=8 SCAR=24.0
Pod undocumented
2560 5 TRACE('%s, %d', @_) if _DEBUG > 2;
2561 5 state $sig = compile(
2562 _ScalarRef,
2563 _PositiveOrZeroInt,
2564 );
2565 5 my ($out, $c) = $sig->(@_);
2566 5 my $guard = TRACE_LEAVE(undef) if _DEBUG > 2;
2567
2568 # Fast path for real Unicode scalar values (<= 0x10FFFF)
2569 5 ▶ if ($c <= 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= 1114111) 4 1 5
2570 4 my $s = chr($c);
2571 4 utf8::encode($s);
2572 4 $$out = $s;
2573 4 return bytes::length($s);
2574 }
2575
2576 # Fallback for "extended UTF-8" (5/6-byte), C-compatible behavior
2577 1 my ($first, $len);
2578
2579 0 ▶ if ($c < 0x80) { $first = 0x00; $len = 1; }
Branch: 5 branches
BranchTrueFalseTotal
if ($c < 128) { } 0 1 1
elsif ($c < 2048) { } 0 1 1
elsif ($c < 65536) { } 0 1 1
elsif ($c < 2097152) { } 1 0 1
elsif ($c < 67108864) { } 0 0 0
2580 0 elsif ($c < 0x800) { $first = 0xC0; $len = 2; }
2581 0 elsif ($c < 0x10000) { $first = 0xE0; $len = 3; }
2582 1 elsif ($c < 0x200000) { $first = 0xF0; $len = 4; }
2583 0 elsif ($c < 0x4000000) { $first = 0xF8; $len = 5; }
2584 0 else { $first = 0xFC; $len = 6; }
2585
2586 1 my @out = (0) x $len;
2587 1 ▶ for (my $i = $len - 1; $i > 0; --$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 3 1 4
2588 3 $out[$i] = ($c & 0x3F) | 0x80;
2589 3 $c >>= 6;
2590 }
2591 1 $out[0] = $c | $first;
2592 1 $$out = pack('C*', @out);
2593
2594 1 return $len;
2595 }
2596
2597 #
2598 # Library utility functions
2599 #
2600
2601 2 ▶sub tb_last_errno { # $int ()
Subroutine tb_last_errno: called CC=1 SCAR=0.0
Pod undocumented
2602 2 TRACE('') if _DEBUG;
2603 2 state $sig = compile();
2604 2 $sig->(@_);
2605 2 my $rv;
2606 2 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2607 2 return $rv = 0+ $global->{last_errno};
2608 }
2609
2610 4 ▶sub tb_strerror { # $str ($err)
Subroutine tb_strerror: called CC=29 SCAR=38.3
Pod undocumented
2611 4 TRACE('%d', @_) if _DEBUG;
2612 4 state $sig = compile(
2613 _Int,
2614 );
2615 4 my ($err) = $sig->(@_);
2616 4 my $guard = TRACE_LEAVE(undef) if _DEBUG;
2617 4 switch: for (int($err)) {
2618 4 ▶ case: TB_OK == $_ and
Branch: 1 branch
BranchTrueFalseTotal
0 == $_ and return "Success" 1 3 4
2619 return "Success";
2620 3 ▶ case: TB_ERR_NEED_MORE == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-2 == $_ and return "Not enough input" 1 2 3
2621 return "Not enough input";
2622 2 ▶ case: TB_ERR_INIT_ALREADY == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-3 == $_ and return "Termbox initialized already" 0 2 2
2623 return "Termbox initialized already";
2624 2 ▶ case: TB_ERR_MEM == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-5 == $_ and return "Out of memory" 0 2 2
2625 return "Out of memory";
2626 2 ▶ case: TB_ERR_NO_EVENT == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-6 == $_ and return "No event" 0 2 2
2627 return "No event";
2628 2 ▶ case: TB_ERR_NO_TERM == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-7 == $_ and return "No TERM in environment" 0 2 2
2629 return "No TERM in environment";
2630 2 ▶ case: TB_ERR_NOT_INIT == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-8 == $_ and return "Termbox not initialized" 0 2 2
2631 return "Termbox not initialized";
2632 2 ▶ case: TB_ERR_OUT_OF_BOUNDS == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-9 == $_ and return "Out of bounds" 0 2 2
2633 return "Out of bounds";
2634 2 ▶ case: TB_ERR_UNSUPPORTED_TERM == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-17 == $_ and return "Unsupported terminal" 0 2 2
2635 return "Unsupported terminal";
2636 2 ▶ case: TB_ERR_CAP_COLLISION == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-22 == $_ and return "Termcaps collision" 0 2 2
2637 return "Termcaps collision";
2638 2 ▶ case: TB_ERR_RESIZE_SSCANF == $_ and
Branch: 1 branch
BranchTrueFalseTotal
-21 == $_ and return "Terminal width/height not received by sscanf() after resize" 0 2 2
2639 return "Terminal width/height not received by sscanf() after resize";
2640 2 if (_WIN32) {
2641 case: TB_ERR_WIN_UNSUPPORTED() == $_ and
2642 return "Unsupporrted (Windows)";
2643 case: TB_ERR_WIN_NO_STDIO() == $_ and
2644 return "Stdio not available (Windows)";
2645 case: TB_ERR_WIN_SET_CONMODE() == $_ and
2646 return "Failed to set console mode (Windows)";
2647 case: TB_ERR_WIN_GET_CONMODE() == $_ and
2648 return "Failed to get console mode (Windows)";
2649 case: TB_ERR_WIN_RESIZE() == $_ and
2650 return "Failed to resize console (Windows)";
2651 } #endif
2652 case:
2653 TB_ERR == $_ ||
2654 TB_ERR_INIT_OPEN == $_ ||
2655 TB_ERR_READ == $_ ||
2656 TB_ERR_RESIZE_IOCTL == $_ ||
2657 TB_ERR_RESIZE_PIPE == $_ ||
2658 TB_ERR_RESIZE_SIGACTION == $_ ||
2659 TB_ERR_POLL == $_ ||
2660 TB_ERR_TCGETATTR == $_ ||
2661 TB_ERR_TCSETATTR == $_ ||
2662 TB_ERR_RESIZE_WRITE == $_ ||
2663 TB_ERR_RESIZE_POLL == $_ ||
2664 TB_ERR_RESIZE_READ == $_
2665 2 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
-1 == $_ || -4 == $_ || -10 == $_ || -11 == $_ || -12 == $_ || -13 == $_ || -14 == $_ || -15 == $_ || -16 == $_ || -18 == $_ || -19 == $_ || -20 == $_ and do { $! = $Termbox::global->{"last_errno"} if $Termbox::global->{"last_errno"}; return $! ? "$!" : "Error: $_" } 1 1 2
Condition66%22/33
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_ or -14 == $_ or -15 == $_ or -16 == $_ or -18 == $_ or -19 == $_ or -20 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_ or -14 == $_ or -15 == $_ or -16 == $_ or -18 == $_ or -19 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_ or -14 == $_ or -15 == $_ or -16 == $_ or -18 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_ or -14 == $_ or -15 == $_ or -16 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_ or -14 == $_ or -15 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_ or -14 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_ or -10 == $_
l!l&&r!l&&!r
101
-1 == $_ or -4 == $_
l!l&&r!l&&!r
101
MC/DC8%1/12
-1 == $_ or -4 == $_ or -10 == $_ or -11 == $_ or -12 == $_ or -13 == $_ or -14 == $_ or -15 == $_ or -16 == $_ or -18 == $_ or -19 == $_ or -20 == $_
-1 == $_ -4 == $_ -10 == $_ -11 == $_ -12 == $_ -13 == $_ -14 == $_ -15 == $_ -16 == $_ -18 == $_ -19 == $_ -20 == $_
Truth table15%2/13
A or B or C or D or E or F or G or H or I or J or K or L
A= -1 == $_B= -4 == $_C= -10 == $_D= -11 == $_E= -12 == $_F= -13 == $_G= -14 == $_H= -15 == $_I= -16 == $_J= -18 == $_K= -19 == $_L= -20 == $_
ABCDEFGHIJKLresult
1XXXXXXXXXXX1
01XXXXXXXXXX1
001XXXXXXXXX1
0001XXXXXXXX1
00001XXXXXXX1
000001XXXXXX1
0000001XXXXX1
00000001XXXX1
000000001XXX1
0000000001XX1
00000000001X1
0000000000011
0000000000000
2666 1 ▶ $! = $global->{last_errno} if $global->{last_errno};
Branch: 1 branch
BranchTrueFalseTotal
if $Termbox::global->{"last_errno"} 0 1 1
2667 1 ▶ return $! ? "$!" : "Error: $_";
Branch: 1 branch
BranchTrueFalseTotal
$! ? : 0 1 1
2668 };
2669 default: {
2670 1 ▶ $! = $global->{last_errno} if $global->{last_errno};
Branch: 1 branch
BranchTrueFalseTotal
if $Termbox::global->{"last_errno"} 0 1 1
2671 1 ▶ return $! ? "$!" : "Unknown Error";
Branch: 1 branch
BranchTrueFalseTotal
$! ? : 0 1 1
2672 }
2673 }
2674 }
2675
2676 4 ▶sub tb_cell_buffer { # \@ ()
Subroutine tb_cell_buffer: called CC=4 SCAR=14.1
Pod undocumented
2677 4 TRACE('') if _DEBUG;
2678 4 state $sig = compile();
2679 4 $sig->(@_);
2680 4 my $guard = TRACE_LEAVE(undef) if _DEBUG;
2681
2682 4 state $warned = 0;
2683 4 ▶ warnings::warnif(
Branch: 1 branch
BranchTrueFalseTotal
unless $warned++ 3 1 4
2684 deprecated =>
2685 "tb_cell_buffer() is deprecated; ".
2686 "use tb_get_cell() and related APIs instead"
2687 ) unless $warned++;
2688
2689 4 my $back = $global->{back};
2690 4 ▶ return [] unless ref($back) eq 'cellbuf' && ref($back->{cells}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $back eq "cellbuf" and ref $back->{"cells"} eq "ARRAY" 0 4 4
Condition33%1/3
ref $back eq "cellbuf" and ref $back->{"cells"} eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $back eq "cellbuf" and ref $back->{"cells"} eq "ARRAY"
ref $back eq "cellbuf" ref $back->{"cells"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $back eq "cellbuf"B= ref $back->{"cells"} eq "ARRAY"
ABresult
0X0
100
111
2691 # Exposing the back buffer allows callers to modify it without going through
2692 # the normal APIs. Dirty tracking can no longer be considered reliable,
2693 # therefore all future tb_present() calls use full repaint mode until
2694 # tb_reset().
2695 4 $global->{full_repaint} = 1;
2696 4 return $back->{cells};
2697 }
2698
2699 1 ▶sub tb_has_truecolor { # $int ()
Subroutine tb_has_truecolor: called CC=1 SCAR=0.0
Pod undocumented
2700 1 TRACE('') if _DEBUG;
2701 1 state $sig = compile();
2702 1 $sig->(@_);
2703 1 my $rv;
2704 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2705 1 return $rv = TB_OPT_ATTR_W >= 32 ? 1 : 0;
2706 }
2707
2708 1 ▶sub tb_has_egc { # $int ()
Subroutine tb_has_egc: called CC=1 SCAR=0.0
Pod undocumented
2709 1 TRACE('') if _DEBUG;
2710 1 state $sig = compile();
2711 1 $sig->(@_);
2712 1 my $rv;
2713 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2714 1 return $rv = TB_OPT_EGC ? 1 : 0;
2715 }
2716
2717 1 ▶sub tb_attr_width { # $int ()
Subroutine tb_attr_width: called CC=1 SCAR=0.0
Pod undocumented
2718 1 TRACE('') if _DEBUG;
2719 1 state $sig = compile();
2720 1 $sig->(@_);
2721 1 my $rv;
2722 1 my $guard = TRACE_LEAVE(\$rv) if _DEBUG;
2723 1 return $rv = TB_OPT_ATTR_W;
2724 }
2725
2726 1 ▶sub tb_version { # $str ()
Subroutine tb_version: called CC=1 SCAR=0.0
Pod undocumented
2727 1 TRACE('') if _DEBUG;
2728 1 state $sig = compile();
2729 1 $sig->(@_);
2730 1 my $guard = TRACE_LEAVE(undef) if _DEBUG;
2731 1 DEBUG("return TB_VERSION_STR = '%s'", TB_VERSION_STR) if _DEBUG;
2732 1 return TB_VERSION_STR;
2733 }
2734
2735 12 ▶sub tb_iswprint { # $int ($codepoint)
Subroutine tb_iswprint: called CC=1 SCAR=0.0
Pod undocumented
2736 12 TRACE('%d', @_) if _DEBUG > 2;
2737 12 state $sig = compile(
2738 _PositiveOrZeroInt,
2739 );
2740 12 my ($codepoint) = $sig->(@_);
2741 12 my $rv;
2742 12 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 2;
2743 12 return $rv = tb_iswprint_ex($codepoint, undef);
2744 }
2745
2746 8 ▶sub tb_wcwidth { # $int ($codepoint)
Subroutine tb_wcwidth: called CC=1 SCAR=0.0
Pod undocumented
2747 8 TRACE('%d', @_) if _DEBUG > 2;
2748 8 state $sig = compile(
2749 _PositiveOrZeroInt,
2750 );
2751 8 my ($codepoint) = $sig->(@_);
2752 8 my $rv;
2753 8 my $guard = TRACE_LEAVE(\$rv) if _DEBUG > 2;
2754 8 if (TB_OPT_LIBC_WCHAR) {
2755 return $rv = wcwidth($codepoint);
2756 } else {
2757 8 return $rv = Terminal::WCWidth::wcwidth($codepoint);
2758 } #endif
2759 }
2760
2761 # ------------------------------------------------------------------------
2762 # Internal helper implementation -----------------------------------------
2763 # ------------------------------------------------------------------------
2764
2765 #
2766 # Process Management Helpers
2767 #
2768
2769 15 ▶sub tb_reset { # $int ()
Subroutine tb_reset: called CC=1 SCAR=0.0
Pod undocumented
2770 15 state $sig = compile();
2771 15 $sig->(@_);
2772
2773 15 my $ttyfd_open = $global->{ttyfd_open};
2774 15 my $orig_tios = _WIN32 ? $global->{orig_tios} : undef;
2775 15 $global = {
2776 ttyfd => _WIN32 ? INVALID_HANDLE_VALUE : -1,
2777 rfd => _WIN32 ? INVALID_HANDLE_VALUE : -1,
2778 wfd => _WIN32 ? INVALID_HANDLE_VALUE : -1,
2779 ttyfd_open => $ttyfd_open,
2780 resize_pipefd => [-1, -1],
2781
2782 width => -1,
2783 height => -1,
2784 cursor_x => -1,
2785 cursor_y => -1,
2786 last_x => -1,
2787 last_y => -1,
2788
2789 fg => TB_DEFAULT,
2790 bg => TB_DEFAULT,
2791 last_fg => ~TB_DEFAULT,
2792 last_bg => ~TB_DEFAULT,
2793
2794 input_mode => TB_INPUT_ESC,
2795 output_mode => TB_OUTPUT_NORMAL,
2796
2797 terminfo => '',
2798
2799 caps => [ (undef) x TB_CAP__COUNT ],
2800 cap_trie => captrie->new(),
2801
2802 inbuf => '',
2803 outbuf => '',
2804 back => cellbuf->new(),
2805 front => cellbuf->new(),
2806
2807 orig_tios => $orig_tios,
2808 has_orig_tios => 0,
2809
2810 last_errno => 0,
2811 initialized => 0,
2812
2813 fn_extract_esc_pre => undef,
2814 fn_extract_esc_post => undef,
2815
2816 full_repaint => 0,
2817 dirty_lines => [],
2818 };
2819
2820 15 return TB_OK;
2821 }
2822
2823 6 ▶sub tb_printf_inner { # $int ($x, $y, $fg, $bg, \$out_w|undef, $fmt, @args)
Subroutine tb_printf_inner: called CC=4 SCAR=14.5
Pod undocumented
2824 6 state $sig = compile(
2825 _Int,
2826 _Int,
2827 _PositiveOrZeroInt,
2828 _PositiveOrZeroInt,
2829 _Maybe[_ScalarRef],
2830 _Str,
2831 );
2832 6 my ($x, $y, $fg, $bg, $out_w, $fmt, @args) = ($sig->(@_[0..5]), @_[6..$#_]);
2833
2834 6 ▶ my $str = @args ? sprintf($fmt, @args) : $fmt;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 2 4 6
2835 6 ▶ return TB_ERR
Branch: 1 branch
BranchTrueFalseTotal
if not defined $str or length $str >= 4096 0 6 6
Condition33%1/3
not defined $str or length $str >= 4096
l!l&&r!l&&!r
006
MC/DC0%0/2
not defined $str or length $str >= 4096
not defined $str length $str >= 4096
Truth table33%1/3
not(A) or B
A= not defined $strB= length $str >= 4096
ABresult
1X1
011
000
2836 if !defined($str)
2837 || length($str) >= TB_OPT_PRINTF_BUF;
2838
2839 6 return tb_print_ex($x, $y, $fg, $bg, $out_w, $str);
2840 }
2841
2842 6 ▶sub tb_deinit { # $int ()
Subroutine tb_deinit: called CC=12 SCAR=31.4
Pod undocumented
2843 6 state $sig = compile();
2844 6 $sig->(@_);
2845
2846 6 ▶ if (defined($global->{caps}[0]) && $global->{wfd} >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Termbox::global->{"caps"}[0] and $Termbox::global->{"wfd"} >= 0) 0 6 6
Condition33%1/3
defined $Termbox::global->{"caps"}[0] and $Termbox::global->{"wfd"} >= 0
!ll&&!rl&&r
600
MC/DC0%0/2
defined $Termbox::global->{"caps"}[0] and $Termbox::global->{"wfd"} >= 0
defined $Termbox::global->{"caps"}[0] $Termbox::global->{"wfd"} >= 0
Truth table33%1/3
A and B
A= defined $Termbox::global->{"caps"}[0]B= $Termbox::global->{"wfd"} >= 0
ABresult
0X0
100
111
2847 0 bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_SHOW_CURSOR]);
2848 0 bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_SGR0]);
2849 0 bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_CLEAR_SCREEN]);
2850 0 bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_EXIT_CA]);
2851 0 bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_EXIT_KEYPAD]);
2852 0 bytebuf_puts(\$global->{outbuf}, TB_HARDCAP_EXIT_MOUSE);
2853 0 bytebuf_flush(\$global->{outbuf}, $global->{wfd});
2854 }
2855
2856 6 ▶ if ($global->{ttyfd} >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"ttyfd"} >= 0) 1 5 6
2857 1 ▶ if ($global->{has_orig_tios}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"has_orig_tios"}) 0 1 1
2858 0 if (_WIN32) {
2859 # Restore the console input mode
2860 my $hInput = $global->{rfd} // INVALID_HANDLE_VALUE;
2861 if ($hInput == INVALID_HANDLE_VALUE) {
2862 $^E = ERROR_INVALID_HANDLE;
2863 $global->{last_errno} = $! = EBADF;
2864 return TB_ERR_WIN_NO_STDIO;
2865 }
2866 my $orig_mode_in = $global->{orig_tios}{mode_in};
2867 if (defined $orig_mode_in) {
2868 $^E = 0;
2869 Win32::Console::_SetConsoleMode($hInput, $orig_mode_in);
2870 if ($^E) {
2871 $global->{last_errno} = $! = ENOTTY;
2872 return TB_ERR_WIN_SET_CONMODE;
2873 }
2874 }
2875
2876 # Restore the console output mode
2877 my $hOutput = $global->{wfd} // INVALID_HANDLE_VALUE;
2878 if ($hOutput == INVALID_HANDLE_VALUE) {
2879 $^E = ERROR_INVALID_HANDLE;
2880 $global->{last_errno} = $! = EBADF;
2881 return TB_ERR_WIN_NO_STDIO;
2882 }
2883 my $orig_mode_out = $global->{orig_tios}{mode_out};
2884 if (defined $orig_mode_out) {
2885 $^E = 0;
2886 Win32::Console::_SetConsoleMode($hOutput, $orig_mode_out);
2887 if ($^E) {
2888 $global->{last_errno} = $! = ENOTTY;
2889 return TB_ERR_WIN_SET_CONMODE;
2890 }
2891 }
2892
2893 # Restore the console output codepage
2894 my $orig_cp_out = $global->{orig_tios}{cp_out};
2895 if (defined $orig_cp_out) {
2896 if (!Win32::Console::_SetConsoleOutputCP($orig_cp_out)) {
2897 $global->{last_errno} = $! = EIO;
2898 return TB_ERR_WIN_SET_CONMODE;
2899 }
2900 }
2901 } else {
2902 0 $global->{orig_tios}->setattr($global->{ttyfd}, TCSAFLUSH);
2903 } #endif
2904 }
2905 1 ▶ if ($global->{ttyfd_open}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"ttyfd_open"}) 0 1 1
2906 0 close(TB_OUT);
2907 0 if (_WIN32) {
2908 close(TB_IN);
2909 } #endif
2910 0 $global->{ttyfd_open} = 0;
2911 0 $global->{ttyfd} = -1;
2912 }
2913 }
2914
2915 6 if (!_WIN32) {
2916 6 ▶ $SIG{WINCH} = 'DEFAULT' if exists $SIG{WINCH};
Branch: 1 branch
BranchTrueFalseTotal
if exists $SIG{"WINCH"} 6 0 6
2917 6 foreach my $fd (@{ $global->{resize_pipefd}}) {
2918 12 ▶ POSIX::close($fd) if $fd >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fd >= 0 0 12 12
2919 }
2920 } #endif
2921
2922 6 cellbuf_free($global->{back});
2923 6 cellbuf_free($global->{front});
2924 6 bytebuf_free(\$global->{inbuf});
2925 6 bytebuf_free(\$global->{outbuf});
2926
2927 6 ▶ $global->{terminfo} = '' if $global->{terminfo};
Branch: 1 branch
BranchTrueFalseTotal
if $Termbox::global->{"terminfo"} 0 6 6
2928
2929 6 ▶ my $rv = $global->{cap_trie}->clear() if $global->{cap_trie};
Branch: 1 branch
BranchTrueFalseTotal
if $Termbox::global->{"cap_trie"} 6 0 6
2930 6 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 6 6
2931
2932 6 tb_reset();
2933 6 return TB_OK;
2934 }
2935
2936 39 ▶END {
Subroutine END: called CC=3 SCAR=11.0
2937 39 ▶ return unless $global && $global->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
unless $Termbox::global and $Termbox::global->{"initialized"} 38 1 39
Condition66%2/3
$Termbox::global and $Termbox::global->{"initialized"}
!ll&&!rl&&r
0381
MC/DC50%1/2
$Termbox::global and $Termbox::global->{"initialized"}
$Termbox::global $Termbox::global->{"initialized"}
Truth table66%2/3
A and B
A= $Termbox::globalB= $Termbox::global->{"initialized"}
ABresult
0X0
100
111
2938 1 DEBUG("tb_shutdown() not called before program exit") if _DEBUG;
2939 1 try: local $@; eval {
2940 1 tb_deinit()
2941 };
2942 }
2943
2944 19 ▶sub tb_iswprint_ex { # $bool ($ch, \$width|undef)
Subroutine tb_iswprint_ex: called CC=11 SCAR=24.1
Pod undocumented
2945 19 state $sig = compile(
2946 _PositiveOrZeroInt,
2947 _Maybe[_ScalarRef],
2948 );
2949 19 my ($ch, $width) = $sig->(@_);
2950 19 if (TB_OPT_LIBC_WCHAR) {
2951 my $w = wcwidth($ch);
2952 $$width = $w if defined $width;
2953
2954 # NUL is not printable even though width is 0.
2955 return $ch != 0 && $w >= 0 ? 1 : 0;
2956 } else {
2957 # Fast path for 1-byte codepoints
2958 19 ▶ if (($ch >= 0x20 && $ch <= 0x7e) || ($ch >= 0xa0 && $ch <= 0xff)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch >= 32 and $ch <= 126 or $ch >= 160 and $ch <= 255) 12 7 19
Condition100%9/9
$ch >= 32 and $ch <= 126 or $ch >= 160 and $ch <= 255
l!l&&r!l&&!r
1117
$ch >= 32 and $ch <= 126
!ll&&!rl&&r
4411
$ch >= 160 and $ch <= 255
!ll&&!rl&&r
431
MC/DC75%3/4
$ch >= 32 and $ch <= 126 or $ch >= 160 and $ch <= 255
$ch >= 32 $ch <= 126 $ch >= 160 $ch <= 255
Truth table57%4/7
A and B or C and D
A= $ch >= 32B= $ch <= 126C= $ch >= 160D= $ch <= 255
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
2959 12 ▶ $$width = 1 if defined $width;
Branch: 1 branch
BranchTrueFalseTotal
if defined $width 6 6 12
2960 12 return 1;
2961 }
2962 7 ▶ if ($ch <= 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch <= 255) 4 3 7
2963 4 ▶ $$width = ($ch == 0 ? 0 : -1) if defined $width;
Branch: 2 branches
BranchTrueFalseTotal
if defined $width 0 4 4
$ch == 0 ? : 0 0 0
2964 4 return 0;
2965 }
2966
2967 3 my $w = Terminal::WCWidth::wcwidth($ch);
2968 3 ▶ $$width = $w if defined $width;
Branch: 1 branch
BranchTrueFalseTotal
if defined $width 1 2 3
2969 3 ▶ return $w >= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$w >= 0 ? : 3 0 3
2970 } #endif
2971 }
2972
2973 8 ▶sub tb_cluster_width { # $int (\@cluster, $nch)
Subroutine tb_cluster_width: called CC=15 SCAR=34.0
Pod undocumented
2974 8 state $sig = compile(
2975 _ArrayRef,
2976 _PositiveOrZeroInt,
2977 );
2978 8 my ($cluster, $nch) = $sig->(@_);
2979
2980 8 my $wmax = -1;
2981 8 my ($vs15, $vs16, $ri, $zwj) = (0, 0, 0, 0);
2982
2983 8 for my $i (0 .. $nch-1) {
2984 8 ▶ my $c = $cluster->[$i] // return -1;
Condition50%1/2
$cluster->[$i] // (return -1)
l!l
80
MC/DC0%0/1
$cluster->[$i] // (return -1)
$cluster->[$i]
Truth table50%1/2
A
A= $cluster->[$i]
Aresult
11
00
2985 0 ▶ if ($c == 0xfe0e) { $vs15++ }
Branch: 4 branches
BranchTrueFalseTotal
if ($c == 65038) { } 0 8 8
elsif ($c == 65039) { } 0 8 8
elsif ($c == 8205) { } 0 8 8
elsif ($c >= 127462 and $c <= 127487) { } 0 16 16
2986 0 elsif ($c == 0xfe0f) { $vs16++ }
2987 0 elsif ($c == 0x200d) { $zwj++ }
2988 0 ▶ elsif ($c >= 0x1f1e6 && $c <= 0x1f1ff) { $ri++ }
Condition33%1/3
$c >= 127462 and $c <= 127487
!ll&&!rl&&r
800
MC/DC0%0/2
$c >= 127462 and $c <= 127487
$c >= 127462 $c <= 127487
Truth table33%1/3
A and B
A= $c >= 127462B= $c <= 127487
ABresult
0X0
100
111
2989
2990 8 my $w = tb_wcwidth($c);
2991 8 ▶ $w = -1 unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 0 8 8
2992 8 ▶ $wmax = $w if $w > $wmax;
Branch: 1 branch
BranchTrueFalseTotal
if $w > $wmax 8 0 8
2993 }
2994
2995 8 ▶ if ($wmax >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wmax >= 1) 7 1 8
2996 7 ▶ return 1 if $vs15;
Branch: 1 branch
BranchTrueFalseTotal
if $vs15 0 7 7
2997 7 ▶ return 2 if $vs16 || $zwj || $ri >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $vs16 or $zwj or $ri >= 2 0 7 7
Condition33%2/6
$vs16 or $zwj or $ri >= 2
l!l&&r!l&&!r
007
$vs16 or $zwj
l!l&&r!l&&!r
007
MC/DC0%0/3
$vs16 or $zwj or $ri >= 2
$vs16 $zwj $ri >= 2
Truth table25%1/4
A or B or C
A= $vs16B= $zwjC= $ri >= 2
ABCresult
1XX1
01X1
0011
0000
2998 }
2999
3000 8 return $wmax;
3001 }
3002
3003 #
3004 # Terminal initialization helpers
3005 #
3006
3007 1 ▶sub init_term_attrs { # $int ()
Subroutine init_term_attrs: called CC=6 SCAR=33.4
Pod undocumented
3008 1 state $sig = compile();
3009 1 $sig->(@_);
3010
3011 1 ▶ return TB_OK if $global->{ttyfd} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $Termbox::global->{"ttyfd"} < 0 1 0 1
3012
3013 0 if (_WIN32) {
3014 # Set the console input mode
3015 my $hInput = $global->{rfd} // INVALID_HANDLE_VALUE;
3016 if ($hInput == INVALID_HANDLE_VALUE) {
3017 $^E = ERROR_INVALID_HANDLE;
3018 $global->{last_errno} = $! = EBADF;
3019 return TB_ERR_WIN_NO_STDIO;
3020 }
3021 $^E = 0;
3022 my $orig_mode_in = Win32::Console::_GetConsoleMode($hInput);
3023 if ($^E) {
3024 $global->{last_errno} = $! = ENOTTY;
3025 return TB_ERR_WIN_GET_CONMODE;
3026 }
3027 my $mode = $orig_mode_in;
3028 $mode &= ~ENABLE_ECHO_INPUT(); # Turn off echo in a terminal
3029 $mode &= ~ENABLE_LINE_INPUT(); # no CR for ReadFile or ReadConsole
3030 $mode |= ENABLE_WINDOW_INPUT(); # Report changes in buffer size
3031 $mode &= ~ENABLE_PROCESSED_INPUT(); # Report CTRL+C and SHIFT+Arrow events
3032 $mode |= ENABLE_EXTENDED_FLAGS(); # Disable the Quick Edit mode,
3033 $mode &= ~ENABLE_QUICK_EDIT_MODE(); # which inhibits the mouse
3034 $mode |= ENABLE_VIRTUAL_TERMINAL_INPUT(); # Allow ANSI escape sequences
3035 $^E = 0;
3036 Win32::Console::_SetConsoleMode($hInput, $mode);
3037 if ($^E) {
3038 $global->{last_errno} = $! = ENOTTY;
3039 return TB_ERR_WIN_SET_CONMODE;
3040 }
3041
3042 # Set the console output mode
3043 my $hOutput = $global->{wfd} // INVALID_HANDLE_VALUE;
3044 if ($hOutput == INVALID_HANDLE_VALUE) {
3045 $^E = ERROR_INVALID_HANDLE;
3046 $global->{last_errno} = $! = EBADF;
3047 return TB_ERR_WIN_NO_STDIO;
3048 }
3049 $^E = 0;
3050 my $orig_mode_out = Win32::Console::_GetConsoleMode($hOutput);
3051 if ($^E) {
3052 $global->{last_errno} = $! = ENOTTY;
3053 return TB_ERR_WIN_GET_CONMODE;
3054 }
3055 $mode = $orig_mode_out;
3056 $mode |= ENABLE_PROCESSED_OUTPUT(); # enable when using escape sequences.
3057 $mode &= ~ENABLE_WRAP_AT_EOL_OUTPUT(); # Avoid scrolling when reaching EOL.
3058 $mode |= DISABLE_NEWLINE_AUTO_RETURN(); # Do not do CR on LF.
3059 $mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING(); # Allow ANSI escape sequences.
3060 $^E = 0;
3061 Win32::Console::_SetConsoleMode($hOutput, $mode);
3062 if ($^E) {
3063 $global->{last_errno} = $! = ENOTTY;
3064 return TB_ERR_WIN_SET_CONMODE;
3065 }
3066
3067 # Set the console output codepage to utf8
3068 $^E = 0;
3069 my $orig_cp_out = Win32::Console::_GetConsoleOutputCP();
3070 if ($^E) {
3071 $global->{last_errno} = $! = EIO;
3072 return TB_ERR_WIN_GET_CONMODE;
3073 }
3074 if (!Win32::Console::_SetConsoleOutputCP(CP_UTF8())) {
3075 $global->{last_errno} = $! = EIO;
3076 return TB_ERR_WIN_SET_CONMODE;
3077 }
3078
3079 if (!$global->{has_orig_tios}) {
3080 $global->{orig_tios} = {
3081 mode_in => $orig_mode_in,
3082 mode_out => $orig_mode_out,
3083 cp_out => $orig_cp_out,
3084 };
3085 $global->{has_orig_tios} = 1;
3086 }
3087
3088 return TB_OK;
3089 } #endif
3090
3091 0 ▶ $global->{orig_tios} = POSIX::Termios->new() // return TB_ERR_TCGETATTR;
Condition0%0/2
"POSIX::Termios"->new // (return -15)
l!l
00
MC/DC0%0/1
"POSIX::Termios"->new // (return -15)
"POSIX::Termios"->new
Truth table0%0/2
A
A= "POSIX::Termios"->new
Aresult
11
00
3092
3093 0 ▶ if (!defined $global->{orig_tios}->getattr($global->{ttyfd})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Termbox::global->{"orig_tios"}->getattr($Termbox::global->{"ttyfd"})) 0 0 0
3094 0 $global->{last_errno} = 0+ $!;
3095 0 return TB_ERR_TCGETATTR;
3096 }
3097
3098 0 ▶ my $tios = $global->{orig_tios}->$clone() // return TB_ERR_TCSETATTR;
Condition0%0/2
$Termbox::global->{"orig_tios"}->$clone // (return -16)
l!l
00
MC/DC0%0/1
$Termbox::global->{"orig_tios"}->$clone // (return -16)
$Termbox::global->{"orig_tios"}->$clone
Truth table0%0/2
A
A= $Termbox::global->{"orig_tios"}->$clone
Aresult
11
00
3099 0 $global->{has_orig_tios} = 1;
3100
3101 0 cfmakeraw($tios);
3102 0 $tios->setcc(VMIN, 1);
3103 0 $tios->setcc(VTIME, 0);
3104
3105 0 ▶ if (!defined $tios->setattr($global->{ttyfd}, TCSAFLUSH)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tios->setattr($Termbox::global->{"ttyfd"}, 2)) 0 0 0
3106 0 $global->{last_errno} = 0+ $!;
3107 0 return TB_ERR_TCSETATTR;
3108 }
3109
3110 0 return TB_OK;
3111 }
3112
3113 1 ▶sub init_term_caps { # $int ()
Subroutine init_term_caps: called CC=2 SCAR=7.2
Pod undocumented
3114 1 state $sig = compile();
3115 1 $sig->(@_);
3116 1 if (_WIN32) {
3117 return load_builtin_caps();
3118 } #endif
3119 1 ▶ if (load_terminfo() == TB_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (load_terminfo() == 0) 1 0 1
3120 1 return parse_terminfo_caps();
3121 }
3122 0 return load_builtin_caps();
3123 }
3124
3125 #
3126 # Terminfo parsing helpers
3127 #
3128
3129 3 ▶sub load_terminfo { # $int ()
Subroutine load_terminfo: called CC=14 SCAR=35.3
Pod undocumented
3130 3 state $sig = compile();
3131 3 $sig->(@_);
3132 3 if (_WIN32) {
3133 return TB_ERR_WIN_UNSUPPORTED;
3134 } #endif
3135 3 my $rv;
3136
3137 # See terminfo(5) "Fetching Compiled Descriptions" for a description of
3138 # this behavior. Some of these paths are compile-time ncurses options, so
3139 # best guesses are used here.
3140 3 my $term = $ENV{TERM};
3141 3 ▶ return TB_ERR unless $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $term 0 3 3
3142
3143 # If TERMINFO is set, try that directory first
3144 3 ▶ if (defined(my $terminfo = $ENV{"TERMINFO"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $terminfo = $ENV{"TERMINFO"})) 0 3 3
3145 0 $rv = load_terminfo_from_path($terminfo, $term);
3146 0 ▶ return $rv if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 0 0 0
3147 }
3148
3149 # Next try ~/.terminfo
3150 3 ▶ if (defined(my $home = $ENV{"HOME"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $home = $ENV{"HOME"})) 3 0 3
3151 3 my $tmp = "$home/.terminfo";
3152
3153 3 ▶ return TB_ERR if length($tmp) >= TB_PATH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if length $tmp >= 4096 0 3 3
3154
3155 3 $rv = load_terminfo_from_path($tmp, $term);
3156 3 ▶ return $rv if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 0 3 3
3157 }
3158
3159 # Next try TERMINFO_DIRS
3160 #
3161 # Note, empty entries are supposed to be interpretted as the "compiled-in
3162 # default", which is of course system-dependent. Previously /etc/terminfo
3163 # was used here. Let's skip empty entries altogether rather than give
3164 # precedence to a guess, and check common paths after this loop.
3165 3 ▶ if (defined(my $dirs = $ENV{"TERMINFO_DIRS"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $dirs = $ENV{"TERMINFO_DIRS"})) 0 3 3
3166 0 ▶ return TB_ERR if length($dirs) >= TB_PATH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if length $dirs >= 4096 0 0 0
3167 0 foreach my $dir (split /:/, $dirs, -1) {
3168 0 ▶ next if $dir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" 0 0 0
3169 0 $rv = load_terminfo_from_path($dir, $term);
3170 0 ▶ return $rv if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 0 0 0
3171 }
3172 }
3173
3174 # Try compile-time terminfo directory if available
3175 3 if (defined TB_TERMINFO_DIR) {
3176 $rv = load_terminfo_from_path(TB_TERMINFO_DIR, $term);
3177 return $rv if $rv == TB_OK;
3178 }
3179
3180 3 foreach my $dir (qw(
3181 /usr/local/etc/terminfo
3182 /usr/local/share/terminfo
3183 /usr/local/lib/terminfo
3184 /etc/terminfo
3185 /usr/share/terminfo
3186 /usr/lib/terminfo
3187 /usr/share/lib/terminfo
3188 /lib/terminfo
3189 )) {
3190 15 $rv = load_terminfo_from_path($dir, $term);
3191 15 ▶ return $rv if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 3 12 15
3192 }
3193
3194 0 return TB_ERR;
3195 }
3196
3197 19 ▶sub load_terminfo_from_path { # $int ($path, $term)
Subroutine load_terminfo_from_path: called CC=5 SCAR=18.0
Pod undocumented
3198 19 state $sig = compile(
3199 _Str,
3200 _Str,
3201 );
3202 19 my ($path, $term) = $sig->(@_);
3203 19 my $rv;
3204 my $tmp;
3205
3206 # Look for term at this terminfo location, e.g., <terminfo>/x/xterm
3207 19 $tmp = "$path/" . substr($term, 0, 1) . "/$term";
3208 19 ▶ return TB_ERR if length($tmp) >= TB_PATH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if length $tmp >= 4096 0 19 19
3209
3210 19 $rv = read_terminfo_path($tmp);
3211 19 ▶ return $rv if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 4 15 19
3212
3213 15 ▶ if ($^O eq 'darwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "darwin") 0 15 15
3214 # Try the Darwin equivalent path, e.g., <terminfo>/78/xterm
3215 0 $tmp = "$path/" . sprintf('%x', ord(substr($term, 0, 1))) . "/$term";
3216 0 ▶ return TB_ERR if length($tmp) >= TB_PATH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if length $tmp >= 4096 0 0 0
3217
3218 0 return read_terminfo_path($tmp);
3219 }
3220
3221 15 return TB_ERR;
3222 }
3223
3224 19 ▶sub read_terminfo_path { # $int ($path)
Subroutine read_terminfo_path: called CC=5 SCAR=17.6
Pod undocumented
3225 19 state $sig = compile(
3226 _Str,
3227 );
3228 19 my ($path) = $sig->(@_);
3229
3230 19 ▶ open(my $fp, '<:raw', $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $fp, "<:raw", $path or return -1 4 15 19
3231 or return TB_ERR;
3232
3233 4 my @st = stat($fp);
3234 4 ▶ if (!@st) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@st) 0 4 4
3235 0 close($fp);
3236 0 return TB_ERR;
3237 }
3238
3239 4 my $fsize = $st[7];
3240
3241 4 my $data = '';
3242 4 my $nread = sysread($fp, $data, $fsize);
3243
3244 4 ▶ if (!defined($nread) || $nread != $fsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $nread or $nread != $fsize) 0 4 4
Condition33%1/3
not defined $nread or $nread != $fsize
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $nread or $nread != $fsize
not defined $nread $nread != $fsize
Truth table33%1/3
not(A) or B
A= not defined $nreadB= $nread != $fsize
ABresult
1X1
011
000
3245 0 close($fp);
3246 0 return TB_ERR;
3247 }
3248
3249 4 $global->{terminfo} = $data;
3250
3251 4 close($fp);
3252 4 return TB_OK;
3253 }
3254
3255 2 ▶sub parse_terminfo_caps { # $int ()
Subroutine parse_terminfo_caps: called CC=9 SCAR=22.3
Pod undocumented
3256 2 state $sig = compile();
3257 2 $sig->(@_);
3258
3259 2 my $terminfo = $global->{terminfo};
3260 2 ▶ my $nterminfo = length($terminfo) // 0;
Condition50%1/2
length $terminfo // 0
l!l
20
MC/DC0%0/1
length $terminfo // 0
length $terminfo
Truth table50%1/2
A
A= length $terminfo
Aresult
11
00
3261
3262 # Ensure there's at least a header's worth of data
3263 2 ▶ return TB_ERR if $nterminfo < 6 * INT16_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $nterminfo < 12 0 2 2
3264
3265 2 ▶ my $terminfo_cap_indexes = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $terminfo_cap_indexes = eval { do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} require Termbox::PP::Terminfo::Builtin; \@Termbox::PP::Terminfo::Builtin::terminfo_cap_indexes } } or return -1 2 0 2
3266 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
3267 2 require Termbox::PP::Terminfo::Builtin;
3268 2 \@Termbox::PP::Terminfo::Builtin::terminfo_cap_indexes;
3269 } or return TB_ERR;
3270
3271 2 my ($magic_number, $nbytes_names, $nbytes_bools, $num_ints, $num_offsets,
3272 $nbytes_strings);
3273
3274 # header[0] the magic number (octal 0432 or 01036)
3275 # header[1] the size, in bytes, of the names section
3276 # header[2] the number of bytes in the boolean section
3277 # header[3] the number of short integers in the numbers section
3278 # header[4] the number of offsets (short integers) in the strings section
3279 # header[5] the size, in bytes, of the string table
3280 2 get_terminfo_int16(0 * INT16_SIZE, \$magic_number);
3281 2 get_terminfo_int16(1 * INT16_SIZE, \$nbytes_names);
3282 2 get_terminfo_int16(2 * INT16_SIZE, \$nbytes_bools);
3283 2 get_terminfo_int16(3 * INT16_SIZE, \$num_ints);
3284 2 get_terminfo_int16(4 * INT16_SIZE, \$num_offsets);
3285 2 get_terminfo_int16(5 * INT16_SIZE, \$nbytes_strings);
3286
3287 # Legacy ints are 16-bit, extended ints are 32-bit
3288 2 ▶ my $bytes_per_int = $magic_number == 01036 ? 4 # 32-bit
Branch: 1 branch
BranchTrueFalseTotal
$magic_number == 542 ? : 0 2 2
3289 : 2; # 16-bit
3290
3291 # Between the boolean section and the number section, a null byte will be
3292 # inserted, if necessary, to ensure that the number section begins on
3293 # an even byte
3294 2 ▶ my $align_offset = (($nbytes_names + $nbytes_bools) % 2 != 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($nbytes_names + $nbytes_bools) % 2 != 0 ? : 2 0 2
3295
3296 2 my $nbytes_header = 6 * INT16_SIZE;
3297 2 my $pos_str_offsets =
3298 $nbytes_header # header (12 bytes)
3299 + $nbytes_names # length of names section
3300 + $nbytes_bools # length of boolean section
3301 + $align_offset
3302 + ($num_ints * $bytes_per_int); # length of string offsets section
3303
3304 2 my $pos_str_table =
3305 $pos_str_offsets
3306 + ($num_offsets * INT16_SIZE); # length of string offsets table
3307
3308 2 ▶ $global->{caps} ||= [];
Condition50%1/2
$Termbox::global->{"caps"} ||= []
l!l
20
MC/DC0%0/1
$Termbox::global->{"caps"} ||= []
$Termbox::global->{"caps"}
Truth table50%1/2
A
A= $Termbox::global->{"caps"}
Aresult
11
00
3309
3310 # Load caps
3311 2 ▶ for (my $i = 0; $i < TB_CAP__COUNT; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 38) 76 2 78
3312 76 my $cap = get_terminfo_string($pos_str_offsets, $num_offsets,
3313 $pos_str_table, $nbytes_strings, $terminfo_cap_indexes->[$i]);
3314 # Something is not right
3315 76 ▶ return TB_ERR unless defined $cap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cap 0 76 76
3316 76 $global->{caps}->[$i] = $cap;
3317 }
3318
3319 2 return TB_OK;
3320 }
3321
3322 1 ▶sub load_builtin_caps { # $int ()
Subroutine load_builtin_caps: called CC=8 SCAR=24.5
Pod undocumented
3323 1 state $sig = compile();
3324 1 $sig->(@_);
3325 1 my $term = $ENV{"TERM"};
3326 1 if (_WIN32) {
3327 # Windows Virtual Terminal is xterm-256color compatible
3328 # https://github.com/microsoft/terminal/issues/6045#issuecomment-631645277
3329 # https://superuser.com/a/1691012
3330 $term ||= 'xterm-256color'
3331 } #endif
3332 1 ▶ return TB_ERR_NO_TERM unless $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $term 0 1 1
3333
3334 1 ▶ my $builtin_terms = do {
Branch: 1 branch
BranchTrueFalseTotal
my $builtin_terms = do { require Termbox::PP::Terminfo::Builtin; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} \%Termbox::PP::Terminfo::Builtin::builtin_terms } or return -1 1 0 1
3335 1 require Termbox::PP::Terminfo::Builtin;
3336 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
3337 1 \%Termbox::PP::Terminfo::Builtin::builtin_terms;
3338 } or return TB_ERR;
3339 1 ▶ my $builtin_terms_orders = do {
Branch: 1 branch
BranchTrueFalseTotal
my $builtin_terms_orders = do { require Termbox::PP::Terminfo::Builtin; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} \@Termbox::PP::Terminfo::Builtin::builtin_terms_orders } or return -1 1 0 1
3340 1 require Termbox::PP::Terminfo::Builtin;
3341 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
3342 1 \@Termbox::PP::Terminfo::Builtin::builtin_terms_orders;
3343 } or return TB_ERR;
3344
3345 # Check for exact TERM match
3346 1 ▶ if (exists $builtin_terms->{$term}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $builtin_terms->{$term}) 1 0 1
3347 1 my $caps = $builtin_terms->{$term};
3348 1 @{ $global->{caps} } = @$caps[0 .. TB_CAP__COUNT - 1];
3349 1 return TB_OK;
3350 }
3351
3352 # Check for partial TERM or alias match
3353 0 foreach my $name (@$builtin_terms_orders) {
3354 0 ▶ next if index($term, $name) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($term, $name) == -1 0 0 0
3355 0 ▶ next unless exists $builtin_terms->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $builtin_terms->{$name} 0 0 0
3356
3357 0 my $caps = $builtin_terms->{$name};
3358 0 @{ $global->{caps} } = @$caps[0 .. TB_CAP__COUNT - 1];
3359 0 return TB_OK;
3360 }
3361
3362 0 return TB_ERR_UNSUPPORTED_TERM;
3363 }
3364
3365 80 ▶sub get_terminfo_string { # $str|undef ($offsets_pos, $offsets_len, $table_pos, $table_size, $index)
Subroutine get_terminfo_string: called CC=7 SCAR=21.4
Pod undocumented
3366 80 state $sig = compile(
3367 _Int,
3368 _Int,
3369 _Int,
3370 _Int,
3371 _Int,
3372 );
3373 80 my ($offsets_pos, $offsets_len, $table_pos, $table_size, $index) = $sig->(@_);
3374
3375 80 ▶ if ($index >= $offsets_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= $offsets_len) 1 79 80
3376 # An index beyond the offset table indicates absent
3377 # See 'convert_strings' in tinfo 'read_entry.c'
3378 1 return '';
3379 }
3380
3381 79 my $table_offset;
3382 79 my $table_offset_offset = $offsets_pos + ($index * INT16_SIZE);
3383 79 ▶ if (get_terminfo_int16($table_offset_offset, \$table_offset) != TB_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_terminfo_int16($table_offset_offset, \$table_offset) != 0) 0 79 79
3384 # offset beyond end of terminfo entry
3385 # Truncated/corrupt terminfo entry?
3386 0 return undef;
3387 }
3388
3389 79 ▶ if ($table_offset < 0 || $table_offset >= $table_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_offset < 0 or $table_offset >= $table_size) 0 79 79
Condition33%1/3
$table_offset < 0 or $table_offset >= $table_size
l!l&&r!l&&!r
0079
MC/DC0%0/2
$table_offset < 0 or $table_offset >= $table_size
$table_offset < 0 $table_offset >= $table_size
Truth table33%1/3
A or B
A= $table_offset < 0B= $table_offset >= $table_size
ABresult
1X1
011
000
3390 # A negative offset indicates absent
3391 # An offset beyond the string table indicates absent
3392 # See 'convert_strings' in tinfo 'read_entry.c'
3393 0 return '';
3394 }
3395
3396 79 my $str_offset = $table_pos + $table_offset;
3397 79 ▶ if ($str_offset >= length($global->{terminfo})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_offset >= length $Termbox::global->{"terminfo"}) 0 79 79
3398 # string beyond end of terminfo entry
3399 # Truncated/corrupt terminfo entry?
3400 0 return undef;
3401 }
3402
3403 79 my $str = substr($global->{terminfo}, $str_offset);
3404 79 my $len = index($str, "\0");
3405 79 ▶ return $len >= 0 ? substr($str, 0, $len) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$len >= 0 ? : 79 0 79
3406 }
3407
3408 95 ▶sub get_terminfo_int16 { # $int ($offset, \$val)
Subroutine get_terminfo_int16: called CC=3 SCAR=11.0
Pod undocumented
3409 95 state $sig = compile(
3410 _Int,
3411 _ScalarRef,
3412 );
3413 95 my ($offset, $val) = $sig->(@_);
3414 95 ▶ if ($offset < 0 || $offset + INT16_SIZE > length($global->{terminfo})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0 or $offset + 2 > length $Termbox::global->{"terminfo"}) 1 94 95
Condition66%2/3
$offset < 0 or $offset + 2 > length $Termbox::global->{"terminfo"}
l!l&&r!l&&!r
0194
MC/DC50%1/2
$offset < 0 or $offset + 2 > length $Termbox::global->{"terminfo"}
$offset < 0 $offset + 2 > length $Termbox::global->{"terminfo"}
Truth table66%2/3
A or B
A= $offset < 0B= $offset + 2 > length $Termbox::global->{"terminfo"}
ABresult
1X1
011
000
3415 1 $$val = -1;
3416 1 return TB_ERR;
3417 }
3418 94 $$val = unpack('s<', substr($global->{terminfo}, $offset, INT16_SIZE));
3419 94 return TB_OK;
3420 }
3421
3422 #
3423 # Resize handling helpers
3424 #
3425
3426 1 ▶sub init_resize_handler { # $int ()
Subroutine init_resize_handler: called CC=3 SCAR=12.2
Pod undocumented
3427 1 state $sig = compile();
3428 1 $sig->(@_);
3429 1 if (_WIN32) {
3430 return TB_OK;
3431 } #endif
3432 1 my ($rfd, $wfd);
3433 1 ▶ unless (($rfd, $wfd) = POSIX::pipe()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = POSIX::pipe()) 0 1 1
3434 0 $global->{last_errno} = 0+ $!;
3435 0 return TB_ERR_RESIZE_PIPE;
3436 }
3437 1 $global->{resize_pipefd} = [$rfd, $wfd];
3438
3439 1 ▶ unless (exists $SIG{WINCH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $SIG{"WINCH"}) 0 1 1
3440 0 $global->{last_errno} = $! = EACCES;
3441 0 return TB_ERR_RESIZE_SIGACTION;
3442 }
3443 1 $SIG{WINCH} = \&handle_resize;
3444 1 return TB_OK;
3445 }
3446
3447 2 ▶sub resize_cellbufs { # $int ()
Subroutine resize_cellbufs: called CC=6 SCAR=18.2
Pod undocumented
3448 2 state $sig = compile();
3449 2 $sig->(@_);
3450 2 my $rv;
3451 2 ▶ $global->{dirty_lines} = [ (1) x ($global->{height} || 0) ];
Condition50%1/2
$Termbox::global->{'height'} || 0
l!l
02
MC/DC0%0/1
$Termbox::global->{'height'} || 0
$Termbox::global->{'height'}
Truth table50%1/2
A
A= $Termbox::global->{'height'}
Aresult
11
00
3452 2 $rv = cellbuf_resize($global->{back}, $global->{width}, $global->{height});
3453 2 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 1 1 2
3454 1 $rv = cellbuf_resize($global->{front}, $global->{width}, $global->{height});
3455 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
3456 1 $rv = cellbuf_clear($global->{front});
3457 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
3458 1 $rv = send_clear();
3459 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
3460 1 return TB_OK;
3461 }
3462
3463 1 ▶sub handle_resize { # void ($sig)
Subroutine handle_resize: called CC=1 SCAR=0.0
Pod undocumented
3464 1 state $sig = compile(
3465 _Int,
3466 );
3467 1 my ($signo) = $sig->(@_);
3468 1 if (_WIN32) {
3469 return;
3470 } #endif
3471 1 local $! = $!;
3472 1 my $payload = pack('i', $signo);
3473 1 POSIX::write($global->{resize_pipefd}[1], $payload, length($payload));
3474 1 return;
3475 }
3476
3477 1 ▶sub update_term_size { # $int ()
Subroutine update_term_size: called CC=6 SCAR=32.1
Pod undocumented
3478 1 state $sig = compile();
3479 1 $sig->(@_);
3480
3481 1 if (_WIN32) {
3482 my $hOutput = $global->{ttyfd} // INVALID_HANDLE_VALUE;
3483 return TB_OK if $hOutput == INVALID_HANDLE_VALUE;
3484 my ($col, $row) = Win32::Console::_GetConsoleScreenBufferInfo($hOutput);
3485 unless ($col && $row) {
3486 $global->{last_errno} = $! = ENOTTY;
3487 return TB_ERR_WIN_RESIZE;
3488 }
3489 $global->{width} = $col;
3490 $global->{height} = $row;
3491 return TB_OK;
3492 } #endif
3493
3494 1 my ($rv, $ioctl_errno);
3495
3496 1 ▶ return TB_OK if $global->{ttyfd} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $Termbox::global->{"ttyfd"} < 0 1 0 1
3497
3498 0 my $fh = IO::File->new_from_fd($global->{ttyfd}, 'w');
3499 0 ▶ return TB_OK if !$fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
3500
3501 0 my $sz = pack('S4', 0, 0, 0, 0);
3502
3503 # Try ioctl TIOCGWINSZ
3504 0 ▶ if (eval { require 'sys/ioctl.ph'; 1 } && ioctl($fh, &TIOCGWINSZ, $sz)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require "sys/ioctl.ph"; 1 } } and ioctl $fh, &TIOCGWINSZ, $sz) 0 0 0
Condition0%0/3
eval { do { require "sys/ioctl.ph"; 1 } } and ioctl $fh, &TIOCGWINSZ, $sz
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { require "sys/ioctl.ph"; 1 } } and ioctl $fh, &TIOCGWINSZ, $sz
eval { do { require "sys/ioctl.ph"; 1 } } ioctl $fh, &TIOCGWINSZ, $sz
Truth table0%0/3
A and B
A= eval { do { require "sys/ioctl.ph"; 1 } }B= ioctl $fh, &TIOCGWINSZ, $sz
ABresult
0X0
100
111
3505 0 my ($row, $col) = unpack('S4', $sz);
3506 0 $global->{width} = $col;
3507 0 $global->{height} = $row;
3508 0 return TB_OK;
3509 }
3510 0 $ioctl_errno = 0+ $!;
3511
3512 # Try >cursor(9999,9999), >u7, <u6
3513 0 $rv = update_term_size_via_esc();
3514 0 ▶ return TB_OK if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 0 0 0
3515
3516 0 $global->{last_errno} = $ioctl_errno;
3517 0 return TB_ERR_RESIZE_IOCTL;
3518 }
3519
3520 1 ▶sub update_term_size_via_esc { # $int ()
Subroutine update_term_size_via_esc: called CC=7 SCAR=22.3
Pod undocumented
3521 1 state $sig = compile();
3522 1 $sig->(@_);
3523 1 if (_WIN32) {
3524 return TB_ERR_WIN_UNSUPPORTED;
3525 } #endif
3526 1 my $move_and_report = "\e[9999;9999H\e[6n";
3527
3528 my $write_rv = POSIX::write(
3529 $global->{wfd},
3530 1 ▶ $move_and_report,
Condition50%1/2
POSIX::write($Termbox::global->{"wfd"}, $move_and_report, length $move_and_report) // 0
l!l
10
MC/DC0%0/1
POSIX::write($Termbox::global->{"wfd"}, $move_and_report, length $move_and_report) // 0
POSIX::write($Termbox::global->{"wfd"}, $move_and_report, length $move_and_report)
Truth table50%1/2
A
A= POSIX::write($Termbox::global->{"wfd"}, $move_and_report, length $move_and_report)
Aresult
11
00
3531 length($move_and_report),
3532 ) // 0;
3533 1 ▶ if ($write_rv != length($move_and_report)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_rv != length $move_and_report) 0 1 1
3534 0 $global->{last_errno} = 0+ $!;
3535 0 return TB_ERR_RESIZE_WRITE;
3536 }
3537
3538 1 my $rin = '';
3539 1 vec($rin, $global->{rfd}, 1) = 1;
3540
3541 1 my $timeout = TB_RESIZE_FALLBACK_MS / 1000;
3542 1 my $select_rv = select($rin, undef, undef, $timeout);
3543 1 ▶ if ($select_rv != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_rv != 1) 0 1 1
3544 0 $global->{last_errno} = 0+ $!;
3545 0 return TB_ERR_RESIZE_POLL;
3546 }
3547
3548 1 my $buf = '';
3549 1 ▶ my $read_rv = POSIX::read($global->{rfd}, $buf, TB_OPT_READ_BUF) // -1;
Condition50%1/2
POSIX::read($Termbox::global->{"rfd"}, $buf, 64) // -1
l!l
10
MC/DC0%0/1
POSIX::read($Termbox::global->{"rfd"}, $buf, 64) // -1
POSIX::read($Termbox::global->{"rfd"}, $buf, 64)
Truth table50%1/2
A
A= POSIX::read($Termbox::global->{"rfd"}, $buf, 64)
Aresult
11
00
3550 1 ▶ if ($read_rv < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_rv < 1) 0 1 1
3551 0 $global->{last_errno} = 0+ $!;
3552 0 return TB_ERR_RESIZE_READ;
3553 }
3554
3555 1 ▶ if ($buf !~ /\e\[(\d+);(\d+)R/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ /\e\[(\d+);(\d+)R/) 0 1 1
3556 0 $global->{last_errno} = $! = EIO;
3557 0 return TB_ERR_RESIZE_SSCANF;
3558 }
3559 1 my ($rh, $rw) = ($1, $2);
3560
3561 1 $global->{width} = $rw;
3562 1 $global->{height} = $rh;
3563 1 return TB_OK;
3564 }
3565
3566 #
3567 # Escape-cap parser helpers
3568 #
3569
3570 0 ▶sub init_cap_trie { # $int ()
Subroutine init_cap_trie: not called CC=12 SCAR=48.5
Pod undocumented
3571 0 state $sig = compile();
3572 0 $sig->(@_);
3573
3574 0 ▶ my $trie = $global->{cap_trie} //= captrie->new();
Condition0%0/3
$Termbox::global->{"cap_trie"} //= "captrie"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Termbox::global->{"cap_trie"} //= "captrie"->new
$Termbox::global->{"cap_trie"} "captrie"->new
Truth table0%0/3
A //= B
A= $Termbox::global->{"cap_trie"}B= "captrie"->new
ABresult
1X1
011
000
3575 0 my $rv = $trie->clear();
3576 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
3577
3578 0 ▶ my $builtin_mod_caps = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $builtin_mod_caps = eval { do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} require Termbox::PP::Terminfo::Builtin; \%Termbox::PP::Terminfo::Builtin::builtin_mod_caps } } or return -1 0 0 0
3579 39 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
3580 0 require Termbox::PP::Terminfo::Builtin;
3581 0 \%Termbox::PP::Terminfo::Builtin::builtin_mod_caps;
3582 } or return TB_ERR;
3583
3584 # Add caps from terminfo or built-in
3585 # Collisions are expected as some terminfo entries have dupes. (For
3586 # example, att605-pc collides on TB_CAP_F4 and TB_CAP_DELETE.) First cap
3587 # in TB_CAP_* index order will win.
3588 #
3589 # TODO: Reorder TB_CAP_* so more critical caps come first.
3590 0 for my $i (0 .. TB_CAP__COUNT_KEYS - 1) {
3591 0 $rv = $trie->add($global->{caps}[$i], tb_key_i($i), 0);
3592 0 ▶ return $rv if $rv != TB_OK && $rv != TB_ERR_CAP_COLLISION;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 and $rv != -22 0 0 0
Condition0%0/3
$rv != 0 and $rv != -22
!ll&&!rl&&r
000
MC/DC0%0/2
$rv != 0 and $rv != -22
$rv != 0 $rv != -22
Truth table0%0/3
A and B
A= $rv != 0B= $rv != -22
ABresult
0X0
100
111
3593 }
3594
3595 # Add built-in mod caps
3596 #
3597 # Collisions are OK here as well. This can happen if $global->{caps} collides
3598 # with builtin_mod_caps. It is desirable to give precedence to $global->{caps}
3599 # here.
3600 0 ▶ while (my ($cap, $entry) = each %$builtin_mod_caps) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$builtin_mod_caps) 0 0 0
3601 0 ▶ my $key = $entry->{key} // next;
Condition0%0/2
$entry->{"key"} // (next)
l!l
00
MC/DC0%0/1
$entry->{"key"} // (next)
$entry->{"key"}
Truth table0%0/2
A
A= $entry->{"key"}
Aresult
11
00
3602 0 ▶ my $mod = $entry->{mod} // next;
Condition0%0/2
$entry->{"mod"} // (next)
l!l
00
MC/DC0%0/1
$entry->{"mod"} // (next)
$entry->{"mod"}
Truth table0%0/2
A
A= $entry->{"mod"}
Aresult
11
00
3603 0 $rv = $trie->add($cap, $key, $mod);
3604 0 ▶ return $rv if $rv != TB_OK && $rv != TB_ERR_CAP_COLLISION;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 and $rv != -22 0 0 0
Condition0%0/3
$rv != 0 and $rv != -22
!ll&&!rl&&r
000
MC/DC0%0/2
$rv != 0 and $rv != -22
$rv != 0 $rv != -22
Truth table0%0/3
A and B
A= $rv != 0B= $rv != -22
ABresult
0X0
100
111
3605 }
3606
3607 0 return TB_OK;
3608 }
3609
3610 0 ▶sub cap_trie_add { # $int ($cap|undef, $key, $mod)
Subroutine cap_trie_add: not called CC=2 SCAR=17.9
Pod undocumented
3611 0 state $sig = compile(
3612 _Maybe[_Str],
3613 _PositiveOrZeroInt,
3614 _PositiveOrZeroInt,
3615 );
3616 0 my ($cap, $key, $mod) = $sig->(@_);
3617 0 ▶ my $trie = $global->{cap_trie} or return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
my $trie = $Termbox::global->{"cap_trie"} or return -1 0 0 0
3618 0 return $trie->add($cap, $key, $mod);
3619 }
3620
3621 # Returns a hash-ref node in $$last with {is_leaf,key,mod,nchildren,cap}.
3622 0 ▶sub cap_trie_find { # $int ($buf|undef, $nbuf, \$last, \$depth)
Subroutine cap_trie_find: not called CC=2 SCAR=17.9
Pod undocumented
3623 0 state $sig = compile(
3624 _Maybe[_Str],
3625 _PositiveOrZeroInt,
3626 _Ref,
3627 _ScalarRef,
3628 );
3629 0 my ($buf, $nbuf, $last, $depth) = $sig->(@_);
3630 0 ▶ my $trie = $global->{cap_trie} or return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
my $trie = $Termbox::global->{"cap_trie"} or return -1 0 0 0
3631 0 return $trie->find($buf, $last, $depth); # Perl does not need nbuf
3632 }
3633
3634 0 ▶sub cap_trie_deinit { # $int ($node)
Subroutine cap_trie_deinit: not called CC=1 SCAR=6.9
Pod undocumented
3635 0 state $sig = compile(
3636 _Object,
3637 );
3638 0 my ($node) = $sig->(@_);
3639 0 return $node->clear();
3640 }
3641
3642 #
3643 # Event extraction helpers
3644 #
3645
3646 2 ▶sub wait_event { # $int ($event, $timeout)
Subroutine wait_event: called CC=14 SCAR=33.2
Pod undocumented
3647 2 state $sig = compile(
3648 _Object,
3649 _Int,
3650 );
3651 2 my ($event, $timeout) = $sig->(@_);
3652
3653 2 my $rv;
3654 2 my $buf = '';
3655 2 state $empty_event = Termbox::Event->new();
3656
3657 2 %$event = %$empty_event;
3658
3659 # Fast path: buffered input already yields a full event
3660 2 $rv = extract_event($event);
3661 2 ▶ return TB_OK if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 1 1 2
3662
3663 1 if (_WIN32) {
3664 # Get console input handle for WaitForSingleObject() and ReadConsoleInputW()
3665 my $hInput = $global->{rfd} // INVALID_HANDLE_VALUE;
3666 if ($hInput == INVALID_HANDLE_VALUE) {
3667 $^E = ERROR_INVALID_HANDLE;
3668 $global->{last_errno} = $! = EBADF;
3669 return TB_ERR_POLL;
3670 }
3671
3672 # Poll timeout tracking
3673 my $start = Time::HiRes::time();
3674
3675 # Undefined on the first iteration, defined after the first wait attempt
3676 my $wait_time;
3677
3678 # Preserve a pending UTF-16 high surrogate across poll calls
3679 state $pending_high_surrogate;
3680
3681 while (1) {
3682 # Compute remaining wait time for WaitForSingleObject()
3683 if ($timeout < 0) {
3684 $wait_time = INFINITE;
3685 } elsif ($timeout == 0) {
3686 # Allow only a single non-blocking poll attempt
3687 return TB_ERR_NO_EVENT if defined $wait_time;
3688 $wait_time = 0;
3689 } else {
3690 my $elapsed = int((Time::HiRes::time() - $start) * 1000);
3691 # Allow at least one wait attempt for positive timeouts
3692 return TB_ERR_NO_EVENT if defined $wait_time && $elapsed >= $timeout;
3693 $wait_time = $timeout - $elapsed;
3694 }
3695
3696 # Use WaitForSingleObject() to wait for console input events
3697 $rv = WaitForSingleObject($hInput, $wait_time);
3698 if ($rv == WAIT_TIMEOUT) {
3699 $global->{last_errno} = $! = 0;
3700 return TB_ERR_NO_EVENT;
3701 }
3702 elsif ($rv != WAIT_OBJECT_0) {
3703 my $err = 0+ $^E;
3704 if ($err == ERROR_INVALID_HANDLE) { $! = EBADF }
3705 elsif ($err == ERROR_ACCESS_DENIED) { $! = EACCES }
3706 else { $! = EINVAL }
3707 $global->{last_errno} = 0+ $!;
3708 return TB_ERR_POLL;
3709 }
3710
3711 # Indicates whether at least one relevant input record was processed
3712 my $dispatched = 0;
3713
3714 # Read wide input records from the console because higher-level console
3715 # I/O is still not reliable for full Unicode input handling.
3716 $^E = 0;
3717 my $nevent = Win32::Console::_GetNumberOfConsoleInputEvents($hInput);
3718 if ($^E) {
3719 $global->{last_errno} = $! = EIO;
3720 return TB_ERR_POLL;
3721 }
3722 while ($nevent--) {
3723 my @ir = ReadConsoleInputW($hInput);
3724
3725 # Skip null events
3726 next unless @ir;
3727
3728 if ($ir[wEventType] == KEY_EVENT) {
3729 # Discard key-up events unless Windows reports pasted text through
3730 # VK_MENU.
3731 next
3732 unless $ir[bKeyDown]
3733 || ($ir[wVirtualKeyCode] == VK_MENU && $ir[UnicodeChar]);
3734
3735 # Discard pure modifier/lock key events only if they do not carry text
3736 next if !$ir[UnicodeChar] && (
3737 $ir[wVirtualKeyCode] == VK_SHIFT
3738 || $ir[wVirtualKeyCode] == VK_CONTROL
3739 || $ir[wVirtualKeyCode] == VK_MENU
3740 || $ir[wVirtualKeyCode] == VK_CAPITAL
3741 || $ir[wVirtualKeyCode] == VK_NUMLOCK
3742 || $ir[wVirtualKeyCode] == VK_SCROLL
3743 );
3744
3745 while ($ir[wRepeatCount]--) {
3746 my $wc = $ir[UnicodeChar];
3747
3748 # Skip null characters
3749 next if !$wc;
3750
3751 # High surrogate
3752 if ($wc >= 0xD800 && $wc <= 0xDBFF) {
3753 $pending_high_surrogate = $wc;
3754 next;
3755 }
3756
3757 # Low surrogate
3758 if ($wc >= 0xDC00 && $wc <= 0xDFFF) {
3759 if (defined $pending_high_surrogate) {
3760 my $high = $pending_high_surrogate;
3761 $pending_high_surrogate = undef;
3762 # combine surrogate pair to get the codepoint
3763 my $codepoint =
3764 0x10000 + (($high - 0xD800) << 10) + ($wc - 0xDC00);
3765 my $ch = chr($codepoint);
3766 utf8::encode($ch);
3767 bytebuf_nputs(\$global->{inbuf}, $ch, length($ch));
3768 $dispatched = 1;
3769 next;
3770 }
3771
3772 # Ignore dangling low surrogates
3773 next;
3774 }
3775
3776 # Drop a previously pending high surrogate if a non-low-surrogate
3777 # arrives
3778 $pending_high_surrogate = undef;
3779
3780 # Normal BMP character
3781 my $ch = chr($wc);
3782 utf8::encode($ch);
3783 bytebuf_nputs(\$global->{inbuf}, $ch, length($ch));
3784 $dispatched = 1;
3785 }
3786 }
3787 elsif ($ir[wEventType] == WINDOW_BUFFER_SIZE_EVENT) {
3788 my $w = $global->{width};
3789 my $h = $global->{height};
3790
3791 $rv = update_term_size();
3792 return $rv if $rv != TB_OK;
3793
3794 # Only dispatch a resize event if the size actually changed
3795 next if $w == $global->{width} && $h == $global->{height};
3796
3797 $rv = resize_cellbufs();
3798 return $rv if $rv != TB_OK;
3799
3800 $event->{type} = TB_EVENT_RESIZE;
3801 $event->{w} = $global->{width};
3802 $event->{h} = $global->{height};
3803
3804 # Reset any pending high surrogate since the resize event is dispatched
3805 $pending_high_surrogate = undef;
3806 $dispatched = 1;
3807 return TB_OK;
3808 }
3809 else {
3810 # MOUSE_EVENT, FOCUS_EVENT, MENU_EVENT are ignored explicitly
3811 next;
3812 }
3813 }
3814
3815 # No relevant input was dispatched, continue polling
3816 next unless $dispatched;
3817
3818 %$event = %$empty_event;
3819 $rv = extract_event($event);
3820 return TB_OK if $rv == TB_OK;
3821
3822 } #/ while (1);
3823
3824 return $rv;
3825 } #endif
3826
3827 1 my $rfd = $global->{rfd};
3828 1 my $resizefd = $global->{resize_pipefd}[0];
3829
3830 # Perl select timeout is seconds as float; undef means block forever
3831 1 ▶ my $timeout_sec = ($timeout < 0) ? undef : ($timeout / 1000);
Branch: 1 branch
BranchTrueFalseTotal
$timeout < 0 ? : 0 1 1
3832
3833 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$timeout < 0 and do { do { my $rin = ""; vec($rin, $rfd, 1) = 1; vec($rin, $resizefd, 1) = 1; my $rout = $rin; my $select_rv = select($rout, undef, undef, $timeout_sec); if ($select_rv < 0) { $Termbox::global->{"last_errno"} = 0 + $!; return -14; } ; if ($select_rv == 0) { return -6; } ; my $tty_has_events = vec($rout, $rfd, 1); my $resize_has_events = vec($rout, $resizefd, 1); if ($tty_has_events) { my $buf = ""; my $read_rv = POSIX::read($rfd, $buf, 64) // -1; if ($read_rv < 0) { $Termbox::global->{"last_errno"} = 0 + $!; return -10; } elsif ($read_rv > 0) { bytebuf_nputs(\$Termbox::global->{"inbuf"}, $buf, $read_rv); } ; } ; if ($resize_has_events) { my $ignore = "\0\0\0\0"; POSIX::read($resizefd, $ignore, length $ignore); $rv = update_term_size(); return $rv if $rv != 0; $rv = resize_cellbufs(); return $rv if $rv != 0; $event->{"type"} = 2; $event->{"w"} = $Termbox::global->{"width"}; $event->{"h"} = $Termbox::global->{"height"}; return 0; } ; %$event = %$empty_event; $rv = extract_event($event); return 0 if $rv == 0 } } 0 0 0
3834 1 my $rin = '';
3835 1 vec($rin, $rfd, 1) = 1;
3836 1 vec($rin, $resizefd, 1) = 1;
3837
3838 1 my $rout = $rin;
3839
3840 1 my $select_rv = select($rout, undef, undef, $timeout_sec);
3841
3842 1 ▶ if ($select_rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_rv < 0) 0 1 1
3843 # Let EINTR/EAGAIN bubble up
3844 0 $global->{last_errno} = 0+ $!;
3845 0 return TB_ERR_POLL;
3846 }
3847 1 ▶ if ($select_rv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_rv == 0) 0 1 1
3848 0 return TB_ERR_NO_EVENT;
3849 }
3850
3851 1 my $tty_has_events = vec($rout, $rfd, 1);
3852 1 my $resize_has_events = vec($rout, $resizefd, 1);
3853
3854 1 ▶ if ($tty_has_events) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tty_has_events) 0 1 1
3855 0 my $buf = '';
3856 0 ▶ my $read_rv = POSIX::read($rfd, $buf, TB_OPT_READ_BUF) // -1;
Condition0%0/2
POSIX::read($rfd, $buf, 64) // -1
l!l
00
MC/DC0%0/1
POSIX::read($rfd, $buf, 64) // -1
POSIX::read($rfd, $buf, 64)
Truth table0%0/2
A
A= POSIX::read($rfd, $buf, 64)
Aresult
11
00
3857 0 ▶ if ($read_rv < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($read_rv < 0) { } 0 0 0
elsif ($read_rv > 0) { } 0 0 0
3858 0 $global->{last_errno} = 0+ $!;
3859 0 return TB_ERR_READ;
3860 } elsif ($read_rv > 0) {
3861 0 bytebuf_nputs(\$global->{inbuf}, $buf, $read_rv);
3862 }
3863 }
3864
3865 1 ▶ if ($resize_has_events) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resize_has_events) 1 0 1
3866 1 my $ignore = pack('i', 0);
3867 1 POSIX::read($resizefd, $ignore, length($ignore));
3868 # TODO: Harden against errors encountered mid-resize
3869 1 $rv = update_term_size();
3870 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
3871 1 $rv = resize_cellbufs();
3872 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
3873
3874 1 $event->{type} = TB_EVENT_RESIZE;
3875 1 $event->{w} = $global->{width};
3876 1 $event->{h} = $global->{height};
3877 1 return TB_OK;
3878 }
3879
3880 # Try to extract an event after consuming input / handling resize
3881 0 %$event = %$empty_event;
3882 0 $rv = extract_event($event);
3883 0 ▶ return TB_OK if $rv == TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 0 0 0
3884
3885 } while ($timeout < 0);
3886
3887 0 return $rv;
3888 }
3889
3890 3 ▶sub extract_event { # $int ($event)
Subroutine extract_event: called CC=12 SCAR=37.5
Pod undocumented
3891 3 state $sig = compile(
3892 _Object,
3893 );
3894 3 my ($event) = $sig->(@_);
3895 3 my $trie = $global->{cap_trie};
3896 3 alias: for my $in ($global->{inbuf}) {
3897
3898 3 ▶ return TB_ERR unless length($in);
Branch: 1 branch
BranchTrueFalseTotal
unless length $in 1 2 3
3899
3900 39 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
3901 2 my $b0 = substr($in, 0, 1);
3902 2 ▶ if ($b0 eq "\e") {
Branch: 1 branch
BranchTrueFalseTotal
if ($b0 eq "\e") 0 2 2
3903 # Escape sequence?
3904 # In TB_INPUT_ESC, skip if the buffer is a single escape char
3905 0 ▶ unless (($global->{input_mode} & TB_INPUT_ESC) && length($in) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Termbox::global->{"input_mode"} & 1 and length $in == 1) 0 0 0
Condition0%0/3
$Termbox::global->{"input_mode"} & 1 and length $in == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$Termbox::global->{"input_mode"} & 1 and length $in == 1
$Termbox::global->{"input_mode"} & 1 length $in == 1
Truth table0%0/3
A and B
A= $Termbox::global->{"input_mode"} & 1B= length $in == 1
ABresult
0X0
100
111
3906 0 my $rv = extract_esc($event);
3907 0 ▶ return $rv if $rv == TB_OK || $rv == TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 or $rv == -2 0 0 0
Condition0%0/3
$rv == 0 or $rv == -2
l!l&&r!l&&!r
000
MC/DC0%0/2
$rv == 0 or $rv == -2
$rv == 0 $rv == -2
Truth table0%0/3
A or B
A= $rv == 0B= $rv == -2
ABresult
1X1
011
000
3908 }
3909
3910 # Escape key?
3911 0 ▶ if ($global->{input_mode} & TB_INPUT_ESC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"input_mode"} & 1) 0 0 0
3912 0 $event->{type} = TB_EVENT_KEY;
3913 0 $event->{ch} = 0;
3914 0 $event->{key} = TB_KEY_ESC;
3915 0 $event->{mod} = 0;
3916 0 substr($in, 0, 1, '');
3917 0 return TB_OK;
3918 }
3919
3920 # Recurse for alt key
3921 0 $event->{mod} |= TB_MOD_ALT;
3922 0 substr($in, 0, 1, '');
3923 0 return extract_event($event);
3924 }
3925
3926 # ASCII control key?
3927 2 ▶ my $is_ctrl = ord($b0) < TB_KEY_SPACE || ord($b0) == TB_KEY_BACKSPACE2;
Condition33%1/3
ord $b0 < 32 || ord $b0 == 127
l!l&&r!l&&!r
002
MC/DC0%0/2
ord $b0 < 32 || ord $b0 == 127
ord $b0 < 32 ord $b0 == 127
Truth table33%1/3
A || B
A= ord $b0 < 32B= ord $b0 == 127
ABresult
1X1
011
000
3928 2 ▶ if ($is_ctrl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ctrl) 0 2 2
3929 0 $event->{type} = TB_EVENT_KEY;
3930 0 $event->{ch} = 0;
3931 0 $event->{key} = ord($in);
3932 0 $event->{mod} |= TB_MOD_CTRL;
3933 0 substr($in, 0, 1, '');
3934 0 return TB_OK;
3935 }
3936
3937 # UTF-8?
3938 2 my $need = tb_utf8_char_length($b0);
3939 2 ▶ if (length($in) >= $need) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $in >= $need) 2 0 2
3940 2 $event->{type} = TB_EVENT_KEY;
3941 2 tb_utf8_char_to_unicode(\$event->{ch}, $in);
3942 2 $event->{key} = 0;
3943 2 substr($in, 0, $need, '');
3944 2 return TB_OK;
3945 }
3946
3947 # Need more input
3948 0 return TB_ERR
3949 } #/ alias:
3950 }
3951
3952 2 ▶sub extract_esc { # $int ($event)
Subroutine extract_esc: called CC=9 SCAR=32.6
Pod undocumented
3953 2 state $sig = compile(
3954 _Object,
3955 );
3956 2 my ($event) = $sig->(@_);
3957
3958 2 my $rv;
3959
3960 2 $rv = extract_esc_user($event, 0);
3961 2 ▶ return $rv if $rv == TB_OK || $rv == TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 or $rv == -2 1 1 2
Condition66%2/3
$rv == 0 or $rv == -2
l!l&&r!l&&!r
101
MC/DC50%1/2
$rv == 0 or $rv == -2
$rv == 0 $rv == -2
Truth table66%2/3
A or B
A= $rv == 0B= $rv == -2
ABresult
1X1
011
000
3962
3963 1 $rv = extract_esc_cap($event);
3964 1 ▶ return $rv if $rv == TB_OK || $rv == TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 or $rv == -2 1 0 1
Condition33%1/3
$rv == 0 or $rv == -2
l!l&&r!l&&!r
100
MC/DC0%0/2
$rv == 0 or $rv == -2
$rv == 0 $rv == -2
Truth table33%1/3
A or B
A= $rv == 0B= $rv == -2
ABresult
1X1
011
000
3965
3966 0 $rv = extract_esc_mouse($event);
3967 0 ▶ return $rv if $rv == TB_OK || $rv == TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 or $rv == -2 0 0 0
Condition0%0/3
$rv == 0 or $rv == -2
l!l&&r!l&&!r
000
MC/DC0%0/2
$rv == 0 or $rv == -2
$rv == 0 $rv == -2
Truth table0%0/3
A or B
A= $rv == 0B= $rv == -2
ABresult
1X1
011
000
3968
3969 0 $rv = extract_esc_user($event, 1);
3970 0 ▶ return $rv if $rv == TB_OK || $rv == TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 or $rv == -2 0 0 0
Condition0%0/3
$rv == 0 or $rv == -2
l!l&&r!l&&!r
000
MC/DC0%0/2
$rv == 0 or $rv == -2
$rv == 0 $rv == -2
Truth table0%0/3
A or B
A= $rv == 0B= $rv == -2
ABresult
1X1
011
000
3971
3972 0 return TB_ERR;
3973 }
3974
3975 3 ▶sub extract_esc_user { # $int ($event, $is_post)
Subroutine extract_esc_user: called CC=6 SCAR=18.7
Pod undocumented
3976 3 state $sig = compile(
3977 _Object,
3978 _Bool,
3979 );
3980 3 my ($event, $is_post) = $sig->(@_);
3981
3982 my $fn = $is_post ? $global->{fn_extract_esc_post}
3983 3 ▶ : $global->{fn_extract_esc_pre};
Branch: 1 branch
BranchTrueFalseTotal
$is_post ? : 1 2 3
3984 3 ▶ return TB_ERR unless ref($fn) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fn eq "CODE" 2 1 3
3985
3986 1 my $consumed = 0;
3987 1 my $rv = $fn->($event, \$consumed);
3988
3989 1 ▶ if ($rv == TB_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == 0) 1 0 1
3990 1 bytebuf_shift(\$global->{inbuf}, $consumed);
3991 }
3992
3993 1 ▶ return $rv if $rv == TB_OK || $rv == TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if $rv == 0 or $rv == -2 1 0 1
Condition33%1/3
$rv == 0 or $rv == -2
l!l&&r!l&&!r
100
MC/DC0%0/2
$rv == 0 or $rv == -2
$rv == 0 $rv == -2
Truth table33%1/3
A or B
A= $rv == 0B= $rv == -2
ABresult
1X1
011
000
3994 0 return TB_ERR;
3995 }
3996
3997 3 ▶sub extract_esc_mouse { # $int ($event)
Subroutine extract_esc_mouse: called CC=46 SCAR=48.3
Pod undocumented
3998 3 state $sig = compile(
3999 _Object,
4000 );
4001 3 my ($event) = $sig->(@_);
4002
4003 39 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
4004 3 alias: for my $in ($global->{inbuf}) {
4005 3 my @in = unpack('C*', $in);
4006
4007 # Bail if not enough to determine type
4008 3 ▶ if (@in < 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (@in < 2) { } 0 3 3
elsif ($in[1] != 91) { } 0 3 3
elsif (@in < 3) { } 0 3 3
4009 0 return TB_ERR_NEED_MORE;
4010 } elsif ($in[1] != ord('[')) {
4011 0 return TB_ERR;
4012 } elsif (@in < 3) {
4013 0 return TB_ERR_NEED_MORE;
4014 }
4015
4016 # Discern type of mouse event from 3rd byte
4017 3 my $type = TYPE_VT200;
4018 3 ▶ if ($in[2] == ord('M')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($in[2] == 77) { } 1 2 3
elsif ($in[2] == 60) { } 1 1 2
4019 # X10 mouse encoding, the simplest one: \x1b [ M Cb Cx Cy
4020 1 $type = TYPE_VT200;
4021 }
4022 elsif ($in[2] == ord('<')) {
4023 # xterm 1006 extended mode or urxvt 1015 extended mode
4024 # xterm: \x1b [ < Cb ; Cx ; Cy (M or m)
4025 1 $type = TYPE_1006;
4026 }
4027 else {
4028 # urxvt: \x1b [ Cb ; Cx ; Cy M
4029 1 $type = TYPE_1015;
4030 }
4031
4032 3 my $buf_shift = 0;
4033
4034 3 switch_parse: for ($type) {
4035 3 ▶ case: TYPE_VT200 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
0 == $_ and do { return -2 if @in < 6; my $b = $in[3] - 32; local $_; switch: foreach $_ ($b & 3) { case: 0 == $_ and do { $event->{"key"} = $b & 64 ? 65508 : 65512; last }; case: 1 == $_ and do { $event->{"key"} = $b & 64 ? 65507 : 65510; last }; case: 2 == $_ and do { $event->{"key"} = 65511; last }; case: 3 == $_ and do { $event->{"key"} = 65509; last }; default: { return -1; } ; } ; if (($b & 32) != 0) { $event->{"mod"} |= 8; } ; $event->{"x"} = $in[4] - 33; $event->{"y"} = $in[5] - 33; $buf_shift = 6; last } 1 2 3
4036 # In this mode, we need 6 bytes
4037 1 ▶ return TB_ERR_NEED_MORE if @in < 6;
Branch: 1 branch
BranchTrueFalseTotal
if @in < 6 0 1 1
4038
4039 1 my $b = $in[3] - 0x20;
4040
4041 1 local $_;
4042 1 switch: for ($b & 3) {
4043 1 ▶ case: 0 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
0 == $_ and do { $event->{"key"} = $b & 64 ? 65508 : 65512; last } 1 0 1
4044 1 ▶ $event->{key} = ($b & 64) ? TB_KEY_MOUSE_WHEEL_UP
Branch: 1 branch
BranchTrueFalseTotal
$b & 64 ? : 0 1 1
4045 : TB_KEY_MOUSE_LEFT;
4046 1 last;
4047 };
4048 0 ▶ case: 1 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { $event->{"key"} = $b & 64 ? 65507 : 65510; last } 0 0 0
4049 0 ▶ $event->{key} = ($b & 64) ? TB_KEY_MOUSE_WHEEL_DOWN
Branch: 1 branch
BranchTrueFalseTotal
$b & 64 ? : 0 0 0
4050 : TB_KEY_MOUSE_MIDDLE;
4051 0 last;
4052 };
4053 0 ▶ case: 2 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
2 == $_ and do { $event->{"key"} = 65511; last } 0 0 0
4054 0 $event->{key} = TB_KEY_MOUSE_RIGHT;
4055 0 last;
4056 };
4057 0 ▶ case: 3 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
3 == $_ and do { $event->{"key"} = 65509; last } 0 0 0
4058 0 $event->{key} = TB_KEY_MOUSE_RELEASE;
4059 0 last;
4060 };
4061 default: {
4062 0 return TB_ERR;
4063 };
4064 }
4065
4066 1 ▶ if (($b & 32) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($b & 32) != 0) 0 1 1
4067 0 $event->{mod} |= TB_MOD_MOTION;
4068 }
4069
4070 # The coord is 1,1 for upper left
4071 1 $event->{x} = $in[4] - 0x21;
4072 1 $event->{y} = $in[5] - 0x21;
4073
4074 # Eat 6 bytes
4075 1 $buf_shift = 6;
4076 1 last;
4077 };
4078
4079 case: TYPE_1006 == $_ || # fallthrough
4080 TYPE_1015 == $_
4081 2 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ || 2 == $_ and do { my(@num) = (-1, -1, -1); my $num_i = 0; my $cur_num = -1; my $trail = 32; my $i = 2; ++$i if $type == 1; while ($i < @in and $num_i < 3) { my $c = $in[$i]; if ($c >= 48 and $c <= 57) { $cur_num = 0 if $cur_num == -1; $cur_num *= 10; $cur_num += $c - 48; ++$i; next; } ; if ($cur_num != -1 and $num_i < 2 && $c == 59 || $num_i == 2 && ($c == 109 || $c == 77)) { $num[$num_i] = $cur_num; ++$num_i; $cur_num = -1; $trail = $c; ++$i; next; } ; return -1; } ; return -2 if $num[2] == -1; $buf_shift = $i; $num[0] -= 32 if $type == 2; local $_; switch: foreach $_ ($num[0] & 3) { case: 0 == $_ and do { $event->{"key"} = $num[0] & 64 ? 65508 : 65512; last }; case: 1 == $_ and do { $event->{"key"} = $num[0] & 64 ? 65507 : 65510; last }; case: 2 == $_ and do { $event->{"key"} = 65511; last }; case: 3 == $_ and do { $event->{"key"} = 65509; last }; default: { return -1; } ; } ; if ($trail == 109) { $event->{"key"} = 65509; } ; if (($num[0] & 32) != 0) { $event->{"mod"} |= 8; } ; $event->{"x"} = $num[1] - 1 < 0 ? 0 : $num[1] - 1; $event->{"y"} = $num[2] - 1 < 0 ? 0 : $num[2] - 1; last } 2 0 2
Condition66%2/3
1 == $_ or 2 == $_
l!l&&r!l&&!r
110
MC/DC0%0/2
1 == $_ or 2 == $_
1 == $_ 2 == $_
Truth table66%2/3
A or B
A= 1 == $_B= 2 == $_
ABresult
1X1
011
000
4082 2 my @num = (-1, -1, -1);
4083 2 my $num_i = 0;
4084 2 my $cur_num = -1;
4085 2 my $trail = ord(' ');
4086
4087 2 my $i = 2;
4088 2 ▶ ++$i if $type == TYPE_1006; # skip '<'
Branch: 1 branch
BranchTrueFalseTotal
if $type == 1 1 1 2
4089
4090 # Parse %d;%d;%d[mM] into @num
4091 2 ▶ while ($i < @in && $num_i < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @in and $num_i < 3) 19 2 21
Condition66%2/3
$i < @in and $num_i < 3
!ll&&!rl&&r
0219
MC/DC50%1/2
$i < @in and $num_i < 3
$i < @in $num_i < 3
Truth table66%2/3
A and B
A= $i < @inB= $num_i < 3
ABresult
0X0
100
111
4092 19 my $c = $in[$i];
4093
4094 19 ▶ if ($c >= ord('0') && $c <= ord('9')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= 48 and $c <= 57) 13 6 19
Condition66%2/3
$c >= 48 and $c <= 57
!ll&&!rl&&r
0613
MC/DC50%1/2
$c >= 48 and $c <= 57
$c >= 48 $c <= 57
Truth table66%2/3
A and B
A= $c >= 48B= $c <= 57
ABresult
0X0
100
111
4095 # Digit
4096 13 ▶ $cur_num = 0 if $cur_num == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_num == -1 6 7 13
4097 13 $cur_num *= 10;
4098 13 $cur_num += $c - ord('0');
4099 13 ++$i;
4100 13 next;
4101 }
4102
4103 6 ▶ if ($cur_num != -1
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_num != -1 and $num_i < 2 && $c == 59 || $num_i == 2 && ($c == 109 || $c == 77)) 6 0 6
Condition46%7/15
$cur_num != -1 and $num_i < 2 && $c == 59 || $num_i == 2 && ($c == 109 || $c == 77)
!ll&&!rl&&r
006
$num_i < 2 and $c == 59 or $num_i == 2 and $c == 109 || $c == 77
l!l&&r!l&&!r
420
$num_i < 2 and $c == 59
!ll&&!rl&&r
204
$num_i == 2 and $c == 109 || $c == 77
!ll&&!rl&&r
002
$c == 109 or $c == 77
l!l&&r!l&&!r
020
MC/DC0%0/6
$cur_num != -1 and $num_i < 2 && $c == 59 || $num_i == 2 && ($c == 109 || $c == 77)
$cur_num != -1 $num_i < 2 $c == 59 $num_i == 2 $c == 109 $c == 77
Truth table20%2/10
A and B and C or D and E or F
A= $cur_num != -1B= $num_i < 2C= $c == 59D= $num_i == 2E= $c == 109F= $c == 77
ABCDEFresult
0XXXXX0
10X0XX0
10X1000
1100XX0
1101000
111XXX1
10X11X1
10X1011
11011X1
1101011
4104 && (($num_i < 2 && $c == ord(';'))
4105 || ($num_i == 2 && ($c == ord('m') || $c == ord('M')))))
4106 {
4107 # We're at a semi-colon, 'm', or 'M' and we have a number
4108 6 $num[$num_i] = $cur_num;
4109 6 ++$num_i;
4110 6 $cur_num = -1;
4111 6 $trail = $c;
4112 6 ++$i;
4113 6 next;
4114 }
4115
4116 # Something else; not a mouse event
4117 0 return TB_ERR;
4118 }
4119
4120 # If we didn't get to the 3rd number, we need more
4121 2 ▶ return TB_ERR_NEED_MORE if $num[2] == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $num[2] == -1 0 2 2
4122
4123 # We have a valid mouse event, eat i bytes from the buffer
4124 2 $buf_shift = $i;
4125
4126 2 ▶ $num[0] -= 0x20 if $type == TYPE_1015;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 1 1 2
4127
4128 2 local $_;
4129 2 switch: for ($num[0] & 3) {
4130 2 ▶ case: 0 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
0 == $_ and do { $event->{"key"} = $num[0] & 64 ? 65508 : 65512; last } 0 2 2
4131 0 ▶ $event->{key} = ($num[0] & 64) ? TB_KEY_MOUSE_WHEEL_UP
Branch: 1 branch
BranchTrueFalseTotal
$num[0] & 64 ? : 0 0 0
4132 : TB_KEY_MOUSE_LEFT;
4133 0 last;
4134 };
4135 2 ▶ case: 1 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { $event->{"key"} = $num[0] & 64 ? 65507 : 65510; last } 1 1 2
4136 1 ▶ $event->{key} = ($num[0] & 64) ? TB_KEY_MOUSE_WHEEL_DOWN
Branch: 1 branch
BranchTrueFalseTotal
$num[0] & 64 ? : 1 0 1
4137 : TB_KEY_MOUSE_MIDDLE;
4138 1 last;
4139 };
4140 1 ▶ case: 2 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
2 == $_ and do { $event->{"key"} = 65511; last } 0 1 1
4141 0 $event->{key} = TB_KEY_MOUSE_RIGHT;
4142 0 last;
4143 };
4144 1 ▶ case: 3 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
3 == $_ and do { $event->{"key"} = 65509; last } 1 0 1
4145 1 $event->{key} = TB_KEY_MOUSE_RELEASE;
4146 1 last;
4147 };
4148 default: {
4149 0 return TB_ERR;
4150 };
4151 }
4152
4153 # On xterm mouse release is signaled by lowercase m
4154 2 ▶ if ($trail == ord('m')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trail == 109) 0 2 2
4155 0 $event->{key} = TB_KEY_MOUSE_RELEASE;
4156 }
4157
4158 2 ▶ if (($num[0] & 32) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($num[0] & 32) != 0) 1 1 2
4159 1 $event->{mod} |= TB_MOD_MOTION;
4160 }
4161
4162 2 ▶ $event->{x} = ($num[1] - 1 < 0) ? 0 : $num[1] - 1;
Branch: 1 branch
BranchTrueFalseTotal
$num[1] - 1 < 0 ? : 0 2 2
4163 2 ▶ $event->{y} = ($num[2] - 1 < 0) ? 0 : $num[2] - 1;
Branch: 1 branch
BranchTrueFalseTotal
$num[2] - 1 < 0 ? : 0 2 2
4164 2 last;
4165 };
4166 }
4167
4168 3 ▶ substr($in, 0, $buf_shift, '') if $buf_shift > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $buf_shift > 0 3 0 3
4169
4170 3 $event->{type} = TB_EVENT_MOUSE;
4171
4172 3 return TB_OK;
4173 } #/ alias:
4174 }
4175
4176 3 ▶sub extract_esc_cap { # $int ($event)
Subroutine extract_esc_cap: called CC=6 SCAR=18.4
Pod undocumented
4177 3 state $sig = compile(
4178 _Object,
4179 );
4180 3 my ($event) = $sig->(@_);
4181 3 my $trie = $global->{cap_trie};
4182 3 alias: for my $in ($global->{inbuf}) {
4183 3 my $node;
4184 my $depth;
4185
4186 3 my $rv = $trie->find($in, \$node, \$depth);
4187 3 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 3 3
4188
4189 3 ▶ if ($node->{is_leaf}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->{'is_leaf'}) { } 2 1 3
elsif ($node->{'nchildren'} > 0 and length $in <= $depth) { } 1 0 1
4190 # Found a leaf node
4191 2 $event->{type} = TB_EVENT_KEY;
4192 2 $event->{ch} = 0;
4193 2 $event->{key} = $node->{key};
4194 2 $event->{mod} = $node->{mod};
4195 2 substr($in, 0, $depth, '');
4196 2 return TB_OK;
4197 }
4198 ▶ elsif ($node->{nchildren} > 0 && length($in) <= $depth) {
Condition33%1/3
$node->{"nchildren"} > 0 and length $in <= $depth
!ll&&!rl&&r
001
MC/DC0%0/2
$node->{"nchildren"} > 0 and length $in <= $depth
$node->{"nchildren"} > 0 length $in <= $depth
Truth table33%1/3
A and B
A= $node->{"nchildren"} > 0B= length $in <= $depth
ABresult
0X0
100
111
4199 # Found a branch node (not enough input)
4200 1 return TB_ERR_NEED_MORE;
4201 }
4202
4203 0 return TB_ERR;
4204 } #/ alias:
4205 }
4206
4207 #
4208 # Cell related helpers
4209 #
4210
4211 2 ▶sub cell_cmp { # $int ($a, $b)
Subroutine cell_cmp: called CC=2 SCAR=7.1
Pod undocumented
4212 2 state $sig = compile(
4213 _Object,
4214 _Object,
4215 );
4216 2 my ($a, $b) = $sig->(@_);
4217 2 ▶ return $a->equal($b) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$a->equal($b) ? : 2 0 2
4218 }
4219
4220 1 ▶sub cell_copy { # $int ($dst, $src)
Subroutine cell_copy: called CC=1 SCAR=0.0
Pod undocumented
4221 1 goto &Termbox::Cell::copy;
4222 }
4223
4224 10 ▶sub cell_set { # $int ($cell, \@ch, $nch, $fg, $bg)
Subroutine cell_set: called CC=1 SCAR=0.0
Pod undocumented
4225 10 state $sig = compile(
4226 _Object,
4227 _ArrayRef,
4228 _PositiveOrZeroInt,
4229 _PositiveOrZeroInt,
4230 _PositiveOrZeroInt,
4231 );
4232 10 my ($cell, $ch, $nch, $fg, $bg) = $sig->(@_);
4233 8 return $cell->set(pack('U*', @$ch), $fg, $bg);
4234 }
4235
4236 1 ▶sub cell_reserve_ech { # $int ($cell, $n)
Subroutine cell_reserve_ech: called CC=1 SCAR=0.0
Pod undocumented
4237 1 state $sig = compile(
4238 _Object,
4239 _PositiveOrZeroInt,
4240 );
4241 1 my ($cell, $n) = $sig->(@_);
4242 1 return TB_OPT_EGC ? TB_OK : TB_ERR;
4243 }
4244
4245 7685 ▶sub cell_free { # $int ($cell)
Subroutine cell_free: called CC=1 SCAR=0.0
Pod undocumented
4246 7685 state $sig = compile(
4247 _Object,
4248 );
4249 7685 my ($cell) = $sig->(@_);
4250 7685 $cell->[0] = "\0"; # ch
4251 7685 $cell->[1] = 0; # fg
4252 7685 $cell->[2] = 0; # bg
4253 7685 return TB_OK;
4254 }
4255
4256 #
4257 # Cell buffer related helpers
4258 #
4259
4260 3 ▶sub init_cellbuf { # $int ()
Subroutine init_cellbuf: called CC=8 SCAR=22.6
Pod undocumented
4261 3 state $sig = compile();
4262 3 $sig->(@_);
4263 3 my $rv;
4264
4265 3 ▶ $global->{back} ||= cellbuf->new();
Condition33%1/3
$Termbox::global->{"back"} ||= "cellbuf"->new
l!l&&r!l&&!r
300
MC/DC0%0/2
$Termbox::global->{"back"} ||= "cellbuf"->new
$Termbox::global->{"back"} "cellbuf"->new
Truth table33%1/3
A ||= B
A= $Termbox::global->{"back"}B= "cellbuf"->new
ABresult
1X1
011
000
4266 3 ▶ $global->{front} ||= cellbuf->new();
Condition33%1/3
$Termbox::global->{"front"} ||= "cellbuf"->new
l!l&&r!l&&!r
300
MC/DC0%0/2
$Termbox::global->{"front"} ||= "cellbuf"->new
$Termbox::global->{"front"} "cellbuf"->new
Truth table33%1/3
A ||= B
A= $Termbox::global->{"front"}B= "cellbuf"->new
ABresult
1X1
011
000
4267
4268 3 ▶ $global->{dirty_lines} = [ (1) x ($global->{height} || 0) ];
Condition50%1/2
$Termbox::global->{'height'} || 0
l!l
30
MC/DC0%0/1
$Termbox::global->{'height'} || 0
$Termbox::global->{'height'}
Truth table50%1/2
A
A= $Termbox::global->{'height'}
Aresult
11
00
4269 3 $rv = $global->{back}->init($global->{width}, $global->{height});
4270 3 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 3 3
4271 3 $rv = $global->{front}->init($global->{width}, $global->{height});
4272 3 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 3 3
4273 3 $rv = $global->{back}->clear();
4274 3 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 3 3
4275 3 $rv = $global->{front}->clear();
4276 3 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 3 3
4277
4278 3 return TB_OK;
4279 }
4280
4281 5 ▶sub cellbuf_init { # $int ($c, $w, $h)
Subroutine cellbuf_init: called CC=1 SCAR=0.0
Pod undocumented
4282 5 goto &cellbuf::init;
4283 }
4284
4285 13 ▶sub cellbuf_free { # $int ($c)
Subroutine cellbuf_free: called CC=4 SCAR=14.0
Pod undocumented
4286 13 state $sig = compile(
4287 _Object,
4288 );
4289 13 my ($c) = $sig->(@_);
4290 13 ▶ for my $cell (@{ $c->{cells} || [] }) {
Condition50%1/2
$c->{'cells'} || []
l!l
130
MC/DC0%0/1
$c->{'cells'} || []
$c->{'cells'}
Truth table50%1/2
A
A= $c->{'cells'}
Aresult
11
00
4291 7684 my $rv = cell_free($cell);
4292 7684 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 7684 7684
4293 }
4294
4295 13 $c->{width} = 0;
4296 13 $c->{height} = 0;
4297 13 $c->{cells} = [];
4298 13 return TB_OK;
4299 }
4300
4301 2 ▶sub cellbuf_clear { # $int ($c)
Subroutine cellbuf_clear: called CC=1 SCAR=0.0
Pod undocumented
4302 2 goto &cellbuf::clear;
4303 }
4304
4305 15 ▶sub cellbuf_get { # $int ($c, $x, $y, \$out)
Subroutine cellbuf_get: called CC=3 SCAR=11.0
Pod undocumented
4306 15 state $sig = compile(
4307 _Object,
4308 _Int,
4309 _Int,
4310 _Ref,
4311 );
4312 15 my ($c, $x, $y, $out) = $sig->(@_);
4313 15 ▶ return TB_ERR_OUT_OF_BOUNDS unless $c->in_bounds($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $c->in_bounds($x, $y) 2 13 15
4314 13 $$out = $c->get($x, $y);
4315 13 ▶ return defined $$out ? TB_OK : TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
defined $$out ? : 13 0 13
4316 }
4317
4318 2 ▶sub cellbuf_in_bounds { # $int ($c, $x, $y)
Subroutine cellbuf_in_bounds: called CC=1 SCAR=0.0
Pod undocumented
4319 2 goto &cellbuf::in_bounds;
4320 }
4321
4322 1 ▶sub cellbuf_resize { # $int ($c, $w, $h)
Subroutine cellbuf_resize: called CC=1 SCAR=0.0
Pod undocumented
4323 1 goto &cellbuf::resize;
4324 }
4325
4326 #
4327 # Sending output helpers
4328 #
4329
4330 14 ▶sub send_literal { # $int ($rv, $a)
Subroutine send_literal: called CC=2 SCAR=6.9
Pod undocumented
4331 14 state $sig = compile(
4332 _Maybe[_Int],
4333 _Str,
4334 );
4335 14 my ($rv, $a) = $sig->(@_);
4336 14 alias: for $rv ($_[0]) {
4337 14 $global->{outbuf} .= $a;
4338 14 return $rv = TB_OK;
4339 } #/ alias:
4340 }
4341
4342 10 ▶sub send_num { # $int ($rv, \$buf, $n)
Subroutine send_num: called CC=2 SCAR=6.9
Pod undocumented
4343 10 state $sig = compile(
4344 _Maybe[_Int],
4345 _ScalarRef,
4346 _PositiveOrZeroInt,
4347 );
4348 10 my ($rv, $buf, $n) = $sig->(@_);
4349 10 alias: for $rv ($_[0]) {
4350 10 $global->{outbuf} .= sprintf('%u', $n);
4351 10 return $rv = TB_OK;
4352 } #/ alias:
4353 }
4354
4355 2 ▶sub send_init_escape_codes { # $int ()
Subroutine send_init_escape_codes: called CC=4 SCAR=14.1
Pod undocumented
4356 2 state $sig = compile();
4357 2 $sig->(@_);
4358 2 my $rv;
4359 2 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_ENTER_CA]);
4360 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
4361 1 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_ENTER_KEYPAD]);
4362 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
4363 1 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_HIDE_CURSOR]);
4364 1 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 1 1
4365 1 return TB_OK;
4366 }
4367
4368 1 ▶sub send_clear { # $int ()
Subroutine send_clear: called CC=5 SCAR=29.3
Pod undocumented
4369 1 state $sig = compile();
4370 1 $sig->(@_);
4371 1 my $rv;
4372
4373 1 $rv = send_attr($global->{fg}, $global->{bg});
4374 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4375 0 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_CLEAR_SCREEN]);
4376 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4377
4378 0 $rv = send_cursor_if($global->{cursor_x}, $global->{cursor_y});
4379 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4380 0 $rv = bytebuf_flush(\$global->{outbuf}, $global->{wfd});
4381 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4382
4383 0 $global->{last_x} = -1;
4384 0 $global->{last_y} = -1;
4385
4386 0 return TB_OK;
4387 }
4388
4389 3 ▶sub send_attr { # $int ($fg, $bg)
Subroutine send_attr: called CC=34 SCAR=68.6
Pod undocumented
4390 3 state $sig = compile(
4391 _PositiveOrZeroInt,
4392 _PositiveOrZeroInt,
4393 );
4394 3 my ($fg, $bg) = $sig->(@_);
4395 3 my $rv;
4396
4397 3 ▶ if ($fg == $global->{last_fg} && $bg == $global->{last_bg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg == $Termbox::global->{"last_fg"} and $bg == $Termbox::global->{"last_bg"}) 1 2 3
Condition66%2/3
$fg == $Termbox::global->{"last_fg"} and $bg == $Termbox::global->{"last_bg"}
!ll&&!rl&&r
201
MC/DC50%1/2
$fg == $Termbox::global->{"last_fg"} and $bg == $Termbox::global->{"last_bg"}
$fg == $Termbox::global->{"last_fg"} $bg == $Termbox::global->{"last_bg"}
Truth table66%2/3
A and B
A= $fg == $Termbox::global->{"last_fg"}B= $bg == $Termbox::global->{"last_bg"}
ABresult
0X0
100
111
4398 1 return TB_OK;
4399 }
4400
4401 2 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_SGR0]);
4402 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4403
4404 0 my ($cfg, $cbg);
4405 0 switch: for ($global->{output_mode}) {
4406 0 DEFAULT: do {};
4407 0 ▶ case: TB_OUTPUT_NORMAL == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { $cfg = ($fg & 16384 ? 90 : 30) + ($fg & 15) - 1; $cbg = ($bg & 16384 ? 100 : 40) + ($bg & 15) - 1; last } 0 0 0
4408 # The minus 1 below is because our colors are 1-indexed starting
4409 # from black. Black is represented by a 30, 40, 90, or 100 for fg,
4410 # bg, bright fg, or bright bg respectively. Red is 31, 41, 91,
4411 # 101, etc.
4412 0 ▶ $cfg = ($fg & TB_BRIGHT ? 90 : 30) + ($fg & 0x0f) - 1;
Branch: 1 branch
BranchTrueFalseTotal
$fg & 16384 ? : 0 0 0
4413 0 ▶ $cbg = ($bg & TB_BRIGHT ? 100 : 40) + ($bg & 0x0f) - 1;
Branch: 1 branch
BranchTrueFalseTotal
$bg & 16384 ? : 0 0 0
4414 0 last;
4415 };
4416 0 ▶ case: TB_OUTPUT_256 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
2 == $_ and do { $cfg = $fg & 255; $cbg = $bg & 255; $cfg = 0 if $fg & 8192; $cbg = 0 if $bg & 8192; last } 0 0 0
4417 0 $cfg = $fg & 0xff;
4418 0 $cbg = $bg & 0xff;
4419 0 ▶ $cfg = 0 if $fg & TB_HI_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $fg & 8192 0 0 0
4420 0 ▶ $cbg = 0 if $bg & TB_HI_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $bg & 8192 0 0 0
4421 0 last;
4422 };
4423 0 ▶ case: TB_OUTPUT_216 == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
4 == $_ and do { $cfg = $fg & 255; $cbg = $bg & 255; $cfg = 216 if $cfg > 216; $cbg = 216 if $cbg > 216; $cfg += 15; $cbg += 15; last } 0 0 0
4424 0 $cfg = $fg & 0xff;
4425 0 $cbg = $bg & 0xff;
4426 0 ▶ $cfg = 216 if $cfg > 216;
Branch: 1 branch
BranchTrueFalseTotal
if $cfg > 216 0 0 0
4427 0 ▶ $cbg = 216 if $cbg > 216;
Branch: 1 branch
BranchTrueFalseTotal
if $cbg > 216 0 0 0
4428 0 $cfg += 0x0f;
4429 0 $cbg += 0x0f;
4430 0 last;
4431 };
4432 0 ▶ case: TB_OUTPUT_GRAYSCALE == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
8 == $_ and do { $cfg = $fg & 255; $cbg = $bg & 255; $cfg = 24 if $cfg > 24; $cbg = 24 if $cbg > 24; $cfg += 231; $cbg += 231; last } 0 0 0
4433 0 $cfg = $fg & 0xff;
4434 0 $cbg = $bg & 0xff;
4435 0 ▶ $cfg = 24 if $cfg > 24;
Branch: 1 branch
BranchTrueFalseTotal
if $cfg > 24 0 0 0
4436 0 ▶ $cbg = 24 if $cbg > 24;
Branch: 1 branch
BranchTrueFalseTotal
if $cbg > 24 0 0 0
4437 0 $cfg += 0xe7;
4438 0 $cbg += 0xe7;
4439 0 last;
4440 };
4441 0 if (TB_OPT_ATTR_W >= 32) {
4442 case: TB_OUTPUT_TRUECOLOR() == $_ and do {
4443 $cfg = $fg & 0xffffff;
4444 $cbg = $bg & 0xffffff;
4445 $cfg = 0 if $fg & TB_HI_BLACK;
4446 $cbg = 0 if $bg & TB_HI_BLACK;
4447 last;
4448 };
4449 } # endif
4450 default: {
4451 0 $_ = TB_OUTPUT_NORMAL;
4452 0 goto DEFAULT;
4453 };
4454 }
4455
4456 0 ▶ if ($fg & TB_BOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg & 256) 0 0 0
4457 0 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_BOLD]);
4458 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4459 }
4460 0 ▶ if ($fg & TB_BLINK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg & 4096) 0 0 0
4461 0 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_BLINK]);
4462 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4463 }
4464 0 ▶ if ($fg & TB_UNDERLINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg & 512) 0 0 0
4465 0 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_UNDERLINE]);
4466 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4467 }
4468 0 ▶ if ($fg & TB_ITALIC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg & 2048) 0 0 0
4469 0 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_ITALIC]);
4470 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4471 }
4472 0 ▶ if ($fg & TB_DIM) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg & 32768) 0 0 0
4473 0 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_DIM]);
4474 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4475 }
4476 0 if (TB_OPT_ATTR_W == 64) {
4477 if ($fg & TB_STRIKEOUT) {
4478 $rv = bytebuf_puts(\$global->{outbuf},
4479 $global->{caps}[TB_HARDCAP_STRIKEOUT]);
4480 return $rv if $rv != TB_OK;
4481 }
4482 if ($fg & TB_UNDERLINE_2) {
4483 $rv = bytebuf_puts(\$global->{outbuf},
4484 $global->{caps}[TB_HARDCAP_UNDERLINE_2]);
4485 return $rv if $rv != TB_OK;
4486 }
4487 if ($fg & TB_OVERLINE) {
4488 $rv = bytebuf_puts(\$global->{outbuf},
4489 $global->{caps}[TB_HARDCAP_OVERLINE]);
4490 return $rv if $rv != TB_OK;
4491 }
4492 if ($fg & TB_INVISIBLE) {
4493 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_INVISIBLE]);
4494 return $rv if $rv != TB_OK;
4495 }
4496 } #endif
4497
4498 0 ▶ if (($fg & TB_REVERSE) || ($bg & TB_REVERSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fg & 1024 or $bg & 1024) 0 0 0
Condition0%0/3
$fg & 1024 or $bg & 1024
l!l&&r!l&&!r
000
MC/DC0%0/2
$fg & 1024 or $bg & 1024
$fg & 1024 $bg & 1024
Truth table0%0/3
A or B
A= $fg & 1024B= $bg & 1024
ABresult
1X1
011
000
4499 0 $rv = bytebuf_puts(\$global->{outbuf}, $global->{caps}[TB_CAP_REVERSE]);
4500 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4501 }
4502 0 my $fg_is_default = ($fg & 0xff) == 0;
4503 0 my $bg_is_default = ($bg & 0xff) == 0;
4504 0 ▶ if ($global->{output_mode} == TB_OUTPUT_256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"output_mode"} == 2) 0 0 0
4505 0 ▶ $fg_is_default = 0 if $fg & TB_HI_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $fg & 8192 0 0 0
4506 0 ▶ $bg_is_default = 0 if $bg & TB_HI_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $bg & 8192 0 0 0
4507 }
4508 0 if (TB_OPT_ATTR_W >= 32) {
4509 if ($global->{output_mode} == TB_OUTPUT_TRUECOLOR) {
4510 $fg_is_default = (($fg & 0xffffff) == 0) && (($fg & TB_HI_BLACK) == 0);
4511 $bg_is_default = (($bg & 0xffffff) == 0) && (($bg & TB_HI_BLACK) == 0);
4512 }
4513 } #endif
4514
4515 0 $rv = send_sgr($cfg, $cbg, $fg_is_default, $bg_is_default);
4516 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4517
4518 0 $global->{last_fg} = $fg;
4519 0 $global->{last_bg} = $bg;
4520
4521 0 return TB_OK;
4522 }
4523
4524 2 ▶sub send_sgr { # $int ($cfg, $cbg, $fg_is_default, $bg_is_default)
Subroutine send_sgr: called CC=26 SCAR=49.4
Pod undocumented
4525 2 state $sig = compile(
4526 _PositiveOrZeroInt,
4527 _PositiveOrZeroInt,
4528 _Bool,
4529 _Bool,
4530 );
4531 2 my ($cfg, $cbg, $fg_is_default, $bg_is_default) = $sig->(@_);
4532 2 my $rv;
4533 2 my $nbuf = '';
4534
4535 2 ▶ return TB_OK if $fg_is_default && $bg_is_default;
Branch: 1 branch
BranchTrueFalseTotal
if $fg_is_default and $bg_is_default 0 2 2
Condition33%1/3
$fg_is_default and $bg_is_default
!ll&&!rl&&r
200
MC/DC0%0/2
$fg_is_default and $bg_is_default
$fg_is_default $bg_is_default
Truth table33%1/3
A and B
A= $fg_is_defaultB= $bg_is_default
ABresult
0X0
100
111
4536
4537 2 switch: for ($global->{output_mode}) {
4538 3 DEFAULT: do {};
4539 3 ▶ case: TB_OUTPUT_NORMAL == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { send_literal($rv, "\e[") == 0 or return $rv; unless ($fg_is_default) { send_num($rv, \$nbuf, $cfg) == 0 or return $rv; unless ($bg_is_default) { send_literal($rv, ";") == 0 or return $rv; } ; } ; unless ($bg_is_default) { send_num($rv, \$nbuf, $cbg) == 0 or return $rv; } ; send_literal($rv, "m") == 0 or return $rv; last } 2 1 3
4540 2 ▶ send_literal($rv, "\x1b[") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "\e[") == 0 or return $rv 2 0 2
4541 2 ▶ if (!$fg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fg_is_default) 2 0 2
4542 2 ▶ send_num($rv, \$nbuf, $cfg) == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_num($rv, \$nbuf, $cfg) == 0 or return $rv 2 0 2
4543 2 ▶ if (!$bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bg_is_default) 2 0 2
4544 2 ▶ send_literal($rv, ";") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, ";") == 0 or return $rv 2 0 2
4545 }
4546 }
4547 2 ▶ if (!$bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bg_is_default) 2 0 2
4548 2 ▶ send_num($rv, \$nbuf, $cbg) == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_num($rv, \$nbuf, $cbg) == 0 or return $rv 2 0 2
4549 }
4550 2 ▶ send_literal($rv, "m") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "m") == 0 or return $rv 2 0 2
4551 2 last;
4552 };
4553 case: TB_OUTPUT_256 == $_ ||
4554 TB_OUTPUT_216 == $_ ||
4555 TB_OUTPUT_GRAYSCALE == $_
4556 1 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
2 == $_ || 4 == $_ || 8 == $_ and do { send_literal($rv, "\e[") == 0 or return $rv; unless ($fg_is_default) { send_literal($rv, "38;5;") == 0 or return $rv; send_num($rv, \$nbuf, $cfg) == 0 or return $rv; unless ($bg_is_default) { send_literal($rv, ";") == 0 or return $rv; } ; } ; unless ($bg_is_default) { send_literal($rv, "48;5;") == 0 or return $rv; send_num($rv, \$nbuf, $cbg) == 0 or return $rv; } ; send_literal($rv, "m") == 0 or return $rv; last } 0 1 1
Condition33%2/6
2 == $_ or 4 == $_ or 8 == $_
l!l&&r!l&&!r
001
2 == $_ or 4 == $_
l!l&&r!l&&!r
001
MC/DC0%0/3
2 == $_ or 4 == $_ or 8 == $_
2 == $_ 4 == $_ 8 == $_
Truth table25%1/4
A or B or C
A= 2 == $_B= 4 == $_C= 8 == $_
ABCresult
1XX1
01X1
0011
0000
4557 0 ▶ send_literal($rv, "\x1b[") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "\e[") == 0 or return $rv 0 0 0
4558 0 ▶ if (!$fg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fg_is_default) 0 0 0
4559 0 ▶ send_literal($rv, "38;5;") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "38;5;") == 0 or return $rv 0 0 0
4560 0 ▶ send_num($rv, \$nbuf, $cfg) == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_num($rv, \$nbuf, $cfg) == 0 or return $rv 0 0 0
4561 0 ▶ if (!$bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bg_is_default) 0 0 0
4562 0 ▶ send_literal($rv, ";") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, ";") == 0 or return $rv 0 0 0
4563 }
4564 }
4565 0 ▶ if (!$bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bg_is_default) 0 0 0
4566 0 ▶ send_literal($rv, "48;5;") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "48;5;") == 0 or return $rv 0 0 0
4567 0 ▶ send_num($rv, \$nbuf, $cbg) == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_num($rv, \$nbuf, $cbg) == 0 or return $rv 0 0 0
4568 }
4569 0 ▶ send_literal($rv, "m") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "m") == 0 or return $rv 0 0 0
4570 0 last;
4571 };
4572 1 if (TB_OPT_ATTR_W >= 32) {
4573 case: TB_OUTPUT_TRUECOLOR() == $_ and do {
4574 send_literal($rv, "\x1b[") == TB_OK or return $rv;
4575 if (!$fg_is_default) {
4576 send_literal($rv, "38;2;") == TB_OK or return $rv;
4577 send_num($rv, \$nbuf, ($cfg >> 16) & 0xff) == TB_OK or return $rv;
4578 send_literal($rv, ";") == TB_OK or return $rv;
4579 send_num($rv, \$nbuf, ($cfg >> 8) & 0xff) == TB_OK or return $rv;
4580 send_literal($rv, ";") == TB_OK or return $rv;
4581 send_num($rv, \$nbuf, $cfg & 0xff) == TB_OK or return $rv;
4582 if (!$bg_is_default) {
4583 send_literal($rv, ";") == TB_OK or return $rv;
4584 }
4585 }
4586 if (!$bg_is_default) {
4587 send_literal($rv, "48;2;") == TB_OK or return $rv;
4588 send_num($rv, \$nbuf, ($cbg >> 16) & 0xff) == TB_OK or return $rv;
4589 send_literal($rv, ";") == TB_OK or return $rv;
4590 send_num($rv, \$nbuf, ($cbg >> 8) & 0xff) == TB_OK or return $rv;
4591 send_literal($rv, ";") == TB_OK or return $rv;
4592 send_num($rv, \$nbuf, $cbg & 0xff) == TB_OK or return $rv;
4593 }
4594 send_literal($rv, "m") == TB_OK or return $rv;
4595 last;
4596 };
4597 } #endif
4598 default: {
4599 1 $_ = TB_OUTPUT_NORMAL;
4600 1 goto DEFAULT;
4601 };
4602 }
4603 2 return TB_OK;
4604 }
4605
4606 3 ▶sub send_cursor_if { # $int ($x, $y)
Subroutine send_cursor_if: called CC=8 SCAR=21.7
Pod undocumented
4607 3 state $sig = compile(
4608 _Int,
4609 _Int,
4610 );
4611 3 my ($x, $y) = $sig->(@_);
4612 3 my $rv;
4613 3 my $nbuf = '';
4614 3 ▶ return TB_OK if $x < 0 || $y < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 or $y < 0 1 2 3
Condition66%2/3
$x < 0 or $y < 0
l!l&&r!l&&!r
102
MC/DC50%1/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table66%2/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
4615 2 ▶ send_literal($rv, "\x1b[") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "\e[") == 0 or return $rv 2 0 2
4616 2 ▶ send_num($rv, \$nbuf, $y + 1) == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_num($rv, \$nbuf, $y + 1) == 0 or return $rv 2 0 2
4617 2 ▶ send_literal($rv, ";") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, ";") == 0 or return $rv 2 0 2
4618 2 ▶ send_num($rv, \$nbuf, $x + 1) == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_num($rv, \$nbuf, $x + 1) == 0 or return $rv 2 0 2
4619 2 ▶ send_literal($rv, "H") == TB_OK or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
send_literal($rv, "H") == 0 or return $rv 2 0 2
4620 2 return TB_OK;
4621 }
4622
4623 0 ▶sub send_char { # $int ($x, $y, $ch)
Subroutine send_char: not called CC=1 SCAR=6.9
Pod undocumented
4624 0 state $sig = compile(
4625 _Int,
4626 _Int,
4627 _PositiveOrZeroInt,
4628 );
4629 0 my ($x, $y, $ch) = $sig->(@_);
4630 0 return send_cluster($x, $y, [$ch], 1);
4631 }
4632
4633 0 ▶sub send_cluster { # $int ($x, $y, \@ch, $nch)
Subroutine send_cluster: not called CC=7 SCAR=40.3
Pod undocumented
4634 0 state $sig = compile(
4635 _Int,
4636 _Int,
4637 _ArrayRef,
4638 _PositiveOrZeroInt,
4639 );
4640 0 my ($x, $y, $ch, $nch) = $sig->(@_);
4641
4642 0 ▶ if ($global->{last_x} != $x - 1 || $global->{last_y} != $y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Termbox::global->{"last_x"} != $x - 1 or $Termbox::global->{"last_y"} != $y) 0 0 0
Condition0%0/3
$Termbox::global->{"last_x"} != $x - 1 or $Termbox::global->{"last_y"} != $y
l!l&&r!l&&!r
000
MC/DC0%0/2
$Termbox::global->{"last_x"} != $x - 1 or $Termbox::global->{"last_y"} != $y
$Termbox::global->{"last_x"} != $x - 1 $Termbox::global->{"last_y"} != $y
Truth table0%0/3
A or B
A= $Termbox::global->{"last_x"} != $x - 1B= $Termbox::global->{"last_y"} != $y
ABresult
1X1
011
000
4643 0 my $rv = send_cursor_if($x, $y);
4644 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4645 }
4646 0 $global->{last_x} = $x;
4647 0 $global->{last_y} = $y;
4648
4649 0 foreach my $ch32 (@$ch) {
4650 0 ▶ if (!tb_iswprint_ex($ch32, undef)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (tb_iswprint_ex($ch32, undef)) 0 0 0
4651 0 $ch32 = 0xfffd; # replace non-printable codepoints with U+FFFD
4652 }
4653 0 my $cu8_len = tb_utf8_unicode_to_char(\my $chu8, $ch32);
4654 0 my $rv = bytebuf_nputs(\$global->{outbuf}, $chu8, $cu8_len);
4655 0 ▶ return $rv if $rv != TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 0 0
4656 }
4657
4658 0 return TB_OK;
4659 }
4660
4661 0 ▶sub convert_num { # $len ($num, \$buf)
Subroutine convert_num: not called CC=1 SCAR=6.9
Pod undocumented
4662 0 state $sig = compile(
4663 _PositiveOrZeroInt,
4664 _ScalarRef,
4665 );
4666 0 my ($num, $buf) = $sig->(@_);
4667 0 $$buf = sprintf('%u', $num);
4668 0 return bytes::length($$buf);
4669 }
4670
4671 #
4672 # Byte buffer related helpers
4673 #
4674
4675 10 ▶sub bytebuf_puts { # $int (\$buf, $str)
Subroutine bytebuf_puts: called CC=2 SCAR=6.9
Pod undocumented
4676 10 state $sig = compile(
4677 _ScalarRef,
4678 _Str,
4679 );
4680 10 my ($b, $str) = $sig->(@_);
4681 # Nothing to do for empty caps
4682 5 ▶ $$b .= $str if bytes::length($str);
Branch: 1 branch
BranchTrueFalseTotal
if &bytes::length($str) 4 1 5
4683 5 return TB_OK
4684 }
4685
4686 1 ▶sub bytebuf_nputs { # $int (\$buf, $str, $nstr)
Subroutine bytebuf_nputs: called CC=1 SCAR=0.0
Pod undocumented
4687 1 state $sig = compile(
4688 _ScalarRef,
4689 _Str,
4690 _PositiveOrZeroInt,
4691 );
4692 1 my ($b, $str, $nstr) = $sig->(@_);
4693 1 $$b .= bytes::substr($str, 0, $nstr);
4694 1 return TB_OK;
4695 }
4696
4697 3 ▶sub bytebuf_shift { # $int (\$buf, $n)
Subroutine bytebuf_shift: called CC=2 SCAR=6.9
Pod undocumented
4698 3 state $sig = compile(
4699 _ScalarRef,
4700 _PositiveOrZeroInt,
4701 );
4702 3 my ($b, $n) = $sig->(@_);
4703 39 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
4704 3 ▶ $n = length($$b) if $n > length($$b);
Branch: 1 branch
BranchTrueFalseTotal
if $n > length $$b 1 2 3
4705 3 substr($$b, 0, $n, '');
4706 3 return TB_OK;
4707 }
4708
4709 4 ▶sub bytebuf_flush { # $int (\$buf, $fd)
Subroutine bytebuf_flush: called CC=5 SCAR=16.1
Pod undocumented
4710 4 state $sig = compile(
4711 _ScalarRef,
4712 _Int,
4713 );
4714 4 my ($b, $fd) = $sig->(@_);
4715 39 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
4716 4 ▶ return TB_OK unless length($$b);
Branch: 1 branch
BranchTrueFalseTotal
unless length $$b 2 2 4
4717 2 my $want = length($$b);
4718 2 my $wrote;
4719 2 if (_WIN32) {
4720 my $hFile = $fd // INVALID_HANDLE_VALUE;
4721 if (!Win32API::File::WriteFile($hFile, $$b, $want, $wrote, [])) {
4722 my $err = 0+ $^E;
4723 if ($err == ERROR_INVALID_HANDLE) { $! = EBADF }
4724 elsif ($err == ERROR_INVALID_PARAMETER) { $! = EINVAL }
4725 elsif ($err == ERROR_BROKEN_PIPE) { $! = EPIPE }
4726 else { $! = EIO }
4727 }
4728 } else {
4729 2 ▶ $wrote = POSIX::write($fd // -1, $$b, $want) // 0;
Condition50%2/4
POSIX::write($fd // -1, $$b, $want) // 0
l!l
20
$fd // -1
l!l
20
MC/DC0%0/1
POSIX::write($fd // -1, $$b, $want) // 0
POSIX::write($fd // -1, $$b, $want)
MC/DC0%0/1
$fd // -1
$fd
Truth table50%1/2
A
A= POSIX::write($fd // -1, $$b, $want)
Aresult
11
00
Truth table50%1/2
A
A= $fd
Aresult
11
00
4730 } #endif
4731 # Partial writes are treated as errors
4732 2 ▶ if ($wrote != $want) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrote != $want) 1 1 2
4733 1 $global->{last_errno} = 0+ $!;
4734 1 return TB_ERR;
4735 }
4736 1 $$b = '';
4737 1 return TB_OK;
4738 }
4739
4740 1 ▶sub bytebuf_reserve { # $int (\$buf, $sz)
Subroutine bytebuf_reserve: called CC=1 SCAR=0.0
Pod undocumented
4741 1 state $sig = compile(
4742 _ScalarRef,
4743 _PositiveOrZeroInt,
4744 );
4745 1 $sig->(@_);
4746 1 return TB_OK;
4747 }
4748
4749 14 ▶sub bytebuf_free { # $int (\$buf)
Subroutine bytebuf_free: called CC=1 SCAR=0.0
Pod undocumented
4750 14 state $sig = compile(
4751 _ScalarRef,
4752 );
4753 14 my ($b) = $sig->(@_);
4754 13 $$b = '';
4755 13 return TB_OK;
4756 }
4757
4758 # Hack to make 'use Termbox' work without an actual Termbox.pm file.
4759 39 $INC{"Termbox.pm"} = __FILE__;
↑ Summary lib/Terminal/WCWidth.pm »