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

Go to the source code of this file.

Macros

#define UI_POPUP_RENDERED_BUFFER_SIZE   1024

Functions

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)
asciichat_error_t ui_popup_frame_with_encoding (frame_buffer_t *out, int row, int col, int width, int height, log_color_t color, bool unicode)
asciichat_error_t ui_popup_frame (frame_buffer_t *out, int row, int col, int width, int height, log_color_t color)
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)
bool ui_popup_render (frame_buffer_t *output, terminal_size_t size, const ui_popup_t *box)

Macro Definition Documentation

◆ UI_POPUP_RENDERED_BUFFER_SIZE

#define UI_POPUP_RENDERED_BUFFER_SIZE   1024

Definition at line 3 of file popup.c.

Referenced by ui_popup_frame_titled(), and ui_popup_render().

Function Documentation

◆ ui_popup_frame()

asciichat_error_t ui_popup_frame ( frame_buffer_t * out,
int row,
int col,
int width,
int height,
log_color_t color )

Definition at line 55 of file popup.c.

56 {
58 out, row, col, width, height, color,
60}
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define GET_OPTION(field)
Definition options.h:1339
asciichat_error_t ui_popup_frame_with_encoding(frame_buffer_t *out, int row, int col, int width, int height, log_color_t color, bool unicode)
Definition popup.c:37
static bool size_t gotel_query_t * out
Definition queries.h:10
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)

References GET_OPTION, height, out, terminal_should_use_utf8(), ui_popup_frame_with_encoding(), and width.

Referenced by ui_error_popup_render(), ui_popup_frame_titled(), and ui_popup_render().

◆ ui_popup_frame_titled()

asciichat_error_t ui_popup_frame_titled ( frame_buffer_t * output,
int row,
int column,
int width,
int height,
log_color_t color,
const char * title )

Draw a frame with a centered, clipped title embedded in its top border.

Definition at line 62 of file popup.c.

64 {
65 if (!title || width < 5)
66 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid popup title or width");
67 asciichat_error_t result =
68 ui_popup_frame(out, row, col, width, height, color);
69 if (result != ASCIICHAT_OK)
70 return result;
71 char rendered[UI_POPUP_RENDERED_BUFFER_SIZE];
72 truncate_with_ellipsis(title, rendered, sizeof(rendered), width - 4);
73 int title_width = display_width(rendered);
74 frame_buffer_printf(out, "\033[%d;%dH %s ", row,
75 col + (width - title_width - 2) / 2,
77 return ASCIICHAT_OK;
78}
#define SET_ERRNO(code,...)
@ LOG_COLOR_RESET
Definition color.h:14
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
void truncate_with_ellipsis(const char *input, char *output, size_t output_size, int max_width)
Truncate a string to a maximum display width, with ellipsis.
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
#define UI_POPUP_RENDERED_BUFFER_SIZE
Definition popup.c:3

References ASCIICHAT_OK, colored_string(), display_width(), ERROR_INVALID_PARAM, frame_buffer_printf(), height, LOG_COLOR_RESET, out, SET_ERRNO, truncate_with_ellipsis(), ui_popup_frame(), UI_POPUP_RENDERED_BUFFER_SIZE, and width.

◆ ui_popup_frame_with_encoding()

asciichat_error_t ui_popup_frame_with_encoding ( frame_buffer_t * out,
int row,
int col,
int width,
int height,
log_color_t color,
bool unicode )

Definition at line 37 of file popup.c.

40 {
41 if (!out || row < 1 || col < 1 || width < 2 || height < 2)
43 "Invalid popup frame output or rectangle");
44 frame_buffer_printf(out, "\033[%d;%dH", row, col);
45 ui_popup_rule(out, width, UI_POPUP_TOP, false, unicode, color);
46 const char *vertical = unicode ? "│" : "|";
47 for (int i = 1; i < height - 1; ++i)
48 frame_buffer_printf(out, "\033[%d;%dH%s%*s%s", row + i, col,
49 colored_string(color, vertical), width - 2, "",
50 colored_string(color, vertical));
51 frame_buffer_printf(out, "\033[%d;%dH", row + height - 1, col);
52 return ui_popup_rule(out, width, UI_POPUP_BOTTOM, false, unicode, color);
53}
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

References colored_string(), ERROR_INVALID_PARAM, frame_buffer_printf(), height, out, SET_ERRNO, UI_POPUP_BOTTOM, ui_popup_rule(), UI_POPUP_TOP, and width.

Referenced by ui_popup_frame(), and ui_reconnect_render().

◆ ui_popup_render()

bool ui_popup_render ( frame_buffer_t * output,
terminal_size_t size,
const ui_popup_t * box )

Draw a centered box without clearing the frame beneath it.

Definition at line 80 of file popup.c.

