ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
frame_buffer.c
Go to the documentation of this file.
9
10#include "ascii-chat/common.h"
12#include <ascii-chat/log/log.h>
15#include <libplatform/compiler.h>
16#include <libplatform/system.h>
17#include <libplatform/terminal.h>
18#include <options/options.h>
19
20#include <stdarg.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
37 char *data;
38 size_t len;
39 size_t capacity;
40};
41
42frame_buffer_t *frame_buffer_create(int rows, int cols) {
44 if (!buf) {
45 return NULL;
46 }
47
48 // Initial capacity: rows * cols * 16 to account for ANSI codes
49 size_t initial_capacity =
50 (rows > 0 && cols > 0) ? (size_t)rows * cols * 16 : 4096;
51
52 buf->data = SAFE_MALLOC(initial_capacity, char *);
53 if (!buf->data) {
54 SAFE_FREE(buf);
55 return NULL;
56 }
57
58 buf->len = 0;
59 buf->capacity = initial_capacity;
60
61 return buf;
62}
63
65 if (!buf) {
66 return;
67 }
68
69 SAFE_FREE(buf->data);
70 SAFE_FREE(buf);
71}
72
74 if (!buf) {
75 return;
76 }
77
78 buf->len = 0;
79}
80
81void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len) {
82 if (!buf || !str || len == 0) {
83 return;
84 }
85
86 // Grow buffer if needed
87 if (buf->len + len > buf->capacity) {
88 size_t new_capacity = buf->capacity * 2;
89 while (new_capacity < buf->len + len) {
90 new_capacity *= 2;
91 }
92
93 char *new_data = SAFE_MALLOC(new_capacity, char *);
94 if (!new_data) {
95 return; // Failed to grow, silently ignore
96 }
97
98 if (buf->len > 0 && buf->data) {
99 memcpy(new_data, buf->data, buf->len);
100 }
101
102 SAFE_FREE(buf->data);
103 buf->data = new_data;
104 buf->capacity = new_capacity;
105 }
106
107 // Append the data
108 memcpy(buf->data + buf->len, str, len);
109 buf->len += len;
110}
111
112void frame_buffer_printf(frame_buffer_t *buf, const char *fmt, ...) {
113 if (!buf || !fmt) {
114 return;
115 }
116
117 // Ensure we have space for a formatted string
118 // Start with a reasonable buffer size
119 size_t remaining = buf->capacity - buf->len;
120 if (remaining < 256) {
121 // Grow buffer to have at least 512 bytes of space
122 size_t new_capacity = buf->capacity + 512;
123 char *new_data = SAFE_MALLOC(new_capacity, char *);
124 if (!new_data) {
125 return;
126 }
127
128 if (buf->len > 0 && buf->data) {
129 memcpy(new_data, buf->data, buf->len);
130 }
131
132 SAFE_FREE(buf->data);
133 buf->data = new_data;
134 buf->capacity = new_capacity;
135 remaining = buf->capacity - buf->len;
136 }
137
138 // Format the string into the buffer
139 va_list args;
140 va_start(args, fmt);
141 int written = vsnprintf(buf->data + buf->len, remaining, fmt, args);
142 va_end(args);
143
144 if (written > 0 && written < (int)remaining) {
145 buf->len += (size_t)written;
146 }
147 // If vsnprintf failed or truncated, we silently ignore (buffer may be full)
148}
149
151 if (!buf) {
152 return;
153 }
154
155 frame_buffer_append(buf, "\033[H", 3);
156}
157
159 if (!buf) {
160 return;
161 }
162
163 // Combine clear screen and home in one operation
164 static const char clear[] = "\033[2J\033[H";
165 frame_buffer_append(buf, clear, sizeof(clear) - 1);
166}
167
168// Global configuration for terminal screen output FD (used by splash/status
169// screens)
170static _Thread_local int g_terminal_screen_output_fd = STDOUT_FILENO;
171
173 if (!buf || buf->len == 0 || !buf->data) {
174 return;
175 }
176
177 // Repaint from the top while erasing each line before writing it. A full
178 // screen erase on every animation frame exposes a black frame on Windows
179 // terminals and makes the splash appear to flicker.
180 size_t line_count = 1;
181 for (size_t i = 0; i < buf->len; i++) {
182 if (buf->data[i] == '\n') {
183 line_count++;
184 }
185 }
186
187 size_t output_size = 3 + buf->len + (line_count * 4);
188 char *output = SAFE_MALLOC(output_size, char *);
189 if (!output) {
190 return;
191 }
192
193 size_t output_len = 0;
194 memcpy(output + output_len, "\033[H", 3);
195 output_len += 3;
196 bool at_line_start = true;
197 for (size_t i = 0; i < buf->len; i++) {
198 if (at_line_start) {
199 memcpy(output + output_len, "\033[2K", 4);
200 output_len += 4;
201 at_line_start = false;
202 }
203 output[output_len++] = buf->data[i];
204 if (buf->data[i] == '\n') {
205 at_line_start = true;
206 }
207 }
208
209 ui_output_write(g_terminal_screen_output_fd, output, output_len);
210 SAFE_FREE(output);
211}
212
214 g_terminal_screen_output_fd = fd;
216}
217
219 return g_terminal_screen_output_fd;
220}
221
223 if (!buf) {
224 return 0;
225 }
226 return buf->len;
227}
228
230 if (!buf || !buf->data) {
231 return NULL;
232 }
233 return buf->data;
234}
235
236static void render_rule(frame_buffer_t *buf, int width, const char *color,
237 bool use_utf8) {
238 if (!buf || width <= 0)
239 return;
240 if (color)
241 frame_buffer_printf(buf, "%s", color);
242 for (int i = 0; i < width; ++i)
243 frame_buffer_printf(buf, "%s", use_utf8 ? "━" : "-");
244 if (color)
245 frame_buffer_printf(buf, "\033[0m");
246 frame_buffer_printf(buf, "\n");
247}
248
250 const char *color) {
251 render_rule(buf, cols - 1, color,
253}
254
256 bool use_colors) {
257 render_rule(buf, width,
258 use_colors ? ui_text_color_prefix(LOG_COLOR_DEBUG) : NULL,
259 use_utf8);
260}
261
263 int cols) {
264 if (!buf || !text || cols <= 0) {
265 return 0;
266 }
267
268 // Calculate visible width accounting for ANSI codes
269 int visible_width = display_width(text);
270 if (visible_width < 0) {
271 visible_width = (int)strlen(text);
272 }
273
274 int padding = (cols - visible_width) / 2;
275 if (padding < 0) {
276 padding = 0;
277 }
278
279 for (int i = 0; i < padding; i++) {
280 frame_buffer_printf(buf, " ");
281 }
282 frame_buffer_printf(buf, "%s\n", text);
283
284 return padding;
285}
@ LOG_COLOR_DEBUG
Definition color.h:8
unsigned int width
Definition fft.c:24
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
int frame_buffer_render_centered(frame_buffer_t *buf, const char *text, int cols)
Render centered text (used by both splash and status screens).
void frame_buffer_cursor_home(frame_buffer_t *buf)
Append cursor home escape code (\033[H).
void frame_buffer_set_screen_output_fd(int fd)
Set output file descriptor for terminal screen (splash/status) rendering.
void frame_buffer_reset(frame_buffer_t *buf)
Clear buffer contents while keeping allocation.
void frame_buffer_destroy(frame_buffer_t *buf)
Destroy a frame buffer.
void frame_buffer_flush(frame_buffer_t *buf)
Flush the entire buffer to stdout in one atomic write.
void frame_buffer_render_divider(frame_buffer_t *buf, int width, bool use_utf8, bool use_colors)
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.
int frame_buffer_get_screen_output_fd(void)
Get current output file descriptor for terminal screens.
void frame_buffer_render_border(frame_buffer_t *buf, int cols, const char *color)
Render a horizontal border line (used by both splash and status screens).
void frame_buffer_clear_screen(frame_buffer_t *buf)
Append clear screen and home escape codes (\033[2J\033[H).
Buffered terminal frame rendering.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define GET_OPTION(field)
Definition options.h:1339
int colored_string_set_output_fd(int fd)
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🖥️ Display Width and Terminal Centering Utilities
📝 Logging API with multiple log levels and terminal output control
action_args_t args
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
asciichat_error_t ui_output_write(int fd, const char *data, size_t length)
Definition output.c:16
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
Growable byte buffer for assembling one complete terminal frame.
char * data
Allocated buffer.
size_t len
Current content length.
size_t capacity
Allocated capacity.
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18