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.h File Reference

Go to the source code of this file.

Functions

asciichat_error_t ui_prompt_present (const char *prompt, const char *visible, size_t cursor, bool secret)
asciichat_error_t ui_prompt_present_kind (const char *prompt, const char *visible, size_t cursor, bool secret, ui_prompt_kind_t kind)
const char * ui_prompt_kind_name (ui_prompt_kind_t kind)
const char * ui_prompt_usage_name (const void *snapshot)
void ui_prompt_dismiss (int previous)
void ui_prompt_remove (void)
const char * ui_prompt_title (const void *snapshot)
int ui_prompt_required_rows (const void *snapshot, int cols)
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)
asciichat_error_t ui_media_loading_present (void)
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)

Function Documentation

◆ ui_media_loading_present()

asciichat_error_t ui_media_loading_present ( void )

Present the media-loading screen while capture resolves its next source.

Definition at line 382 of file prompt.c.

382 {
384 (terminal_size_t){.cols = 40, .rows = 8},
385 render_media_loading, "", 1);
386}
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)
@ UI_SCREEN_RENDER_PROGRESS

References ui_controller_submit(), and UI_SCREEN_RENDER_PROGRESS.

◆ ui_media_panel_render()

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 )

Shared centered media panel with the animated splash logo. NULL text shows loading.

Definition at line 77 of file prompt.c.

79 {
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}
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_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.
#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
void invitation_render_logo(frame_buffer_t *buf, int cols, int frame, bool colors, bool compact)
Definition invitation.c:45
bool terminal_should_color_output(int fd, const terminal_options_t *options)
For animation config, this is the live record that ties type, fps, speed, hidden_policy together....
const animation_sample_t * ui_controller_animation_sample(void)
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

References colors, frame_buffer_append(), frame_buffer_get_screen_output_fd(), frame_buffer_printf(), GET_OPTION, height, invitation_render_logo(), NS_PER_MS_INT, options_terminal_settings, animation_sample_t::seconds, terminal_should_color_output(), ui_controller_animation_sample(), ui_input_render(), utf8_display_width_n(), and width.

◆ ui_password_prompt_present()

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 )

The snapshot contains masked text only. Selection: confirm=0, cancel=1.

Definition at line 388 of file prompt.c.

392 {
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,...)
#define SAFE_FREE(ptr)
#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
bool newline_limit
Definition prompt.c:29
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
@ UI_SCREEN_PROMPT

References prompt_snapshot_t::can_confirm, prompt_snapshot_t::color, prompt_snapshot_t::cursor, ERROR_INVALID_PARAM, ERROR_MEMORY, GET_OPTION, prompt_snapshot_t::incorrect, prompt_snapshot_t::kind, prompt_snapshot_t::newline_limit, options_terminal_settings, prompt_snapshot_t::password_dialog, prompt_snapshot_t::prompt_bytes, SAFE_CALLOC, SAFE_FREE, prompt_snapshot_t::secret, prompt_snapshot_t::selected_button, SET_ERRNO, terminal_should_color_output(), prompt_snapshot_t::text, ui_controller_submit(), and UI_SCREEN_PROMPT.

◆ ui_prompt_dismiss()

void ui_prompt_dismiss ( int previous)

Dismiss a prompt and restore its previous interactive owner, if still live.

Definition at line 349 of file prompt.c.

349 {
352 ui_controller_write(prompt_fd(), "\r\n", 2);
353}
void ui_controller_dismiss(ui_screen_t screen, int previous)
bool ui_controller_is_presenting(void)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)

References ui_controller_dismiss(), ui_controller_is_presenting(), ui_controller_write(), and UI_SCREEN_PROMPT.

Referenced by ui_prompt_remove().

◆ ui_prompt_kind_name()

const char * ui_prompt_kind_name ( ui_prompt_kind_t kind)

Definition at line 272 of file prompt.c.

272 {
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}

Referenced by ui_prompt_usage_name().

◆ ui_prompt_present()

asciichat_error_t ui_prompt_present ( const char * prompt,
const char * visible,
size_t cursor,
bool secret )

Only display-safe text crosses this boundary; never pass an unmasked password.

Definition at line 303 of file prompt.c.

304 {
305 return ui_prompt_present_kind(prompt, visible, cursor, secret,
306 secret ? UI_PROMPT_PASSWORD : UI_PROMPT_INPUT);
307}
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

References ui_prompt_present_kind().

◆ ui_prompt_present_kind()

asciichat_error_t ui_prompt_present_kind ( const char * prompt,
const char * visible,
size_t cursor,
bool secret,
ui_prompt_kind_t kind )

Definition at line 309 of file prompt.c.

311 {
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}
#define SAFE_MALLOC(size, cast)
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
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
ui_notice_severity_t severity
Definition prompt.c:30

References prompt_snapshot_t::can_confirm, prompt_snapshot_t::color, prompt_snapshot_t::cursor, ERROR_MEMORY, GET_OPTION, prompt_snapshot_t::incorrect, prompt_snapshot_t::kind, prompt_snapshot_t::newline_limit, options_terminal_settings, prompt_snapshot_t::password_dialog, prompt_snapshot_t::prompt_bytes, SAFE_FREE, SAFE_MALLOC, prompt_snapshot_t::secret, prompt_snapshot_t::selected_button, SET_ERRNO, prompt_snapshot_t::severity, terminal_should_color_output(), terminal_should_use_utf8(), prompt_snapshot_t::text, ui_controller_submit(), ui_notice_prompt_severity(), ui_notice_render(), UI_SCREEN_PROMPT, and prompt_snapshot_t::unicode.

Referenced by ui_input_media_begin(), ui_input_read_key(), and ui_prompt_present().

◆ ui_prompt_remove()

void ui_prompt_remove ( void )

Remove an asynchronous prompt without restoring an earlier interactive view.

Definition at line 355 of file prompt.c.

355{ ui_prompt_dismiss(-1); }
void ui_prompt_dismiss(int previous)
Definition prompt.c:349

References ui_prompt_dismiss().

Referenced by session_handle_keyboard_input(), and ui_input_read_key().

◆ ui_prompt_required_rows()

int ui_prompt_required_rows ( const void * snapshot,
int cols )

Definition at line 357 of file prompt.c.

357 {
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}

References prompt_snapshot_t::password_dialog, prompt_snapshot_t::severity, prompt_snapshot_t::text, and ui_notice_render().

◆ ui_prompt_title()

const char * ui_prompt_title ( const void * snapshot)

Definition at line 212 of file prompt.c.

212 {
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}

References prompt_snapshot_t::secret, and prompt_snapshot_t::text.

◆ ui_prompt_usage_name()

const char * ui_prompt_usage_name ( const void * snapshot)

Definition at line 265 of file prompt.c.

265 {
266 const prompt_snapshot_t *snapshot = data;
267 if (!snapshot)
268 return "input_prompt";
269 return ui_prompt_kind_name(snapshot->kind);
270}
const char * ui_prompt_kind_name(ui_prompt_kind_t kind)
Definition prompt.c:272

References prompt_snapshot_t::kind, and ui_prompt_kind_name().