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 File Reference

Buffered terminal frame rendering implementation. More...

Go to the source code of this file.

Data Structures

struct  frame_buffer
 Growable byte buffer for assembling one complete terminal frame. More...

Functions

frame_buffer_t * frame_buffer_create (int rows, int cols)
 Create a new frame buffer.
void frame_buffer_destroy (frame_buffer_t *buf)
 Destroy a frame buffer.
void frame_buffer_reset (frame_buffer_t *buf)
 Clear buffer contents while keeping allocation.
void frame_buffer_append (frame_buffer_t *buf, const char *str, size_t len)
 Append raw bytes to the buffer.
void frame_buffer_printf (frame_buffer_t *buf, const char *fmt,...)
 Printf-style append to the buffer.
void frame_buffer_cursor_home (frame_buffer_t *buf)
 Append cursor home escape code (\033[H).
void frame_buffer_clear_screen (frame_buffer_t *buf)
 Append clear screen and home escape codes (\033[2J\033[H).
void frame_buffer_flush (frame_buffer_t *buf)
 Flush the entire buffer to stdout in one atomic write.
void frame_buffer_set_screen_output_fd (int fd)
 Set output file descriptor for terminal screen (splash/status) rendering.
int frame_buffer_get_screen_output_fd (void)
 Get current output file descriptor for terminal screens.
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.
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_render_divider (frame_buffer_t *buf, int width, bool use_utf8, bool use_colors)
int frame_buffer_render_centered (frame_buffer_t *buf, const char *text, int cols)
 Render centered text (used by both splash and status screens).

Detailed Description

Buffered terminal frame rendering implementation.

Accumulates terminal output into a growable buffer and flushes atomically.

Definition in file frame_buffer.c.

Function Documentation

◆ frame_buffer_append()

void frame_buffer_append ( frame_buffer_t * buf,
const char * str,
size_t len )

Append raw bytes to the buffer.

Parameters
bufFrame buffer
strPointer to data (may contain ANSI codes, not NULL-terminated)
lenNumber of bytes to append

Grows the buffer as needed. Does not add newlines or NULL terminators.

Definition at line 81 of file frame_buffer.c.

81 {
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}
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
char * data
Allocated buffer.
size_t len
Current content length.
size_t capacity
Allocated capacity.

References frame_buffer::capacity, frame_buffer::data, frame_buffer::len, SAFE_FREE, and SAFE_MALLOC.

Referenced by ascii_create_grid(), frame_buffer_clear_screen(), frame_buffer_cursor_home(), terminal_screen_compose(), ui_controller_write(), ui_error_popup_preserve(), ui_error_popup_render(), ui_input_render(), ui_media_panel_render(), ui_notice_flush_snapshot(), ui_notice_present(), ui_notice_render(), ui_reconnect_render(), ui_scrollbar_render(), ui_session_media(), ui_session_render(), ui_text_render_values(), and ui_too_small_render().

◆ frame_buffer_clear_screen()

void frame_buffer_clear_screen ( frame_buffer_t * buf)

Append clear screen and home escape codes (\033[2J\033[H).

Parameters
bufFrame buffer

Combines clear screen and cursor home into a single operation for atomicity.

Definition at line 158 of file frame_buffer.c.

158 {
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}
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.

References frame_buffer_append().

Referenced by ui_session_render().

◆ frame_buffer_create()

frame_buffer_t * frame_buffer_create ( int rows,
int cols )

Create a new frame buffer.

Parameters
rowsTerminal row count (used for initial capacity estimate)
colsTerminal column count (used for initial capacity estimate)
Returns
Newly allocated frame buffer, or NULL on allocation failure

Initial capacity is set to rows * cols * 16 to account for ANSI escape overhead.

Definition at line 42 of file frame_buffer.c.

42 {
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}
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.

References frame_buffer::capacity, frame_buffer::data, frame_buffer::len, SAFE_FREE, and SAFE_MALLOC.

Referenced by ascii_create_grid(), terminal_screen_compose(), tui_stats_render(), ui_error_popup_preserve(), ui_notice_flush_snapshot(), ui_notice_present(), ui_session_context_help_render(), ui_session_create(), and ui_session_help_render().

◆ frame_buffer_cursor_home()

void frame_buffer_cursor_home ( frame_buffer_t * buf)

Append cursor home escape code (\033[H).

Parameters
bufFrame buffer

Definition at line 150 of file frame_buffer.c.

150 {
151 if (!buf) {
152 return;
153 }
154
155 frame_buffer_append(buf, "\033[H", 3);
156}

References frame_buffer_append().

Referenced by tui_stats_render().

◆ frame_buffer_destroy()

void frame_buffer_destroy ( frame_buffer_t * buf)

Destroy a frame buffer.

Parameters
bufFrame buffer to destroy (may be NULL)

Definition at line 64 of file frame_buffer.c.

64 {
65 if (!buf) {
66 return;
67 }
68
69 SAFE_FREE(buf->data);
70 SAFE_FREE(buf);
71}

References frame_buffer::data, and SAFE_FREE.

Referenced by ascii_create_grid(), terminal_screen_compose(), tui_stats_render(), ui_error_popup_preserve(), ui_notice_flush_snapshot(), ui_notice_present(), ui_session_context_help_render(), ui_session_destroy(), and ui_session_help_render().

◆ frame_buffer_flush()

void frame_buffer_flush ( frame_buffer_t * buf)

Flush the entire buffer to stdout in one atomic write.

Parameters
bufFrame buffer

Writes all accumulated content to stdout via platform_write_all(), bypassing libc buffering. Safe for high-frequency rendering.

Definition at line 172 of file frame_buffer.c.

172 {
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}
asciichat_error_t ui_output_write(int fd, const char *data, size_t length)
Definition output.c:16

References frame_buffer::data, frame_buffer::len, SAFE_FREE, SAFE_MALLOC, and ui_output_write().

◆ frame_buffer_get_content()

const char * frame_buffer_get_content ( const frame_buffer_t * buf)

Get a pointer to the buffer content.

Parameters
bufFrame buffer
Returns
Pointer to the buffer data (NULL if buffer is empty or NULL)

Returns the raw buffer content for reading. The content is NOT null-terminated. Use frame_buffer_get_length() to get the actual size.

Definition at line 229 of file frame_buffer.c.

229 {
230 if (!buf || !buf->data) {
231 return NULL;
232 }
233 return buf->data;
234}

References frame_buffer::data.

Referenced by ascii_create_grid(), terminal_screen_compose(), tui_stats_render(), ui_error_popup_preserve(), ui_notice_flush_snapshot(), ui_notice_present(), ui_session_context_help_render(), ui_session_help_render(), and ui_session_render().

◆ frame_buffer_get_length()

size_t frame_buffer_get_length ( const frame_buffer_t * buf)

Get the current content length of the frame buffer.

Parameters
bufFrame buffer
Returns
Number of bytes currently in the buffer

Useful for debugging and verifying frame consistency.

Definition at line 222 of file frame_buffer.c.

222 {
223 if (!buf) {
224 return 0;
225 }
226 return buf->len;
227}

References frame_buffer::len.

Referenced by ascii_create_grid(), terminal_screen_compose(), tui_stats_render(), ui_error_popup_preserve(), ui_notice_flush_snapshot(), ui_notice_present(), ui_session_context_help_render(), ui_session_frame_length(), ui_session_help_render(), and ui_session_tick().

◆ frame_buffer_get_screen_output_fd()

int frame_buffer_get_screen_output_fd ( void )

Get current output file descriptor for terminal screens.

Returns
File descriptor (STDOUT_FILENO or STDERR_FILENO)

Definition at line 218 of file frame_buffer.c.

218 {
219 return g_terminal_screen_output_fd;
220}

Referenced by ui_buttons_render(), ui_error_popup_render(), ui_media_panel_render(), and ui_status_render_header().

◆ frame_buffer_printf()

void frame_buffer_printf ( frame_buffer_t * buf,
const char * fmt,
... )

Printf-style append to the buffer.

Parameters
bufFrame buffer
fmtFormat string (standard printf syntax)
...Arguments

Formats the string into the buffer using snprintf. Grows buffer as needed.

Definition at line 112 of file frame_buffer.c.

112 {
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}
action_args_t args

References args, frame_buffer::capacity, frame_buffer::data, frame_buffer::len, SAFE_FREE, and SAFE_MALLOC.

Referenced by frame_buffer_render_centered(), invitation_render(), terminal_screen_compose(), tui_stats_render(), ui_buttons_render(), ui_error_popup_preserve(), ui_error_popup_render(), ui_input_render(), ui_media_panel_render(), ui_notice_render(), ui_popup_frame_titled(), ui_popup_frame_with_encoding(), ui_popup_render(), ui_popup_rule(), ui_reconnect_render(), ui_scrollbar_render(), ui_session_render(), ui_session_splash_render(), ui_spinner_render(), ui_status_render_header(), and ui_table_render().

◆ frame_buffer_render_border()

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).

Parameters
bufFrame buffer to write into
colsTerminal width (number of columns)
colorANSI color code (e.g., "\033[1;36m" for cyan) or NULL for no color

Definition at line 249 of file frame_buffer.c.

250 {
251 render_rule(buf, cols - 1, color,
253}
#define GET_OPTION(field)
Definition options.h:1339
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)

References GET_OPTION, and terminal_should_use_utf8().

Referenced by tui_stats_render().

◆ frame_buffer_render_centered()

int frame_buffer_render_centered ( frame_buffer_t * buf,
const char * text,
int cols )

Render centered text (used by both splash and status screens).

Parameters
bufFrame buffer to write into
textText to center (may contain ANSI codes)
colsTerminal width
Returns
Padding added on left side (for manual padding if needed)

Automatically centers the text accounting for ANSI escape codes.

Definition at line 262 of file frame_buffer.c.

263 {
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}
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.

References display_width(), and frame_buffer_printf().

◆ frame_buffer_render_divider()

void frame_buffer_render_divider ( frame_buffer_t * buf,
int width,
bool use_utf8,
bool use_colors )

Theme-blue divider of exactly width cells, followed by a newline.

Definition at line 255 of file frame_buffer.c.

256 {
257 render_rule(buf, width,
258 use_colors ? ui_text_color_prefix(LOG_COLOR_DEBUG) : NULL,
259 use_utf8);
260}
@ LOG_COLOR_DEBUG
Definition color.h:8
unsigned int width
Definition fft.c:24
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18

References LOG_COLOR_DEBUG, ui_text_color_prefix(), and width.

Referenced by ui_session_splash_render(), and ui_status_render_header().

◆ frame_buffer_reset()

void frame_buffer_reset ( frame_buffer_t * buf)

Clear buffer contents while keeping allocation.

Resets the buffer for a new frame without deallocating the underlying memory. More efficient than destroy/create for repeated frame rendering.

Parameters
bufFrame buffer to reset

Definition at line 73 of file frame_buffer.c.

73 {
74 if (!buf) {
75 return;
76 }
77
78 buf->len = 0;
79}

References frame_buffer::len.

Referenced by ui_session_clear(), ui_session_media(), and ui_session_render().

◆ frame_buffer_set_screen_output_fd()

void frame_buffer_set_screen_output_fd ( int fd)

Set output file descriptor for terminal screen (splash/status) rendering.

Parameters
fdFile descriptor (STDOUT_FILENO for TTY, STDERR_FILENO for piped output)

Controls where splash and status screens write their output. Used internally by terminal_screen_render() for consistent output routing.

Definition at line 213 of file frame_buffer.c.

213 {
214 g_terminal_screen_output_fd = fd;
216}
int colored_string_set_output_fd(int fd)

References colored_string_set_output_fd().

Referenced by terminal_screen_set_output_fd().