ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
reconnect.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
11#include <libplatform/terminal.h>
12#include <options/options.h>
13#include <stdio.h>
14#include <string.h>
15
31
32static const char *status_or(const char *value) {
33 return value && value[0] ? value : "WAITING";
34}
35
36static log_color_t status_color(const char *status) {
37 if (!status)
38 return LOG_COLOR_WARN;
39 if (!strcmp(status, "READY") || !strcmp(status, "CONNECTED") ||
40 !strcmp(status, "SECURE") || !strcmp(status, "COMPLETE"))
41 return LOG_COLOR_INFO;
42 if (!strcmp(status, "LOST") || !strcmp(status, "FAILED"))
43 return LOG_COLOR_ERROR;
44 return LOG_COLOR_WARN;
45}
46
47static void put_centered_colored(frame_buffer_t *out, int row, int cols,
48 const char *text, log_color_t color,
49 bool colors) {
50 int width = utf8_display_width(text);
51 if (width < 0)
52 width = (int)strlen(text);
53 int left = (cols - width) / 2;
54 if (left < 0)
55 left = 0;
56 const char *rendered = colors ? colored_string(color, text) : text;
57 frame_buffer_printf(out, "\033[%d;%dH%s", row, left + 1, rendered);
58}
59
60static void put_status(frame_buffer_t *out, int row, int left, int label_width,
61 int value_width, const char *label, const char *value,
62 bool colors) {
63 const char *status = status_or(value);
65 truncate_with_ellipsis(status, clipped, sizeof(clipped), value_width);
66 const char *colored =
67 colors ? colored_string(status_color(value), clipped) : clipped;
68 frame_buffer_printf(out, "\033[%d;%dH%-*s %s", row, left + 1, label_width,
69 label, colored);
70}
71
72static void put_shortcuts(frame_buffer_t *out, int row, int cols, bool colors,
73 const ui_reconnect_view_t *view) {
74 const char *cancel = view->can_cancel ? " cancel" : "";
75 const char *retry = view->can_retry ? " retry now" : "";
76 const char *gap = view->can_cancel && view->can_retry ? " " : "";
77 int width = (int)(strlen(cancel) + strlen(retry) + strlen(gap)) +
78 view->can_cancel + view->can_retry;
79 int left = (cols - width) / 2;
80 if (left < 0)
81 left = 0;
82 const char *q = !view->can_cancel ? ""
84 : "q";
85 const char *r = !view->can_retry ? ""
87 : "r";
88 frame_buffer_printf(out, "\033[%d;%dH%s%s%s%s%s", row, left + 1, q, cancel,
89 gap, r, retry);
90}
91
92static void put_connection_timeline(frame_buffer_t *out, int row, int left,
93 int width, bool unicode, bool colors) {
94 const char *node = unicode ? "●" : "o";
95 const char *rule = unicode ? "━" : "=";
96 const char *left_label = "Connected";
97 const char *middle_label = "Connection Lost";
98 const char *right_label = "Reconnecting...";
99 int left_label_width = utf8_display_width(left_label);
100 int right_label_width = utf8_display_width(right_label);
101 int second = left + width / 2 - 1;
102 int first = second - 28;
103 int third = second + 28;
104 int first_min = left + left_label_width / 2;
105 int third_max =
106 left + width - 2 - (right_label_width - right_label_width / 2);
107 if (first < first_min)
108 first = first_min;
109 if (third > third_max)
110 third = third_max;
111 frame_buffer_printf(out, "\033[%d;%dH%s", row, first,
112 colors ? colored_string(LOG_COLOR_INFO, node) : node);
113 for (int i = first + 1; i < second; ++i)
115 colors ? colored_string(LOG_COLOR_INFO, rule) : rule);
117 colors ? colored_string(LOG_COLOR_WARN, node) : node);
118 for (int i = second + 1; i < third; ++i)
120 colors ? colored_string(LOG_COLOR_FATAL, rule) : rule);
122 colors ? colored_string(LOG_COLOR_FATAL, node) : node);
124 out, "\033[%d;%dH%s", row + 1, first - left_label_width / 2 + 1,
125 colors ? colored_string(LOG_COLOR_INFO, left_label) : left_label);
126 frame_buffer_printf(out, "\033[%d;%dH%s", row + 1,
127 second - utf8_display_width(middle_label) / 2 + 1,
128 colors ? colored_string(LOG_COLOR_WARN, middle_label)
129 : middle_label);
131 out, "\033[%d;%dH%s", row + 1, third - right_label_width / 2 + 1,
132 colors ? colored_string(LOG_COLOR_FATAL, right_label) : right_label);
133}
134
136 const ui_reconnect_view_t *view,
137 uint64_t animation_frame) {
138 if (!out || !view)
140 "Reconnect view requires an output buffer and view");
141 terminal_size_t minimum = ui_reconnect_minimum();
142 if (ui_too_small(size, minimum)) {
143 ui_too_small_render(out, size, minimum);
144 return ASCIICHAT_OK;
145 }
146
147 bool unicode = terminal_should_use_utf8(GET_OPTION(force_utf8));
148 bool colors = view->use_colors && !GET_OPTION(strip_ansi) &&
150 int width = size.cols < 100 ? size.cols - 4 : 92;
151 int height = 22;
152 int top = (size.rows - height) / 2 + 1;
153 int left = (size.cols - width) / 2 + 1;
154 // The cloud row and four logo rows precede the panel.
155 if (top < 6)
156 top = 6;
157
158 static const char clear[] = "\033[?25l\033[2J\033[H";
159 frame_buffer_append(out, clear, sizeof(clear) - 1);
160 invitation_render_rainbow_clouds(out, size.cols - 1, (int)animation_frame,
161 colors, unicode);
162 invitation_render_logo(out, size.cols - 1, (int)animation_frame, colors,
163 false);
165 unicode);
166
167 bool transport_lost =
168 view->transport && strncmp(view->transport, "LOST", 4) == 0;
169 put_centered_colored(out, top + 2, size.cols,
170 transport_lost ? "CONNECTION LOST" : "RECONNECTING",
171 transport_lost ? LOG_COLOR_FATAL : LOG_COLOR_WARN,
172 colors);
173
175 char target_clipped[UI_RECONNECT_TARGET_BUFFER_SIZE];
176 const char *target_name =
177 view->target && view->target[0] ? view->target : "server";
178 snprintf(target, sizeof(target), "Reconnecting to %s", target_name);
179 truncate_with_ellipsis(target, target_clipped, sizeof(target_clipped),
180 width - 8);
181 put_centered_colored(out, top + 4, size.cols, target_clipped, LOG_COLOR_DEBUG,
182 colors);
183
184 static const char *const utf8_spinner[] = {"⠋", "⠙", "⠹", "⠸", "⠼",
185 "⠴", "⠦", "⠧", "⠇", "⠏"};
186 static const char ascii_spinner[] = "|/-\\";
187 const char *spinner =
188 unicode ? utf8_spinner[animation_frame % 10]
189 : (char[]){ascii_spinner[animation_frame % 4], '\0'};
190 char attempt[48] = "";
191 if (view->attempts > 0)
192 snprintf(attempt, sizeof(attempt), "Attempt %u of %u ", view->attempt,
193 view->attempts);
194 else if (view->attempt > 0)
195 snprintf(attempt, sizeof(attempt), "Attempt %u ", view->attempt);
196 char retry[128];
197 if (view->retry_seconds > 0)
198 snprintf(retry, sizeof(retry), "%s %sRetrying in %us", spinner, attempt,
199 view->retry_seconds);
200 else
201 snprintf(retry, sizeof(retry), "%s %sReconnecting...", spinner, attempt);
202 put_centered_colored(out, top + 6, size.cols, retry, LOG_COLOR_WARN, colors);
203
204 put_connection_timeline(out, top + 8, left, width, unicode, colors);
205
206 int status_left = left + 5;
207 const int status_label_width = 12;
208 int status_value_width = width - status_label_width - 8;
209 put_status(out, top + 11, status_left, status_label_width, status_value_width,
210 "Transport", view->transport, colors);
211 put_status(out, top + 12, status_left, status_label_width, status_value_width,
212 "Encryption", view->encryption, colors);
213 put_status(out, top + 13, status_left, status_label_width, status_value_width,
214 "Handshake", view->handshake, colors);
215 put_status(out, top + 14, status_left, status_label_width, status_value_width,
216 "Protocol", view->protocol, colors);
217
219 char error_clipped[UI_RECONNECT_DETAIL_BUFFER_SIZE];
220 const char *last_error = view->last_error && view->last_error[0]
221 ? view->last_error
222 : "connection closed";
223 snprintf(error, sizeof(error), "Last error: %s", last_error);
224 truncate_with_ellipsis(error, error_clipped, sizeof(error_clipped),
225 width - 8);
226 put_centered_colored(out, top + 16, size.cols, error_clipped, LOG_COLOR_FATAL,
227 colors);
228
229 frame_buffer_printf(out, "\033[%d;%dH", top + height - 3, left + 3);
230 const char *divider =
231 colors ? colored_string(LOG_COLOR_DEBUG, unicode ? "─" : "-")
232 : (unicode ? "─" : "-");
233 for (int i = 0; i < width - 4; ++i)
234 frame_buffer_append(out, divider, strlen(divider));
235 if (view->can_cancel || view->can_retry)
236 put_shortcuts(out, top + height - 2, size.cols, colors, view);
237 return ASCIICHAT_OK;
238}
239
240terminal_size_t ui_reconnect_minimum(void) {
241 return (terminal_size_t){.cols = 64, .rows = 28};
242}
243
244#ifndef __EMSCRIPTEN__
245static void ui_reconnect_controller_render(terminal_size_t size,
246 const void *data) {
247 if (!data) {
249 "Reconnect controller render requires a snapshot");
250 return;
251 }
252 const ui_reconnect_snapshot_t *snapshot = data;
253 ui_reconnect_view_t view = {.target = snapshot->target,
254 .transport = snapshot->transport,
255 .encryption = snapshot->encryption,
256 .handshake = snapshot->handshake,
257 .protocol = snapshot->protocol,
258 .last_error = snapshot->last_error,
259 .attempt = snapshot->attempt,
260 .attempts = snapshot->attempts,
261 .retry_seconds = snapshot->retry_seconds,
262 .can_cancel = snapshot->can_cancel,
263 .can_retry = snapshot->can_retry,
264 .connected_before = snapshot->connected_before,
265 .use_colors = snapshot->use_colors};
266 if (view.retry_seconds == 0) {
268 bool transport_ready = activity == UI_TITLE_HANDSHAKE ||
269 activity == UI_TITLE_NEGOTIATING ||
270 activity == UI_TITLE_RECEIVING;
271 if (transport_ready && strncmp(view.transport, "LOST / ", 7) == 0)
272 view.transport += 7;
273 if (activity == UI_TITLE_HANDSHAKE)
274 view.handshake = "ACTIVE";
275 if (activity == UI_TITLE_NEGOTIATING || activity == UI_TITLE_RECEIVING) {
276 view.handshake = "COMPLETE";
277 view.protocol = activity == UI_TITLE_RECEIVING ? "READY" : "NEGOTIATING";
278 }
279 }
280 frame_buffer_t *output = frame_buffer_create(size.rows, size.cols);
281 if (!output) {
282 SET_ERRNO(ERROR_MEMORY, "Cannot allocate reconnect frame buffer");
283 return;
284 }
286 output, size, &view, time_get_ns() / (NS_PER_SEC_INT / 60));
287 if (render_result != ASCIICHAT_OK) {
288 frame_buffer_destroy(output);
289 return;
290 }
294 if (write_result != ASCIICHAT_OK)
295 SET_ERRNO(write_result, "Cannot present reconnect view");
296 frame_buffer_destroy(output);
297}
298
300 const ui_reconnect_view_t *view) {
301 if (!visible) {
303 return ASCIICHAT_OK;
304 }
305 if (!view)
307 "Reconnect controller requires a view");
309 ui_reconnect_snapshot_t snapshot = {0};
310 SAFE_STRNCPY(snapshot.target, view->target ? view->target : "server",
311 sizeof(snapshot.target));
312 SAFE_STRNCPY(snapshot.transport, view->transport ? view->transport : "LOST",
313 sizeof(snapshot.transport));
314 SAFE_STRNCPY(snapshot.encryption,
315 view->encryption ? view->encryption : "WAITING",
316 sizeof(snapshot.encryption));
317 SAFE_STRNCPY(snapshot.handshake,
318 view->handshake ? view->handshake : "WAITING",
319 sizeof(snapshot.handshake));
320 SAFE_STRNCPY(snapshot.protocol, view->protocol ? view->protocol : "WAITING",
321 sizeof(snapshot.protocol));
322 SAFE_STRNCPY(snapshot.last_error,
323 view->last_error ? view->last_error : "connection closed",
324 sizeof(snapshot.last_error));
325 snapshot.attempt = view->attempt;
326 snapshot.attempts = view->attempts;
327 snapshot.retry_seconds = view->retry_seconds;
328 snapshot.can_cancel = view->can_cancel;
329 snapshot.can_retry = view->can_retry;
330 snapshot.connected_before = view->connected_before;
331 snapshot.use_colors = view->use_colors;
334 ui_reconnect_controller_render, &snapshot, sizeof(snapshot));
335}
336#endif
#define SET_ERRNO(code,...)
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_DEBUG
Definition color.h:8
@ LOG_COLOR_INFO
Definition color.h:9
@ LOG_COLOR_WARN
Definition color.h:10
void ui_error_popup_reset(void)
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.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
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
#define NS_PER_SEC_INT
Definition time.h:173
#define GET_OPTION(field)
Definition options.h:1339
@ COLOR_SETTING_FALSE
Force colors OFF: disable all colors and logging colors.
Definition options.h:250
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
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.
⚙️ 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
void invitation_render_rainbow_clouds(frame_buffer_t *buf, int cols, int frame, bool colors, bool unicode)
Definition invitation.c:72
📝 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_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
terminal_size_t ui_reconnect_minimum(void)
Definition reconnect.c:240
asciichat_error_t ui_reconnect_controller_set(bool visible, const ui_reconnect_view_t *view)
Definition reconnect.c:299
asciichat_error_t ui_reconnect_render(frame_buffer_t *out, terminal_size_t size, const ui_reconnect_view_t *view, uint64_t animation_frame)
Definition reconnect.c:135
#define UI_RECONNECT_TARGET_BUFFER_SIZE
Definition reconnect.h:13
#define UI_RECONNECT_DETAIL_BUFFER_SIZE
Definition reconnect.h:14
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
char target[UI_RECONNECT_TARGET_BUFFER_SIZE]
Definition reconnect.c:17
char transport[UI_RECONNECT_DETAIL_BUFFER_SIZE]
Definition reconnect.c:18
char last_error[UI_RECONNECT_DETAIL_BUFFER_SIZE]
Definition reconnect.c:22
char handshake[UI_RECONNECT_DETAIL_BUFFER_SIZE]
Definition reconnect.c:20
char protocol[UI_RECONNECT_DETAIL_BUFFER_SIZE]
Definition reconnect.c:21
char encryption[UI_RECONNECT_DETAIL_BUFFER_SIZE]
Definition reconnect.c:19
uint32_t retry_seconds
Definition reconnect.h:30
const char * last_error
Definition reconnect.h:27
bool can_retry
The host can immediately start the next attempt.
Definition reconnect.h:32
const char * encryption
Definition reconnect.h:24
const char * protocol
Definition reconnect.h:26
const char * transport
Definition reconnect.h:23
const char * target
Definition reconnect.h:22
const char * handshake
Definition reconnect.h:25
bool can_cancel
The host can cancel from this view.
Definition reconnect.h:31
void ui_too_small_render(frame_buffer_t *buffer, terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:44
bool ui_too_small(terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:10
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)
ui_title_activity_t ui_controller_get_title_activity(void)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
int ui_controller_active_fd(void)
ui_title_activity_t
@ UI_TITLE_NEGOTIATING
@ UI_TITLE_HANDSHAKE
@ UI_TITLE_RECEIVING
@ UI_SCREEN_RECONNECT
🔤 UTF-8 Encoding and Decoding Utilities