lib/Termbox/PP.pm

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