ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
error_popup.c File Reference

Go to the source code of this file.

Macros

#define TUI_ERROR_POPUP_WRAPPED_BUFFER_SIZE   65536
#define TUI_ERROR_POPUP_STYLE_BUFFER_SIZE   128
#define TUI_ERROR_POPUP_THREAD_BUFFER_SIZE   160
#define TUI_ERROR_POPUP_FILE_BUFFER_SIZE   1024
#define TUI_ERROR_POPUP_FUNCTION_BUFFER_SIZE   256
#define TUI_ERROR_POPUP_LINE_BUFFER_SIZE   24
#define TUI_ERROR_POPUP_ADDRESS_BUFFER_SIZE   40
#define TUI_ERROR_POPUP_TITLE_BUFFER_SIZE   128
#define TUI_ERROR_POPUP_HEADING_BUFFER_SIZE   256
#define TUI_ERROR_POPUP_CODE_BUFFER_SIZE   512
#define TUI_ERROR_POPUP_CELL_BUFFER_SIZE   1024
#define TUI_ERROR_POPUP_FINAL_OUTPUT_BUFFER_SIZE   65536
#define ERROR_POPUP_LINE_CAPACITY   2048

Functions

bool ui_error_popup_visible (void)
uint64_t ui_error_popup_revision (void)
terminal_size_t ui_error_popup_minimum (void)
void ui_error_popup_key (keyboard_key_t key)
void ui_error_popup_render (frame_buffer_t *out, terminal_size_t size)
void ui_error_popup_reset (void)
void ui_error_popup_preserve (int fd, terminal_size_t size)
void ui_error_popup_flush (void)

Macro Definition Documentation

◆ ERROR_POPUP_LINE_CAPACITY

#define ERROR_POPUP_LINE_CAPACITY   2048

Definition at line 17 of file error_popup.c.

◆ TUI_ERROR_POPUP_ADDRESS_BUFFER_SIZE

#define TUI_ERROR_POPUP_ADDRESS_BUFFER_SIZE   40

Definition at line 9 of file error_popup.c.

◆ TUI_ERROR_POPUP_CELL_BUFFER_SIZE

#define TUI_ERROR_POPUP_CELL_BUFFER_SIZE   1024

Definition at line 13 of file error_popup.c.

Referenced by ui_error_popup_preserve(), and ui_error_popup_render().

◆ TUI_ERROR_POPUP_CODE_BUFFER_SIZE

#define TUI_ERROR_POPUP_CODE_BUFFER_SIZE   512

Definition at line 12 of file error_popup.c.

Referenced by ui_error_popup_preserve(), and ui_error_popup_render().

◆ TUI_ERROR_POPUP_FILE_BUFFER_SIZE

#define TUI_ERROR_POPUP_FILE_BUFFER_SIZE   1024

Definition at line 6 of file error_popup.c.

◆ TUI_ERROR_POPUP_FINAL_OUTPUT_BUFFER_SIZE

#define TUI_ERROR_POPUP_FINAL_OUTPUT_BUFFER_SIZE   65536

Definition at line 14 of file error_popup.c.

◆ TUI_ERROR_POPUP_FUNCTION_BUFFER_SIZE

#define TUI_ERROR_POPUP_FUNCTION_BUFFER_SIZE   256

Definition at line 7 of file error_popup.c.

◆ TUI_ERROR_POPUP_HEADING_BUFFER_SIZE

#define TUI_ERROR_POPUP_HEADING_BUFFER_SIZE   256

Definition at line 11 of file error_popup.c.

Referenced by ui_error_popup_render().

◆ TUI_ERROR_POPUP_LINE_BUFFER_SIZE

#define TUI_ERROR_POPUP_LINE_BUFFER_SIZE   24

Definition at line 8 of file error_popup.c.

◆ TUI_ERROR_POPUP_STYLE_BUFFER_SIZE

#define TUI_ERROR_POPUP_STYLE_BUFFER_SIZE   128

Definition at line 4 of file error_popup.c.

◆ TUI_ERROR_POPUP_THREAD_BUFFER_SIZE

#define TUI_ERROR_POPUP_THREAD_BUFFER_SIZE   160

Definition at line 5 of file error_popup.c.

