ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
progress.h File Reference

Go to the source code of this file.

Functions

void ui_progress_format (char *output, size_t capacity, double fraction, int width)
const char * ui_spinner_glyph (uint64_t elapsed_ns, bool use_colors)
void ui_spinner_render (frame_buffer_t *output, int row, int column, uint64_t elapsed_ns)

Function Documentation

◆ ui_progress_format()

void ui_progress_format ( char * output,
size_t capacity,
double fraction,
int width )

Caller-owned ASCII/UTF-8 bar, styled using the active theme.

Definition at line 8 of file progress.c.

8 {
9 if (!out || !cap || width < 1 || !isfinite(fraction)) {
10 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid progress buffer, value or width");
11 return;
12 }
13 out[0] = '\0';
14 if (fraction < 0)
15 fraction = 0;
16 if (fraction > 1)
17 fraction = 1;
18 int filled = (int)(fraction * width);
19 size_t used = 0;
20 for (int i = 0; i < width; ++i) {
21 const char *glyph =
22 terminal_should_use_utf8(GET_OPTION(force_utf8)) ? (i < filled ? "█" : "░") : (i < filled ? "#" : ".");
23 const char *cell = colored_string(i < filled ? LOG_COLOR_INFO : LOG_COLOR_GREY, glyph);
24 size_t n = strlen(cell);
25 if (n >= cap - used) {
26 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Progress output buffer is too small");
27 return;
28 }
29 memcpy(out + used, cell, n);
30 used += n;
31 out[used] = '\0';
32 }
33}
#define SET_ERRNO(code,...)
unsigned int width
Definition fft.c:23
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
@ LOG_COLOR_GREY
Definition log/log.h:154
@ LOG_COLOR_INFO
Definition log/log.h:150
#define GET_OPTION(field)
Safely get a specific option field (lock-free read).
Definition options.h:1336
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
Definition string.c:284
static bool size_t gotel_query_t * out
Definition queries.h:8

References colored_string(), ERROR_BUFFER_OVERFLOW, ERROR_INVALID_PARAM, GET_OPTION, LOG_COLOR_GREY, LOG_COLOR_INFO, out, SET_ERRNO, and width.

◆ ui_spinner_glyph()

const char * ui_spinner_glyph ( uint64_t elapsed_ns,
bool use_colors )

Definition at line 34 of file progress.c.

34 {
35 // Each rotating segment has three dots spanning both Braille columns.
36 static const char *unicode[] = {"⠋", "⠙", "⠴", "⠦"};
37 static const char *ascii[] = {"|", "/", "-", "\\"};
38 uint64_t step = elapsed_ns / 125000000;
39 const char *glyph = terminal_should_use_utf8(GET_OPTION(force_utf8)) ? unicode[step % 4] : ascii[step % 4];
40 return use_colors ? colored_string(LOG_COLOR_WARN, glyph) : glyph;
41}
unsigned long long uint64_t
Definition common.h:63
@ LOG_COLOR_WARN
Definition log/log.h:151

References colored_string(), GET_OPTION, and LOG_COLOR_WARN.

◆ ui_spinner_render()

void ui_spinner_render ( frame_buffer_t * output,
int row,
int column,
uint64_t elapsed_ns )

Eight-dot animated indicator extracted from the update checker.

Definition at line 42 of file progress.c.

42 {
43 if (!out || row < 1 || col < 1) {
44 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid spinner output or position");
45 return;
46 }
47 static const int x[] = {3, 5, 6, 5, 3, 1, 0, 1}, y[] = {0, 0, 1, 2, 2, 2, 1, 0};
48 unsigned phase = (unsigned)((elapsed_ns / 100000000) % 8);
49 for (unsigned dot = 0; dot < 8; ++dot) {
50 bool lit = (phase + 8 - dot) % 8 < 3;
52 out, "\033[%d;%dH%s", row + y[dot], col + x[dot],
53 colored_string(lit ? LOG_COLOR_WARN : LOG_COLOR_GREY, terminal_should_use_utf8(GET_OPTION(force_utf8)) ? "●"
54 : lit ? "o"
55 : "."));
56 }
57}
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.

References colored_string(), ERROR_INVALID_PARAM, frame_buffer_printf(), GET_OPTION, LOG_COLOR_GREY, LOG_COLOR_WARN, out, and SET_ERRNO.