ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
table.h File Reference

Go to the source code of this file.

Data Structures

struct  ui_table_column_t
struct  ui_table_t
 The useful part of ui table column is in heading, width, alignment, ellipsis: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path. More...

Typedefs

typedef void(* ui_table_cells_fn) (size_t row, const char **cells, size_t columns, void *data)

Functions

size_t ui_table_row (char *out, size_t capacity, const ui_table_column_t *columns, const char *const *cells, size_t count, int width, bool selectable, bool selected)
 When code needs to work with ui table, it keeps columns, column_count, row_count, viewport here instead of scattering it across globals. Initialization establishes the handles and sizes; teardown must release any allocation, lock, thread, or callback context recorded in those fields. terminal columns, never by bytes. The caller owns data and sorting.
void ui_table_render (frame_buffer_t *out, ui_table_t *table, int row, int column, int width, int height)

Typedef Documentation

◆ ui_table_cells_fn

typedef void(* ui_table_cells_fn) (size_t row, const char **cells, size_t columns, void *data)

Definition at line 27 of file table.h.

Function Documentation

◆ ui_table_render()

void ui_table_render ( frame_buffer_t * out,
ui_table_t * table,
int row,
int column,
int width,
int height )

Rectangular table with a fixed header and independent page/scroll state.

Definition at line 81 of file table.c.

82 {
83 if (!out || !t || !t->columns || !t->column_count || t->column_count > 32 ||
84 !t->cells || width < 2 || height < 1 || row < 1 || col < 1) {
86 "Invalid table output, data source or geometry");
87 return;
88 }
90 if (t->header) {
91 ui_table_row(line, sizeof(line), t->columns, NULL, t->column_count,
92 width - 1, t->selectable, false);
93 frame_buffer_printf(out, "\033[%d;%dH%s%s%s", row++, col,
96 --height;
97 }
98 if (height < 1)
99 return;
100 ui_viewport_range_t range =
101 ui_viewport_update(&t->viewport, t->row_count, (size_t)height);
102 for (int i = 0; i < height; ++i) {
103 const char *cells[UI_TABLE_CELL_CAPACITY] = {0};
104 if ((size_t)i < range.count) {
105 t->cells(range.first + (size_t)i, cells, t->column_count, t->data);
106 ui_table_row(line, sizeof(line), t->columns, cells, t->column_count,
107 width - 1, t->selectable,
108 range.first + (size_t)i == t->viewport.selected);
109 } else
110 snprintf(line, sizeof(line), "%*s", width - 1, "");
111 bool selected = t->selectable && (size_t)i < range.count &&
112 range.first + (size_t)i == t->viewport.selected;
113 frame_buffer_printf(out, "\033[%d;%dH%s%s%s", row + i, col,
114 selected ? "\033[7m" : "", line,
115 selected ? "\033[27m" : "");
116 }
117 ui_scrollbar_render(out, row, col + width - 1, height, range.page_count,
118 (size_t)height, t->viewport.first_visible);
119}
#define SET_ERRNO(code,...)
@ LOG_COLOR_RESET
Definition color.h:14
@ LOG_COLOR_DEBUG
Definition color.h:8
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
@ ERROR_INVALID_PARAM
static bool size_t gotel_query_t * out
Definition queries.h:10
void ui_scrollbar_render(frame_buffer_t *out, int row, int col, int height, size_t total, size_t visible, size_t offset)
Definition scrollbar.c:46
size_t ui_table_row(char *out, size_t cap, const ui_table_column_t *cols, const char *const *cells, size_t count, int width, bool selectable, bool selected)
When code needs to work with ui table, it keeps columns, column_count, row_count, viewport here inste...
Definition table.c:18
#define UI_TABLE_LINE_BUFFER_SIZE
Definition table.c:4
#define UI_TABLE_CELL_CAPACITY
Definition table.c:7
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
ui_viewport_range_t ui_viewport_update(ui_viewport_t *v, size_t count, size_t rows)
Definition viewport.c:4

