ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
render_progress.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define TUI_RENDER_PROGRESS_LABEL_BUFFER_SIZE 160
4#define TUI_RENDER_PROGRESS_TOTAL_BUFFER_SIZE 32
5#define TUI_RENDER_PROGRESS_TITLE_BUFFER_SIZE 64
6#define TUI_RENDER_PROGRESS_BAR_BUFFER_SIZE 4096
7
12#include <ascii-chat/ui/text.h>
17#include <libplatform/compiler.h>
18#include <libplatform/memory.h>
19#include <libplatform/system.h>
20#include <libplatform/terminal.h>
21#include <stdio.h>
22#include <string.h>
23
24// Preview storage is bounded independently of recording duration and queue
25// depth.
26#define PREVIEW_MAX_BYTES (1024 * 1024)
27
28typedef struct {
34 bool failed;
35 size_t length;
37 // The NUL-terminated destination follows the NUL-terminated preview.
38 char frame[1];
46
60
61static void progress_render(terminal_size_t size, const void *data) {
62 const progress_snapshot_t *snapshot = data;
63 frame_buffer_t *buffer = frame_buffer_create(size.rows, size.cols);
64 if (!buffer)
65 return;
66 char *line = SAFE_MALLOC(snapshot->length + 1, char *);
67 char *clipped =
68 SAFE_MALLOC(snapshot->length + UI_CLIPPED_TEXT_OVERHEAD, char *);
69 const char *cursor = snapshot->frame;
70 const char *end = cursor + snapshot->length;
71 // Address each row explicitly, leaving the last column free to prevent
72 // wrapping.
73 for (int row = 1; row <= size.rows; ++row) {
74 frame_buffer_printf(buffer, "\033[%d;1H\033[0m\033[2K", row);
75 if (cursor < end) {
76 const char *newline = memchr(cursor, '\n', (size_t)(end - cursor));
77 size_t length = (size_t)((newline ? newline : end) - cursor);
78 memcpy(line, cursor, length);
79 line[length] = '\0';
80 char *expanded = ansi_expand_rle(line, length);
81 truncate_with_ellipsis(expanded ? expanded : line, clipped,
83 size.cols - 1);
84 SAFE_FREE(expanded);
85 frame_buffer_append(buffer, clipped, strlen(clipped));
86 cursor += length + (newline ? 1 : 0);
87 }
88 }
89 uint64_t elapsed =
90 snapshot->started_ns ? time_get_ns() - snapshot->started_ns : 0;
91 uint64_t seconds = elapsed / NS_PER_SEC_INT;
94 if (snapshot->total_known)
95 snprintf(total, sizeof(total), "%llu", (unsigned long long)snapshot->total);
96 else
97 snprintf(total, sizeof(total), "?");
98 const char *phase = snapshot->failed ? "Recording failed"
99 : snapshot->finalizing ? "Finalizing file"
100 : "Rendering file";
101 snprintf(label, sizeof(label), " %c %s (%llu:%02llu) (frame %llu/%s) ",
102 "|/-\\"[(elapsed / (125 * NS_PER_MS_INT)) % 4], phase,
103 (unsigned long long)(seconds / 60),
104 (unsigned long long)(seconds % 60),
105 (unsigned long long)snapshot->completed, total);
106 int width = (int)strlen(label);
107 if (width > size.cols - 2) {
108 width = size.cols - 2;
109 label[width] = '\0';
110 }
112 truncate_with_ellipsis("--render-file Progress", title, sizeof(title),
113 size.cols - 4);
114 int col = (size.cols - display_width(title) - 2) / 2 + 1;
115 frame_buffer_printf(buffer, "\033[%d;%dH\033[0;1;7m %s \033[0m",
116 (size.rows + 1) / 2 - 1, col, title);
117 col = (size.cols - width) / 2 + 1;
118 frame_buffer_printf(buffer, "\033[%d;%dH\033[0;7m%s\033[0m",
119 (size.rows + 1) / 2, col, label);
120 size_t destination_size =
122 char *destination = SAFE_MALLOC(destination_size, char *);
123 truncate_with_ellipsis(snapshot->frame + snapshot->length + 1, destination,
124 destination_size, size.cols - 4);
125 int destination_width = display_width(destination) + 2;
126 col = (size.cols - destination_width) / 2 + 1;
127 frame_buffer_printf(buffer, "\033[%d;%dH\033[0;7m %s \033[0m",
128 (size.rows + 1) / 2 + 1, col, destination);
129 if (snapshot->total_known && snapshot->total && size.rows >= 9) {
131 int bar_width = size.cols > 44 ? 40 : size.cols - 4;
132 ui_progress_format(bar, sizeof(bar),
133 (double)snapshot->completed / (double)snapshot->total,
134 bar_width);
135 frame_buffer_printf(buffer, "\033[%d;%dH%s", (size.rows + 1) / 2 + 2,
136 (size.cols - bar_width) / 2 + 1, bar);
137 }
138 SAFE_FREE(destination);
139 ui_controller_write(STDOUT_FILENO, frame_buffer_get_content(buffer),
141 SAFE_FREE(line);
142 SAFE_FREE(clipped);
143 frame_buffer_destroy(buffer);
144}
145
146// The presentation callback reads only the controller-owned copy and never
147// takes this mutex.
148static void progress_publish(render_progress_t *progress) {
149 if (progress->active)
151 (terminal_size_t){.cols = 20, .rows = 5},
152 progress_render, progress->snapshot,
153 sizeof(*progress->snapshot) +
154 progress->snapshot->length +
155 progress->snapshot->destination_length + 2);
156}
157
159 const char *destination) {
161 return NULL;
162 render_progress_t *progress =
163 SAFE_CALLOC(1, sizeof(*progress), render_progress_t *);
164 progress->fd = platform_dup(STDOUT_FILENO);
165 if (progress->fd < 0) {
166 SAFE_FREE(progress);
167 return NULL;
168 }
169 mutex_init(&progress->mutex, "render_progress");
170 size_t destination_length = strlen(destination);
171 progress->snapshot =
172 SAFE_CALLOC(1, sizeof(*progress->snapshot) + destination_length + 2,
174 progress->snapshot->destination_length = destination_length;
175 char *name = progress->snapshot->frame + 1;
176 memcpy(name, destination, destination_length);
177 // A path must not inject terminal control sequences into the overlay.
178 for (size_t i = 0; i < destination_length; ++i) {
179 if ((unsigned char)name[i] < 0x20 || name[i] == 0x7f)
180 name[i] = '?';
181 }
182 progress->snapshot->started_ns = time_get_ns();
183 return progress;
184}
185
186void render_progress_frame(render_progress_t *progress, const char *frame,
187 bool succeeded) {
188 if (!progress)
189 return;
190 mutex_lock(&progress->mutex);
191 uint64_t now = time_get_ns();
192 // The overlay refreshes at most eight times per second. Expanding and copying
193 // every full-resolution frame only slows the encoder and cannot be displayed.
194 if (succeeded && !progress->snapshot->failed && progress->last_preview_ns &&
195 now >= progress->last_preview_ns &&
196 now - progress->last_preview_ns < 125 * NS_PER_MS_INT) {
197 ++progress->snapshot->completed;
198 mutex_unlock(&progress->mutex);
199 return;
200 }
201 progress->last_preview_ns = now;
202 size_t length = strnlen(frame, (size_t)PREVIEW_MAX_BYTES + 1u);
203 // Oversized frames still encode normally; omit their terminal preview.
204 if (length > PREVIEW_MAX_BYTES)
205 length = 0;
207 1, sizeof(*next) + length + progress->snapshot->destination_length + 2,
209 memcpy(next, progress->snapshot, sizeof(*next));
210 next->length = length;
211 memcpy(next->frame, frame, length);
212 memcpy(next->frame + length + 1,
213 progress->snapshot->frame + progress->snapshot->length + 1,
214 next->destination_length + 1);
215 if (!next->started_ns)
216 next->started_ns = time_get_ns();
217 if (succeeded)
218 next->completed++;
219 else
220 next->failed = true;
221 SAFE_FREE(progress->snapshot);
222 progress->snapshot = next;
223 progress_publish(progress);
224 mutex_unlock(&progress->mutex);
225}
226
228 bool total_known) {
229 if (!progress)
230 return;
231 mutex_lock(&progress->mutex);
232 progress->active = true;
233 progress->snapshot->total = total;
234 progress->snapshot->total_known = total_known;
235 progress_publish(progress);
236 mutex_unlock(&progress->mutex);
237}
238
240 if (!progress)
241 return;
242 mutex_lock(&progress->mutex);
243 progress->active = true;
244 progress->snapshot->finalizing = true;
245 if (!progress->snapshot->total_known) {
246 progress->snapshot->total = progress->snapshot->completed;
247 progress->snapshot->total_known = true;
248 }
249 progress_publish(progress);
250 mutex_unlock(&progress->mutex);
251}
252
254 if (!progress)
255 return;
257 platform_close(progress->fd);
258 SAFE_FREE(progress->snapshot);
259 mutex_destroy(&progress->mutex);
260 SAFE_FREE(progress);
261}
unsigned int width
Definition fft.c:24
int platform_close(int fd)
Definition filesystem.c:91
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
void frame_buffer_destroy(frame_buffer_t *buf)
Destroy a frame buffer.
size_t frame_buffer_get_length(const frame_buffer_t *buf)
Get the current content length of the frame buffer.
const char * frame_buffer_get_content(const frame_buffer_t *buf)
Get a pointer to the buffer content.
frame_buffer_t * frame_buffer_create(int rows, int cols)
Create a new frame buffer.
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
Buffered terminal frame rendering.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
bool enabled
Is filtering active?
Definition grep.c:63
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
#define NS_PER_SEC_INT
Definition time.h:173
#define NS_PER_MS_INT
Definition time.h:172
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
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.
char * ansi_expand_rle(const char *input, size_t input_len)
Expand RLE escape sequences in a string.
Definition rle.c:14
🖥️ Display Width and Terminal Centering Utilities
void ui_progress_format(char *out, size_t cap, double fraction, int width)
Definition progress.c:8
void render_progress_finalize(render_progress_t *progress)
#define TUI_RENDER_PROGRESS_TOTAL_BUFFER_SIZE
render_progress_t * render_progress_create(bool enabled, const char *destination)
void render_progress_frame(render_progress_t *progress, const char *frame, bool succeeded)
#define TUI_RENDER_PROGRESS_BAR_BUFFER_SIZE
#define PREVIEW_MAX_BYTES
#define TUI_RENDER_PROGRESS_TITLE_BUFFER_SIZE
void render_progress_begin(render_progress_t *progress, uint64_t total, bool total_known)
void render_progress_destroy(render_progress_t *progress)
#define TUI_RENDER_PROGRESS_LABEL_BUFFER_SIZE
struct render_progress render_progress_t
bool terminal_is_stdout_tty(void)
🔤 String Manipulation and Shell Escaping Utilities
render progress keeps the values that move through the subsystem: mutex, snapshot,...
uint64_t last_preview_ns
progress_snapshot_t * snapshot
#define UI_CLIPPED_TEXT_OVERHEAD
Definition text.h:5
⏱️ High-precision timing utilities using sokol_time.h and uthash
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)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
@ UI_SCREEN_RENDER_PROGRESS