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
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define UI_POPUP_RENDERED_BUFFER_SIZE 1024
4
9#include <options/options.h>
10#include <string.h>
11
13 ui_popup_edge_t edge, bool double_border,
14 bool unicode, log_color_t color) {
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}
36
38 int col, int width, int height,
39 log_color_t color,
40 bool unicode) {
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}
54
56 int width, int height, log_color_t color) {
58 out, row, col, width, height, color,
60}
61
63 int width, int height,
64 log_color_t color, const char *title) {
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}
79
80bool ui_popup_render(frame_buffer_t *output, terminal_size_t size,
81 const ui_popup_t *box) {
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}
#define SET_ERRNO(code,...)
log_color_t
Definition color.h:6
@ LOG_COLOR_RESET
Definition color.h:14
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
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.
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
#define GET_OPTION(field)
Definition options.h:1339
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.
🖥️ Display Width and Terminal Centering Utilities
📝 Logging API with multiple log levels and terminal output control
⚙️ 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_with_encoding(frame_buffer_t *out, int row, int col, int width, int height, log_color_t color, bool unicode)
Definition popup.c:37
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
#define UI_POPUP_RENDERED_BUFFER_SIZE
Definition popup.c:3
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
ui_popup_edge_t
Definition popup.h:13
@ UI_POPUP_BOTTOM
Definition popup.h:16
@ UI_POPUP_RULE
Definition popup.h:17
@ UI_POPUP_TOP
Definition popup.h:14
static bool size_t gotel_query_t * out
Definition queries.h:10
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with ui popup, it keeps title, lines, line_count, footer here instead of scat...
Definition popup.h:42
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