ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
prompt.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define TUI_PROMPT_CENTERED_BUFFER_SIZE 2048
4#define TUI_PROMPT_TARGET_BUFFER_SIZE 256
5#define TUI_PROMPT_SHORTCUTS_BUFFER_SIZE 1024
6
7#include <ascii-chat/common.h>
15#include <ascii-chat/ui/input.h>
16#include <ascii-chat/ui/popup.h>
17#include <ascii-chat/ui/text.h>
21#include <options/options.h>
22#include <string.h>
23
43
44static void media_center_line(frame_buffer_t *buffer, const char *text, int row,
45 int cols) {
46 int width = utf8_display_width(text);
47 int col = (cols - width) / 2 + 1;
48 frame_buffer_printf(buffer, "\033[%d;1H%*s", row, col > 1 ? col - 1 : 0, "");
49 int used = 0;
50 for (const char *p = text; *p;) {
51 int bytes = utf8_next_char_bytes(p, strlen(p));
52 if (bytes <= 0)
53 break;
54 int cells = utf8_display_width_n(p, (size_t)bytes);
55 if (used + cells > cols - 2)
56 break;
57 frame_buffer_append(buffer, p, (size_t)bytes);
58 used += cells;
59 p += bytes;
60 }
61 frame_buffer_append(buffer, "\033[K", 3);
62}
63
64static void media_panel_clear_background(frame_buffer_t *buffer,
65 terminal_size_t size, int top,
66 int logo_rows, int shown) {
67 for (int row = 1; row <= size.rows; ++row) {
68 bool logo = row >= top && row < top + logo_rows;
69 bool heading = row == top + logo_rows + 1 || row == top + logo_rows + 2;
70 bool input =
71 row >= top + logo_rows + 4 && row < top + logo_rows + 4 + shown;
72 if (!logo && !heading && !input)
73 frame_buffer_printf(buffer, "\033[%d;1H\033[K", row);
74 }
75}
76
77void ui_media_panel_render(frame_buffer_t *buffer, terminal_size_t size,
78 const char *title, const char *instruction,
79 const char *text, size_t cursor) {
80 bool compact = size.cols < 52;
81 int logo_rows = compact ? 1 : 4;
82 int lines = 1;
83 if (text) {
84 for (size_t i = 0; text[i]; ++i) {
85 if (text[i] == '\n') {
86 ++lines;
87 }
88 }
89 }
90 int available = size.rows - logo_rows - 5;
91 if (available < 1)
92 available = 1;
93 int shown = text ? (lines < available ? lines : available) : 0;
94 int height = logo_rows + 3 + (text ? 1 + shown : 0);
95 int top = (size.rows - height) / 2 + 1;
96 if (top < 1)
97 top = 1;
98 bool colors =
99 !GET_OPTION(strip_ansi) &&
102 frame_buffer_append(buffer, "\033[0m\033[H\033[?25l", 13);
103 frame_buffer_printf(buffer, "\033[%d;1H", top);
105 int tick = sample ? (int)(sample->seconds * 60)
106 : (int)((time_get_ns() / NS_PER_MS_INT / 16) % 100000);
107 invitation_render_logo(buffer, size.cols, tick, colors, compact);
108 media_center_line(buffer, title, top + logo_rows + 1, size.cols);
109 media_center_line(buffer, instruction, top + logo_rows + 2, size.cols);
110 if (!text) {
111 media_panel_clear_background(buffer, size, top, logo_rows, 0);
112 return;
113 }
114 int width = size.cols - 6;
115 if (width > 76)
116 width = 76;
117 int longest = 20;
118 const char *line = text;
119 for (const char *p = text;; ++p) {
120 if (!*p || *p == '\n') {
121 int n = utf8_display_width_n(line, (size_t)(p - line));
122 if (n > longest)
123 longest = n;
124 if (!*p)
125 break;
126 line = p + 1;
127 }
128 }
129 if (longest < width)
130 width = longest;
131 int col = (size.cols - (width + 2)) / 2 + 1;
132 for (int i = 0; i < shown; ++i)
133 frame_buffer_printf(buffer, "\033[%d;1H\033[K", top + logo_rows + 4 + i);
134 ui_input_render(buffer, top + logo_rows + 4, col, width, shown, text, cursor,
135 true);
136 media_panel_clear_background(buffer, size, top, logo_rows, shown);
137}
138
139static int password_lines(const prompt_snapshot_t *s) {
140 int lines = 1;
141 for (const char *p = s->text + s->prompt_bytes; *p; ++p)
142 lines += *p == '\n';
143 return lines;
144}
145
146static void password_center_line(frame_buffer_t *buffer, const char *text,
147 int row, int left, int width) {
148 char centered[TUI_PROMPT_CENTERED_BUFFER_SIZE];
149 ui_text_cell(centered, sizeof(centered), text, width - 4, UI_ALIGN_CENTER);
150 frame_buffer_printf(buffer, "\033[%d;%dH%s", row, left + 2, centered);
151}
152
153static void password_panel(frame_buffer_t *buffer, terminal_size_t size,
154 const prompt_snapshot_t *s) {
155 int width = size.cols - 4;
156 if (width > 64)
157 width = 64;
158 int lines = password_lines(s);
159 int height = 13 + lines;
160 int top = (size.rows - height) / 2 + 1;
161 int logo_rows = size.cols >= 52 && top >= 6 ? 4 : 1;
162 int logo_top = top - logo_rows - (top >= 3 ? 1 : 0);
163 frame_buffer_printf(buffer, "\033[0m\033[H\033[2J\033[?25l\033[%d;1H",
164 logo_top);
166 invitation_render_logo(buffer, size.cols,
167 sample ? (int)(sample->seconds * 60) : 0, s->color,
168 logo_rows == 1);
169 if (top >= 3)
170 media_center_line(buffer, "Video chat in your terminal", top - 1,
171 size.cols);
172 int left = (size.cols - width) / 2 + 1;
174 "Password required");
176 snprintf(target, sizeof(target), "Connect to %.*s", width - 15, s->text);
177 password_center_line(buffer, target, top + 2, left, width);
178 password_center_line(buffer, "This server requires a password.", top + 3,
179 left, width);
180 if (s->incorrect || s->newline_limit)
181 password_center_line(
182 buffer,
184 ? "Maximum 3 newlines."
185 : "Incorrect password. Try again."),
186 top + 4, left, width);
187 frame_buffer_printf(buffer, "\033[%d;%dHPassword", top + 6, left + 2);
188 ui_popup_frame(buffer, top + 7, left + 2, width - 4, lines + 2,
190 const ui_button_t buttons[] = {
191 {.label = "[ Confirm ]",
192 .disabled = !s->can_confirm,
193 .color = LOG_COLOR_INFO},
194 {.label = "[ Cancel ]", .color = LOG_COLOR_ERROR}};
195 ui_buttons_render(buffer, top + 9 + lines, left + 2, width - 4, buttons, 2,
196 s->selected_button);
197 const char *orange = s->color ? ui_text_color_prefix(LOG_COLOR_DEV) : "";
198 const char *foreground =
200 char shortcuts[TUI_PROMPT_SHORTCUTS_BUFFER_SIZE];
201 snprintf(shortcuts, sizeof(shortcuts),
202 "%sTab%s select %sEnter%s confirm %sEsc%s cancel", orange,
203 foreground, orange, foreground, orange, foreground);
204 password_center_line(buffer, shortcuts, top + 10 + lines, left, width);
205 snprintf(shortcuts, sizeof(shortcuts), "%sShift+Enter%s newline (maximum 3)",
206 orange, foreground);
207 password_center_line(buffer, shortcuts, top + 11 + lines, left, width);
208 ui_input_render(buffer, top + 8, left + 3, width - 8, lines,
209 s->text + s->prompt_bytes, s->cursor, false);
210}
211
212const char *ui_prompt_title(const void *data) {
213 const prompt_snapshot_t *snapshot = data;
214 if (strncmp(snapshot->text, "Open media:", 11) == 0)
215 return "load file/url";
216 if (strstr(snapshot->text, "(y/N)") || strstr(snapshot->text, "(Y/n)"))
217 return "confirmation";
218 if (snapshot->secret)
219 return "password required";
220 return "input";
221}
222
223static void render_prompt(terminal_size_t size, const void *data) {
224 const prompt_snapshot_t *snapshot = data;
225 const char *visible = snapshot->text + snapshot->prompt_bytes;
226 frame_buffer_t *buffer = frame_buffer_create(size.rows, size.cols);
227 if (!buffer)
228 return;
229 if (snapshot->password_dialog) {
230 password_panel(buffer, size, snapshot);
231 ui_controller_write(STDERR_FILENO, frame_buffer_get_content(buffer),
233 frame_buffer_destroy(buffer);
234 return;
235 }
236 if (strncmp(snapshot->text, "Open media:", 11) == 0) {
238 buffer, size, "Open media:",
239 "Drop files / paste paths or URLs; Enter plays; Esc cancels", visible,
240 snapshot->cursor);
241 ui_controller_write(STDERR_FILENO, frame_buffer_get_content(buffer),
243 frame_buffer_destroy(buffer);
244 return;
245 }
246 frame_buffer_append(buffer, "\033[0m\033[H", 7);
247 int row = ui_notice_render(buffer, snapshot->text, snapshot->severity,
248 size.cols, snapshot->unicode, snapshot->color) +
249 2;
250 frame_buffer_printf(buffer, "\033[%d;1H\033[2K", row);
251 ui_input_render(buffer, row, 1, size.cols - 3, 1, visible, snapshot->cursor,
252 false);
253 ui_controller_write(STDERR_FILENO, frame_buffer_get_content(buffer),
255 frame_buffer_destroy(buffer);
256}
257
258static int prompt_fd(void) {
260 return STDERR_FILENO; // The controller inherits the active screen's
261 // retained terminal.
262 return platform_isatty(STDERR_FILENO) ? STDERR_FILENO : STDOUT_FILENO;
263}
264
265const char *ui_prompt_usage_name(const void *data) {
266 const prompt_snapshot_t *snapshot = data;
267 if (!snapshot)
268 return "input_prompt";
269 return ui_prompt_kind_name(snapshot->kind);
270}
271
272const char *ui_prompt_kind_name(ui_prompt_kind_t kind) {
273 switch (kind) {
274 case UI_PROMPT_PASSWORD:
275 return "password_prompt";
276 case UI_PROMPT_MEDIA_INPUT:
277 return "media_input_prompt";
278 case UI_PROMPT_CONFIRMATION:
279 return "confirmation_prompt";
280 case UI_PROMPT_SSH_PASSPHRASE:
281 return "ssh_passphrase_prompt";
282 case UI_PROMPT_GPG_PASSPHRASE:
283 return "gpg_passphrase_prompt";
284 case UI_PROMPT_TRUST_HOST:
285 return "trust_host_prompt";
286 case UI_PROMPT_UNVERIFIED_HOST:
287 return "unverified_host_prompt";
288 case UI_PROMPT_DISCOVERY_KEY_CHANGE:
289 return "discovery_key_change_prompt";
290 case UI_PROMPT_IP_DISCLOSURE:
291 return "ip_disclosure_prompt";
292 case UI_PROMPT_CONFIG_OVERWRITE:
293 return "config_overwrite_prompt";
294 case UI_PROMPT_MANPAGE_OVERWRITE:
295 return "manpage_overwrite_prompt";
296 case UI_PROMPT_COMPLETION_OVERWRITE:
297 return "completion_overwrite_prompt";
298 default:
299 return "input_prompt";
300 }
301}
302
303asciichat_error_t ui_prompt_present(const char *prompt, const char *visible,
304 size_t cursor, bool secret) {
305 return ui_prompt_present_kind(prompt, visible, cursor, secret,
306 secret ? UI_PROMPT_PASSWORD : UI_PROMPT_INPUT);
307}
308
310 const char *visible, size_t cursor,
311 bool secret, ui_prompt_kind_t kind) {
312 size_t prompt_bytes = strlen(prompt) + 1;
313 size_t visible_bytes = strlen(visible) + 1;
314 prompt_snapshot_t *snapshot = SAFE_MALLOC(
315 sizeof(*snapshot) + prompt_bytes + visible_bytes, prompt_snapshot_t *);
316 if (!snapshot)
317 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate prompt presentation");
318 snapshot->kind = kind;
319 snapshot->password_dialog = snapshot->incorrect = snapshot->can_confirm =
320 snapshot->newline_limit = false;
321 snapshot->selected_button = 0;
322 snapshot->prompt_bytes = prompt_bytes;
323 snapshot->cursor = cursor;
324 snapshot->secret = secret;
326 bool media = strncmp(prompt, "Open media:", 11) == 0;
327 snapshot->color =
328 !media && !GET_OPTION(strip_ansi) &&
330 snapshot->unicode =
331 !media && terminal_should_use_utf8(GET_OPTION(force_utf8));
332 memcpy(snapshot->text, prompt, prompt_bytes);
333 memcpy(snapshot->text + prompt_bytes, visible, visible_bytes);
334 // Height is measured at the minimum width, so every larger terminal fits.
335 terminal_size_t minimum = {
336 .cols = 40,
337 .rows =
338 ui_notice_render(NULL, prompt, snapshot->severity, 40, false, false) +
339 3};
340 if (media)
341 minimum.rows = 8;
343 UI_SCREEN_PROMPT, prompt_fd(), minimum, render_prompt, snapshot,
344 sizeof(*snapshot) + prompt_bytes + visible_bytes);
345 SAFE_FREE(snapshot);
346 return result;
347}
348
349void ui_prompt_dismiss(int previous) {
352 ui_controller_write(prompt_fd(), "\r\n", 2);
353}
354
356
357int ui_prompt_required_rows(const void *data, int cols) {
358 const prompt_snapshot_t *snapshot = data;
359 if (snapshot->password_dialog) {
360 int rows = 15 + password_lines(snapshot);
361 return rows < 19 ? 19 : rows;
362 }
363 if (strncmp(snapshot->text, "Open media:", 11) == 0)
364 return cols < 52 ? 8 : 11;
365 return ui_notice_render(NULL, snapshot->text, snapshot->severity, cols, false,
366 false) +
367 3;
368}
369
370static void render_media_loading(terminal_size_t size, const void *data) {
371 (void)data;
372 frame_buffer_t *buffer = frame_buffer_create(size.rows, size.cols);
373 if (!buffer)
374 return;
375 ui_media_panel_render(buffer, size, "Loading media...",
376 "Esc cancels; current playback continues.", NULL, 0);
377 ui_controller_write(STDERR_FILENO, frame_buffer_get_content(buffer),
379 frame_buffer_destroy(buffer);
380}
381
384 (terminal_size_t){.cols = 40, .rows = 8},
385 render_media_loading, "", 1);
386}
387
389 const char *masked, size_t cursor,
390 size_t selected_button,
391 bool incorrect, bool can_confirm,
392 bool newline_limit) {
393 if (!target || !masked || selected_button > 1)
394 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid password presentation");
395 size_t target_bytes = strlen(target) + 1, masked_bytes = strlen(masked) + 1;
396 size_t bytes = sizeof(prompt_snapshot_t) + target_bytes + masked_bytes;
398 if (!s)
399 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate password presentation");
400 s->prompt_bytes = target_bytes;
401 s->cursor = cursor;
402 s->selected_button = selected_button;
403 s->kind = UI_PROMPT_PASSWORD;
404 s->password_dialog = true;
405 s->secret = true;
406 s->incorrect = incorrect;
407 s->can_confirm = can_confirm;
408 s->newline_limit = newline_limit;
409 s->color =
410 !GET_OPTION(strip_ansi) &&
412 memcpy(s->text, target, target_bytes);
413 memcpy(s->text + target_bytes, masked, masked_bytes);
415 UI_SCREEN_PROMPT, prompt_fd(), (terminal_size_t){.cols = 40, .rows = 19},
416 render_prompt, s, bytes);
417 SAFE_FREE(s);
418 return result;
419}
#define SET_ERRNO(code,...)
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_RESET
Definition color.h:14
@ LOG_COLOR_ERROR
Definition color.h:11
@ LOG_COLOR_DEV
Definition color.h:7
@ LOG_COLOR_DEBUG
Definition color.h:8
@ LOG_COLOR_INFO
Definition color.h:9
@ LOG_COLOR_WARN
Definition color.h:10
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned int height
Definition fft.c:24
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.
int frame_buffer_get_screen_output_fd(void)
Get current output file descriptor for terminal screens.
Buffered terminal frame rendering.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_INVALID_PARAM
#define NS_PER_MS_INT
Definition time.h:172
#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
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
int utf8_display_width(const char *str)
Calculate terminal display width of a UTF-8 string.
Definition utf8.c:102
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:183
⚙️ 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
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
ui_notice_severity_t ui_notice_prompt_severity(void)
Definition notice.c:572
ui_notice_severity_t
Definition notice.h:13
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
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
asciichat_error_t ui_popup_frame_titled(frame_buffer_t *out, int row, int col, int width, int height, log_color_t color, const char *title)
Definition popup.c:62
asciichat_error_t ui_prompt_present(const char *prompt, const char *visible, size_t cursor, bool secret)
Definition prompt.c:303
const char * ui_prompt_title(const void *data)
Definition prompt.c:212
#define TUI_PROMPT_SHORTCUTS_BUFFER_SIZE
Definition prompt.c:5
const char * ui_prompt_usage_name(const void *data)
Definition prompt.c:265
void ui_media_panel_render(frame_buffer_t *buffer, terminal_size_t size, const char *title, const char *instruction, const char *text, size_t cursor)
Definition prompt.c:77
#define TUI_PROMPT_CENTERED_BUFFER_SIZE
Definition prompt.c:3
int ui_prompt_required_rows(const void *data, int cols)
Definition prompt.c:357
asciichat_error_t ui_password_prompt_present(const char *target, const char *masked, size_t cursor, size_t selected_button, bool incorrect, bool can_confirm, bool newline_limit)
Definition prompt.c:388
#define TUI_PROMPT_TARGET_BUFFER_SIZE
Definition prompt.c:4
void ui_prompt_remove(void)
Definition prompt.c:355
asciichat_error_t ui_prompt_present_kind(const char *prompt, const char *visible, size_t cursor, bool secret, ui_prompt_kind_t kind)
Definition prompt.c:309
asciichat_error_t ui_media_loading_present(void)
Definition prompt.c:382
const char * ui_prompt_kind_name(ui_prompt_kind_t kind)
Definition prompt.c:272
void ui_prompt_dismiss(int previous)
Definition prompt.c:349
bool terminal_should_color_output(int fd, const terminal_options_t *options)
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
For animation config, this is the live record that ties type, fps, speed, hidden_policy together....
bool newline_limit
Definition prompt.c:29
ui_notice_severity_t severity
Definition prompt.c:30
size_t cursor
Definition prompt.c:27
size_t prompt_bytes
Definition prompt.c:26
bool password_dialog
Definition prompt.c:29
char text[1]
Definition prompt.c:34
ui_prompt_kind_t kind
Definition prompt.c:25
size_t selected_button
Definition prompt.c:28
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
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_CENTER
Definition text.h:14
⏱️ High-precision timing utilities using sokol_time.h and uthash
void ui_controller_dismiss(ui_screen_t screen, int previous)
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)
const animation_sample_t * ui_controller_animation_sample(void)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
@ UI_SCREEN_PROMPT
@ UI_SCREEN_RENDER_PROGRESS
asciichat_error_t ui_input_render(frame_buffer_t *buffer, int row, int col, int width, int height, const char *text, size_t cursor, bool preserve_quotes)
Definition ui/input.c:6
🔤 UTF-8 Encoding and Decoding Utilities
int platform_isatty(int fd)
Definition util.c:70