ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
notice.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
11#include <libplatform/filesystem.h>
12#include <libplatform/keyboard.h>
13#include <libplatform/question.h>
14#include <options/options.h>
15#include <stdatomic.h>
16#include <string.h>
17
18static _Thread_local ui_prompt_kind_t g_prompt_kind = UI_PROMPT_CONFIRMATION;
19static _Thread_local ui_notice_severity_t g_prompt_severity = UI_NOTICE_INFO;
20
21static const char *notice_color(ui_notice_severity_t severity) {
22 switch (severity) {
23 case UI_NOTICE_FATAL:
31 default:
33 }
34}
35
36static const char *notice_label(ui_notice_severity_t severity) {
37 switch (severity) {
38 case UI_NOTICE_FATAL:
39 return "FATAL";
41 return "ERROR";
43 return "WARNING";
45 return "SUCCESS";
46 default:
47 return "INFO";
48 }
49}
50
51static void border(frame_buffer_t *buffer, int width, ui_popup_edge_t edge,
52 bool unicode, ui_notice_severity_t severity, bool color) {
53 if (!buffer)
54 return;
55 log_color_t shade = severity == UI_NOTICE_FATAL ? LOG_COLOR_FATAL
56 : severity == UI_NOTICE_DANGER ? LOG_COLOR_ERROR
57 : severity == UI_NOTICE_WARNING ? LOG_COLOR_WARN
58 : severity == UI_NOTICE_SUCCESS ? LOG_COLOR_INFO
60 ui_popup_rule(buffer, width, edge, true, unicode,
61 color ? shade : LOG_COLOR_RESET);
62 frame_buffer_printf(buffer, "\n%s%s", color ? "\033[1m" : "",
63 color ? notice_color(severity) : "");
64}
65
66int ui_notice_render(frame_buffer_t *buffer, const char *text,
67 ui_notice_severity_t severity, int cols, bool unicode,
68 bool color) {
69 if (!text)
70 text = "";
71 const char *body = text;
72 text = notice_label(severity);
73 bool rendering_label = true;
74 const char *text_end = text + strlen(text);
75 // Reserve the final column to avoid terminal autowrap at the right border.
76 int width = cols > 81 ? 80 : cols - 1;
77 bool boxed = width >= 8;
78 int content = boxed ? width - 4 : (cols > 2 ? cols - 1 : 2);
79 const char *side = unicode ? "║" : "|";
80 if (buffer && color)
81 frame_buffer_printf(buffer, "\033[1m%s", notice_color(severity));
82 if (boxed)
83 border(buffer, width, UI_POPUP_TOP, unicode, severity, color);
84 int rows = boxed ? 2 : 0;
85 const char *art_end = NULL;
86 const char *word_end = NULL;
87 bool title_word = false;
88 bool first_letter = false;
89 do {
90 // The Keymask formatter identifies complete plain-text blocks; input ANSI
91 // escapes still pass through the normal control-character sanitization.
92 const char *block_end = key_identity_art_end(text);
93 if (block_end)
94 art_end = block_end;
95 bool neutral = art_end && text < art_end;
96 int used = 0;
97 if (buffer && boxed)
98 frame_buffer_printf(buffer, "%s ", side);
99 if (buffer && color && neutral)
100 frame_buffer_append(buffer, "\033[0;39m", 7);
101 while (*text && *text != '\n') {
102 int bytes = utf8_next_char_bytes(text, (size_t)(text_end - text));
103 if (bytes <= 0)
104 bytes = 1;
105 bool control =
106 (unsigned char)*text < 32 || (unsigned char)*text == 127 ||
107 (bytes == 2 && (unsigned char)text[0] == 0xc2 &&
108 (unsigned char)text[1] >= 0x80 && (unsigned char)text[1] <= 0x9f);
109 int cells = control ? 1 : utf8_display_width_n(text, (size_t)bytes);
110 if (cells < 0)
111 cells = 1;
112 if (used + cells > content && used)
113 break;
114 if (buffer) {
115 if (control)
116 frame_buffer_append(buffer, "?", 1);
117 else if (!rendering_label && !neutral) {
118 if (!word_end || text >= word_end) {
119 word_end = text;
120 title_word = (*text >= 'A' && *text <= 'Z') ||
121 (*text >= 'a' && *text <= 'z');
122 first_letter = true;
123 while (*word_end && *word_end != ' ' && *word_end != '\n' &&
124 *word_end != '\t') {
125 unsigned char c = (unsigned char)*word_end;
126 bool letter = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
127 bool punctuation =
128 strchr(",.;:!?", c) &&
129 (!word_end[1] || word_end[1] == ' ' || word_end[1] == '\n');
130 if (!letter && !punctuation)
131 title_word = false;
132 ++word_end;
133 }
134 }
135 if (title_word && bytes == 1) {
136 char c = *text;
137 if (first_letter && c >= 'a' && c <= 'z')
138 c -= 'a' - 'A';
139 else if (!first_letter && c >= 'A' && c <= 'Z')
140 c += 'a' - 'A';
141 first_letter = false;
142 frame_buffer_append(buffer, &c, 1);
143 } else {
144 frame_buffer_append(buffer, text, (size_t)bytes);
145 }
146 } else
147 frame_buffer_append(buffer, text, (size_t)bytes);
148 }
149 used += cells;
150 text += bytes;
151 }
152 if (buffer) {
153 if (color && neutral)
154 frame_buffer_printf(buffer, "\033[1m%s", notice_color(severity));
155 if (boxed)
156 frame_buffer_printf(buffer, "%*s %s", content - used, "", side);
157 frame_buffer_append(buffer, "\n", 1);
158 }
159 ++rows;
160 if (*text == '\n')
161 ++text;
162 if (!*text) {
163 if (!rendering_label)
164 break;
165 if (boxed) {
166 border(buffer, width, UI_POPUP_SEPARATOR, unicode, severity, color);
167 ++rows;
168 }
169 rendering_label = false;
170 text = body;
171 text_end = text + strlen(text);
172 }
173 } while (true);
174 if (boxed)
175 border(buffer, width, UI_POPUP_BOTTOM, unicode, severity, color);
176 if (buffer && color)
177 frame_buffer_append(buffer, "\033[0m", 4);
178 return rows;
179}
180
202
203const char *ui_notice_title(const void *data) {
204 const notice_snapshot_t *notice = data;
205 return notice->severity == UI_NOTICE_WARNING ? "warning"
206 : notice->severity == UI_NOTICE_DANGER ||
207 notice->severity == UI_NOTICE_FATAL
208 ? "error"
209 : "notice";
210}
211
212#define FIRST_USE_LINE_COUNT 64
213#define FIRST_USE_LINE_BYTES 512
214#define FIRST_USE_CONTENT_COLUMNS 66
215
216static size_t
217first_use_wrap(const char *text, int width,
219 size_t count = 0;
220 for (const char *line = text; *line && count < FIRST_USE_LINE_COUNT;) {
221 const char *end = strchr(line, '\n');
222 if (!end)
223 end = line + strlen(line);
224 const char *tab = memchr(line, '\t', (size_t)(end - line));
225 bool shortcut = tab && tab - line == 1;
226 char key[2] = {0}, styled[128] = {0};
227 if (shortcut) {
228 key[0] = *line;
229 ui_help_keys(styled, sizeof(styled), key);
230 line = tab + 1;
231 }
232 if (line == end) {
233 lines[count++][0] = '\0';
234 } else {
235 bool first = true;
236 while (line < end && count < FIRST_USE_LINE_COUNT) {
237 while (line < end && *line == ' ')
238 ++line;
239 if (line == end)
240 break;
241 int available = width - (shortcut ? 3 : 0);
242 size_t length = (size_t)(end - line);
243 if (length > (size_t)available) {
244 length = (size_t)available;
245 size_t split = length;
246 while (split > 0 && line[split] != ' ')
247 --split;
248 if (split)
249 length = split;
250 }
251 if (shortcut)
252 snprintf(lines[count], FIRST_USE_LINE_BYTES, "%s %.*s",
253 first ? styled : " ", (int)length, line);
254 else
255 snprintf(lines[count], FIRST_USE_LINE_BYTES, "%.*s", (int)length,
256 line);
257 ++count;
258 first = false;
259 line += length;
260 }
261 }
262 line = *end ? end + 1 : end;
263 }
264 return count;
265}
266
267static void render_first_use(terminal_size_t size,
268 const notice_snapshot_t *notice) {
269 int width = size.cols - 6;
272 if (width < 4 || size.rows < 8)
273 return;
275 size_t count = first_use_wrap(notice->text, width, lines);
276 if (!count)
277 return;
278 int logo_rows = size.rows >= 15 && size.cols >= 52 ? 4
279 : size.rows >= 10 ? 1
280 : 0;
281 int page_rows = size.rows - 7 - logo_rows;
282 int pages = (int)((count + (size_t)page_rows - 1) / (size_t)page_rows);
283 notice_snapshot_t *state = (notice_snapshot_t *)notice;
284 if (state->rendered_cols != size.cols || state->rendered_rows != size.rows) {
285 atomic_store(&state->presented_page, -1);
286 state->rendered_cols = size.cols;
287 state->rendered_rows = size.rows;
288 }
289 atomic_store(&state->pages, pages);
290 int page = atomic_load(&state->page);
291 if (page >= pages) {
292 page = pages - 1;
293 atomic_store(&state->page, page);
294 }
295 const char *visible[FIRST_USE_LINE_COUNT];
296 size_t start = (size_t)page * (size_t)page_rows;
297 size_t visible_count = count - start;
298 if (visible_count > (size_t)page_rows)
299 visible_count = (size_t)page_rows;
300 for (size_t i = 0; i < visible_count; ++i)
301 visible[i] = lines[start + i];
302 char enter[128], escape[128], footer[512];
303 ui_help_keys(enter, sizeof(enter), "Enter");
304 ui_help_keys(escape, sizeof(escape), "Esc");
305 snprintf(footer, sizeof(footer), "%d/%d %s %s %s%s", page + 1, pages, enter,
306 page + 1 == pages ? "done" : "next", escape,
307 width >= 25 ? " close" : "");
308 ui_popup_t popup = {.title = notice->text + notice->title_offset,
309 .lines = visible,
310 .line_count = visible_count,
311 .footer = footer,
312 .top_margin = logo_rows,
313 .minimum_width = width + 4};
314 frame_buffer_t *output = frame_buffer_create(size.rows, size.cols);
315 if (!output)
316 return;
317 frame_buffer_append(output, "\033[H\033[2J", 7);
318 if (ui_popup_render(output, size, &popup)) {
319 if (logo_rows) {
320 int popup_height = (int)visible_count + 5;
321 int popup_top =
322 logo_rows + (size.rows - logo_rows - popup_height) / 2 + 1;
323 frame_buffer_printf(output, "\033[%d;1H", popup_top - logo_rows);
324 int tick = (int)((time_get_ns() / (NS_PER_SEC_INT / 60)) % 1800);
325 invitation_render_logo(output, size.cols - 1, tick, notice->color,
326 logo_rows == 1);
327 }
328 state->rendered_page = page;
329 ui_controller_write(STDERR_FILENO, frame_buffer_get_content(output),
331 }
332 frame_buffer_destroy(output);
333}
334
335static void render_notice(terminal_size_t size, const void *data) {
336 const notice_snapshot_t *notice = data;
337 if (notice->marker_offset) {
338 render_first_use(size, notice);
339 return;
340 }
341 // Only the presentation thread owns these counters. Every queued notice gets
342 // its own full reading interval when it first becomes visible.
343 static uint64_t active_id;
344 static uint64_t started;
345 static uint64_t last_tick;
346 static terminal_size_t previous_size;
347 uint64_t now = time_get_ns();
348 if (active_id != notice->started || previous_size.cols != size.cols ||
349 previous_size.rows != size.rows) {
350 active_id = notice->started;
351 started = now;
352 } else if (last_tick && now - last_tick > NS_PER_SEC_INT) {
353 // Time spent behind a prompt must not consume the reading interval.
354 started += now - last_tick;
355 }
356 last_tick = now;
357 previous_size = size;
358 frame_buffer_t *buffer = frame_buffer_create(0, 0);
359 if (!buffer)
360 return;
361 int rows = ui_notice_render(buffer, notice->text, notice->severity, size.cols,
362 notice->unicode, notice->color);
363 int page_rows = size.rows > 2 ? size.rows - 2 : 1;
364 int pages = (rows + page_rows - 1) / page_rows;
365 uint64_t page = (time_get_ns() - started) / (10 * NS_PER_SEC_INT);
366 if (page >= (uint64_t)pages) {
367 frame_buffer_destroy(buffer);
369 return;
370 }
371 const char *start = frame_buffer_get_content(buffer);
372 const char *end = start + frame_buffer_get_length(buffer);
373 for (uint64_t i = 0; i < page * (uint64_t)page_rows && start < end; ++i) {
374 const char *newline = memchr(start, '\n', (size_t)(end - start));
375 start = newline ? newline + 1 : end;
376 }
377 const char *stop = start;
378 for (int i = 0; i < page_rows && stop < end; ++i) {
379 const char *newline = memchr(stop, '\n', (size_t)(end - stop));
380 stop = newline ? newline + 1 : end;
381 }
382 ui_controller_write(STDERR_FILENO, "\033[H\033[2J", 7);
383 if (notice->color) {
384 ui_controller_write(STDERR_FILENO, "\033[1m", 4);
385 ui_controller_write(STDERR_FILENO, notice_color(notice->severity),
386 strlen(notice_color(notice->severity)));
387 }
388 ui_controller_write(STDERR_FILENO, start, (size_t)(stop - start));
389 if (notice->color)
390 ui_controller_write(STDERR_FILENO, "\033[0m", 4);
391 if (pages > 1)
392 ui_controller_printf(STDERR_FILENO, "Page %llu/%d (advances automatically)",
393 (unsigned long long)page + 1, pages);
394 frame_buffer_destroy(buffer);
395}
396
398 const char *text) {
399 if (!text)
400 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing notice text");
401 bool tty = platform_isatty(STDERR_FILENO);
402 bool color =
403 !GET_OPTION(strip_ansi) &&
405 bool unicode = tty && terminal_should_use_utf8(GET_OPTION(force_utf8));
406 if (severity != UI_NOTICE_FATAL && tty && ui_controller_is_presenting() &&
407 !ui_controller_is_owner() && !GET_OPTION(snapshot_mode)) {
408 size_t text_bytes = strlen(text) + 1;
409 size_t bytes = sizeof(notice_snapshot_t) + text_bytes;
410 notice_snapshot_t *snapshot = SAFE_MALLOC(bytes, notice_snapshot_t *);
411 if (!snapshot)
412 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate notice");
413 snapshot->severity = severity;
414 snapshot->unicode = unicode;
415 snapshot->color = color;
416 snapshot->started = time_get_ns();
417 snapshot->marker_offset = snapshot->title_offset = 0;
418 atomic_init(&snapshot->page, 0);
419 atomic_init(&snapshot->pages, 0);
420 atomic_init(&snapshot->presented_page, -1);
421 snapshot->rendered_page = -1;
422 snapshot->rendered_cols = snapshot->rendered_rows = 0;
423 memcpy(snapshot->text, text, text_bytes);
424 asciichat_error_t result =
426 (terminal_size_t){.cols = 1, .rows = 1},
427 render_notice, snapshot, bytes);
428 SAFE_FREE(snapshot);
429 return result;
430 }
431 frame_buffer_t *buffer = frame_buffer_create(0, 0);
432 if (!buffer)
433 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate notice buffer");
434 if (severity == UI_NOTICE_FATAL && tty)
435 frame_buffer_append(buffer, "\033[0m\033[?25h\r\n", 12);
436 int cols = tty ? ui_controller_size().cols : 80;
437 ui_notice_render(buffer, text, severity, cols, unicode, color);
439 if (severity == UI_NOTICE_FATAL && !ui_controller_is_owner())
440 ui_controller_finish(STDERR_FILENO, frame_buffer_get_content(buffer),
442 else
443 result =
444 ui_controller_write(STDERR_FILENO, frame_buffer_get_content(buffer),
446 frame_buffer_destroy(buffer);
447 return result;
448}
449
451 const char *text,
452 const char *marker_path) {
453 if (!title || !text || !marker_path || !platform_isatty(STDERR_FILENO) ||
454 GET_OPTION(snapshot_mode))
455 return SET_ERRNO(
457 "First-use notice requires an interactive terminal and marker path");
458 size_t text_bytes = strlen(text) + 1, title_bytes = strlen(title) + 1,
459 marker_bytes = strlen(marker_path) + 1;
460 if (title_bytes > SIZE_MAX - marker_bytes - sizeof(notice_snapshot_t) ||
461 text_bytes >
462 SIZE_MAX - title_bytes - marker_bytes - sizeof(notice_snapshot_t))
463 return SET_ERRNO(ERROR_BUFFER_OVERFLOW, "First-use notice is too large");
464 size_t bytes =
465 sizeof(notice_snapshot_t) + text_bytes + title_bytes + marker_bytes;
466 notice_snapshot_t *snapshot = SAFE_MALLOC(bytes, notice_snapshot_t *);
467 if (!snapshot)
468 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate first-use notice");
469 snapshot->severity = UI_NOTICE_INFO;
470 snapshot->unicode = terminal_should_use_utf8(GET_OPTION(force_utf8));
471 snapshot->color =
472 !GET_OPTION(strip_ansi) &&
474 snapshot->started = time_get_ns();
475 snapshot->title_offset = text_bytes;
476 snapshot->marker_offset = text_bytes + title_bytes;
477 atomic_init(&snapshot->page, 0);
478 atomic_init(&snapshot->pages, 0);
479 atomic_init(&snapshot->presented_page, -1);
480 snapshot->rendered_page = -1;
481 snapshot->rendered_cols = snapshot->rendered_rows = 0;
482 memcpy(snapshot->text, text, text_bytes);
483 memcpy(snapshot->text + text_bytes, title, title_bytes);
484 memcpy(snapshot->text + snapshot->marker_offset, marker_path, marker_bytes);
486 UI_SCREEN_NOTICE, STDERR_FILENO, (terminal_size_t){.cols = 20, .rows = 8},
487 render_notice, snapshot, bytes);
488 SAFE_FREE(snapshot);
489 return result;
490}
491
492void ui_notice_frame_presented(const void *data) {
493 notice_snapshot_t *notice = (notice_snapshot_t *)data;
494 if (notice && notice->marker_offset)
495 atomic_store(&notice->presented_page, notice->rendered_page);
496}
497
499 notice_snapshot_t *notice = (notice_snapshot_t *)data;
500 if (!notice || !notice->marker_offset)
502 if (key == KEY_ESCAPE || key == 'q' || key == 3)
504 if (key == KEY_QUESTION)
505 return UI_NOTICE_KEY_HELP;
506 int page = atomic_load(&notice->page);
507 int pages = atomic_load(&notice->pages);
508 if (key == KEY_LEFT && page > 0) {
509 atomic_store(&notice->presented_page, -1);
510 atomic_store(&notice->page, page - 1);
511 } else if ((key == '\r' || key == '\n' || key == KEY_RIGHT) && pages > 0 &&
512 atomic_load(&notice->presented_page) == page) {
513 if (page + 1 < pages) {
514 atomic_store(&notice->presented_page, -1);
515 atomic_store(&notice->page, page + 1);
516 } else if (key == '\r' || key == '\n') {
518 }
519 }
521}
522
523void ui_notice_first_use_complete(const void *data) {
524 const notice_snapshot_t *notice = data;
525 if (!notice || !notice->marker_offset)
526 return;
528 FILE *marker = platform_fopen("first-use instructions",
529 notice->text + notice->marker_offset, "w");
530 if (!marker) {
531 log_warn("Could not save first-use instructions state; the notice may "
532 "appear again: %s",
533 notice->text + notice->marker_offset);
534 } else if (fclose(marker) != 0) {
535 log_warn("Could not finish saving first-use instructions state; the notice "
536 "may appear again: %s",
537 notice->text + notice->marker_offset);
538 }
540}
541
542bool ui_notice_confirm(ui_notice_severity_t severity, const char *text,
543 unsigned timeout_seconds) {
544 return ui_notice_confirm_kind(UI_PROMPT_CONFIRMATION, severity, text,
545 timeout_seconds);
546}
547
548bool ui_notice_confirm_kind(ui_prompt_kind_t kind,
549 ui_notice_severity_t severity, const char *text,
550 unsigned timeout_seconds) {
551 ui_prompt_kind_t previous_kind = g_prompt_kind;
552 g_prompt_kind = kind;
553 ui_notice_severity_t previous = g_prompt_severity;
554 g_prompt_severity = severity;
555 if (terminal_can_prompt_user(!GET_OPTION(snapshot_mode)))
556 log_msg(severity == UI_NOTICE_FATAL ? LOG_FATAL
557 : severity == UI_NOTICE_DANGER ? LOG_ERROR
558 : severity == UI_NOTICE_WARNING ? LOG_WARN
559 : LOG_INFO,
560 __FILE__, __LINE__, __func__, "%s", text);
561 else
562 ui_notice_log(severity, __FILE__, __LINE__, __func__,
563 "CONFIRMATION REQUIRED", "%s", text);
564 bool accepted = platform_prompt_yes_no_timeout(text, false, timeout_seconds);
565 g_prompt_kind = previous_kind;
566 g_prompt_severity = previous;
567 return accepted;
568}
569
570ui_prompt_kind_t ui_notice_prompt_kind(void) { return g_prompt_kind; }
571
573 return g_prompt_severity;
574}
575
576void ui_notice_flush_snapshot(int fd, const void *data) {
577 const notice_snapshot_t *notice = data;
578 frame_buffer_t *buffer = frame_buffer_create(0, 0);
579 if (!buffer)
580 return;
581 if (platform_isatty(fd))
582 frame_buffer_append(buffer, "\033[0m\n", 5);
583 terminal_size_t size = {.cols = 80};
584 if (platform_isatty(fd))
585 (void)terminal_get_size_fd(fd, &size);
586 ui_notice_render(buffer, notice->text, notice->severity, size.cols,
587 notice->unicode, notice->color);
588 platform_write_all(fd, frame_buffer_get_content(buffer),
590 frame_buffer_destroy(buffer);
591}
592
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_DISMISSED
log_color_t
Definition color.h:6
@ LOG_COLOR_RESET
Definition color.h:14
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_ERROR
Definition color.h:11
@ LOG_COLOR_INFO
Definition color.h:9
@ LOG_COLOR_WARN
Definition color.h:10
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int width
Definition fft.c:24
FILE * platform_fopen(const char *name, const char *filename, const char *mode)
Definition filesystem.c:18
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 SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
void log_msg(log_level_t level, const char *file, int line, const char *func, const char *fmt,...)
Log a message at a specific level.
Definition log/log.c:1313
#define NS_PER_SEC_INT
Definition time.h:173
#define GET_OPTION(field)
Definition options.h:1339
#define options_terminal_settings()
Definition options.h:1287
int utf8_display_width_n(const char *str, size_t max_bytes)
Calculate terminal display width of a UTF-8 string up to byte limit.
Definition utf8.c:106
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:183
void ui_help_keys(char *out, size_t cap, const char *keys)
Definition help_menu.c:25
⚙️ Common definitions, error codes, macros, and types shared throughout the application
void invitation_render_logo(frame_buffer_t *buf, int cols, int frame, bool colors, bool compact)
Definition invitation.c:45
const char * key_identity_art_end(const char *text)
void ui_notice_log(ui_notice_severity_t severity, const char *file, int line, const char *func, const char *title, const char *format,...)
Definition log/log.c:1296
#define LOG_FATAL
Definition log/types.h:47
#define LOG_ERROR
Definition log/types.h:46
#define LOG_WARN
Definition log/types.h:45
#define LOG_INFO
Definition log/types.h:44
std::string escape(const std::string &str)
Escape a string for JSON output.
ui_prompt_kind_t ui_notice_prompt_kind(void)
Definition notice.c:570
#define FIRST_USE_CONTENT_COLUMNS
Definition notice.c:214
void ui_notice_dismiss(void)
Definition notice.c:593
asciichat_error_t ui_notice_present_first_use(const char *title, const char *text, const char *marker_path)
Definition notice.c:450
bool ui_notice_confirm(ui_notice_severity_t severity, const char *text, unsigned timeout_seconds)
Definition notice.c:542
int ui_notice_render(frame_buffer_t *buffer, const char *text, ui_notice_severity_t severity, int cols, bool unicode, bool color)
Definition notice.c:66
#define FIRST_USE_LINE_BYTES
Definition notice.c:213
#define FIRST_USE_LINE_COUNT
Definition notice.c:212
void ui_notice_flush_snapshot(int fd, const void *data)
Definition notice.c:576
ui_notice_key_result_t ui_notice_first_use_key(const void *data, int key)
Definition notice.c:498
void ui_notice_frame_presented(const void *data)
Definition notice.c:492
ui_notice_severity_t ui_notice_prompt_severity(void)
Definition notice.c:572
bool ui_notice_confirm_kind(ui_prompt_kind_t kind, ui_notice_severity_t severity, const char *text, unsigned timeout_seconds)
Definition notice.c:548
asciichat_error_t ui_notice_present(ui_notice_severity_t severity, const char *text)
Definition notice.c:397
const char * ui_notice_title(const void *data)
Definition notice.c:203
void ui_notice_first_use_complete(const void *data)
Definition notice.c:523
ui_notice_key_result_t
Definition notice.h:40
@ UI_NOTICE_KEY_HELP
Definition notice.h:43
@ UI_NOTICE_KEY_ORDINARY
Definition notice.h:41
@ UI_NOTICE_KEY_DISMISS
Definition notice.h:44
@ UI_NOTICE_KEY_COMPLETE
Definition notice.h:45
@ UI_NOTICE_KEY_HANDLED
Definition notice.h:42
ui_notice_severity_t
Definition notice.h:13
@ UI_NOTICE_WARNING
Definition notice.h:15
@ UI_NOTICE_SUCCESS
Definition notice.h:18
@ UI_NOTICE_INFO
Definition notice.h:14
@ UI_NOTICE_DANGER
Definition notice.h:16
@ UI_NOTICE_FATAL
Definition notice.h:17
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
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
bool ui_popup_render(frame_buffer_t *output, terminal_size_t size, const ui_popup_t *box)
Definition popup.c:80
ui_popup_edge_t
Definition popup.h:13
@ UI_POPUP_BOTTOM
Definition popup.h:16
@ UI_POPUP_SEPARATOR
Definition popup.h:15
@ UI_POPUP_TOP
Definition popup.h:14
int terminal_get_size_fd(int fd, terminal_size_t *size)
bool terminal_should_color_output(int fd, const terminal_options_t *options)
bool terminal_can_prompt_user(bool user_setting)
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
size_t marker_offset
Definition notice.c:186
atomic_int page
Definition notice.c:188
uint64_t started
Definition notice.c:185
ui_notice_severity_t severity
Definition notice.c:182
atomic_int presented_page
Definition notice.c:190
size_t title_offset
Definition notice.c:187
atomic_int pages
Definition notice.c:189
When code needs to work with ui popup, it keeps title, lines, line_count, footer here instead of scat...
Definition popup.h:42
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
⏱️ High-precision timing utilities using sokol_time.h and uthash
void ui_controller_remove(ui_screen_t screen)
asciichat_error_t ui_controller_submit(ui_screen_t screen, int fd, terminal_size_t minimum, ui_render_fn render, const void *snapshot, size_t bytes)
bool ui_controller_is_presenting(void)
bool ui_controller_is_owner(void)
void ui_controller_finish(int fd, const char *data, size_t len)
terminal_size_t ui_controller_size(void)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
asciichat_error_t ui_controller_printf(int fd, const char *format,...)
@ UI_SCREEN_NOTICE
🔤 UTF-8 Encoding and Decoding Utilities
int platform_isatty(int fd)
Definition util.c:70
bool platform_prompt_yes_no_timeout(const char *question, bool default_yes, unsigned timeout_seconds)
Definition util.c:95