References cells, ui_table_t::cells, ui_table_t::column_count, ui_table_t::columns, ui_viewport_range_t::count, ui_table_t::data, ERROR_INVALID_PARAM, ui_viewport_range_t::first, ui_viewport_t::first_visible, frame_buffer_printf(), ui_table_t::header, height, LOG_COLOR_DEBUG, LOG_COLOR_RESET, out, ui_viewport_range_t::page_count, ui_table_t::row_count, ui_table_t::selectable, ui_viewport_t::selected, SET_ERRNO, ui_scrollbar_render(), UI_TABLE_CELL_CAPACITY, UI_TABLE_LINE_BUFFER_SIZE, ui_table_row(), ui_text_color_prefix(), ui_viewport_update(), ui_table_t::viewport, and width.

Referenced by tui_stats_render().

◆ ui_table_row()

size_t ui_table_row ( char * out,
size_t capacity,
const ui_table_column_t * columns,
const char *const * cells,
size_t count,
int width,
bool selectable,
bool selected )

When code needs to work with ui table, it keeps columns, column_count, row_count, viewport here instead of scattering it across globals. Initialization establishes the handles and sizes; teardown must release any allocation, lock, thread, or callback context recorded in those fields. terminal columns, never by bytes. The caller owns data and sorting.

Format a complete row without allocation. Styled cells are clipped by

Definition at line 18 of file table.c.

20 {
21 if (!out || !cap) {
22 SET_ERRNO(ERROR_INVALID_PARAM, "Table row requires output storage");
23 return 0;
24 }
25 out[0] = '\0';
26 if (!cols || !count || count > 32 || width <= 0) {
27 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid table columns or width");
28 return 0;
29 }
30 int64_t remaining = (int64_t)width - (selectable ? 1 : 0) - (int)(count - 1);
31 size_t flexible = 0;
32 for (size_t i = 0; i < count; ++i) {
33 if (cols[i].width < 0) {
34 SET_ERRNO(ERROR_INVALID_PARAM, "Table column width cannot be negative");
35 return 0;
36 }
37 if (cols[i].width > 0)
38 remaining -= cols[i].width;
39 else
40 ++flexible;
41 }
42 size_t used = 0;
43 int available = width;
44 if (selectable && cap > 1) {
45 out[used++] = selected ? '>' : ' ';
46 out[used] = '\0';
47 --available;
48 }
49 for (size_t i = 0; i < count && available > 0; ++i) {
50 if (i && used + 1 < cap) {
51 out[used++] = ' ';
52 out[used] = '\0';
53 --available;
54 }
55 int cell_width = cols[i].width > 0 ? cols[i].width
56 : flexible ? (int)(remaining / (int)flexible)
57 : 0;
58 if (cell_width < 0)
59 cell_width = 0;
60 if (!cols[i].width && flexible) {
61 remaining -= cell_width;
62 --flexible;
63 }
64 if (cell_width > available)
65 cell_width = available;
66 const char *text = cells ? cells[i] : cols[i].heading;
67 char clipped[UI_TABLE_CLIPPED_BUFFER_SIZE];
68 if (text && cols[i].ellipsis && display_width(text) > cell_width) {
70 text, clipped, sizeof(clipped), cell_width,
72 text = clipped;
73 }
74 used += ui_text_cell(out + used, cap - used, text, cell_width,
75 cols[i].alignment);
76 available -= cell_width;
77 }
78 return used;
79}
#define GET_OPTION(field)
Definition options.h:1339
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
void truncate_with_ellipsis_mode(const char *input, char *output, size_t output_size, int max_width, bool utf8)
Truncate a string to a maximum display width with a terminal-appropriate ellipsis.
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
#define UI_TABLE_CLIPPED_BUFFER_SIZE
Definition table.c:3
size_t ui_text_cell(char *out, size_t cap, const char *text, int width, ui_alignment_t align)
Definition text.c:51

References cells, display_width(), ERROR_INVALID_PARAM, GET_OPTION, ui_table_column_t::heading, out, SET_ERRNO, terminal_should_use_utf8(), truncate_with_ellipsis_mode(), UI_TABLE_CLIPPED_BUFFER_SIZE, ui_text_cell(), ui_table_column_t::width, and width.

Referenced by ui_sync_render(), and ui_table_render().