ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
session_help.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define TUI_SESSION_HELP_SETTINGS_COUNT 20
4#define TUI_SESSION_HELP_VOLUME_BAR_BUFFER_SIZE 32
5#define TUI_SESSION_HELP_KEY_STORAGE_COUNT 40
6#define TUI_SESSION_HELP_FOOTER_BUFFER_SIZE 128
7
8/* Existing buffer budgets in bytes, including NUL for text storage. */
9#define SESSION_HELP_SETTING_BUFFER_SIZE 256
10
11/* Existing buffer budgets in bytes, including NUL for text storage. */
12#define SESSION_HELP_KEY_BUFFER_SIZE 48
13
14/* Existing buffer budgets in bytes, including NUL for text storage. */
15#define SESSION_HELP_ENTRY_CAPACITY 40
16
17#include <ascii-chat/common.h>
18#include <ascii-chat/log/log.h>
26#include <options/options.h>
27#include <stdio.h>
28#include <string.h>
29/* Color codes for enabled/disabled settings */
30#define ENABLED_COLOR LOG_COLOR_INFO /* Green */
31#define DISABLED_COLOR LOG_COLOR_ERROR /* Red */
32
33void ui_session_help_free(char *text) { SAFE_FREE(text); }
34
35/* ============================================================================
36 * Keyboard Help Rendering
37 * ============================================================================
38 */
39
49static void format_volume_bar(double volume, char *bar_output,
50 size_t bar_output_size) {
51 if (!bar_output || bar_output_size < 25) {
52 return;
53 }
54
55 // Clamp volume to [0.0, 1.0]
56 if (volume < 0.0) {
57 volume = 0.0;
58 }
59 if (volume > 1.0) {
60 volume = 1.0;
61 }
62
63 // Calculate filled blocks (10 blocks total for 10% granularity)
64 int filled = (int)(volume * 10.0);
65 int empty = 10 - filled;
66
67 // Build bar: "[====== ] 80%"
68 // Use simple ASCII characters to avoid UTF-8 encoding issues
69 snprintf(bar_output, bar_output_size, "[%.*s%.*s] %d%%", filled,
70 "==========", empty, " ", (int)(volume * 100.0));
71}
72
76static const char *color_mode_to_string(int mode) {
77 switch (mode) {
78 case -1:
79 return "Auto";
80 case 0:
81 return "Mono";
82 case 1:
83 return "16-color";
84 case 2:
85 return "256-color";
86 case 3:
87 return "Truecolor";
88 default:
89 return "Unknown";
90 }
91}
92
96static const char *render_mode_to_string(int mode) {
97 switch (mode) {
98 case 0:
99 return "Foreground";
100 case 1:
101 return "Background";
102 case 2:
103 return "Half-block";
104 default:
105 return "Unknown";
106 }
107}
108
110static const char *renderer_to_string(int renderer) {
111 return renderer == ASCII_RENDERER_GLYPH ? "Glyph" : "Luminance";
112}
113
117static const char *color_filter_to_string(color_filter_t filter) {
118 if (filter == COLOR_FILTER_NONE) {
119 return "None";
120 }
122 return def ? def->name : "Unknown";
123}
124
125static const char *status_indicator(bool enabled) {
127 enabled ? "O" : "X");
128}
129
130char *ui_session_help_render(terminal_size_t size, bool has_media, bool browser,
131 terminal_size_t *minimum) {
132 if (!minimum) {
133 SET_ERRNO(ERROR_INVALID_PARAM, "Help minimum geometry output is required");
134 return NULL;
135 }
136 const char *navigation[] = {
137 "Navigation & Control:",
138 "─────────────────────",
139 "? Toggle this help screen TUI",
140 "q / Esc Quit ascii-chat (Shift+Key to panic exit)",
141 browser ? NULL : "v Open from clipboard",
142 browser ? NULL : "Paste Queue dropped/pasted media",
143 browser ? NULL : "< / > Previous/next media (restart)",
144 has_media ? "Space Play/Pause (files only)" : NULL,
145 has_media ? "[ Toggle Media Playback" : NULL,
146 has_media ? (terminal_should_use_utf8(GET_OPTION(force_utf8))
147 ? "← / → Seek backward/forward 30s"
148 : "Left / Right Seek backward/forward 30s")
149 : NULL,
150 "m Mute/Unmute audio",
152 ? "↑ / ↓ Volume up/down (10%)"
153 : "Up / Down Volume up/down (10%)",
154 "c Cycle color mode",
155 "f Cycle color filter",
156 "x Flip webcam horizontally",
157 "y Flip webcam vertically",
158 "w Toggle Face Follow",
159 "Shift-s Toggle stabilization",
160 "r Cycle render mode",
161 "Shift-r Cycle text renderer",
162 "- FPS counter",
163#if defined(ASCIICHAT_DIAGNOSTIC_UI)
164 browser ? NULL : "` Error stacks diagnostic TUI",
165#endif
166 browser ? NULL : "o TUI to open media from path or url",
167 "] Live log viewer/searcher TUI",
168 "F12 Version update checker TUI",
169 browser ? NULL : "= Running app statistics TUI",
170#ifndef NDEBUG
171 browser ? NULL : "0 C code sync primitives TUI",
172#endif
173 };
175 [SESSION_HELP_SETTING_BUFFER_SIZE] = {"Current Settings:", ""};
177 format_volume_bar(GET_OPTION(speakers_volume), volume_bar,
178 sizeof(volume_bar));
179 snprintf(settings[2], sizeof(settings[2]), "Audio : %s",
180 status_indicator(GET_OPTION(audio_enabled)));
181 snprintf(settings[3], sizeof(settings[3]), "Volume : %s", volume_bar);
182 snprintf(settings[4], sizeof(settings[4]), "Color : %s",
184 snprintf(settings[5], sizeof(settings[5]), "Filter : %s",
185 color_filter_to_string(GET_OPTION(color_filter)));
186 snprintf(settings[6], sizeof(settings[6]), "Render : %s",
188 snprintf(settings[7], sizeof(settings[7]), "Renderer: %s",
189 renderer_to_string(GET_OPTION(renderer)));
190 snprintf(settings[8], sizeof(settings[8]), "Flip : rows=%s cols=%s",
191 status_indicator(GET_OPTION(flip_y)),
192 status_indicator(GET_OPTION(flip_x)));
193 snprintf(settings[9], sizeof(settings[9]), "Face Follow : %s",
194 status_indicator(GET_OPTION(face_follow)));
195 snprintf(settings[10], sizeof(settings[10]), "Stabilize: %s",
196 status_indicator(GET_OPTION(stabilize)));
197 int settings_count = 11;
198 settings[settings_count++][0] = '\0';
199 snprintf(settings[settings_count++], sizeof(settings[0]),
200 "Animations (number key toggle):");
201 snprintf(settings[settings_count++], sizeof(settings[0]),
202 "(1) Matrix \"Digital Rain\" : %s",
203 status_indicator(GET_OPTION(matrix_rain)));
204 snprintf(settings[settings_count++], sizeof(settings[0]),
205 "(2) Audio Waveform : %s", status_indicator(GET_OPTION(waveform)));
206 snprintf(settings[settings_count++], sizeof(settings[0]),
207 "(3) Audio Frequencies (FFT) : %s",
208 status_indicator(GET_OPTION(fft)));
209 snprintf(settings[settings_count++], sizeof(settings[0]),
210 "(z) Zalgo intensity : %d", GET_OPTION(zalgo));
211 snprintf(settings[settings_count++], sizeof(settings[0]),
212 "(-) FPS Counter : %s", status_indicator(GET_OPTION(fps_counter)));
213 bool server = !browser && (GET_OPTION(detected_mode) == MODE_SERVER ||
214 GET_OPTION(detected_mode) == MODE_MCP_SERVER);
215 bool discovery =
216 !browser && GET_OPTION(detected_mode) == MODE_DISCOVERY_SERVICE;
218 char key_storage[TUI_SESSION_HELP_KEY_STORAGE_COUNT]
220 size_t count = 0;
221 for (size_t i = 0; i < sizeof(navigation) / sizeof(navigation[0]); ++i) {
222 const char *line = navigation[i];
223 if (!line || i == 1)
224 continue;
225 if (line[0] == '?' || line[0] == ']' || line[0] == '=' || line[0] == '0' ||
226 line[0] == '`' || line[0] == 'o' || !strncmp(line, "F12", 3))
227 continue;
228 if ((server || discovery) && i > 1 && line[0] != '?' &&
229 strncmp(line, "Esc", 3) && line[0] != ']' && line[0] != '=' &&
230 line[0] != '0' && line[0] != '`' && strncmp(line, "F12", 3))
231 continue;
232 const char *end = strstr(line, " ");
233 if (end) {
234 size_t length = (size_t)(end - line);
235 if (length >= sizeof(key_storage[0]))
236 length = sizeof(key_storage[0]) - 1;
237 memcpy(key_storage[count], line, length);
238 key_storage[count][length] = '\0';
239 while (*end == ' ')
240 ++end;
241 entries[count] =
242 (ui_help_entry_t){.keys = key_storage[count], .description = end};
243 } else
244 entries[count] = (ui_help_entry_t){.description = line};
245 ++count;
246 }
247 count += tui_shortcut_help(entries + count, 40 - count, server || discovery,
248 browser);
249 if (server || discovery) {
250 settings_count = 4;
251 snprintf(settings[0], sizeof(settings[0]), "%s",
252 discovery ? "Discovery service:" : "Server:");
253 snprintf(settings[1], sizeof(settings[1]), "Port : %d", GET_OPTION(port));
254 snprintf(settings[2], sizeof(settings[2]), "Status screen : %s",
255 status_indicator(GET_OPTION(status_screen)));
256 snprintf(settings[3], sizeof(settings[3]), "Log level : %d",
257 GET_OPTION(log_level));
258 }
259 const char *settings_text[TUI_SESSION_HELP_SETTINGS_COUNT];
260 for (int i = 0; i < settings_count; ++i)
261 settings_text[i] = settings[i];
263 snprintf(footer, sizeof(footer), "Press %s to close",
265 ui_help_menu_t menu = {
266 .title = discovery ? "ascii-chat Discovery Service Keyboard Shortcuts"
267 : server ? "ascii-chat Server Keyboard Shortcuts"
268 : "ascii-chat Keyboard Shortcuts",
269 .entries = entries,
270 .count = count,
271 .settings = settings_text,
272 .settings_count = (size_t)settings_count,
273 .footer = footer,
274 // Browser sessions draw a four-row animated logo above the popup.
275 .top_margin = browser ? 6 : 0};
276 *minimum = ui_help_menu_measure(&menu);
277 frame_buffer_t *output = frame_buffer_create(size.rows, size.cols);
278 if (!output)
279 return NULL;
280 ui_help_menu_render(output, size, &menu);
281 size_t length = frame_buffer_get_length(output);
282 char *result = SAFE_MALLOC(length + 1, char *);
283 memcpy(result, frame_buffer_get_content(output), length);
284 result[length] = '\0';
285 frame_buffer_destroy(output);
286 return result;
287}
288
290 terminal_size_t *minimum) {
291 if (!minimum) {
292 SET_ERRNO(ERROR_INVALID_PARAM, "Help minimum geometry output is required");
293 return NULL;
294 }
295 static const ui_help_entry_t sync[] = {{"0 / Esc", "Close sync viewer"},
296 {"Up / Down", "Select primitive"},
297 {"Left / Right", "Previous/next page"},
298 {"Home / End", "First/last primitive"},
299 {"=", "Running app statistics"},
300 {"]", "Log viewer"},
301 {"?", "Close this help"}};
302 static const ui_help_entry_t stats[] = {
303 {"= / Esc", "Close statistics"},
304 {"Tab / Left / Right", "Switch data page"},
305 {"Up / Down", "Select and scroll rows"},
306 {"Home / End", "First/last row"},
307 {"0", "Sync primitives"},
308 {"]", "Log viewer"},
309 {"?", "Close this help"}};
310 static const ui_help_entry_t errors[] = {
311 {"` / Esc", "Close error stacks"},
312 {"Tab", "Current/history page"},
313 {"Up / Down", "Select and scroll errors"},
314 {"s", "Change sort order"},
315 {"x", "Clear selected error"},
316 {"=", "Running app statistics"},
317 {"0", "Sync primitives"},
318 {"?", "Close this help"}};
319 static const ui_help_entry_t update[] = {
320 {"F12 / Esc", "Close update checker"},
321 {"Left / Right / Tab", "Select button"},
322 {"Enter", "Activate selected button"},
323 {"O", "Open release page"},
324 {"R", "Check again"},
325 {"?", "Close this help"}};
326 static const ui_help_entry_t lan[] = {
327 {"Up / Down / j / k", "Select LAN server"},
328 {"0-9", "Enter server number"},
329 {"Enter", "Connect to selected server"},
330 {"Esc", "Cancel LAN selection"},
331 {"=", "Running app statistics"},
332 {"0", "Sync primitives"},
333 {"?", "Close this help"}};
334 static const ui_help_entry_t media[] = {
335 {"Enter", "Play the entered paths or URLs"},
336 {"Esc / Ctrl-O", "Cancel media input"},
337 {"Left / Right", "Move the text cursor"},
338 {"Home / End", "Beginning/end of input"},
339 {"Backspace / Delete", "Delete a character"},
340 {"?", "Help when the input is empty"}};
341 const ui_help_entry_t *entries = NULL;
342 size_t count = 0;
343 const char *title = NULL;
344 switch (screen) {
345 case UI_SCREEN_ERROR: {
346 static const ui_help_entry_t popup[] = {
347 {"Left / Right", "Previous / next error"},
348 {"Home / End", "First / last error"},
349 {"Up / Down", "Scroll error details"},
350 {"PgUp / PgDn", "Scroll a page"},
351 {"Enter / Esc", "Dismiss all errors"},
352 {"q", "Dismiss all errors"},
353 {"?", "Close this help"}};
354 entries = popup;
355 count = sizeof(popup) / sizeof(*popup);
356 title = "Error Popup Keyboard Shortcuts";
357 break;
358 }
359 case UI_SCREEN_SYNC:
360 entries = sync;
361 count = sizeof(sync) / sizeof(*sync);
362 title = "Sync Viewer Keyboard Shortcuts";
363 break;
364 case UI_SCREEN_STATS:
365 entries = stats;
366 count = sizeof(stats) / sizeof(*stats);
367 title = "Statistics Keyboard Shortcuts";
368 break;
369 case UI_SCREEN_ERRNO:
370 entries = errors;
371 count = sizeof(errors) / sizeof(*errors);
372 title = "Error Stacks Keyboard Shortcuts";
373 break;
374 case UI_SCREEN_PROMPT:
375 entries = media;
376 count = sizeof(media) / sizeof(*media);
377 title = "Media Input Keyboard Shortcuts";
378 break;
379 case UI_SCREEN_MDNS:
380 entries = lan;
381 count = sizeof(lan) / sizeof(*lan);
382 title = "LAN Selection Keyboard Shortcuts";
383 break;
384 case UI_SCREEN_UPDATE:
385 entries = update;
386 count = sizeof(update) / sizeof(*update);
387 title = "Update Checker Keyboard Shortcuts";
388 break;
389 default:
391 size, GET_OPTION(media_file)[0] || GET_OPTION(media_url)[0], false,
392 minimum);
393 }
395 snprintf(footer, sizeof(footer), "Press %s to close",
397 ui_help_menu_t menu = {.title = title,
398 .entries = entries,
399 .count = count,
400 .footer = footer,
401 .top_margin = 0};
402 *minimum = ui_help_menu_measure(&menu);
403 frame_buffer_t *buffer = frame_buffer_create(size.rows, size.cols);
404 if (!buffer)
405 return NULL;
406 ui_help_menu_render(buffer, size, &menu);
407 size_t length = frame_buffer_get_length(buffer);
408 char *result = SAFE_MALLOC(length + 1, char *);
409 memcpy(result, frame_buffer_get_content(buffer), length);
410 result[length] = '\0';
411 frame_buffer_destroy(buffer);
412 return result;
413}
#define SET_ERRNO(code,...)
@ LOG_COLOR_ERROR
Definition color.h:11
@ LOG_COLOR_INFO
Definition color.h:9
@ LOG_COLOR_WARN
Definition color.h:10
int color_mode
Definition fft.c:26
terminal_render_mode_t render_mode
Definition fft.c:27
void frame_buffer_destroy(frame_buffer_t *buf)
Destroy a frame buffer.
size_t frame_buffer_get_length(const frame_buffer_t *buf)
Get the current content length of the frame buffer.
const char * frame_buffer_get_content(const frame_buffer_t *buf)
Get a pointer to the buffer content.
frame_buffer_t * frame_buffer_create(int rows, int cols)
Create a new frame buffer.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
bool enabled
Is filtering active?
Definition grep.c:63
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
@ ERROR_INVALID_PARAM
#define GET_OPTION(field)
Definition options.h:1339
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:922
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:920
@ MODE_SERVER
Server mode - network server options.
Definition options.h:917
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
const color_filter_def_t * color_filter_get_metadata(color_filter_t filter)
Get color filter metadata by enum value.
terminal_size_t ui_help_menu_measure(const ui_help_menu_t *m)
Definition help_menu.c:86
bool ui_help_menu_render(frame_buffer_t *out, terminal_size_t size, const ui_help_menu_t *m)
Definition help_menu.c:103
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🖥️ Display Width and Terminal Centering Utilities
📝 Logging API with multiple log levels and terminal output control
const char * color_mode_to_string(terminal_color_mode_t mode)
Convert color mode enum to string.
const char * render_mode_to_string(terminal_render_mode_t mode)
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
void ui_session_help_free(char *text)
#define TUI_SESSION_HELP_KEY_STORAGE_COUNT
Definition session_help.c:5
#define SESSION_HELP_ENTRY_CAPACITY
char * ui_session_context_help_render(terminal_size_t size, ui_screen_t screen, terminal_size_t *minimum)
char * ui_session_help_render(terminal_size_t size, bool has_media, bool browser, terminal_size_t *minimum)
#define TUI_SESSION_HELP_VOLUME_BAR_BUFFER_SIZE
Definition session_help.c:4
#define SESSION_HELP_KEY_BUFFER_SIZE
#define SESSION_HELP_SETTING_BUFFER_SIZE
Definition session_help.c:9
#define TUI_SESSION_HELP_FOOTER_BUFFER_SIZE
Definition session_help.c:6
#define TUI_SESSION_HELP_SETTINGS_COUNT
Definition session_help.c:3
size_t tui_shortcut_help(ui_help_entry_t *entries, size_t capacity, bool server, bool browser)
Definition shortcuts.c:70
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
For color filter def, this is the live record that ties name, cli_name, r, g together....
const char * name
Human-readable filter name (e.g., "matrix-green").
Definition help_menu.h:7
ui help entry keeps the values that move through the subsystem: keys, description....
Definition help_menu.h:19
ui_screen_t
@ UI_SCREEN_PROMPT
@ UI_SCREEN_ERRNO
@ UI_SCREEN_STATS
@ UI_SCREEN_UPDATE
@ UI_SCREEN_SYNC
@ UI_SCREEN_MDNS
@ UI_SCREEN_ERROR
🔤 UTF-8 Encoding and Decoding Utilities
@ ASCII_RENDERER_GLYPH