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

Go to the source code of this file.

Data Structures

struct  ui_button_t
struct  ui_buttons_t
 When code needs to work with ui button, it keeps label, shortcut, disabled, color here instead of scattering it across globals. Initialization establishes the handles and sizes; teardown must release any allocation, lock, thread, or callback context recorded in those fields. More...

Functions

int ui_buttons_key (ui_buttons_t *state, const ui_button_t *buttons, size_t count, keyboard_key_t key)
void ui_buttons_render (frame_buffer_t *out, int row, int left, int width, const ui_button_t *buttons, size_t count, size_t selected)

Function Documentation

◆ ui_buttons_key()

int ui_buttons_key ( ui_buttons_t * state,
const ui_button_t * buttons,
size_t count,
keyboard_key_t key )

Returns the activated index or -1. Disabled buttons cannot activate.

Definition at line 8 of file buttons.c.

9 {
10 if (!s || !b || !n) {
11 SET_ERRNO(ERROR_INVALID_PARAM, "Button state and entries are required");
12 return -1;
13 }
14 if (s->selected >= n)
15 s->selected = 0;
16 if (key == KEY_LEFT || key == KEY_UP)
17 s->selected = s->selected ? s->selected - 1 : n - 1;
18 else if (key == KEY_RIGHT || key == KEY_DOWN || key == '\t')
19 s->selected = (s->selected + 1) % n;
20 else {
21 for (size_t i = 0; i < n; ++i)
22 if (!b[i].disabled && b[i].shortcut && key == b[i].shortcut)
23 return (int)i;
24 if ((key == '\r' || key == '\n' || key == ' ') && !b[s->selected].disabled)
25 return (int)s->selected;
26 }
27 return -1;
28}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM

References ui_button_t::disabled, ERROR_INVALID_PARAM, ui_buttons_t::selected, and SET_ERRNO.

◆ ui_buttons_render()

void ui_buttons_render ( frame_buffer_t * out,
int row,
int left,
int width,
const ui_button_t * buttons,
size_t count,
size_t selected )

Definition at line 30 of file buttons.c.

31 {
32 if (!out || !b || row < 1 || left < 1 || width < 1) {
33 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid buttons output or geometry");
34 return;
35 }
36 int total = n ? (int)(n - 1) * 3 : 0;
37 for (size_t i = 0; i < n; ++i)
38 total += display_width(b[i].label);
39 bool stacked = total > width;
40 int col = left + (width - total) / 2;
41 for (size_t i = 0; i < n; ++i) {
42 int length = display_width(b[i].label);
43 int x = stacked ? left + (width - length) / 2 : col;
44 bool selected_button = i == selected;
45 bool colors =
46 !GET_OPTION(strip_ansi) && GET_OPTION(color_mode) != TERM_COLOR_NONE &&
49 if (selected_button && colors) {
50 terminal_color_mode_t mode = GET_OPTION(color_mode);
51 if (mode == TERM_COLOR_AUTO)
52 mode = (terminal_color_mode_t)log_get_color_mode();
53 if (mode == TERM_COLOR_NONE)
54 mode = TERM_COLOR_16;
56 colorscheme_current_palette(&palette, mode);
58 frame_buffer_printf(out, "\033[%d;%dH\033[0m%s",
59 row + (stacked ? (int)i : 0), x, palette.foreground);
60 if (b[i].color > LOG_COLOR_DEV && b[i].color < LOG_COLOR_RESET)
61 frame_buffer_printf(out, "%s", palette.background_codes[b[i].color]);
62 else if (mode >= TERM_COLOR_TRUECOLOR)
63 frame_buffer_printf(out, "\033[48;2;%u;%u;%um", grey, grey, grey);
64 else if (mode >= TERM_COLOR_256)
65 frame_buffer_printf(out, "\033[48;5;%um",
66 232 + (unsigned)grey * 23 / 255);
67 else
68 frame_buffer_printf(out, "\033[%dm",
69 palette.background == TERM_BACKGROUND_DARK ? 100
70 : 47);
71 frame_buffer_printf(out, "%s\033[0m", b[i].label);
72 } else {
73 const char *label = colored_string(b[i].disabled ? LOG_COLOR_GREY
74 : b[i].color ? b[i].color
76 b[i].label);
77 frame_buffer_printf(out, "\033[%d;%dH%s%s\033[0m",
78 row + (stacked ? (int)i : 0), x,
79 selected_button ? "\033[7m" : "", label);
80 }
81 col += length + 3;
82 }
83}
@ LOG_COLOR_RESET
Definition color.h:14
@ LOG_COLOR_GREY
Definition color.h:13
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_DEV
Definition color.h:7
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:71
int color_mode
Definition fft.c:26
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned int width
Definition fft.c:24
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.
unsigned char uint8_t
int log_get_color_mode(void)
Definition log/log.c:2126
#define GET_OPTION(field)
Definition options.h:1339
#define options_terminal_settings()
Definition options.h:1287
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
static bool size_t gotel_query_t * out
Definition queries.h:10
bool terminal_should_color_output(int fd, const terminal_options_t *options)
char background_codes[8][32]
terminal_background_t background

References colorscheme_palette_t::background, colorscheme_palette_t::background_codes, ui_button_t::color, color_mode, colored_string(), colors, colorscheme_current_palette(), colorscheme_highlight_background(), display_width(), ERROR_INVALID_PARAM, colorscheme_palette_t::foreground, frame_buffer_get_screen_output_fd(), frame_buffer_printf(), GET_OPTION, LOG_COLOR_DEV, LOG_COLOR_FATAL, LOG_COLOR_GREY, LOG_COLOR_RESET, log_get_color_mode(), options_terminal_settings, out, SET_ERRNO, TERM_BACKGROUND_DARK, terminal_should_color_output(), and width.

Referenced by ui_error_popup_render().