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

Go to the source code of this file.

Data Structures

struct  ui_reconnect_view_t

Macros

#define UI_RECONNECT_TARGET_BUFFER_SIZE   256
#define UI_RECONNECT_DETAIL_BUFFER_SIZE   256

Functions

terminal_size_t ui_reconnect_minimum (void)
asciichat_error_t ui_reconnect_render (frame_buffer_t *output, terminal_size_t size, const ui_reconnect_view_t *view, uint64_t animation_frame)
asciichat_error_t ui_reconnect_controller_set (bool visible, const ui_reconnect_view_t *view)

Macro Definition Documentation

◆ UI_RECONNECT_DETAIL_BUFFER_SIZE

#define UI_RECONNECT_DETAIL_BUFFER_SIZE   256

Definition at line 14 of file reconnect.h.

Referenced by ui_reconnect_render().

◆ UI_RECONNECT_TARGET_BUFFER_SIZE

#define UI_RECONNECT_TARGET_BUFFER_SIZE   256

Definition at line 13 of file reconnect.h.

Referenced by ui_reconnect_render().

Function Documentation

◆ ui_reconnect_controller_set()

asciichat_error_t ui_reconnect_controller_set ( bool visible,
const ui_reconnect_view_t * view )

Publish or remove the native controller's reconnect screen.

Definition at line 299 of file reconnect.c.

300 {
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}
#define SET_ERRNO(code,...)
void ui_error_popup_reset(void)
#define SAFE_STRNCPY(dst, src, size)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
terminal_size_t ui_reconnect_minimum(void)
Definition reconnect.c:240
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_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_SCREEN_RECONNECT

References ASCIICHAT_OK, ui_reconnect_snapshot_t::attempt, ui_reconnect_view_t::attempt, ui_reconnect_snapshot_t::attempts, ui_reconnect_view_t::attempts, ui_reconnect_snapshot_t::can_cancel, ui_reconnect_view_t::can_cancel, ui_reconnect_snapshot_t::can_retry, ui_reconnect_view_t::can_retry, ui_reconnect_snapshot_t::connected_before, ui_reconnect_view_t::connected_before, ui_reconnect_snapshot_t::encryption, ui_reconnect_view_t::encryption, ERROR_INVALID_PARAM, ui_reconnect_snapshot_t::handshake, ui_reconnect_view_t::handshake, ui_reconnect_snapshot_t::last_error, ui_reconnect_view_t::last_error, ui_reconnect_snapshot_t::protocol, ui_reconnect_view_t::protocol, ui_reconnect_snapshot_t::retry_seconds, ui_reconnect_view_t::retry_seconds, SAFE_STRNCPY, SET_ERRNO, ui_reconnect_snapshot_t::target, ui_reconnect_view_t::target, ui_reconnect_snapshot_t::transport, ui_reconnect_view_t::transport, ui_controller_remove(), ui_controller_submit(), ui_error_popup_reset(), ui_reconnect_minimum(), UI_SCREEN_RECONNECT, ui_reconnect_snapshot_t::use_colors, and ui_reconnect_view_t::use_colors.

Referenced by session_display_write_ascii_frame().

◆ ui_reconnect_minimum()

terminal_size_t ui_reconnect_minimum ( void )

Minimum dimensions for the centered reconnect panel.

Definition at line 240 of file reconnect.c.

240 {
241 return (terminal_size_t){.cols = 64, .rows = 28};
242}

Referenced by ui_reconnect_controller_set(), ui_reconnect_render(), and ui_session_tick().

◆ ui_reconnect_render()

asciichat_error_t ui_reconnect_render ( frame_buffer_t * output,
terminal_size_t size,
const ui_reconnect_view_t * view,
uint64_t animation_frame )

Render the independent reconnect TUI into an owned frame buffer.

Definition at line 135 of file reconnect.c.

137 {
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}
@ LOG_COLOR_RESET
Definition color.h:14
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_DEBUG
Definition color.h:8
@ LOG_COLOR_WARN
Definition color.h:10
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.
#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.
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.
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
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
#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)
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

References ASCIICHAT_OK, ui_reconnect_view_t::attempt, ui_reconnect_view_t::attempts, ui_reconnect_view_t::can_cancel, ui_reconnect_view_t::can_retry, COLOR_SETTING_FALSE, colored_string(), colors, ui_reconnect_view_t::encryption, ERROR_INVALID_PARAM, frame_buffer_append(), frame_buffer_printf(), GET_OPTION, ui_reconnect_view_t::handshake, height, invitation_render_logo(), invitation_render_rainbow_clouds(), ui_reconnect_view_t::last_error, LOG_COLOR_DEBUG, LOG_COLOR_FATAL, LOG_COLOR_RESET, LOG_COLOR_WARN, out, ui_reconnect_view_t::protocol, ui_reconnect_view_t::retry_seconds, SET_ERRNO, ui_reconnect_view_t::target, terminal_should_use_utf8(), ui_reconnect_view_t::transport, truncate_with_ellipsis(), ui_popup_frame_with_encoding(), UI_RECONNECT_DETAIL_BUFFER_SIZE, ui_reconnect_minimum(), UI_RECONNECT_TARGET_BUFFER_SIZE, ui_too_small(), ui_too_small_render(), ui_reconnect_view_t::use_colors, and width.