◆ TUI_ERROR_POPUP_TITLE_BUFFER_SIZE

#define TUI_ERROR_POPUP_TITLE_BUFFER_SIZE   128

Definition at line 10 of file error_popup.c.

Referenced by ui_error_popup_render().

◆ TUI_ERROR_POPUP_WRAPPED_BUFFER_SIZE

#define TUI_ERROR_POPUP_WRAPPED_BUFFER_SIZE   65536

Definition at line 3 of file error_popup.c.

Referenced by ui_error_popup_preserve().

Function Documentation

◆ ui_error_popup_flush()

void ui_error_popup_flush ( void )

Write preserved terminal output after shutdown diagnostics, without allocating or using options.

Definition at line 511 of file error_popup.c.

511 {
512 if (g_final_length)
513 platform_write_all(g_final_fd, g_final_output, g_final_length);
514 g_final_length = 0;
515}

Referenced by asciichat_shared_destroy().

◆ ui_error_popup_key()

void ui_error_popup_key ( keyboard_key_t key)

Definition at line 88 of file error_popup.c.

88 {
89 lock_state();
90 refresh_locked();
91 uint64_t dismiss = 0;
92 size_t previous = g_selected;
93 size_t previous_scroll = g_scroll;
94 switch ((int)key) {
95 case KEY_ESCAPE:
96 case 'q':
97 case '\r':
98 case '\n':
99 /* Dismiss all notifications currently shown. The popup may receive the
100 * first key before its presentation-thread snapshot has been rendered. */
101 dismiss = UINT64_MAX;
102 break;
103 case KEY_LEFT:
104 if (g_selected)
105 --g_selected;
106 break;
107 case KEY_RIGHT:
108 if (g_selected + 1 < g_count)
109 ++g_selected;
110 break;
111 case KEY_HOME:
112 g_selected = 0;
113 break;
114 case KEY_END:
115 g_selected = g_count ? g_count - 1 : 0;
116 break;
117 case KEY_UP:
118 if (g_scroll)
119 --g_scroll;
120 break;
121 case KEY_DOWN:
122 ++g_scroll;
123 break;
124 case KEY_PAGEUP:
125 g_scroll = g_scroll > g_page_rows ? g_scroll - g_page_rows : 0;
126 break;
127 case KEY_PAGEDOWN:
128 g_scroll += g_page_rows;
129 break;
130 default:
131 break;
132 }
133 if (g_selected != previous)
134 g_scroll = 0;
135 size_t maximum = g_body_rows > g_page_rows ? g_body_rows - g_page_rows : 0;
136 if (g_scroll > maximum)
137 g_scroll = maximum;
138 bool selection_changed = previous != g_selected;
139 bool scrolled = previous_scroll != g_scroll && !selection_changed;
140 unlock_state();
141 if (selection_changed)
142 ascii_otel_ui_usage("error", "selected", 0);
143 if (scrolled)
144 ascii_otel_ui_usage("error", "details_scrolled", 0);
145 if (dismiss)
146 ascii_otel_ui_usage("error", "dismissed", 0);
147 if (dismiss)
150}
void errno_notifications_dismiss(uint64_t through_id)
unsigned long long uint64_t
void ascii_otel_ui_usage(const char *screen, const char *event, uint64_t duration_ns)
Definition otel.c:506
void ui_controller_redraw(void)

References ascii_otel_ui_usage(), errno_notifications_dismiss(), and ui_controller_redraw().

Referenced by ui_input_read_key().

◆ ui_error_popup_minimum()

terminal_size_t ui_error_popup_minimum ( void )

Definition at line 84 of file error_popup.c.

84 {
85 return (terminal_size_t){.cols = 60, .rows = 14};
86}

Referenced by ui_controller_state(), and ui_error_popup_render().

◆ ui_error_popup_preserve()

void ui_error_popup_preserve ( int fd,
terminal_size_t size )

Preserve pending notifications using cached terminal dimensions before queue retirement. Callers own an interactive terminal and must prevent concurrent rendering. This formatter performs no console queries and does not wait for input.

Definition at line 435 of file error_popup.c.

