ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
help_menu.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define UI_HELP_MENU_KEY_BUFFER_SIZE 96
4#define UI_HELP_MENU_TEXT_COUNT 64
5#define UI_HELP_MENU_LHS_BUFFER_SIZE 1024
6#define UI_HELP_MENU_RHS_BUFFER_SIZE 1024
7#define UI_HELP_MENU_PADDED_BUFFER_SIZE 2048
8#define UI_HELP_MENU_KEY_EXTENDED_BUFFER_SIZE 256
9#define UI_HELP_MENU_DESCRIPTION_BUFFER_SIZE 768
10#define UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE 512
11#define UI_HELP_MENU_BINDING_BUFFER_SIZE 64
12#define UI_HELP_MENU_STYLED_BUFFER_SIZE 512
13#define UI_HELP_MENU_VALUES_BUFFER_SIZE 512
14
15/* Existing buffer budgets in bytes, including NUL for text storage. */
16#define HELP_MENU_TEXT_LINE_BUFFER_SIZE 2048
17
19#include <ascii-chat/ui/text.h>
22#include <stdio.h>
23#include <string.h>
24
25void ui_help_keys(char *out, size_t cap, const char *keys) {
26 if (!out || !cap) {
27 SET_ERRNO(ERROR_INVALID_PARAM, "Help keys require output storage");
28 return;
29 }
30 size_t used = 0;
31 out[0] = '\0';
32 if (!keys)
33 return;
34 for (const char *p = keys; *p;) {
35 if (used + 1 >= cap) {
36 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Help key output buffer is too small");
37 return;
38 }
39 if (*p == ' ' || *p == '/' || *p == '|' || *p == '(' || *p == ')') {
40 out[used++] = *p++;
41 out[used] = '\0';
42 } else {
43 const char *start = p;
44 while (*p && *p != ' ' && *p != '/' && *p != '|' && *p != '(' &&
45 *p != ')')
46 ++p;
48 if ((size_t)(p - start) >= sizeof(key)) {
49 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Help key token is too long");
50 return;
51 }
52 snprintf(key, sizeof(key), "%.*s", (int)(p - start), start);
53 const char *styled = colored_string(LOG_COLOR_WARN, key);
54 size_t length = strlen(styled);
55 if (length >= cap - used) {
56 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Help key output buffer is too small");
57 return;
58 }
59 memcpy(out + used, styled, length + 1);
60 used += length;
61 }
62 }
63}
64
65static void measure(const ui_help_menu_t *m, int *keys, int *left, int *right) {
66 *keys = *left = *right = 0;
67 for (size_t i = 0; i < m->count; ++i) {
68 int k = m->entries[i].keys ? display_width(m->entries[i].keys) : 0;
69 if (k > *keys)
70 *keys = k;
71 }
72 for (size_t i = 0; i < m->count; ++i) {
73 int w = display_width(m->entries[i].description);
74 if (m->entries[i].keys)
75 w += *keys + 2;
76 if (w > *left)
77 *left = w;
78 }
79 for (size_t i = 0; i < m->settings_count; ++i) {
80 int w = display_width(m->settings[i]);
81 if (w > *right)
82 *right = w;
83 }
84}
85
86terminal_size_t ui_help_menu_measure(const ui_help_menu_t *m) {
87 if (!m || (m->count && !m->entries) || (m->settings_count && !m->settings)) {
88 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid help menu entries or settings");
89 return (terminal_size_t){0};
90 }
91 int keys, left, right;
92 measure(m, &keys, &left, &right);
93 int width = left + (right ? right + 3 : 0);
94 if (m->title && display_width(m->title) > width)
96 if (m->footer && display_width(m->footer) > width)
98 size_t rows = m->count > m->settings_count ? m->count : m->settings_count;
99 return (terminal_size_t){.cols = width + 6,
100 .rows = (int)rows + 8 + m->top_margin};
101}
102
103bool ui_help_menu_render(frame_buffer_t *out, terminal_size_t size,
104 const ui_help_menu_t *m) {
105 if (!out || !m || (m->count && !m->entries) ||
106 (m->settings_count && !m->settings) || m->count > 62 ||
107 m->settings_count > 62) {
108 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid help output or entry counts");
109 return false;
110 }
112 const char *lines[UI_HELP_MENU_TEXT_COUNT];
113 size_t rows = m->count > m->settings_count ? m->count : m->settings_count;
114 int keys, left, right;
115 measure(m, &keys, &left, &right);
116 lines[0] = "";
117 for (size_t i = 0; i < rows; ++i) {
118 char lhs[UI_HELP_MENU_LHS_BUFFER_SIZE] = "",
121 if (i < m->count) {
122 const ui_help_entry_t *entry = &m->entries[i];
123 if (!entry->keys)
124 snprintf(lhs, sizeof(lhs), "%s", entry->description);
125 else {
128 char styled_keys[UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE];
129 ui_help_keys(styled_keys, sizeof(styled_keys), entry->keys);
130 ui_text_cell(key, sizeof(key), styled_keys, keys, UI_ALIGN_LEFT);
131 ui_text_values_style(description, sizeof(description),
133 const int field_width = (int)((sizeof(lhs) - 3) / 2);
134 snprintf(lhs, sizeof(lhs), "%.*s %.*s", field_width, key, field_width,
135 description);
136 }
137 }
138 if (i < m->settings_count) {
139 const char *setting = m->settings[i];
140 const char *end = setting[0] == '(' ? strchr(setting, ')') : NULL;
141 if (end) {
144 snprintf(binding, sizeof(binding), "%.*s", (int)(end + 1 - setting),
145 setting);
146 ui_help_keys(styled, sizeof(styled), binding);
148 ui_text_values_style(values, sizeof(values), end + 1, LOG_COLOR_FATAL);
149 size_t rhs_pos = 0;
150 size_t styled_len = strlen(styled);
151 size_t styled_copy =
152 styled_len < sizeof(rhs) - 1 ? styled_len : sizeof(rhs) - 1;
153 memcpy(rhs, styled, styled_copy);
154 rhs_pos += styled_copy;
155 size_t values_len = strlen(values);
156 size_t values_copy = values_len < sizeof(rhs) - 1 - rhs_pos
157 ? values_len
158 : sizeof(rhs) - 1 - rhs_pos;
159 memcpy(rhs + rhs_pos, values, values_copy);
160 rhs[rhs_pos + values_copy] = '\0';
161 } else
162 ui_text_values_style(rhs, sizeof(rhs), setting, LOG_COLOR_FATAL);
163 }
164 ui_text_cell(padded, sizeof(padded), lhs, left, UI_ALIGN_LEFT);
165 size_t text_pos = 0;
166 size_t padded_copy = strlen(padded);
167 if (padded_copy >= sizeof(text[i]))
168 padded_copy = sizeof(text[i]) - 1;
169 memcpy(text[i] + text_pos, padded, padded_copy);
170 text_pos += padded_copy;
171 if (right && text_pos + 3 < sizeof(text[i])) {
172 memcpy(text[i] + text_pos, " ", 3);
173 text_pos += 3;
174 }
175 size_t rhs_copy = strlen(rhs);
176 if (rhs_copy > sizeof(text[i]) - text_pos - 1)
177 rhs_copy = sizeof(text[i]) - text_pos - 1;
178 memcpy(text[i] + text_pos, rhs, rhs_copy);
179 text[i][text_pos + rhs_copy] = '\0';
180 lines[i + 1] = text[i];
181 }
182 ui_popup_t box = {.title = m->title,
183 .lines = lines,
184 .line_count = rows + 1,
185 .footer = m->footer,
186 .top_margin = m->top_margin};
187 return ui_popup_render(out, size, &box);
188}
#define SET_ERRNO(code,...)
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_WARN
Definition color.h:10
unsigned int width
Definition fft.c:24
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
#define UI_HELP_MENU_LHS_BUFFER_SIZE
Definition help_menu.c:5
void ui_help_keys(char *out, size_t cap, const char *keys)
Definition help_menu.c:25
#define UI_HELP_MENU_TEXT_COUNT
Definition help_menu.c:4
#define UI_HELP_MENU_KEY_EXTENDED_BUFFER_SIZE
Definition help_menu.c:8
#define UI_HELP_MENU_STYLED_BUFFER_SIZE
Definition help_menu.c:12
terminal_size_t ui_help_menu_measure(const ui_help_menu_t *m)
Definition help_menu.c:86
#define UI_HELP_MENU_BINDING_BUFFER_SIZE
Definition help_menu.c:11
#define HELP_MENU_TEXT_LINE_BUFFER_SIZE
Definition help_menu.c:16
#define UI_HELP_MENU_KEY_BUFFER_SIZE
Definition help_menu.c:3
bool ui_help_menu_render(frame_buffer_t *out, terminal_size_t size, const ui_help_menu_t *m)
Definition help_menu.c:103
#define UI_HELP_MENU_PADDED_BUFFER_SIZE
Definition help_menu.c:7
#define UI_HELP_MENU_VALUES_BUFFER_SIZE
Definition help_menu.c:13
#define UI_HELP_MENU_RHS_BUFFER_SIZE
Definition help_menu.c:6
#define UI_HELP_MENU_DESCRIPTION_BUFFER_SIZE
Definition help_menu.c:9
#define UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE
Definition help_menu.c:10
🖥️ Display Width and Terminal Centering Utilities
bool ui_popup_render(frame_buffer_t *output, terminal_size_t size, const ui_popup_t *box)
Definition popup.c:80
static bool size_t gotel_query_t * out
Definition queries.h:10
🔤 String Manipulation and Shell Escaping Utilities
Definition help_menu.h:7
const char * keys
Definition help_menu.h:8
const char * description
Definition help_menu.h:9
ui help entry keeps the values that move through the subsystem: keys, description....
Definition help_menu.h:19
const char * footer
Definition help_menu.h:25
const char * title
Definition help_menu.h:20
size_t settings_count
Definition help_menu.h:24
const ui_help_entry_t * entries
Definition help_menu.h:21
const char *const * settings
Definition help_menu.h:23
When code needs to work with ui popup, it keeps title, lines, line_count, footer here instead of scat...
Definition popup.h:42
void ui_text_values_style(char *out, size_t cap, const char *text, log_color_t zero_color)
Definition text.c:101
size_t ui_text_cell(char *out, size_t cap, const char *text, int width, ui_alignment_t align)
Definition text.c:51
@ UI_ALIGN_LEFT
Definition text.h:14