ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
help_menu.h File Reference

Go to the source code of this file.

Data Structures

struct  ui_help_entry_t
struct  ui_help_menu_t
 ui help entry keeps the values that move through the subsystem: Keys Module, description. Some members are pointers into caller- or subsystem-owned memory, so lifetime follows the API that populated them rather than the struct copy itself. More...

Functions

terminal_size_t ui_help_menu_measure (const ui_help_menu_t *menu)
bool ui_help_menu_render (frame_buffer_t *output, terminal_size_t size, const ui_help_menu_t *menu)
void ui_help_keys (char *output, size_t capacity, const char *keys)

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 != '(' && *p != ')')
45 ++p;
47 if ((size_t)(p - start) >= sizeof(key)) {
48 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Help key token is too long");
49 return;
50 }
51 snprintf(key, sizeof(key), "%.*s", (int)(p - start), start);
52 const char *styled = colored_string(LOG_COLOR_WARN, key);
53 size_t length = strlen(styled);
54 if (length >= cap - used) {
55 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Help key output buffer is too small");
56 return;
57 }
58 memcpy(out + used, styled, length + 1);
59 used += length;
60 }
61 }
62}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
@ LOG_COLOR_WARN
Definition log/log.h:151
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
Definition string.c:284
#define UI_HELP_MENU_KEY_BUFFER_SIZE
Definition help_menu.c:3
static bool size_t gotel_query_t * out
Definition queries.h:8

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 * menu)

Definition at line 85 of file help_menu.c.

85 {
86 if (!m || (m->count && !m->entries) || (m->settings_count && !m->settings)) {
87 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid help menu entries or settings");
88 return (terminal_size_t){0};
89 }
90 int keys, left, right;
91 measure(m, &keys, &left, &right);
92 int width = left + (right ? right + 3 : 0);
93 if (m->title && display_width(m->title) > width)
94 width = display_width(m->title);
95 if (m->footer && display_width(m->footer) > width)
96 width = display_width(m->footer);
97 size_t rows = m->count > m->settings_count ? m->count : m->settings_count;
98 return (terminal_size_t){.cols = width + 6, .rows = (int)rows + 8 + m->top_margin};
99}
unsigned int width
Definition fft.c:23
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.

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 * output,
terminal_size_t size,
const ui_help_menu_t * menu )

Definition at line 101 of file help_menu.c.

101 {
102 if (!out || !m || (m->count && !m->entries) || (m->settings_count && !m->settings) || m->count > 62 ||
103 m->settings_count > 62) {
104 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid help output or entry counts");
105 return false;
106 }
108 const char *lines[UI_HELP_MENU_TEXT_COUNT];
109 size_t rows = m->count > m->settings_count ? m->count : m->settings_count;
110 int keys, left, right;
111 measure(m, &keys, &left, &right);
112 lines[0] = "";
113 for (size_t i = 0; i < rows; ++i) {
116 if (i < m->count) {
117 const ui_help_entry_t *entry = &m->entries[i];
118 if (!entry->keys)
119 snprintf(lhs, sizeof(lhs), "%s", entry->description);
120 else {
122 char styled_keys[UI_HELP_MENU_STYLED_KEYS_BUFFER_SIZE];
123 ui_help_keys(styled_keys, sizeof(styled_keys), entry->keys);
124 ui_text_cell(key, sizeof(key), styled_keys, keys, UI_ALIGN_LEFT);
125 ui_text_values_style(description, sizeof(description), entry->description, LOG_COLOR_FATAL);
126 snprintf(lhs, sizeof(lhs), "%s %s", key, description);
127 }
128 }
129 if (i < m->settings_count) {
130 const char *setting = m->settings[i];
131 const char *end = setting[0] == '(' ? strchr(setting, ')') : NULL;
132 if (end) {
134 snprintf(binding, sizeof(binding), "%.*s", (int)(end + 1 - setting), setting);
135 ui_help_keys(styled, sizeof(styled), binding);
137 ui_text_values_style(values, sizeof(values), end + 1, LOG_COLOR_FATAL);
138 snprintf(rhs, sizeof(rhs), "%s%s", styled, values);
139 } else
140 ui_text_values_style(rhs, sizeof(rhs), setting, LOG_COLOR_FATAL);
141 }
142 ui_text_cell(padded, sizeof(padded), lhs, left, UI_ALIGN_LEFT);
143 snprintf(text[i], sizeof(text[i]), "%s%s%s", padded, right ? " " : "", rhs);
144 lines[i + 1] = text[i];
145 }
146 ui_popup_t box = {
147 .title = m->title, .lines = lines, .line_count = rows + 1, .footer = m->footer, .top_margin = m->top_margin};
148 return ui_popup_render(out, size, &box);
149}
@ LOG_COLOR_FATAL
Definition log/log.h:153
#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:56
Definition help_menu.h:3
const char * keys
Definition help_menu.h:4
const char * description
Definition help_menu.h:5
When code needs to work with ui popup, it keeps title, lines, line_count, footer here instead of scat...
Definition popup.h:23
void ui_text_values_style(char *out, size_t cap, const char *text, log_color_t zero_color)
Definition text.c:95
size_t ui_text_cell(char *out, size_t cap, const char *text, int width, ui_alignment_t align)
Definition text.c:48
@ UI_ALIGN_LEFT
Definition text.h:9

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().