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.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define UI_TABLE_CLIPPED_BUFFER_SIZE 8192
4#define UI_TABLE_LINE_BUFFER_SIZE 8192
5
6/* Existing buffer budgets in bytes, including NUL for text storage. */
7#define UI_TABLE_CELL_CAPACITY 32
8
10#include <ascii-chat/ui/table.h>
13#include <options/options.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <string.h>
17
18size_t ui_table_row(char *out, size_t cap, const ui_table_column_t *cols,
19 const char *const *cells, size_t count, int width,
20 bool selectable, bool selected) {
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}
80
81void ui_table_render(frame_buffer_t *out, ui_table_t *t, int row, int col,
82 int width, int height) {
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 =
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}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#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.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
@ ERROR_INVALID_PARAM
#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.
🖥️ Display Width and Terminal Centering Utilities
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
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
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
const char * heading
Definition table.h:10
The useful part of ui table column is in heading, width, alignment, ellipsis: together they describe ...
Definition table.h:35
bool header
Definition table.h:41
const ui_table_column_t * columns
Definition table.h:36
size_t row_count
Definition table.h:37
size_t column_count
Definition table.h:37
bool selectable
Definition table.h:41
ui_viewport_t viewport
Definition table.h:38
void * data
Definition table.h:40
ui_table_cells_fn cells
Definition table.h:39
size_t first_visible
Definition viewport.h:21
size_t selected
Definition viewport.h:21
void ui_table_render(frame_buffer_t *out, ui_table_t *t, int row, int col, int width, int height)
Definition table.c:81
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
#define UI_TABLE_CLIPPED_BUFFER_SIZE
Definition table.c:3
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
size_t ui_text_cell(char *out, size_t cap, const char *text, int width, ui_alignment_t align)
Definition text.c:51
ui_viewport_range_t ui_viewport_update(ui_viewport_t *v, size_t count, size_t rows)
Definition viewport.c:4