dte test coverage


Directory: ./
File: src/terminal/output.c
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 267 356 75.0%
Functions: 30 37 81.1%
Branches: 86 150 57.3%

Line Branch Exec Source
1 // Terminal output and control functions.
2 // Copyright © Craig Barnes.
3 // Copyright © Timo Hirvonen.
4 // SPDX-License-Identifier: GPL-2.0-only
5 // See also:
6 // • ECMA-48 5th edition, §8.3 (CUP, ED, EL, REP, SGR):
7 // https://ecma-international.org/publications-and-standards/standards/ecma-48/
8 // • DEC Manual EK-VT510-RM, Chapter 5 (DECRQM, DECRQSS, DECSCUSR, DECTCEM):
9 // https://vt100.net/docs/vt510-rm/contents.html
10 // • XTerm's ctlseqs.html (XTWINOPS, XTQMODKEYS, XTGETTCAP, OSC 12, OSC 112):
11 // https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
12 // • Kitty's keyboard protocol documentation (CSI ? u):
13 // https://sw.kovidgoyal.net/kitty/keyboard-protocol/
14
15 #include <limits.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include "output.h"
21 #include "color.h"
22 #include "cursor.h"
23 #include "indent.h"
24 #include "util/ascii.h"
25 #include "util/debug.h"
26 #include "util/log.h"
27 #include "util/numtostr.h"
28 #include "util/str-util.h"
29 #include "util/utf8.h"
30 #include "util/xmalloc.h"
31 #include "util/xreadwrite.h"
32
33 66 char *term_output_reserve_space(TermOutputBuffer *obuf, size_t count)
34 {
35 66 BUG_ON(count > TERM_OUTBUF_SIZE);
36 66 BUG_ON(obuf->count > TERM_OUTBUF_SIZE);
37
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 66 times.
66 if (unlikely(obuf_avail(obuf) < count)) {
38 term_output_flush(obuf);
39 }
40 66 return obuf->buf + obuf->count;
41 }
42
43 8 void term_output_reset(Terminal *term, size_t start_x, size_t width, size_t scroll_x)
44 {
45 8 TermOutputBuffer *obuf = &term->obuf;
46 8 obuf->x = 0;
47 8 obuf->width = width;
48 8 obuf->scroll_x = scroll_x;
49 8 obuf->tab_width = 8;
50 8 obuf->tab_mode = TAB_CONTROL;
51 8 obuf->can_clear = ((start_x + width) == term->width);
52 8 }
53
54 // Write directly to the terminal, as done when e.g. flushing the output buffer
55 static bool term_direct_write(const char *str, size_t count)
56 {
57 ssize_t n = xwrite_all(STDOUT_FILENO, str, count);
58 if (unlikely(n != count)) {
59 LOG_ERRNO("write");
60 return false;
61 }
62 return true;
63 }
64
65 // NOTE: does not update `obuf.x`; see term_put_byte()
66 27 void term_put_bytes(TermOutputBuffer *obuf, const char *str, size_t count)
67 {
68
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→8) taken 27 times.
27 if (unlikely(count >= TERM_OUTBUF_SIZE)) {
69 term_output_flush(obuf);
70 if (term_direct_write(str, count)) {
71 LOG_INFO("writing %zu bytes directly to terminal", count);
72 }
73 return;
74 }
75
76 27 char *buf = term_output_reserve_space(obuf, count);
77 27 obuf->count += count;
78 27 memcpy(buf, str, count);
79 }
80
81 4 static void term_repeat_byte(TermOutputBuffer *obuf, char ch, size_t count)
82 {
83
1/2
✓ Branch 0 (2→3) taken 4 times.
✗ Branch 1 (2→5) not taken.
4 if (likely(count <= TERM_OUTBUF_SIZE)) {
84 // Repeat count fits in buffer; reserve space and tail-call memset(3)
85 4 char *buf = term_output_reserve_space(obuf, count);
86 4 obuf->count += count;
87 4 memset(buf, ch, count);
88 4 return;
89 }
90
91 // Repeat count greater than buffer size; fill buffer with `ch` and call
92 // write() repeatedly until `count` reaches zero
93 term_output_flush(obuf);
94 memset(obuf->buf, ch, TERM_OUTBUF_SIZE);
95 while (count) {
96 size_t n = MIN(count, TERM_OUTBUF_SIZE);
97 count -= n;
98 term_direct_write(obuf->buf, n);
99 }
100 }
101
102 4 static bool ecma48_repeat_byte(TermOutputBuffer *obuf, char ch, size_t count)
103 {
104
4/4
✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 1 times.
✓ Branch 2 (3→4) taken 1 times.
✓ Branch 3 (3→6) taken 2 times.
4 if (!ascii_isprint(ch) || count < ECMA48_REP_MIN || count > ECMA48_REP_MAX) {
105 2 term_repeat_byte(obuf, ch, count);
106 2 return false;
107 }
108
109 // ECMA-48 REP (CSI Pn b)
110 2 static_assert(ECMA48_REP_MAX == 30000);
111 2 const size_t maxlen = STRLEN("_E[30000b");
112 2 char *buf = term_output_reserve_space(obuf, maxlen);
113 2 size_t i = 0;
114 2 buf[i++] = ch;
115 2 buf[i++] = '\033';
116 2 buf[i++] = '[';
117 2 i += buf_uint_to_str(count - 1, buf + i);
118 2 buf[i++] = 'b';
119 2 BUG_ON(i > maxlen);
120 2 obuf->count += i;
121 2 return true;
122 }
123
124 6 TermSetBytesMethod term_set_bytes(Terminal *term, char ch, size_t count)
125 {
126 6 TermOutputBuffer *obuf = &term->obuf;
127
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 6 times.
6 if (obuf->x + count > obuf->scroll_x + obuf->width) {
128 count = obuf->scroll_x + obuf->width - obuf->x;
129 }
130
131 6 ssize_t skip = obuf->scroll_x - obuf->x;
132
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 6 times.
6 if (skip > 0) {
133 skip = MIN(skip, count);
134 obuf->x += skip;
135 count -= skip;
136 }
137
138 6 obuf->x += count;
139
2/2
✓ Branch 0 (6→7) taken 4 times.
✓ Branch 1 (6→9) taken 2 times.
6 if (term->features & TFLAG_ECMA48_REPEAT) {
140 4 bool used_rep = ecma48_repeat_byte(obuf, ch, count);
141 4 return used_rep ? TERM_SET_BYTES_REP : TERM_SET_BYTES_MEMSET;
142 }
143
144 2 term_repeat_byte(obuf, ch, count);
145 2 return TERM_SET_BYTES_MEMSET;
146 }
147
148 // Append a single byte to the buffer.
149 // NOTE: this does not update `obuf.x`, since it can be used to write
150 // bytes within escape sequences without advancing the cursor position.
151 7 void term_put_byte(TermOutputBuffer *obuf, char ch)
152 {
153 7 char *buf = term_output_reserve_space(obuf, 1);
154 7 buf[0] = ch;
155 7 obuf->count++;
156 7 }
157
158 2 void term_put_str(TermOutputBuffer *obuf, const char *str)
159 {
160 2 size_t i = 0;
161
2/2
✓ Branch 0 (7→3) taken 14 times.
✓ Branch 1 (7→8) taken 1 times.
15 while (str[i]) {
162
2/2
✓ Branch 0 (5→6) taken 13 times.
✓ Branch 1 (5→8) taken 1 times.
14 if (!term_put_char(obuf, u_str_get_char(str, &i))) {
163 break;
164 }
165 }
166 2 }
167
168 /*
169 * See also:
170 * • handle_query_reply()
171 * • parse_csi_query_reply()
172 * • https://vt100.net/docs/vt510-rm/DA1.html
173 * • ECMA-48 §8.3.24
174 */
175 2 void term_put_initial_queries(Terminal *term, unsigned int level)
176 {
177
1/2
✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→12) not taken.
2 if (level < 1) {
178 return;
179 }
180
181 2 LOG_INFO("sending level 1 queries to terminal");
182 2 term_put_literal(&term->obuf, "\033[c"); // ECMA-48 DA (AKA "DA1")
183
184
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→12) taken 1 times.
2 if (level < 2) {
185 return;
186 }
187
188 // Level 6 or greater means emit all query levels and also emit even
189 // conditional queries, i.e. those that are usually omitted when the
190 // corresponding feature flag was already set by term_init()
191 1 bool emit_all = (level >= 6);
192
1/2
✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→8) not taken.
1 if (emit_all) {
193 1 LOG_INFO("query level set to %u; unconditionally sending all queries", level);
194 }
195
196 1 term_put_level_2_queries(term, emit_all);
197 1 term->features |= TFLAG_QUERY_L2;
198
199
1/2
✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→12) not taken.
1 if (level < 3) {
200 return;
201 }
202
203 1 term_put_level_3_queries(term, emit_all);
204 1 term->features |= TFLAG_QUERY_L3;
205 }
206
207 /*
208 * See also:
209 * • handle_query_reply()
210 * • TFLAG_QUERY_L2
211 * • parse_csi_query_reply()
212 * • parse_dcs_query_reply()
213 * • parse_xtwinops_query_reply()
214 */
215 1 void term_put_level_2_queries(Terminal *term, bool emit_all)
216 {
217 1 static const char queries[] =
218 "\033[>0q" // XTVERSION (terminal name and version)
219 "\033[>c" // DA2 (Secondary Device Attributes)
220 "\033[?u" // Kitty keyboard protocol flags
221 "\033[?1036$p" // DECRQM 1036 (metaSendsEscape; must be after kitty query)
222 "\033[?1039$p" // DECRQM 1039 (altSendsEscape; must be after kitty query)
223 ;
224
225 1 static const char debug_queries[] =
226 "\033[?4m" // XTQMODKEYS 4 (xterm modifyOtherKeys mode)
227 "\033[?7$p" // DECRQM 7 (DECAWM; auto-wrap mode)
228 "\033[?25$p" // DECRQM 25 (DECTCEM; cursor visibility)
229 "\033[?45$p" // DECRQM 45 (XTREVWRAP; reverse-wraparound mode)
230 "\033[?67$p" // DECRQM 67 (DECBKM; backspace key sends BS)
231 "\033[?1049$p" // DECRQM 1049 (alternate screen buffer)
232 "\033[?2004$p" // DECRQM 2004 (bracketed paste)
233 "\033[18t" // XTWINOPS 18 (text area size in "characters"/cells)
234 ;
235
236 1 TermOutputBuffer *obuf = &term->obuf;
237 1 LOG_INFO("sending level 2 queries to terminal");
238 1 term_put_bytes(obuf, queries, sizeof(queries) - 1);
239
240
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 1 times.
1 TermFeatureFlags features = emit_all ? 0 : term->features;
241 if (!(features & TFLAG_SYNC)) {
242 1 term_put_literal(obuf, "\033[?2026$p"); // DECRQM 2026
243 }
244
245 // Debug query responses are used purely for logging/informational purposes
246
1/4
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 1 times.
✗ Branch 2 (9→10) not taken.
✗ Branch 3 (9→11) not taken.
1 if (emit_all || log_level_debug_enabled()) {
247 1 term_put_bytes(obuf, debug_queries, sizeof(debug_queries) - 1);
248 }
249 1 }
250
251 /*
252 * Some terminals fail to parse DCS sequences in accordance with ECMA-48,
253 * so DCS queries are sent separately and only after probing for some
254 * known problem cases (e.g. PuTTY).
255 *
256 * See also:
257 * • handle_query_reply()
258 * • TFLAG_QUERY_L3
259 * • parse_dcs_query_reply()
260 * • parse_xtgettcap_reply()
261 * • handle_decrqss_sgr_reply()
262 */
263 1 void term_put_level_3_queries(Terminal *term, bool emit_all)
264 {
265 // Note: the correct (according to ISO 8613-6) format for the SGR
266 // sequence here would be "\033[0;38:2::60:70:80;48:5:255m", but we
267 // use the standards-incorrect (but de facto more widely supported)
268 // format because that's what is actually used in term_set_style()
269 1 static const char sgr_query[] =
270 "\033[0;38;2;60;70;80;48;5;255m" // SGR with direct fg and indexed bg
271 "\033P$qm\033\\" // DECRQSS SGR (check support for SGR params above)
272 "\033[0m" // SGR 0
273 ;
274
275 1 TermOutputBuffer *obuf = &term->obuf;
276
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1 times.
1 TermFeatureFlags features = emit_all ? 0 : term->features;
277 1 LOG_INFO("sending level 3 queries to terminal");
278
279
1/2
✓ Branch 0 (5→6) taken 1 times.
✗ Branch 1 (5→7) not taken.
1 if (!(features & TFLAG_TRUE_COLOR)) {
280 1 term_put_bytes(obuf, sgr_query, sizeof(sgr_query) - 1);
281 }
282
283
1/2
✓ Branch 0 (7→8) taken 1 times.
✗ Branch 1 (7→9) not taken.
1 if (!(features & TFLAG_BACK_COLOR_ERASE)) {
284 1 term_put_literal(obuf, "\033P+q626365\033\\"); // XTGETTCAP "bce"
285 }
286
1/2
✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→11) not taken.
1 if (!(features & TFLAG_ECMA48_REPEAT)) {
287 1 term_put_literal(obuf, "\033P+q726570\033\\"); // XTGETTCAP "rep"
288 }
289
1/2
✓ Branch 0 (11→12) taken 1 times.
✗ Branch 1 (11→13) not taken.
1 if (!(features & TFLAG_SET_WINDOW_TITLE)) {
290 1 term_put_literal(obuf, "\033P+q74736C\033\\"); // XTGETTCAP "tsl"
291 }
292
1/2
✓ Branch 0 (13→14) taken 1 times.
✗ Branch 1 (13→15) not taken.
1 if (!(features & TFLAG_OSC52_COPY)) {
293 1 term_put_literal(obuf, "\033P+q4D73\033\\"); // XTGETTCAP "Ms"
294 }
295
296
1/4
✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 1 times.
✗ Branch 2 (17→18) not taken.
✗ Branch 3 (17→19) not taken.
1 if (emit_all || log_level_debug_enabled()) {
297 1 term_put_literal(obuf, "\033P$q q\033\\"); // DECRQSS DECSCUSR (cursor style)
298 }
299 1 }
300
301 // https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer
302 1 void term_use_alt_screen_buffer(Terminal *term)
303 {
304 1 term_put_literal(&term->obuf, "\033[?1049h"); // DECSET 1049
305 1 }
306
307 1 void term_use_normal_screen_buffer(Terminal *term)
308 {
309 1 term_put_literal(&term->obuf, "\033[?1049l"); // DECRST 1049
310 1 }
311
312 1 void term_hide_cursor(Terminal *term)
313 {
314 1 term_put_literal(&term->obuf, "\033[?25l"); // DECRST 25 (DECTCEM)
315 1 }
316
317 1 void term_show_cursor(Terminal *term)
318 {
319 1 term_put_literal(&term->obuf, "\033[?25h"); // DECSET 25 (DECTCEM)
320 1 }
321
322 1 void term_begin_sync_update(Terminal *term)
323 {
324 1 TermOutputBuffer *obuf = &term->obuf;
325
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1 times.
1 WARN_ON(obuf->sync_pending);
326
1/2
✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→7) not taken.
1 if (term->features & TFLAG_SYNC) {
327 1 term_put_literal(obuf, "\033[?2026h"); // DECSET 2026
328 1 obuf->sync_pending = true;
329 }
330 1 }
331
332 1 void term_end_sync_update(Terminal *term)
333 {
334 1 TermOutputBuffer *obuf = &term->obuf;
335
2/4
✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→6) not taken.
✓ Branch 2 (3→4) taken 1 times.
✗ Branch 3 (3→6) not taken.
1 if ((term->features & TFLAG_SYNC) && obuf->sync_pending) {
336 1 term_put_literal(obuf, "\033[?2026l"); // DECRST 2026
337 1 obuf->sync_pending = false;
338 }
339 1 }
340
341 2 void term_move_cursor(TermOutputBuffer *obuf, unsigned int x, unsigned int y)
342 {
343 // ECMA-48 CUP (CSI Pl ; Pc H)
344 2 const size_t maxlen = STRLEN("E[;H") + (2 * DECIMAL_STR_MAX(x));
345 2 char *buf = term_output_reserve_space(obuf, maxlen);
346 2 size_t i = copyliteral(buf, "\033[");
347 2 i += buf_uint_to_str(y + 1, buf + i);
348
349
2/2
✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→8) taken 1 times.
2 if (x != 0) {
350 1 buf[i++] = ';';
351 1 i += buf_uint_to_str(x + 1, buf + i);
352 }
353
354 2 buf[i++] = 'H';
355 2 BUG_ON(i > maxlen);
356 2 obuf->count += i;
357 2 }
358
359 5 void term_save_title(Terminal *term)
360 {
361
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 5 times.
5 if (term->features & TFLAG_SET_WINDOW_TITLE) {
362 // Save window title on stack (XTWINOPS)
363 // https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
364 term_put_literal(&term->obuf, "\033[22;2t");
365 }
366 5 }
367
368 void term_restore_title(Terminal *term)
369 {
370 if (term->features & TFLAG_SET_WINDOW_TITLE) {
371 // Restore window title from stack (XTWINOPS)
372 term_put_literal(&term->obuf, "\033[23;2t");
373 }
374 }
375
376 5 bool term_can_clear_eol_with_el_sequence(const Terminal *term)
377 {
378 5 const TermOutputBuffer *obuf = &term->obuf;
379 5 bool bce = !!(term->features & TFLAG_BACK_COLOR_ERASE);
380 5 bool rev = !!(obuf->style.attr & ATTR_REVERSE);
381 5 bool bg = (obuf->style.bg >= COLOR_BLACK);
382
6/6
✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→5) taken 1 times.
✓ Branch 2 (3→4) taken 3 times.
✓ Branch 3 (3→5) taken 1 times.
✓ Branch 4 (4→5) taken 1 times.
✓ Branch 5 (4→6) taken 2 times.
5 return obuf->can_clear && (bce || !bg) && !rev;
383 }
384
385 6 int term_clear_eol(Terminal *term)
386 {
387 6 TermOutputBuffer *obuf = &term->obuf;
388 6 const size_t end = obuf->scroll_x + obuf->width;
389
2/2
✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→15) taken 1 times.
6 if (obuf->x >= end) {
390 // Cursor already at EOL; nothing to clear
391 return 0;
392 }
393
394
2/2
✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→6) taken 3 times.
5 if (term_can_clear_eol_with_el_sequence(term)) {
395 2 obuf->x = end;
396 2 term_put_literal(obuf, "\033[K"); // Erase to end of line (EL 0)
397 2 static_assert(ECMA48_REP_MIN > -TERM_CLEAR_EOL_USED_EL);
398 2 return TERM_CLEAR_EOL_USED_EL;
399 }
400
401 3 size_t count = end - obuf->x;
402 3 TermSetBytesMethod method = term_set_bytes(term, ' ', count);
403
404
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→12) taken 3 times.
3 if (unlikely(count > INT_MAX)) {
405 // This is basically impossible, given that POSIX requires INT_MAX
406 // to be at least 2³¹-1 and lines of that length simply aren't
407 // something that happens. In any case, the return value here is
408 // only ever used for logging purposes in update_status_line().
409 LOG_ERROR("repeat count in %s() too large for int return value", __func__);
410 return (method == TERM_SET_BYTES_REP) ? INT_MIN : INT_MAX;
411 }
412
413 // Return a negative count if an ECMA-48 REP sequence was used, or a
414 // positive count if space was emitted `count` times
415
2/2
✓ Branch 0 (12→13) taken 1 times.
✓ Branch 1 (12→14) taken 2 times.
3 return (method == TERM_SET_BYTES_REP) ? -count : count;
416 }
417
418 void term_clear_screen(TermOutputBuffer *obuf)
419 {
420 term_put_literal (
421 obuf,
422 "\033[0m" // Reset colors and attributes (SGR 0)
423 "\033[H" // Move cursor to 1,1 (CUP; done only to mimic terminfo(5) "clear")
424 "\033[2J" // Clear whole screen (ED 2)
425 );
426 }
427
428 void term_output_flush(TermOutputBuffer *obuf)
429 {
430 size_t n = obuf->count;
431 if (n) {
432 BUG_ON(n > TERM_OUTBUF_SIZE);
433 obuf->count = 0;
434 term_direct_write(obuf->buf, n);
435 }
436 }
437
438 static const char *get_tab_str(TermTabOutputMode tab_mode)
439 {
440 static const char tabstr[][8] = {
441 [TAB_NORMAL] = " ",
442 [TAB_SPECIAL] = ">-------",
443 // TAB_CONTROL is printed with u_set_char() and is thus omitted
444 };
445 BUG_ON(tab_mode >= ARRAYLEN(tabstr));
446 return tabstr[tab_mode];
447 }
448
449 static void skipped_too_much(TermOutputBuffer *obuf, CodePoint u)
450 {
451 char *buf = term_output_reserve_space(obuf, 7);
452 size_t n = obuf->x - obuf->scroll_x;
453 BUG_ON(n == 0);
454
455 if (u == '\t' && obuf->tab_mode != TAB_CONTROL) {
456 static_assert(TAB_WIDTH_MAX == 8);
457 BUG_ON(n > 7);
458 memcpy(buf, get_tab_str(obuf->tab_mode) + 1, 7);
459 obuf->count += n;
460 return;
461 }
462
463 if (u < 0x20 || u == 0x7F) {
464 BUG_ON(n != 1);
465 buf[0] = (u + 64) & 0x7F;
466 obuf->count++;
467 return;
468 }
469
470 if (u_is_unprintable(u)) {
471 static_assert(U_SET_HEX_LEN == 4);
472 BUG_ON(n > 3);
473 char tmp[8] = {'\0'};
474 u_set_hex(tmp, u);
475 memcpy(buf, tmp + 4 - n, 4);
476 obuf->count += n;
477 return;
478 }
479
480 BUG_ON(n != 1);
481 buf[0] = '>';
482 obuf->count++;
483 }
484
485 static void buf_skip(TermOutputBuffer *obuf, CodePoint u)
486 {
487 if (u == '\t' && obuf->tab_mode != TAB_CONTROL) {
488 obuf->x = next_indent_width(obuf->x, obuf->tab_width);
489 } else {
490 obuf->x += u_char_width(u);
491 }
492
493 if (obuf->x > obuf->scroll_x) {
494 skipped_too_much(obuf, u);
495 }
496 }
497
498 15 bool term_put_char(TermOutputBuffer *obuf, CodePoint u)
499 {
500
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 15 times.
15 if (unlikely(obuf->x < obuf->scroll_x)) {
501 // Scrolled, char (at least partially) invisible
502 buf_skip(obuf, u);
503 return true;
504 }
505
506 15 const size_t space = obuf->scroll_x + obuf->width - obuf->x;
507
2/2
✓ Branch 0 (5→6) taken 14 times.
✓ Branch 1 (5→27) taken 1 times.
15 if (unlikely(!space)) {
508 return false;
509 }
510
511 14 term_output_reserve_space(obuf, 8);
512
2/2
✓ Branch 0 (7→8) taken 11 times.
✓ Branch 1 (7→19) taken 3 times.
14 if (likely(u < 0x80)) {
513
2/2
✓ Branch 0 (8→9) taken 8 times.
✓ Branch 1 (8→10) taken 3 times.
11 if (likely(!ascii_iscntrl(u))) {
514 8 obuf->buf[obuf->count++] = u;
515 8 obuf->x++;
516
3/4
✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→17) taken 1 times.
✗ Branch 2 (11→12) not taken.
✓ Branch 3 (11→17) taken 2 times.
3 } else if (u == '\t' && obuf->tab_mode != TAB_CONTROL) {
517 size_t width = next_indent_width(obuf->x, obuf->tab_width) - obuf->x;
518 BUG_ON(width > 8);
519 width = MIN(width, space);
520 memcpy(obuf->buf + obuf->count, get_tab_str(obuf->tab_mode), 8);
521 obuf->count += width;
522 obuf->x += width;
523 } else {
524 // Use caret notation for control chars:
525 3 obuf->buf[obuf->count++] = '^';
526 3 obuf->x++;
527
1/2
✓ Branch 0 (17→18) taken 3 times.
✗ Branch 1 (17→27) not taken.
3 if (likely(space > 1)) {
528 3 obuf->buf[obuf->count++] = (u + 64) & 0x7F;
529 3 obuf->x++;
530 }
531 }
532 } else {
533 3 const size_t width = u_char_width(u);
534
1/2
✓ Branch 0 (19→20) taken 3 times.
✗ Branch 1 (19→22) not taken.
3 if (likely(width <= space)) {
535 3 obuf->x += width;
536 3 obuf->count += u_set_char(obuf->buf + obuf->count, u);
537 } else if (u_is_unprintable(u)) {
538 // <xx> would not fit.
539 // There's enough space in the buffer so render all 4 characters
540 // but increment position less.
541 u_set_hex(obuf->buf + obuf->count, u);
542 obuf->count += space;
543 obuf->x += space;
544 } else {
545 obuf->buf[obuf->count++] = '>';
546 obuf->x++;
547 }
548 }
549
550 return true;
551 }
552
553 12 static size_t set_color_suffix(char *buf, int32_t color)
554 {
555 12 BUG_ON(color < 0);
556
2/2
✓ Branch 0 (4→5) taken 7 times.
✓ Branch 1 (4→6) taken 5 times.
12 if (likely(color < 16)) {
557 7 buf[0] = '0' + (color & 7);
558 7 return 1;
559 }
560
561
2/2
✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→12) taken 4 times.
5 if (!color_is_rgb(color)) {
562 1 BUG_ON(color > 255);
563 1 size_t i = copyliteral(buf, "8;5;");
564 1 return i + buf_u8_to_str(color, buf + i);
565 }
566
567 4 size_t i = copyliteral(buf, "8;2;");
568 4 i += buf_u8_to_str(color_r(color), buf + i);
569 4 buf[i++] = ';';
570 4 i += buf_u8_to_str(color_g(color), buf + i);
571 4 buf[i++] = ';';
572 4 i += buf_u8_to_str(color_b(color), buf + i);
573 4 return i;
574 }
575
576 8 static size_t set_fg_color(char *buf, int32_t color)
577 {
578
2/2
✓ Branch 0 (2→3) taken 7 times.
✓ Branch 1 (2→7) taken 1 times.
8 if (color < 0) {
579 return 0;
580 }
581
582 7 bool light = (color >= 8 && color <= 15);
583 7 buf[0] = ';';
584
2/2
✓ Branch 0 (3→4) taken 6 times.
✓ Branch 1 (3→5) taken 1 times.
7 buf[1] = light ? '9' : '3';
585 7 return 2 + set_color_suffix(buf + 2, color);
586 }
587
588 8 static size_t set_bg_color(char *buf, int32_t color)
589 {
590
2/2
✓ Branch 0 (2→3) taken 5 times.
✓ Branch 1 (2→9) taken 3 times.
8 if (color < 0) {
591 return 0;
592 }
593
594 5 bool light = (color >= 8 && color <= 15);
595 5 buf[0] = ';';
596
2/2
✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→5) taken 1 times.
5 buf[1] = light ? '1' : '4';
597 5 buf[2] = '0';
598
2/2
✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→7) taken 1 times.
5 size_t i = light ? 3 : 2;
599 5 return i + set_color_suffix(buf + i, color);
600 }
601
602 16 static int32_t color_normalize(int32_t color)
603 {
604 16 BUG_ON(!color_is_valid(color));
605 16 return (color <= COLOR_KEEP) ? COLOR_DEFAULT : color;
606 }
607
608 8 static void term_style_sanitize(TermStyle *style, unsigned int ncv_attrs)
609 {
610 // Replace COLOR_KEEP fg/bg colors with COLOR_DEFAULT, to normalize the
611 // values set in TermOutputBuffer::style
612 8 style->fg = color_normalize(style->fg);
613 8 style->bg = color_normalize(style->bg);
614
615 // Unset ATTR_KEEP, since it's meaningless at this stage (and shouldn't
616 // be set in TermOutputBuffer::style)
617 8 style->attr &= ~ATTR_KEEP;
618
619 // Unset ncv_attrs bits, if fg and/or bg color is non-default (see "ncv"
620 // in terminfo(5) man page)
621
3/4
✓ Branch 0 (4→5) taken 1 times.
✓ Branch 1 (4→6) taken 7 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (5→7) taken 1 times.
8 bool have_color = (style->fg > COLOR_DEFAULT || style->bg > COLOR_DEFAULT);
622 7 style->attr &= (have_color ? ~ncv_attrs : ~0u);
623 8 }
624
625 8 void term_set_style(Terminal *term, TermStyle style)
626 {
627 8 static const struct {
628 char code;
629 unsigned int attr;
630 } attr_map[] = {
631 {'1', ATTR_BOLD},
632 {'2', ATTR_DIM},
633 {'3', ATTR_ITALIC},
634 {'4', ATTR_UNDERLINE},
635 {'5', ATTR_BLINK},
636 {'7', ATTR_REVERSE},
637 {'8', ATTR_INVIS},
638 {'9', ATTR_STRIKETHROUGH}
639 };
640
641 // TODO: take `TermOutputBuffer::style` into account and only emit
642 // the minimal set of parameters needed to update the terminal's
643 // current state (i.e. without using `0` to reset or emitting
644 // already active attributes/colors)
645
646 8 term_style_sanitize(&style, term->ncv_attributes);
647
648 8 const size_t maxcolor = STRLEN(";38;2;255;255;255");
649 8 const size_t maxlen = STRLEN("E[0m") + (2 * maxcolor) + (2 * ARRAYLEN(attr_map));
650 8 char *buf = term_output_reserve_space(&term->obuf, maxlen);
651 8 size_t pos = copyliteral(buf, "\033[0");
652
653
2/2
✓ Branch 0 (9→6) taken 64 times.
✓ Branch 1 (9→10) taken 8 times.
72 for (size_t i = 0; i < ARRAYLEN(attr_map); i++) {
654
2/2
✓ Branch 0 (6→7) taken 16 times.
✓ Branch 1 (6→8) taken 48 times.
64 if (style.attr & attr_map[i].attr) {
655 16 buf[pos++] = ';';
656 16 buf[pos++] = attr_map[i].code;
657 }
658 }
659
660 8 pos += set_fg_color(buf + pos, style.fg);
661 8 pos += set_bg_color(buf + pos, style.bg);
662 8 buf[pos++] = 'm';
663 8 BUG_ON(pos > maxlen);
664 8 term->obuf.count += pos;
665 8 term->obuf.style = style;
666 8 }
667
668 2 static void cursor_style_normalize(TermCursorStyle *s)
669 {
670 2 BUG_ON(!cursor_type_is_valid(s->type));
671 2 BUG_ON(!cursor_color_is_valid(s->color));
672
1/2
✓ Branch 0 (6→7) taken 2 times.
✗ Branch 1 (6→8) not taken.
2 s->type = (s->type == CURSOR_KEEP) ? CURSOR_DEFAULT : s->type;
673
1/2
✓ Branch 0 (8→9) taken 2 times.
✗ Branch 1 (8→10) not taken.
2 s->color = (s->color == COLOR_KEEP) ? COLOR_DEFAULT : s->color;
674 2 }
675
676 2 void term_set_cursor_style(Terminal *term, TermCursorStyle s)
677 {
678 2 TermOutputBuffer *obuf = &term->obuf;
679 2 cursor_style_normalize(&s);
680 2 obuf->cursor_style = s;
681
682 2 const size_t maxlen = STRLEN("E[7 qE]12;rgb:aa/bb/ccST");
683 2 char *buf = term_output_reserve_space(obuf, maxlen);
684
685 // Set shape with DECSCUSR
686 2 BUG_ON(s.type < 0 || s.type > 9);
687 2 const char seq[] = {'\033', '[', '0' + s.type, ' ', 'q'};
688 2 size_t i = copystrn(buf, seq, sizeof(seq));
689
690
2/2
✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→10) taken 1 times.
2 if (s.color == COLOR_DEFAULT) {
691 // Reset color with OSC 112
692 1 i += copyliteral(buf + i, "\033]112");
693 } else {
694 // Set RGB color with OSC 12
695 1 i += copyliteral(buf + i, "\033]12;rgb:");
696 1 i += hex_encode_byte(buf + i, color_r(s.color));
697 1 buf[i++] = '/';
698 1 i += hex_encode_byte(buf + i, color_g(s.color));
699 1 buf[i++] = '/';
700 1 i += hex_encode_byte(buf + i, color_b(s.color));
701 }
702
703 2 i += copyliteral(buf + i, "\033\\"); // String Terminator (ST)
704 2 BUG_ON(i > maxlen);
705 2 obuf->count += i;
706 2 }
707