ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
scrollbar.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define UI_SCROLLBAR_CELL_BUFFER_SIZE 128
4
7#include <libplatform/terminal.h>
9#include <stdio.h>
10
11ui_scrollbar_geometry_t ui_scrollbar_measure(size_t total, size_t visible, size_t offset, size_t height) {
13 if (!height || !visible || total <= visible)
14 return g;
15 g.visible = true;
16 g.length = (size_t)((long double)visible * height / total);
17 if (!g.length)
18 g.length = 1;
19 if (g.length > height)
20 g.length = height;
21 if (offset > total - visible)
22 offset = total - visible;
23 g.start = (size_t)((long double)offset * (height - g.length) / (total - visible));
24 return g;
25}
26
27void ui_scrollbar_cell(char *out, size_t cap, size_t row, size_t height, size_t total, size_t visible, size_t offset) {
28 if (!out || !cap) {
29 SET_ERRNO(ERROR_INVALID_PARAM, "Scrollbar cell requires output storage");
30 return;
31 }
32 ui_scrollbar_geometry_t g = ui_scrollbar_measure(total, visible, offset, height);
33 bool thumb = g.visible && row >= g.start && row - g.start < g.length;
34 const char *glyph = !g.visible ? " "
35 : terminal_should_use_utf8(GET_OPTION(force_utf8)) ? (thumb ? "█" : "│")
36 : (thumb ? "#" : "|");
37 snprintf(out, cap, "%s", colored_string(thumb ? LOG_COLOR_DEBUG : LOG_COLOR_GREY, glyph));
38}
39
40void ui_scrollbar_render(frame_buffer_t *out, int row, int col, int height, size_t total, size_t visible,
41 size_t offset) {
42 if (!out || row < 1 || col < 1 || height < 1) {
43 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid scrollbar output or geometry");
44 return;
45 }
46 frame_buffer_append(out, "\0337", 2);
47 for (int i = 0; i < height; ++i) {
49 ui_scrollbar_cell(cell, sizeof(cell), (size_t)i, (size_t)height, total, visible, offset);
50 frame_buffer_printf(out, "\033[%d;%dH%s", row + i, col, cell);
51 }
52 frame_buffer_append(out, "\0338", 2);
53}
#define SET_ERRNO(code,...)
unsigned int height
Definition fft.c:23
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.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
@ ERROR_INVALID_PARAM
@ LOG_COLOR_GREY
Definition log/log.h:154
@ LOG_COLOR_DEBUG
Definition log/log.h:149
#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
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
static bool size_t gotel_query_t * out
Definition queries.h:8
void ui_scrollbar_render(frame_buffer_t *out, int row, int col, int height, size_t total, size_t visible, size_t offset)
Definition scrollbar.c:40
ui_scrollbar_geometry_t ui_scrollbar_measure(size_t total, size_t visible, size_t offset, size_t height)
Definition scrollbar.c:11
#define UI_SCROLLBAR_CELL_BUFFER_SIZE
Definition scrollbar.c:3
void ui_scrollbar_cell(char *out, size_t cap, size_t row, size_t height, size_t total, size_t visible, size_t offset)
Definition scrollbar.c:27
🔤 String Manipulation and Shell Escaping Utilities