81 {
82 if (!output || !box || !box->title || (box->line_count && !box->lines) ||
83 box->top_margin < 0) {
84 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid popup output, content or margins");
85 return false;
86 }
87 if (size.cols < 8 || size.rows < 5)
88 return false;
89 int content_width = display_width(box->title);
90 for (size_t i = 0; i < box->line_count; ++i) {
91 int width = display_width(box->lines[i]);
92 if (width > content_width)
93 content_width = width;
94 }
95 if (box->footer && display_width(box->footer) > content_width)
96 content_width = display_width(box->footer);
97 int width = content_width + 4;
98 if (width < box->minimum_width)
99 width = box->minimum_width;
100 if (width > size.cols - 2)
101 width = size.cols - 2;
102 int height = (int)box->line_count + (box->footer ? 5 : 4);
103 if (height > size.rows - 2 - box->top_margin)
104 return false;
105 int left = (size.cols - width) / 2 + 1;
106 int top = box->top_margin + (size.rows - box->top_margin - height) / 2 + 1;
107 if (ui_popup_frame(output, top, left, width, height, LOG_COLOR_RESET) !=
109 return false;
110 char rendered[UI_POPUP_RENDERED_BUFFER_SIZE];
111 truncate_with_ellipsis(box->title, rendered, sizeof(rendered), width - 4);
112 const char *colored_title = colored_string(LOG_COLOR_RESET, rendered);
113 frame_buffer_printf(output, "\033[%d;%dH\033[1m%s\033[0m", top + 1, left + 2,
114 colored_title);
115 for (size_t i = 0; i < box->line_count; ++i) {
116 truncate_with_ellipsis(box->lines[i], rendered, sizeof(rendered),
117 width - 4);
118 frame_buffer_printf(output, "\033[%d;%dH%s", top + 2 + (int)i, left + 2,
119 rendered);
120 }
121 if (box->footer) {
122 truncate_with_ellipsis(box->footer, rendered, sizeof(rendered), width - 4);
123 frame_buffer_printf(output, "\033[%d;%dH\033[2m%s\033[0m", top + height - 2,
124 left + 2, rendered);
125 }
126 return true;
127}
int top_margin
Definition popup.h:48
const char *const * lines
Definition popup.h:44
const char * title
Definition popup.h:43
int minimum_width
Definition popup.h:47
const char * footer
Definition popup.h:46
size_t line_count
Definition popup.h:45

References ASCIICHAT_OK, colored_string(), display_width(), ERROR_INVALID_PARAM, ui_popup_t::footer, frame_buffer_printf(), height, ui_popup_t::line_count, ui_popup_t::lines, LOG_COLOR_RESET, ui_popup_t::minimum_width, SET_ERRNO, ui_popup_t::title, ui_popup_t::top_margin, truncate_with_ellipsis(), ui_popup_frame(), UI_POPUP_RENDERED_BUFFER_SIZE, and width.

Referenced by ui_help_menu_render().

◆ ui_popup_rule()

asciichat_error_t ui_popup_rule ( frame_buffer_t * output,
int width,
ui_popup_edge_t edge,
bool double_border,
bool unicode,
log_color_t color )

Shared chrome for streaming notices and nested dialog panels. No newline.

Definition at line 12 of file popup.c.

14 {
15 if (!out || width < 2 || edge < UI_POPUP_TOP || edge > UI_POPUP_RULE)
17 "Invalid popup rule output, width or edge");
18 static const char *single[][2] = {
19 {"┌", "┐"}, {"├", "┤"}, {"└", "┘"}, {"", ""}};
20 static const char *dual[][2] = {{"╔", "╗"}, {"╠", "╣"}, {"╚", "╝"}, {"", ""}};
21 const char *left = edge == UI_POPUP_RULE ? ""
22 : !unicode ? "+"
23 : double_border ? dual[edge][0]
24 : single[edge][0];
25 const char *right = edge == UI_POPUP_RULE ? ""
26 : !unicode ? "+"
27 : double_border ? dual[edge][1]
28 : single[edge][1];
29 const char *horizontal = !unicode ? "-" : double_border ? "═" : "─";
30 frame_buffer_printf(out, "%s", colored_string(color, left));
31 for (int i = 0; i < width - (edge == UI_POPUP_RULE ? 0 : 2); ++i)
32 frame_buffer_printf(out, "%s", colored_string(color, horizontal));
33 frame_buffer_printf(out, "%s", colored_string(color, right));
34 return ASCIICHAT_OK;
35}
@ UI_POPUP_RULE
Definition popup.h:17

References ASCIICHAT_OK, colored_string(), ERROR_INVALID_PARAM, frame_buffer_printf(), out, SET_ERRNO, UI_POPUP_RULE, and width.

Referenced by ui_error_popup_preserve(), and ui_popup_frame_with_encoding().