ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
text.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define UI_TEXT_NUMBER_BUFFER_SIZE 96
4#define UI_TEXT_STYLED_BUFFER_SIZE 4096
5
6/* Existing buffer budgets in bytes, including NUL for text storage. */
7#define UI_TEXT_COLOR_BUFFER_SIZE 128
8
13#include <ctype.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
19 static _Thread_local char buffers[LOG_COLOR_WHITE + 1]
21 if (color < LOG_COLOR_DEV || color > LOG_COLOR_WHITE) {
22 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid UI text color");
23 return "";
24 }
25 char *out = buffers[color];
26 const char *styled = colored_string(color, "!");
27 const char *marker = strchr(styled, '!');
28 size_t length = marker ? (size_t)(marker - styled) : 0;
29 if (length >= sizeof(buffers[0]))
30 length = sizeof(buffers[0]) - 1;
31 memcpy(out, styled, length);
32 out[length] = '\0';
33 return out;
34}
35
36static void append(char *out, size_t cap, size_t *used, const char *text,
37 size_t length) {
38 if (!cap || *used >= cap - 1)
39 return;
40 size_t room = cap - 1 - *used;
41 if (length > room) {
43 "UI text buffer needs %zu additional bytes", length - room);
44 return;
45 }
46 memcpy(out + *used, text, length);
47 *used += length;
48 out[*used] = '\0';
49}
50
51size_t ui_text_cell(char *out, size_t cap, const char *text, int width,
52 ui_alignment_t align) {
53 if (!out || !cap) {
54 SET_ERRNO(ERROR_INVALID_PARAM, "UI text requires output storage");
55 return 0;
56 }
57 out[0] = '\0';
58 if (width <= 0)
59 return 0;
60 if (!text)
61 text = "";
62 int columns = 0;
63 const char *end = text;
64 while (*end) {
65 if (end[0] == '\033' && end[1] == '[') {
66 const char *escape = end + 2;
67 while (*escape && !(*escape >= '@' && *escape <= '~'))
68 ++escape;
69 if (!*escape)
70 break;
71 end = escape + 1;
72 continue;
73 }
74 if ((unsigned char)*end < 32)
75 break;
76 int bytes = utf8_next_char_bytes(end, strlen(end));
77 if (bytes <= 0)
78 break;
79 int n = utf8_display_width_n(end, (size_t)bytes);
80 if (n < 0 || columns + n > width)
81 break;
82 columns += n;
83 end += bytes;
84 }
85 size_t used = 0;
86 int pad = width - columns;
87 int left = align == UI_ALIGN_RIGHT ? pad
88 : align == UI_ALIGN_CENTER ? pad / 2
89 : 0;
90 for (int i = 0; i < left; ++i)
91 append(out, cap, &used, " ", 1);
92 append(out, cap, &used, text, (size_t)(end - text));
93 // A clipped styled cell must not bleed into the following column.
94 if (memchr(text, '\033', (size_t)(end - text)))
95 append(out, cap, &used, "\033[0m", 4);
96 for (int i = left; i < pad; ++i)
97 append(out, cap, &used, " ", 1);
98 return used;
99}
100
101void ui_text_values_style(char *out, size_t cap, const char *text,
102 log_color_t zero_color) {
103 if (!out || !cap) {
104 SET_ERRNO(ERROR_INVALID_PARAM, "UI text requires output storage");
105 return;
106 }
107 size_t used = 0;
108 out[0] = '\0';
109 if (!text)
110 return;
111 for (const char *p = text; *p;) {
112 if (p[0] == '\033' && p[1] == '[') {
113 const char *end = p + 2;
114 while (*end && !(*end >= '@' && *end <= '~'))
115 ++end;
116 if (*end)
117 ++end;
118 append(out, cap, &used, p, (size_t)(end - p));
119 p = end;
120 } else if (isdigit((unsigned char)*p)) {
121 char *end;
122 double value = p[0] == '0' && p[1] == 'x' ? (double)strtoull(p, &end, 16)
123 : strtod(p, &end);
124 char number[UI_TEXT_NUMBER_BUFFER_SIZE];
125 size_t length = (size_t)(end - p);
126 if (length >= sizeof(number))
127 length = sizeof(number) - 1;
128 memcpy(number, p, length);
129 number[length] = '\0';
130 const char *styled =
131 colored_string(value == 0 ? zero_color : LOG_COLOR_FATAL, number);
132 append(out, cap, &used, styled, strlen(styled));
133 p = end;
134 } else if ((p == text || !isalnum((unsigned char)p[-1])) &&
135 ((!strncmp(p, "enabled", 7) && !isalnum((unsigned char)p[7])) ||
136 (!strncmp(p, "disabled", 8) &&
137 !isalnum((unsigned char)p[8])))) {
138 bool enabled = *p == 'e';
139 const char *styled =
141 enabled ? "enabled" : "disabled");
142 append(out, cap, &used, styled, strlen(styled));
143 p += enabled ? 7 : 8;
144 } else {
145 int bytes = utf8_next_char_bytes(p, strlen(p));
146 if (bytes <= 0)
147 break;
148 append(out, cap, &used, p, (size_t)bytes);
149 p += bytes;
150 }
151 }
152}
153
154void ui_text_values(char *out, size_t cap, const char *text) {
156}
157
158void ui_text_render_values(frame_buffer_t *out, const char *text) {
159 char styled[UI_TEXT_STYLED_BUFFER_SIZE];
160 ui_text_values_style(styled, sizeof(styled), text, LOG_COLOR_FATAL);
161 frame_buffer_append(out, styled, strlen(styled));
162}
#define SET_ERRNO(code,...)
log_color_t
Definition color.h:6
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_ERROR
Definition color.h:11
@ LOG_COLOR_INFO
Definition color.h:9
@ LOG_COLOR_WHITE
Definition color.h:15
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.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
bool enabled
Is filtering active?
Definition grep.c:63
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
int utf8_display_width_n(const char *str, size_t max_bytes)
Calculate terminal display width of a UTF-8 string up to byte limit.
Definition utf8.c:106
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:183
🖥️ Display Width and Terminal Centering Utilities
static bool size_t gotel_query_t * out
Definition queries.h:10
🔤 String Manipulation and Shell Escaping Utilities
#define UI_TEXT_STYLED_BUFFER_SIZE
Definition text.c:4
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
#define UI_TEXT_COLOR_BUFFER_SIZE
Definition text.c:7
void ui_text_values_style(char *out, size_t cap, const char *text, log_color_t zero_color)
Definition text.c:101
size_t ui_text_cell(char *out, size_t cap, const char *text, int width, ui_alignment_t align)
Definition text.c:51
void ui_text_values(char *out, size_t cap, const char *text)
Definition text.c:154
#define UI_TEXT_NUMBER_BUFFER_SIZE
Definition text.c:3
void ui_text_render_values(frame_buffer_t *out, const char *text)
Definition text.c:158
ui_alignment_t
Definition text.h:14
@ UI_ALIGN_RIGHT
Definition text.h:14
@ UI_ALIGN_CENTER
Definition text.h:14
🔤 UTF-8 Encoding and Decoding Utilities