ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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 = terminal_should_use_utf8(GET_OPTION(force_utf8))
22 ? (i < filled ? "█" : "░")
23 : (i < filled ? "#" : ".");
24 const char *cell =
25 colored_string(i < filled ? LOG_COLOR_INFO : LOG_COLOR_GREY, glyph);
26 size_t n = strlen(cell);
27 if (n >= cap - used) {
28 SET_ERRNO(ERROR_BUFFER_OVERFLOW, "Progress output buffer is too small");
29 return;
30 }
31 memcpy(out + used, cell, n);
32 used += n;
33 out[used] = '\0';
34 }
35}
#define SET_ERRNO(code,...)
@ LOG_COLOR_GREY
Definition color.h:13
@ LOG_COLOR_INFO
Definition color.h:9
unsigned int width
Definition fft.c:24
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
#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.
static bool size_t gotel_query_t * out
Definition queries.h:10
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)

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

◆ ui_spinner_glyph()

const char * ui_spinner_glyph ( uint64_t elapsed_ns,
bool use_colors )

Definition at line 36 of file progress.c.

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

References colored_string(), GET_OPTION, LOG_COLOR_WARN, and terminal_should_use_utf8().

◆ 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 46 of file progress.c.

47 {
48 if (!out || row < 1 || col < 1) {
49 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid spinner output or position");
50 return;
51 }
52 static const int x[] = {3, 5, 6, 5, 3, 1, 0, 1},
53 y[] = {0, 0, 1, 2, 2, 2, 1, 0};
54 unsigned phase = (unsigned)((elapsed_ns / 100000000) % 8);
55 for (unsigned dot = 0; dot < 8; ++dot) {
56 bool lit = (phase + 8 - dot) % 8 < 3;
58 out, "\033[%d;%dH%s", row + y[dot], col + x[dot],
60 terminal_should_use_utf8(GET_OPTION(force_utf8)) ? "●"
61 : lit ? "o"
62 : "."));
63 }
64}
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, SET_ERRNO, and terminal_should_use_utf8().