ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
buttons.c
Go to the documentation of this file.
7
8int ui_buttons_key(ui_buttons_t *s, const ui_button_t *b, size_t n, keyboard_key_t key) {
9 if (!s || !b || !n) {
10 SET_ERRNO(ERROR_INVALID_PARAM, "Button state and entries are required");
11 return -1;
12 }
13 if (s->selected >= n)
14 s->selected = 0;
15 if (key == KEY_LEFT || key == KEY_UP)
16 s->selected = s->selected ? s->selected - 1 : n - 1;
17 else if (key == KEY_RIGHT || key == KEY_DOWN || key == '\t')
18 s->selected = (s->selected + 1) % n;
19 else {
20 for (size_t i = 0; i < n; ++i)
21 if (!b[i].disabled && b[i].shortcut && key == b[i].shortcut)
22 return (int)i;
23 if ((key == '\r' || key == '\n' || key == ' ') && !b[s->selected].disabled)
24 return (int)s->selected;
25 }
26 return -1;
27}
28
29void ui_buttons_render(frame_buffer_t *out, int row, int left, int width, const ui_button_t *b, size_t n,
30 size_t selected) {
31 if (!out || !b || row < 1 || left < 1 || width < 1) {
32 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid buttons output or geometry");
33 return;
34 }
35 int total = n ? (int)(n - 1) * 3 : 0;
36 for (size_t i = 0; i < n; ++i)
37 total += display_width(b[i].label);
38 bool stacked = total > width;
39 int col = left + (width - total) / 2;
40 for (size_t i = 0; i < n; ++i) {
41 int length = display_width(b[i].label);
42 int x = stacked ? left + (width - length) / 2 : col;
43 bool selected_button = i == selected;
44 bool colors = !GET_OPTION(strip_ansi) && GET_OPTION(color_mode) != TERM_COLOR_NONE &&
45 terminal_should_color_output(frame_buffer_get_screen_output_fd(), options_terminal_settings());
46 if (selected_button && colors) {
47 terminal_color_mode_t mode = GET_OPTION(color_mode);
48 if (mode == TERM_COLOR_AUTO)
49 mode = (terminal_color_mode_t)log_get_color_mode();
50 if (mode == TERM_COLOR_NONE)
51 mode = TERM_COLOR_16;
53 colorscheme_current_palette(&palette, mode);
55 frame_buffer_printf(out, "\033[%d;%dH\033[0m%s", row + (stacked ? (int)i : 0), x, palette.foreground);
56 if (b[i].color > LOG_COLOR_DEV && b[i].color < LOG_COLOR_RESET)
57 frame_buffer_printf(out, "%s", palette.background_codes[b[i].color]);
58 else if (mode >= TERM_COLOR_TRUECOLOR)
59 frame_buffer_printf(out, "\033[48;2;%u;%u;%um", grey, grey, grey);
60 else if (mode >= TERM_COLOR_256)
61 frame_buffer_printf(out, "\033[48;5;%um", 232 + (unsigned)grey * 23 / 255);
62 else
63 frame_buffer_printf(out, "\033[%dm", palette.background == TERM_BACKGROUND_DARK ? 100 : 47);
64 frame_buffer_printf(out, "%s\033[0m", b[i].label);
65 } else {
66 const char *label = colored_string(b[i].disabled ? LOG_COLOR_GREY
67 : b[i].color ? b[i].color
69 b[i].label);
70 frame_buffer_printf(out, "\033[%d;%dH%s%s\033[0m", row + (stacked ? (int)i : 0), x,
71 selected_button ? "\033[7m" : "", label);
72 }
73 col += length + 3;
74 }
75}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
void ui_buttons_render(frame_buffer_t *out, int row, int left, int width, const ui_button_t *b, size_t n, size_t selected)
Definition buttons.c:29
int ui_buttons_key(ui_buttons_t *s, const ui_button_t *b, size_t n, keyboard_key_t key)
Definition buttons.c:8
void colorscheme_current_palette(colorscheme_palette_t *palette, terminal_color_mode_t mode)
uint8_t colorscheme_highlight_background(void)
@ TERM_BACKGROUND_DARK
Definition colorscheme.h:67
int color_mode
Definition fft.c:25
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:22
unsigned int width
Definition fft.c:23
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
int frame_buffer_get_screen_output_fd(void)
Get current output file descriptor for terminal screens.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
unsigned char uint8_t
Definition common.h:60
@ ERROR_INVALID_PARAM
int log_get_color_mode(void)
Definition log.c:1919
@ LOG_COLOR_RESET
Definition log/log.h:155
@ LOG_COLOR_GREY
Definition log/log.h:154
@ LOG_COLOR_FATAL
Definition log/log.h:153
@ LOG_COLOR_DEV
Definition log/log.h:148
#define GET_OPTION(field)
Safely get a specific option field (lock-free read).
Definition options.h:1336
#define options_terminal_settings()
Definition options.h:1296
keyboard_key_t
Options for interactive line editing.
Definition keyboard.h:53
@ KEY_UP
Up arrow key.
Definition keyboard.h:57
@ KEY_LEFT
Left arrow key.
Definition keyboard.h:59
@ KEY_RIGHT
Right arrow key.
Definition keyboard.h:60
@ KEY_DOWN
Down arrow key.
Definition keyboard.h:58
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
Definition string.c:284
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
🖥️ Display Width and Terminal Centering Utilities
⚙️ 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
🔤 String Manipulation and Shell Escaping Utilities
char background_codes[8][32]
terminal_background_t background
log_color_t color
Definition buttons.h:9
bool disabled
Definition buttons.h:8
When code needs to work with ui button, it keeps label, shortcut, disabled, color here instead of sca...
Definition buttons.h:16
size_t selected
Definition buttons.h:17