lib/Termbox/PP.pm

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