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

Go to the source code of this file.

Macros

#define UI_CLIPPED_TEXT_OVERHEAD   32

Enumerations

enum  ui_alignment_t { UI_ALIGN_LEFT , UI_ALIGN_RIGHT , UI_ALIGN_CENTER }

Functions

size_t ui_text_cell (char *out, size_t capacity, const char *text, int width, ui_alignment_t alignment)
void ui_text_values (char *out, size_t capacity, const char *text)
void ui_text_values_style (char *out, size_t capacity, const char *text, log_color_t zero_color)
void ui_text_render_values (frame_buffer_t *out, const char *text)
const char * ui_text_color_prefix (log_color_t color)

Macro Definition Documentation

◆ UI_CLIPPED_TEXT_OVERHEAD

#define UI_CLIPPED_TEXT_OVERHEAD   32

Extra clipping storage for ellipsis, ANSI resets, and the terminating NUL.

Definition at line 5 of file text.h.

Enumeration Type Documentation

◆ ui_alignment_t

Enumerator
UI_ALIGN_LEFT 
UI_ALIGN_RIGHT 
UI_ALIGN_CENTER 

Definition at line 14 of file text.h.

ui_alignment_t
Definition text.h:14
@ UI_ALIGN_RIGHT
Definition text.h:14
@ UI_ALIGN_CENTER
Definition text.h:14
@ UI_ALIGN_LEFT
Definition text.h:14

Function Documentation

◆ ui_text_cell()

size_t ui_text_cell ( char * out,
size_t capacity,
const char * text,
int width,
ui_alignment_t alignment )

ANSI-aware, UTF-8-safe clipping/padding into caller-owned storage.

Definition at line 51 of file text.c.

52 {
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}
#define SET_ERRNO(code,...)
unsigned int width
Definition fft.c:24
@ ERROR_INVALID_PARAM
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
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:183
std::string escape(const std::string &str)
Escape a string for JSON output.
static bool size_t gotel_query_t * out
Definition queries.h:10

References ERROR_INVALID_PARAM, out, SET_ERRNO, UI_ALIGN_CENTER, UI_ALIGN_RIGHT, utf8_display_width_n(), utf8_next_char_bytes(), and width.

Referenced by ui_error_popup_preserve(), ui_error_popup_render(), ui_help_menu_render(), and ui_table_row().

◆ ui_text_color_prefix()

const char * ui_text_color_prefix ( log_color_t color)

Theme-aware prefix for incremental formatting, derived from colored_string().

Definition at line 18 of file text.c.

18 {
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}
@ LOG_COLOR_WHITE
Definition color.h:15
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
#define UI_TEXT_COLOR_BUFFER_SIZE
Definition text.c:7

References colored_string(), ERROR_INVALID_PARAM, LOG_COLOR_WHITE, out, SET_ERRNO, and UI_TEXT_COLOR_BUFFER_SIZE.

Referenced by frame_buffer_render_divider(), tui_stats_render(), ui_error_popup_render(), and ui_table_render().

◆ ui_text_render_values()

void ui_text_render_values ( frame_buffer_t * out,
const char * text )

Definition at line 158 of file text.c.

158 {
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}
@ LOG_COLOR_FATAL
Definition color.h:12
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
#define UI_TEXT_STYLED_BUFFER_SIZE
Definition text.c:4
void ui_text_values_style(char *out, size_t cap, const char *text, log_color_t zero_color)
Definition text.c:101

References frame_buffer_append(), LOG_COLOR_FATAL, out, UI_TEXT_STYLED_BUFFER_SIZE, and ui_text_values_style().

Referenced by tui_stats_render().

◆ ui_text_values()

void ui_text_values ( char * out,
size_t capacity,
const char * text )

Theme-aware numeric/boolean styling; numeric suffixes retain default color.

Definition at line 154 of file text.c.

154 {
156}
@ LOG_COLOR_ERROR
Definition color.h:11

References LOG_COLOR_ERROR, out, and ui_text_values_style().

◆ ui_text_values_style()

void ui_text_values_style ( char * out,
size_t capacity,
const char * text,
log_color_t zero_color )

Definition at line 101 of file text.c.

102 {
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}
@ LOG_COLOR_INFO
Definition color.h:9
bool enabled
Is filtering active?
Definition grep.c:63
#define UI_TEXT_NUMBER_BUFFER_SIZE
Definition text.c:3

References colored_string(), enabled, ERROR_INVALID_PARAM, LOG_COLOR_ERROR, LOG_COLOR_FATAL, LOG_COLOR_INFO, out, SET_ERRNO, UI_TEXT_NUMBER_BUFFER_SIZE, and utf8_next_char_bytes().

Referenced by ui_help_menu_render(), ui_text_render_values(), and ui_text_values().