skip to content
==> tests/block.c <==
1/* Blocks as ratatui draws them: rounded borders, a title over the top edge, inner areas. */
2#include "mesh/check.h"
3#include "tui/block.h"
4
5#include <string.h>
6
7static const char *row(const tui_buffer *b, uint16_t y) {
8    static char out[512];
9    size_t n = 0;
10    for (uint16_t x = 0; x < b->area.width; x++) {
11        const char *s = tui_cell_symbol(tui_buffer_cell_c(b, x, y));
12        size_t k = strlen(s);
13        memcpy(out + n, s, k);
14        n += k;
15    }
16    out[n] = 0;
17    return out;
18}
19
20int main(void) {
21    tui_arena a = {0};
22    tui_buffer buf;
23    tui_buffer_init(&buf, (tui_rect){0, 0, 12, 3});
24    tui_block b = tui_block_bordered();
25    b.set = &TUI_BORDER_ROUNDED;
26    b.border_style = tui_fg(TUI_BLUE);
27    tui_block_add_title(&a, &b, tui_line_from_str(&a, " hi "), TUI_TITLE_DEFAULT);
28    tui_block_render(&a, &b, buf.area, &buf);
29    CHECK_S(row(&buf, 0), "╭ hi ──────╮");
30    CHECK_S(row(&buf, 1), "│          │");
31    CHECK_S(row(&buf, 2), "╰──────────╯");
32    CHECK(tui_color_eq(tui_buffer_cell(&buf, 0, 0)->fg, TUI_BLUE));
33    CHECK(tui_color_eq(tui_buffer_cell(&buf, 1, 0)->fg, TUI_BLUE)); /* an unstyled title keeps the border colour under it */
34    CHECK(tui_rect_eq(tui_block_inner(&b, buf.area), (tui_rect){1, 1, 10, 1}));
35    /* A title alone still takes the top row. */
36    tui_block t = tui_block_new();
37    tui_block_add_title(&a, &t, tui_line_from_str(&a, "x"), TUI_TITLE_DEFAULT);
38    CHECK(tui_rect_eq(tui_block_inner(&t, (tui_rect){0, 0, 5, 5}), (tui_rect){0, 1, 5, 4}));
39    /* Right and centred titles. */
40    tui_buffer_clear(&buf, buf.area);
41    tui_block r = tui_block_bordered();
42    tui_line rt = tui_line_from_str(&a, "R");
43    rt.align = TUI_ALIGN_RIGHT;
44    tui_line ct = tui_line_from_str(&a, "C");
45    ct.align = TUI_ALIGN_CENTER;
46    tui_block_add_title(&a, &r, rt, TUI_TITLE_DEFAULT);
47    tui_block_add_title(&a, &r, ct, TUI_TITLE_BOTTOM);
48    tui_block_render(&a, &r, buf.area, &buf);
49    CHECK_S(row(&buf, 0), "┌─────────R┐");
50    CHECK_S(row(&buf, 2), "└────C─────┘");
51    tui_buffer_free(&buf);
52    tui_arena_free(&a);
53    return mesh_check_done();
54}