ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
input.h File Reference

Go to the source code of this file.

Functions

void ui_input_edit_key (char *buffer, size_t capacity, size_t *length, size_t *cursor, keyboard_key_t key)
asciichat_error_t ui_input_render (frame_buffer_t *output, int row, int column, int width, int height, const char *text, size_t cursor, bool preserve_quotes)
asciichat_error_t ui_input_insert (char *buffer, size_t capacity, size_t *length, size_t *cursor, const char *text)

Function Documentation

◆ ui_input_edit_key()

void ui_input_edit_key ( char * buffer,
size_t capacity,
size_t * length,
size_t * cursor,
keyboard_key_t key )

UTF-8 cursor movement/deletion in caller-owned single-line storage.

Definition at line 95 of file ui/input.c.

96 {
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}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
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
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

References ERROR_INVALID_PARAM, SET_ERRNO, ui_input_insert(), utf8_is_valid(), and utf8_next_char_bytes().

Referenced by ui_input_read_key().

◆ ui_input_insert()

asciichat_error_t ui_input_insert ( char * buffer,
size_t capacity,
size_t * length,
size_t * cursor,
const char * text )

Insert UTF-8 text atomically, retaining storage unchanged on failure.

Definition at line 145 of file ui/input.c.

146 {
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}
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_BUFFER_OVERFLOW

References ASCIICHAT_OK, ERROR_BUFFER_OVERFLOW, ERROR_INVALID_PARAM, SET_ERRNO, and utf8_is_valid().

Referenced by log_search_insert_text(), and ui_input_edit_key().

◆ ui_input_render()

asciichat_error_t ui_input_render ( frame_buffer_t * output,
int row,
int column,
int width,
int height,
const char * text,
size_t cursor,
bool preserve_quotes )

Render a scrollable UTF-8 field with an active-row marker and visible cursor. Width excludes the two-cell marker. Quoted paths may retain their opening quote.

Definition at line 6 of file ui/input.c.

8 {
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}
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.
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

References ASCIICHAT_OK, cells, ERROR_INVALID_PARAM, frame_buffer_append(), frame_buffer_printf(), height, SET_ERRNO, utf8_display_width_n(), utf8_is_valid(), utf8_next_char_bytes(), and width.

Referenced by ui_media_panel_render().