skip to content
==> include/tui/buffer.h <==
1/* @ajax/tui-buffer: ratatui's Buffer and Cell. Widgets draw into a buffer; the terminal shows it.
2 * A cell holds one grapheme cluster (or nothing: shown as a space), a foreground, a background
3 * and modifiers. A wide grapheme occupies its cell and resets the ones it covers. */
4#ifndef TUI_BUFFER_H
5#define TUI_BUFFER_H
6
7#include "tui/rect.h"
8#include "tui/style.h"
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13
14#define TUI_SYM_MAX 32 /* a grapheme longer than this is cut (never in practice) */
15
16typedef struct {
17    char sym[TUI_SYM_MAX]; /* NUL-terminated; ignored while none */
18    bool none;             /* no symbol yet: reads as " " */
19    tui_color fg, bg;
20    uint16_t mod;
21} tui_cell;
22
23typedef struct {
24    tui_rect area;
25    tui_cell *cells; /* area.width * area.height, row-major */
26} tui_buffer;
27
28extern const tui_cell TUI_CELL_EMPTY;
29
30const char *tui_cell_symbol(const tui_cell *c);
31void tui_cell_set_symbol(tui_cell *c, const char *s, size_t len);
32void tui_cell_set_str(tui_cell *c, const char *s); /* NUL-terminated */
33void tui_cell_append_symbol(tui_cell *c, const char *s, size_t len);
34void tui_cell_set_style(tui_cell *c, tui_style st);
35void tui_cell_reset(tui_cell *c);
36/* The cell's whole look as a style (Cell::style). */
37tui_style tui_cell_style(const tui_cell *c);
38
39/* The columns a grapheme takes (ratatui's CellWidth for str): 1 for any one-byte grapheme, else its
40 * unicode width plus one per halfwidth (semi-)voiced sound mark. */
41uint16_t tui_symbol_width(const char *s, size_t len);
42/* Whether a grapheme holds a control character (char::is_control: Cc). */
43bool tui_symbol_has_control(const char *s, size_t len);
44
45void tui_buffer_init(tui_buffer *b, tui_rect area); /* every cell empty */
46void tui_buffer_free(tui_buffer *b);
47/* The cell at (x, y), or NULL outside the buffer. */
48tui_cell *tui_buffer_cell(tui_buffer *b, uint16_t x, uint16_t y);
49const tui_cell *tui_buffer_cell_c(const tui_buffer *b, uint16_t x, uint16_t y);
50/* Buffer::set_stringn: writes s's graphemes from (x, y) while they fit in max_width and the
51 * buffer; control graphemes and zero-width ones are skipped. Returns the x after the last. */
52uint16_t tui_buffer_set_stringn(tui_buffer *b, uint16_t x, uint16_t y, const char *s, size_t len, size_t max_width,
53                                tui_style st);
54uint16_t tui_buffer_set_string(tui_buffer *b, uint16_t x, uint16_t y, const char *s, tui_style st);
55/* Buffer::set_style: patches every cell of area (cut to the buffer). */
56void tui_buffer_set_style(tui_buffer *b, tui_rect area, tui_style st);
57/* The Clear widget: resets every cell of area. */
58void tui_buffer_clear(tui_buffer *b, tui_rect area);
59
60#endif