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.c
Go to the documentation of this file.
3#include <libplatform/terminal.h>
5#include <math.h>
6#include <stdio.h>
7#include <string.h>
8void ui_progress_format(char *out, size_t cap, double fraction, int width) {
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}
34const char *ui_spinner_glyph(uint64_t elapsed_ns, bool use_colors) {
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}
42void ui_spinner_render(frame_buffer_t *out, int row, int col, uint64_t elapsed_ns) {
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}
#define SET_ERRNO(code,...)
unsigned int width
Definition fft.c:23
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 long long uint64_t
Definition common.h:63
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
@ LOG_COLOR_GREY
Definition log/log.h:154
@ LOG_COLOR_INFO
Definition log/log.h:150
@ LOG_COLOR_WARN
Definition log/log.h:151
#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
🔢 Mathematical Utility Functions
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
const char * ui_spinner_glyph(uint64_t elapsed_ns, bool use_colors)
Definition progress.c:34
void ui_progress_format(char *out, size_t cap, double fraction, int width)
Definition progress.c:8
void ui_spinner_render(frame_buffer_t *out, int row, int col, uint64_t elapsed_ns)
Definition progress.c:42
static bool size_t gotel_query_t * out
Definition queries.h:8
🔤 String Manipulation and Shell Escaping Utilities