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 File Reference

Go to the source code of this file.

Macros

#define UI_HELP_MENU_KEY_BUFFER_SIZE   96
#define UI_HELP_MENU_TEXT_COUNT   64
#define UI_HELP_MENU_LHS_BUFFER_SIZE   1024
#define UI_HELP_MENU_RHS_BUFFER_SIZE   1024
#define UI_HELP_MENU_PADDED_BUFFER_SIZE   2048
#define UI_HELP_MENU_KEY_EXTENDED_BUFFER_SIZE   256
#define UI_HELP_MENU_DESCRIPTION_BUFFER_SIZE   768
#define UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE   512
#define UI_HELP_MENU_BINDING_BUFFER_SIZE   64
#define UI_HELP_MENU_STYLED_BUFFER_SIZE   512
#define UI_HELP_MENU_VALUES_BUFFER_SIZE   512
#define HELP_MENU_TEXT_LINE_BUFFER_SIZE   2048

Functions

void ui_help_keys (char *out, size_t cap, const char *keys)
terminal_size_t ui_help_menu_measure (const ui_help_menu_t *m)
bool ui_help_menu_render (frame_buffer_t *out, terminal_size_t size, const ui_help_menu_t *m)

Macro Definition Documentation

◆ HELP_MENU_TEXT_LINE_BUFFER_SIZE

#define HELP_MENU_TEXT_LINE_BUFFER_SIZE   2048

Definition at line 16 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_BINDING_BUFFER_SIZE

#define UI_HELP_MENU_BINDING_BUFFER_SIZE   64

Definition at line 11 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_DESCRIPTION_BUFFER_SIZE

#define UI_HELP_MENU_DESCRIPTION_BUFFER_SIZE   768

Definition at line 9 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_KEY_BUFFER_SIZE

#define UI_HELP_MENU_KEY_BUFFER_SIZE   96

Definition at line 3 of file help_menu.c.

Referenced by ui_help_keys().

◆ UI_HELP_MENU_KEY_EXTENDED_BUFFER_SIZE

#define UI_HELP_MENU_KEY_EXTENDED_BUFFER_SIZE   256

Definition at line 8 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_LHS_BUFFER_SIZE

#define UI_HELP_MENU_LHS_BUFFER_SIZE   1024

Definition at line 5 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_PADDED_BUFFER_SIZE

#define UI_HELP_MENU_PADDED_BUFFER_SIZE   2048

Definition at line 7 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_RHS_BUFFER_SIZE

#define UI_HELP_MENU_RHS_BUFFER_SIZE   1024

Definition at line 6 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_STYLED_BUFFER_SIZE

#define UI_HELP_MENU_STYLED_BUFFER_SIZE   512

Definition at line 12 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE

#define UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE   512

Definition at line 10 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_TEXT_COUNT

#define UI_HELP_MENU_TEXT_COUNT   64

Definition at line 4 of file help_menu.c.

Referenced by ui_help_menu_render().

◆ UI_HELP_MENU_VALUES_BUFFER_SIZE

#define UI_HELP_MENU_VALUES_BUFFER_SIZE   512

Definition at line 13 of file help_menu.c.

Referenced by ui_help_menu_render().

Function Documentation

◆ ui_help_keys()

void ui_help_keys ( char * output,
size_t capacity,
const char * keys )

Key tokens are yellow; spaces, slashes, parentheses and pipes stay uncolored.

Definition at line 25 of file help_menu.c.

25 {
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}
#define SET_ERRNO(code,...)
@ LOG_COLOR_WARN
Definition color.h:10
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
#define UI_HELP_MENU_KEY_BUFFER_SIZE
Definition help_menu.c:3
static bool size_t gotel_query_t * out
Definition queries.h:10

References colored_string(), ERROR_BUFFER_OVERFLOW, ERROR_INVALID_PARAM, LOG_COLOR_WARN, out, SET_ERRNO, and UI_HELP_MENU_KEY_BUFFER_SIZE.

Referenced by terminal_screen_logs_render_help(), and ui_help_menu_render().

◆ ui_help_menu_measure()

terminal_size_t ui_help_menu_measure ( const ui_help_menu_t * m)

Definition at line 86 of file help_menu.c.

86 {
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}
unsigned int width
Definition fft.c:24
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
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

References ui_help_menu_t::count, display_width(), ui_help_menu_t::entries, ERROR_INVALID_PARAM, ui_help_menu_t::footer, SET_ERRNO, ui_help_menu_t::settings, ui_help_menu_t::settings_count, ui_help_menu_t::title, ui_help_menu_t::top_margin, and width.

Referenced by ui_session_context_help_render(), and ui_session_help_render().

◆ ui_help_menu_render()

bool ui_help_menu_render ( frame_buffer_t * out,
terminal_size_t size,
const ui_help_menu_t * m )

Definition at line 103 of file help_menu.c.

104 {
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}
@ LOG_COLOR_FATAL
Definition color.h:12
#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
#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_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
bool ui_popup_render(frame_buffer_t *output, terminal_size_t size, const ui_popup_t *box)
Definition popup.c:80
Definition help_menu.h:7
const char * keys
Definition help_menu.h:8
const char * description
Definition help_menu.h:9
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

References ui_help_menu_t::count, ui_help_entry_t::description, ui_help_menu_t::entries, ERROR_INVALID_PARAM, ui_help_menu_t::footer, HELP_MENU_TEXT_LINE_BUFFER_SIZE, ui_help_entry_t::keys, LOG_COLOR_FATAL, out, SET_ERRNO, ui_help_menu_t::settings, ui_help_menu_t::settings_count, ui_help_menu_t::title, ui_help_menu_t::top_margin, UI_ALIGN_LEFT, ui_help_keys(), UI_HELP_MENU_BINDING_BUFFER_SIZE, UI_HELP_MENU_DESCRIPTION_BUFFER_SIZE, UI_HELP_MENU_KEY_EXTENDED_BUFFER_SIZE, UI_HELP_MENU_LHS_BUFFER_SIZE, UI_HELP_MENU_PADDED_BUFFER_SIZE, UI_HELP_MENU_RHS_BUFFER_SIZE, UI_HELP_MENU_STYLED_BUFFER_SIZE, UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE, UI_HELP_MENU_TEXT_COUNT, UI_HELP_MENU_VALUES_BUFFER_SIZE, ui_popup_render(), ui_text_cell(), and ui_text_values_style().

Referenced by terminal_screen_logs_render_help(), ui_session_context_help_render(), and ui_session_help_render().