435 {
437 return;
439 size_t count =
440 errno_notifications_snapshot(entries, ARRAY_SIZE(entries), NULL);
441 if (!count)
442 return;
443 bool previous = errno_notifications_suppress(true);
444 frame_buffer_t *output = frame_buffer_create(0, 0);
445 if (output) {
446 bool colors = !GET_OPTION(strip_ansi) &&
448 bool unicode = terminal_should_use_utf8(GET_OPTION(force_utf8));
449 int width = MIN(size.cols > 0 ? size.cols - 1 : 79, 100);
450 width = MAX(width, 8);
451 int body_width = width - 4;
452 size_t first = count > 4 ? count - 4 : 0;
453 frame_buffer_append(output, "\033[0m\033[?25h\r\n", 12);
454 if (first)
456 output, "%zu pending notifications; showing the latest 4.\n", count);
457 for (size_t i = first; i < count; ++i) {
460 const char *side = unicode ? "│" : "|";
461 ui_popup_rule(output, width, UI_POPUP_TOP, false, unicode,
463 frame_buffer_append(output, "\n", 1);
464 ui_text_cell(cell, sizeof(cell), "ERROR", body_width, UI_ALIGN_LEFT);
465 frame_buffer_printf(output, "%s %s %s\n", side,
466 colors ? colored_string(LOG_COLOR_ERROR, cell) : cell,
467 side);
468 format_error_heading(code, sizeof(code), &entries[i]);
469 ui_text_cell(cell, sizeof(cell), code, body_width, UI_ALIGN_LEFT);
470 frame_buffer_printf(output, "%s %s %s\n", side, cell, side);
471 build_body(&entries[i], body_width, colors);
472 for (size_t line = 0; line < g_line_count; ++line) {
473 const char *start = g_wrapped + g_lines[line];
474 const char *end = strchr(start, '\n');
476 size_t length = end ? (size_t)(end - start) : strlen(start);
477 length = MIN(length, sizeof(line_text) - 1);
478 memcpy(line_text, start, length);
479 line_text[length] = '\0';
480 ui_text_cell(cell, sizeof(cell), line_text, body_width, UI_ALIGN_LEFT);
481 frame_buffer_printf(output, "%s %s%s %s\n", side, cell,
482 colors ? "\033[0m" : "", side);
483 }
484 ui_popup_rule(output, width, UI_POPUP_BOTTOM, false, unicode,
486 frame_buffer_append(output, "\n", 1);
487 }
488 if (frame_buffer_get_length(output) <= sizeof(g_final_output)) {
489 g_final_length = frame_buffer_get_length(output);
490 memcpy(g_final_output, frame_buffer_get_content(output), g_final_length);
491 g_final_fd = fd;
492 } else {
493 char message[ASCIICHAT_ERRNO_MESSAGE_MAX];
494 sanitize(message, sizeof(message), entries[count - 1].context_message);
495 int length = safe_snprintf(
496 g_final_output, sizeof(g_final_output),
497 "\nERROR\n%s (%d): %s\nDetails: %s\n",
498 asciichat_error_name(entries[count - 1].code),
499 entries[count - 1].code,
500 asciichat_error_string(entries[count - 1].code), message);
501 g_final_length =
502 length > 0 ? MIN((size_t)length, sizeof(g_final_output) - 1) : 0;
503 g_final_fd = fd;
504 }
505 }
506 if (output)
507 frame_buffer_destroy(output);
509}
bool errno_notifications_pending(void)
size_t errno_notifications_snapshot(asciichat_error_context_t *out, size_t capacity, uint64_t *dropped)
bool errno_notifications_suppress(bool suppress)
#define ASCIICHAT_ERRNO_MESSAGE_MAX
#define ASCIICHAT_ERRNO_HISTORY_SIZE
@ LOG_COLOR_RESET
Definition color.h:14
@ LOG_COLOR_ERROR
Definition color.h:11
#define TUI_ERROR_POPUP_WRAPPED_BUFFER_SIZE
Definition error_popup.c:3
#define TUI_ERROR_POPUP_CELL_BUFFER_SIZE
Definition error_popup.c:13
#define TUI_ERROR_POPUP_CODE_BUFFER_SIZE
Definition error_popup.c:12
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned int width
Definition fft.c:24
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
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.
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.
#define GET_OPTION(field)
Definition options.h:1339
#define options_terminal_settings()
Definition options.h:1287
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
#define MIN(a, b)
Definition param.h:6
#define MAX(a, b)
Definition param.h:10
asciichat_error_t ui_popup_rule(frame_buffer_t *out, int width, ui_popup_edge_t edge, bool double_border, bool unicode, log_color_t color)
Definition popup.c:12
@ UI_POPUP_BOTTOM
Definition popup.h:16
@ UI_POPUP_TOP
Definition popup.h:14
bool terminal_should_color_output(int fd, const terminal_options_t *options)
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
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 ASCIICHAT_ERRNO_HISTORY_SIZE, ASCIICHAT_ERRNO_MESSAGE_MAX, colored_string(), colors, errno_notifications_pending(), errno_notifications_snapshot(), errno_notifications_suppress(), frame_buffer_append(), frame_buffer_create(), frame_buffer_destroy(), frame_buffer_get_content(), frame_buffer_get_length(), frame_buffer_printf(), GET_OPTION, LOG_COLOR_ERROR, LOG_COLOR_RESET, MAX, MIN, options_terminal_settings, terminal_should_color_output(), terminal_should_use_utf8(), TUI_ERROR_POPUP_CELL_BUFFER_SIZE, TUI_ERROR_POPUP_CODE_BUFFER_SIZE, TUI_ERROR_POPUP_WRAPPED_BUFFER_SIZE, UI_ALIGN_LEFT, UI_POPUP_BOTTOM, ui_popup_rule(), UI_POPUP_TOP, ui_text_cell(), and width.

Referenced by ui_controller_shutdown().

◆ ui_error_popup_render()

void ui_error_popup_render ( frame_buffer_t * out,
terminal_size_t size )

Definition at line 341 of file error_popup.c.

341 {
342 asciichat_error_context_t error = {0};
343 lock_state();
344 refresh_locked();
345 size_t count = g_count, selected = g_selected;
346 uint64_t displayed_latest = g_latest, dropped = g_dropped;
347 if (count)
348 error = g_entries[selected];
349 unlock_state();
350 if (!count)
351 return;
352 terminal_size_t minimum = ui_error_popup_minimum();
353 frame_buffer_append(out, "\033[0m\033[?25l", 10);
354 if (ui_too_small(size, minimum)) {
355 ui_too_small_render(out, size, minimum);
356 return;
357 }
358 lock_state();
359 g_displayed_latest = displayed_latest;
360 unlock_state();
361 bool colors =
362 !GET_OPTION(strip_ansi) &&
365 int width = MIN(size.cols - 4, 100);
366 int body_width = width - 6;
367 if (g_cached_id != error.error_id || g_cached_width != body_width ||
368 g_cached_color != colors)
369 build_body(&error, body_width, colors);
370 size_t desired_rows =
371 g_trace_start ? MIN(g_line_count, g_trace_start + 12) : g_line_count;
372 int body_rows = MIN(size.rows - 8, MIN(22, (int)desired_rows));
373 size_t fixed_rows =
374 g_trace_start && g_trace_start < (size_t)body_rows ? g_trace_start : 0;
375 if (g_trace_start && !fixed_rows)
376 body_rows = MIN(body_rows, 12);
377 int height = body_rows + 6;
378 int top = (size.rows - height) / 2 + 1, left = (size.cols - width) / 2 + 1;
379 size_t scroll_rows = g_line_count - fixed_rows;
380 size_t page_rows = (size_t)body_rows - fixed_rows;
381 lock_state();
382 g_body_rows = scroll_rows;
383 g_page_rows = page_rows;
384 size_t maximum = scroll_rows > page_rows ? scroll_rows - page_rows : 0;
385 g_scroll = MIN(g_scroll, maximum);
386 size_t scroll = g_scroll;
387 unlock_state();
390 safe_snprintf(title, sizeof(title), "Error (%zu) %zu of %zu%s", count,
391 selected + 1, count, dropped ? " [older errors dropped]" : "");
393 ui_text_cell(heading, sizeof(heading), title, width - 4, UI_ALIGN_LEFT);
394 frame_buffer_printf(out, "\033[%d;%dH%s", top + 1, left + 2,
396 : heading);
399 format_error_heading(code, sizeof(code), &error);
400 ui_text_cell(cell, sizeof(cell), code, width - 4, UI_ALIGN_LEFT);
401 frame_buffer_printf(out, "\033[%d;%dH%s", top + 2, left + 2, cell);
402 for (int i = 0; i < body_rows; ++i) {
403 size_t index = (size_t)i < fixed_rows ? (size_t)i : (size_t)i + scroll;
404 if (index >= g_line_count)
405 break;
406 const char *start = g_wrapped + g_lines[index];
407 const char *end = strchr(start, '\n');
408 frame_buffer_printf(out, "\033[%d;%dH%.*s\033[0m", top + 4 + i, left + 2,
409 (int)(end ? end - start : (ptrdiff_t)strlen(start)),
410 start);
411 }
412 ui_scrollbar_render(out, top + 4 + (int)fixed_rows, left + width - 3,
413 (int)page_rows, scroll_rows, page_rows, scroll);
414 const char *orange = colors ? ui_text_color_prefix(LOG_COLOR_DEV) : "";
415 const char *reset = colors ? "\033[0m" : "";
416 frame_buffer_printf(out, "\033[%d;%dH%s?%s Help", top + height - 2, left + 2,
417 orange, reset);
418 ui_button_t button = {.label = "Dismiss all", .shortcut = '\r'};
419 ui_buttons_render(out, top + height - 2, left + 10, width - 20, &button, 1,
420 0);
421}
void ui_buttons_render(frame_buffer_t *out, int row, int left, int width, const ui_button_t *b, size_t n, size_t selected)
Definition buttons.c:30
@ LOG_COLOR_DEV
Definition color.h:7
#define TUI_ERROR_POPUP_HEADING_BUFFER_SIZE
Definition error_popup.c:11
#define TUI_ERROR_POPUP_TITLE_BUFFER_SIZE
Definition error_popup.c:10
terminal_size_t ui_error_popup_minimum(void)
Definition error_popup.c:84
unsigned int height
Definition fft.c:24
int frame_buffer_get_screen_output_fd(void)
Get current output file descriptor for terminal screens.
asciichat_error_t ui_popup_frame(frame_buffer_t *out, int row, int col, int width, int height, log_color_t color)
Definition popup.c:55
static bool size_t index
Definition queries.h:10
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
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
void ui_too_small_render(frame_buffer_t *buffer, terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:44
bool ui_too_small(terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:10

References colored_string(), colors, asciichat_error_context_t::error_id, frame_buffer_append(), frame_buffer_get_screen_output_fd(), frame_buffer_printf(), GET_OPTION, height, index, LOG_COLOR_DEV, LOG_COLOR_ERROR, LOG_COLOR_RESET, MIN, options_terminal_settings, out, terminal_should_color_output(), TUI_ERROR_POPUP_CELL_BUFFER_SIZE, TUI_ERROR_POPUP_CODE_BUFFER_SIZE, TUI_ERROR_POPUP_HEADING_BUFFER_SIZE, TUI_ERROR_POPUP_TITLE_BUFFER_SIZE, UI_ALIGN_LEFT, ui_buttons_render(), ui_error_popup_minimum(), ui_popup_frame(), ui_scrollbar_render(), ui_text_cell(), ui_text_color_prefix(), ui_too_small(), ui_too_small_render(), and width.

◆ ui_error_popup_reset()

void ui_error_popup_reset ( void )

Definition at line 423 of file error_popup.c.

423 {
424 lock_state();
425 g_count = g_selected = g_scroll = 0;
426 g_latest = g_displayed_latest = g_dropped = 0;
427 unlock_state();
428 g_cached_id = 0;
429}

Referenced by ui_controller_shutdown(), and ui_reconnect_controller_set().

◆ ui_error_popup_revision()

uint64_t ui_error_popup_revision ( void )

Definition at line 76 of file error_popup.c.

76 {
77 lock_state();
78 refresh_locked();
79 uint64_t revision = g_latest;
80 unlock_state();
81 return revision;
82}

◆ ui_error_popup_visible()

bool ui_error_popup_visible ( void )

Error notifications are an overlay, independent of the selected application screen.

Definition at line 74 of file error_popup.c.

References errno_notifications_pending().

Referenced by ui_controller_current_screen(), ui_input_read_key(), and update_banner_show_prompt().