skip to content
==> include/tui/table.h <==
1/* @ajax/tui-table: ratatui's Table, Row, Cell and TableState. Column widths come from a Layout of
2 * the width constraints (Flex::Start, column_spacing between); rows scroll to keep the selected
3 * one visible; the highlight symbol takes a column of its own while something is selected. */
4#ifndef TUI_TABLE_H
5#define TUI_TABLE_H
6
7#include "tui/block.h"
8#include "tui/layout.h"
9
10typedef struct {
11    tui_text content;
12    tui_style style;
13    uint16_t column_span; /* 1 */
14} tui_table_cell;
15
16typedef struct {
17    tui_table_cell *cells;
18    size_t n, cap;
19    uint16_t height; /* 1 */
20    uint16_t top_margin, bottom_margin;
21    tui_style style;
22} tui_table_row;
23
24enum { TUI_HIGHLIGHT_ALWAYS, TUI_HIGHLIGHT_WHEN_SELECTED, TUI_HIGHLIGHT_NEVER };
25
26typedef struct {
27    const tui_table_row *rows;
28    size_t nrows;
29    const tui_table_row *header, *footer; /* optional */
30    const tui_constraint *widths;
31    size_t nwidths;
32    uint16_t column_spacing; /* 1 */
33    const tui_block *block;
34    tui_style style, row_highlight_style, column_highlight_style, cell_highlight_style;
35    tui_text highlight_symbol;
36    int highlight_spacing; /* TUI_HIGHLIGHT_WHEN_SELECTED */
37    tui_flex flex;         /* TUI_FLEX_START */
38} tui_table;
39
40typedef struct {
41    size_t offset;
42    bool has_selected, has_selected_column;
43    size_t selected, selected_column;
44} tui_table_state;
45
46static inline tui_table_cell tui_table_cell_text(tui_text t) { return (tui_table_cell){.content = t, .column_span = 1}; }
47static inline tui_table_row tui_table_row_new(void) {
48    tui_table_row r = {0};
49    r.height = 1;
50    return r;
51}
52void tui_table_row_push(tui_arena *a, tui_table_row *r, tui_table_cell c);
53static inline tui_table tui_table_new(const tui_table_row *rows, size_t nrows, const tui_constraint *widths, size_t nwidths) {
54    tui_table t = {0};
55    t.rows = rows;
56    t.nrows = nrows;
57    t.widths = widths;
58    t.nwidths = nwidths;
59    t.column_spacing = 1;
60    t.highlight_symbol = tui_text_empty();
61    t.highlight_spacing = TUI_HIGHLIGHT_WHEN_SELECTED;
62    t.flex = TUI_FLEX_START;
63    return t;
64}
65
66void tui_table_render(tui_arena *a, const tui_table *t, tui_rect area, tui_buffer *buf, tui_table_state *state);
67
68#endif