==> tests/buffer.c <==1/* Buffer::set_stringn's rules from ratatui's tests: truncation at the edge and at max_width, wide
2 * graphemes resetting the cell they cover, control characters skipped, styles patched. */
3#include "mesh/check.h"
4#include "tui/buffer.h"
5
6#include <string.h>
7
8static const char *row(const tui_buffer *b, uint16_t y) {
9 static char out[256];
10 size_t n = 0;
11 for (uint16_t x = 0; x < b->area.width; x++) {
12 const char *s = tui_cell_symbol(tui_buffer_cell_c(b, x, y));
13 size_t k = strlen(s);
14 memcpy(out + n, s, k);
15 n += k;
16 }
17 out[n] = 0;
18 return out;
19}
20
21int main(void) {
22 tui_buffer b;
23 tui_buffer_init(&b, (tui_rect){0, 0, 5, 2});
24 CHECK_S(row(&b, 0), " ");
25 tui_buffer_set_string(&b, 0, 0, "hello world", tui_fg(TUI_RED));
26 CHECK_S(row(&b, 0), "hello");
27 CHECK(tui_color_eq(tui_buffer_cell(&b, 4, 0)->fg, TUI_RED));
28 tui_buffer_set_stringn(&b, 0, 1, "abcdef", 6, 2, tui_style_new());
29 CHECK_S(row(&b, 1), "ab ");
30 /* A wide grapheme that doesn't fit is not written. */
31 tui_buffer_set_string(&b, 3, 1, "x\xE6\x97\xA5", tui_style_new());
32 CHECK_S(row(&b, 1), "ab x ");
33 /* A wide grapheme resets the cell it covers. */
34 tui_buffer_set_string(&b, 0, 1, "\xE6\x97\xA5", tui_style_new());
35 CHECK_S(tui_cell_symbol(tui_buffer_cell(&b, 0, 1)), "\xE6\x97\xA5");
36 CHECK(tui_buffer_cell(&b, 1, 1)->none);
37 /* Control characters vanish; zero-width graphemes too. */
38 tui_buffer_clear(&b, b.area);
39 uint16_t x = tui_buffer_set_string(&b, 0, 0, "a\tb\n", tui_style_new());
40 CHECK_U(x, 2);
41 CHECK_S(row(&b, 0), "ab ");
42 /* set_style patches. */
43 tui_buffer_set_style(&b, (tui_rect){1, 0, 10, 1}, tui_style_add(tui_fg(TUI_BLUE), TUI_BOLD));
44 CHECK(tui_color_eq(tui_buffer_cell(&b, 1, 0)->fg, TUI_BLUE));
45 CHECK_U(tui_buffer_cell(&b, 1, 0)->mod, TUI_BOLD);
46 CHECK(tui_color_eq(tui_buffer_cell(&b, 0, 0)->fg, TUI_RESET));
47 CHECK(tui_buffer_cell(&b, 5, 0) == NULL);
48 CHECK_U(tui_symbol_width("\xEF\xBD\xB1\xEF\xBE\x9E", 6), 2);
49 tui_cell c = TUI_CELL_EMPTY;
50 tui_cell_append_symbol(&c, "\xCC\x81", 2);
51 CHECK_S(tui_cell_symbol(&c), "\xCC\x81");
52 tui_buffer_free(&b);
53 return mesh_check_done();
54}