skip to content
==> tests/paragraph.c <==
1/* Paragraphs as ratatui's reflow tests describe them: word wrapping with and without trimming,
2 * long words broken, truncation, alignment, scrolling, a block around it. */
3#include "mesh/check.h"
4#include "tui/paragraph.h"
5
6#include <string.h>
7
8static const char *row(const tui_buffer *b, uint16_t y) {
9    static char out[512];
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
21static void draw(tui_arena *a, tui_buffer *b, const char *text, int wrap, int8_t align, uint16_t scroll) {
22    tui_buffer_clear(b, b->area);
23    tui_paragraph p = tui_paragraph_new(tui_text_from_str(a, text));
24    p.wrap = wrap;
25    p.alignment = align;
26    p.scroll_y = scroll;
27    tui_paragraph_render(a, &p, b->area, b);
28}
29
30int main(void) {
31    tui_arena a = {0};
32    tui_buffer b;
33    tui_buffer_init(&b, (tui_rect){0, 0, 10, 4});
34    draw(&a, &b, "The quick brown fox jumps", TUI_WRAP_TRIM, TUI_ALIGN_LEFT, 0);
35    CHECK_S(row(&b, 0), "The quick ");
36    CHECK_S(row(&b, 1), "brown fox ");
37    CHECK_S(row(&b, 2), "jumps     ");
38    draw(&a, &b, "abcdefghijklmnop", TUI_WRAP_TRIM, TUI_ALIGN_LEFT, 0);
39    CHECK_S(row(&b, 0), "abcdefghij");
40    CHECK_S(row(&b, 1), "klmnop    ");
41    draw(&a, &b, "  indented words here", TUI_WRAP_KEEP, TUI_ALIGN_LEFT, 0);
42    CHECK_S(row(&b, 0), "  indented");
43    draw(&a, &b, "  indented words here", TUI_WRAP_TRIM, TUI_ALIGN_LEFT, 0); /* trim drops the indent */
44    CHECK_S(row(&b, 0), "indented  ");
45    CHECK_S(row(&b, 1), "words here");
46    draw(&a, &b, "a long line cut here\nsecond", TUI_WRAP_NONE, TUI_ALIGN_LEFT, 0);
47    CHECK_S(row(&b, 0), "a long lin");
48    CHECK_S(row(&b, 1), "second    ");
49    draw(&a, &b, "mid", TUI_WRAP_NONE, TUI_ALIGN_CENTER, 0);
50    CHECK_S(row(&b, 0), "    mid   ");
51    draw(&a, &b, "r", TUI_WRAP_NONE, TUI_ALIGN_RIGHT, 0);
52    CHECK_S(row(&b, 0), "         r");
53    draw(&a, &b, "one\ntwo\nthree", TUI_WRAP_TRIM, TUI_ALIGN_LEFT, 1);
54    CHECK_S(row(&b, 0), "two       ");
55    /* In a block. */
56    tui_buffer_clear(&b, b.area);
57    tui_block blk = tui_block_bordered();
58    tui_paragraph p = tui_paragraph_new(tui_text_from_str(&a, "inside the box"));
59    p.block = &blk;
60    p.wrap = TUI_WRAP_TRIM;
61    tui_paragraph_render(&a, &p, b.area, &b);
62    CHECK_S(row(&b, 0), "┌────────┐");
63    CHECK_S(row(&b, 1), "│inside  │");
64    CHECK_S(row(&b, 2), "│the box │");
65    tui_buffer_free(&b);
66    tui_arena_free(&a);
67    return mesh_check_done();
68}