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.c
Go to the documentation of this file.
3#include <libplatform/terminal.h>
4#include <math.h>
5#include <options/options.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 = 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}
36const char *ui_spinner_glyph(uint64_t elapsed_ns, bool use_colors) {
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}
46void ui_spinner_render(frame_buffer_t *out, int row, int col,
47 uint64_t elapsed_ns) {
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}
#define SET_ERRNO(code,...)
@ LOG_COLOR_GREY
Definition color.h:13
@ LOG_COLOR_INFO
Definition color.h:9
@ LOG_COLOR_WARN
Definition color.h:10
unsigned int width
Definition fft.c:24
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
@ 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.
🔢 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:36
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:46
static bool size_t gotel_query_t * out
Definition queries.h:10
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities