ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
ui/input.c
Go to the documentation of this file.
5#include <string.h>
7 int width, int height, const char *text,
8 size_t cursor, bool preserve_quotes) {
9 if (!buffer || !text || row < 1 || col < 1 || width < 5 || height < 1 ||
10 cursor > strlen(text) || !utf8_is_valid(text) ||
11 (cursor && ((unsigned char)text[cursor] & 0xc0) == 0x80))
13 "Invalid input field text, cursor or geometry");
14 int lines = 1;
15 size_t cursor_line = 0;
16 for (size_t i = 0; text[i]; ++i)
17 if (text[i] == '\n') {
18 ++lines;
19 if (i < cursor)
20 ++cursor_line;
21 }
22 int shown = lines < height ? lines : height;
23 int first = cursor_line >= (size_t)shown ? (int)cursor_line - shown + 1 : 0;
24 const char *line = text;
25 int number = 0, cursor_col = col + 2;
26 while (line && number < first + shown) {
27 const char *end = strchr(line, '\n');
28 if (!end)
29 end = line + strlen(line);
30 if (number >= first) {
31 const char *start = line;
32 bool active_line = (size_t)number == cursor_line;
33 bool quoted = preserve_quotes && end - line >= 2 &&
34 (*line == '"' || *line == '\'') && end[-1] == *line;
35 const char *anchor = active_line ? text + cursor : end;
36 int prefix = 0;
37 if (quoted &&
38 utf8_display_width_n(line, (size_t)(anchor - line)) > width - 1) {
39 // Keep the opening quote visible while scrolling the path's interior.
40 // The tail includes the closing quote when the cursor is at the end.
41 start = line + 1;
42 prefix = 4;
43 while (start < anchor &&
44 utf8_display_width_n(start, (size_t)(anchor - start)) >
45 width - prefix - 1)
46 start += utf8_next_char_bytes(start, (size_t)(end - start));
47 } else if (active_line) {
48 while (start < anchor &&
49 utf8_display_width_n(start, (size_t)(anchor - start)) >
50 width - 1)
51 start += utf8_next_char_bytes(start, (size_t)(end - start));
52 }
53 if (active_line)
54 cursor_col = col + 2 + prefix +
55 utf8_display_width_n(start, (size_t)(anchor - start));
56 frame_buffer_printf(buffer, "\033[%d;%dH%s", row + number - first, col,
57 active_line ? "> " : " ");
58 if (prefix)
59 frame_buffer_printf(buffer, "%c...", *line);
60 int used = prefix;
61 while (start < end) {
62 int bytes = utf8_next_char_bytes(start, (size_t)(end - start));
63 if (bytes <= 0)
64 break;
65 int cells = utf8_display_width_n(start, (size_t)bytes);
66 if (used + cells > width)
67 break;
68 frame_buffer_append(buffer, start, (size_t)bytes);
69 used += cells;
70 start += bytes;
71 }
72 frame_buffer_printf(buffer, "%*s", width - used, "");
73 }
74 ++number;
75 line = *end ? end + 1 : NULL;
76 }
77 frame_buffer_printf(buffer, "\033[%d;%dH\033[?25h",
78 row + (int)cursor_line - first, cursor_col);
79 return ASCIICHAT_OK;
80}
81
82static bool word_space(unsigned char byte) {
83 return byte == ' ' || (byte >= '\t' && byte <= '\r');
84}
85
86static size_t previous(const char *text, size_t cursor) {
87 if (cursor) {
88 do {
89 --cursor;
90 } while (cursor && ((unsigned char)text[cursor] & 0xc0) == 0x80);
91 }
92 return cursor;
93}
94
95void ui_input_edit_key(char *buffer, size_t capacity, size_t *length,
96 size_t *position, keyboard_key_t key) {
97 if (!buffer || !capacity || !length || !position || *length >= capacity ||
98 *position > *length || strnlen(buffer, capacity) != *length ||
99 !utf8_is_valid(buffer) ||
100 (*position && ((unsigned char)buffer[*position] & 0xc0) == 0x80)) {
102 "Invalid line editor storage, UTF-8 or cursor");
103 return;
104 }
105 size_t len = *length, cursor = *position, start = cursor, end = cursor;
106 if (key == KEY_HOME)
107 cursor = 0;
108 else if (key == KEY_END)
109 cursor = len;
110 else if (key == KEY_LEFT)
111 cursor = previous(buffer, cursor);
112 else if (key == KEY_RIGHT && cursor < len)
113 cursor += (size_t)utf8_next_char_bytes(buffer + cursor, len - cursor);
114 else if (key == 8 || key == 127)
115 start = previous(buffer, cursor);
116 else if (key == KEY_DELETE && cursor < len)
117 end += (size_t)utf8_next_char_bytes(buffer + cursor, len - cursor);
118 else if (key == 23) {
119 while (start && word_space((unsigned char)buffer[start - 1]))
120 start = previous(buffer, start);
121 while (start && !word_space((unsigned char)buffer[start - 1]))
122 start = previous(buffer, start);
123 } else if (key == KEY_CTRL_DELETE) {
124 while (end < len && !word_space((unsigned char)buffer[end]))
125 end += (size_t)utf8_next_char_bytes(buffer + end, len - end);
126 while (end < len && word_space((unsigned char)buffer[end]))
127 ++end;
128 } else {
129 char text[8];
130 if (key == '\t' || (key >= 32 && key != 127)) {
131 if (keyboard_text_encode(key, text))
132 (void)ui_input_insert(buffer, capacity, length, position, text);
133 }
134 return;
135 }
136 if (start != end) {
137 memmove(buffer + start, buffer + end, len - end + 1);
138 len -= end - start;
139 cursor = start;
140 }
141 *length = len;
142 *position = cursor;
143}
144
145asciichat_error_t ui_input_insert(char *buffer, size_t capacity, size_t *length,
146 size_t *cursor, const char *text) {
147 if (!buffer || !length || !cursor || !text || *length >= capacity ||
148 *cursor > *length || strnlen(buffer, capacity) != *length ||
149 !utf8_is_valid(text) || !utf8_is_valid(buffer) ||
150 (*cursor && ((unsigned char)buffer[*cursor] & 0xc0) == 0x80))
152 "Invalid input insertion storage, text or cursor");
153 size_t bytes = strlen(text);
154 if (bytes >= capacity - *length)
156 "Input insertion exceeds field capacity");
157 memmove(buffer + *cursor + bytes, buffer + *cursor, *length - *cursor + 1);
158 memcpy(buffer + *cursor, text, bytes);
159 *cursor += bytes;
160 *length += bytes;
161 return ASCIICHAT_OK;
162}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
Common buffer size definitions.
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int height
Definition fft.c:24
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.
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.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ 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
bool utf8_is_valid(const char *str)
Check if a string is valid UTF-8.
Definition utf8.c:132
int utf8_next_char_bytes(const char *str, size_t max_bytes)
Get byte length of next UTF-8 character.
Definition utf8.c:183
🔤 String Manipulation and Shell Escaping Utilities
asciichat_error_t ui_input_insert(char *buffer, size_t capacity, size_t *length, size_t *cursor, const char *text)
Definition ui/input.c:145
void ui_input_edit_key(char *buffer, size_t capacity, size_t *length, size_t *position, keyboard_key_t key)
Definition ui/input.c:95
asciichat_error_t ui_input_render(frame_buffer_t *buffer, int row, int col, int width, int height, const char *text, size_t cursor, bool preserve_quotes)
Definition ui/input.c:6
🔤 UTF-8 Encoding and Decoding Utilities