ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
stats.c File Reference

Go to the source code of this file.

Data Structures

struct  stats_table_context_t

Macros

#define TUI_STATS_TITLE_BUFFER_SIZE   160
#define TUI_STATS_FOOTER_BUFFER_SIZE   160
#define TUI_STATS_TEXT_BUFFER_SIZE   4096

Functions

terminal_size_t tui_stats_minimum (const stats_view_t *view)
void tui_stats_render (terminal_size_t size, const void *data)

Macro Definition Documentation

◆ TUI_STATS_FOOTER_BUFFER_SIZE

#define TUI_STATS_FOOTER_BUFFER_SIZE   160

Definition at line 4 of file lib/tui/stats.c.

Referenced by tui_stats_minimum(), and tui_stats_render().

◆ TUI_STATS_TEXT_BUFFER_SIZE

#define TUI_STATS_TEXT_BUFFER_SIZE   4096

Definition at line 5 of file lib/tui/stats.c.

◆ TUI_STATS_TITLE_BUFFER_SIZE

#define TUI_STATS_TITLE_BUFFER_SIZE   160

Definition at line 3 of file lib/tui/stats.c.

Referenced by tui_stats_minimum().

Function Documentation

◆ tui_stats_minimum()

terminal_size_t tui_stats_minimum ( const stats_view_t * view)

Definition at line 50 of file lib/tui/stats.c.

50 {
52 snprintf(title, sizeof(title), " ASCII-CHAT / %-20s LIVE STATS", view->mode);
53 stats_footer(view, footer, sizeof(footer));
54 size_t width = stats_body_width(view) + 4;
55 if (strlen(title) + 1 > width)
56 width = strlen(title) + 1;
57 if (strlen(footer) + 3 > width)
58 width = strlen(footer) + 3;
59 // Both subtitles are shorter than the title. Keep one spare row and column
60 // to avoid terminal auto-wrap/scroll, in addition to the fixed header/footer.
61 return (terminal_size_t){.cols = (int)width, .rows = 10};
62}
unsigned int width
Definition fft.c:23
#define TUI_STATS_FOOTER_BUFFER_SIZE
#define TUI_STATS_TITLE_BUFFER_SIZE
char mode[32]

References stats_view_t::mode, TUI_STATS_FOOTER_BUFFER_SIZE, TUI_STATS_TITLE_BUFFER_SIZE, and width.

◆ tui_stats_render()

void tui_stats_render ( terminal_size_t size,
const void * data )

Definition at line 83 of file lib/tui/stats.c.

83 {
84 const stats_view_t *view = data;
85 frame_buffer_t *buffer = frame_buffer_create(size.rows, size.cols + 64);
86 if (!buffer)
87 return;
88 terminal_capabilities_t caps = detect_terminal_capabilities(options_terminal_settings());
89 terminal_color_mode_t mode = GET_OPTION(color_mode);
90 bool color = mode != TERM_COLOR_NONE && GET_OPTION(color) != COLOR_SETTING_FALSE && !GET_OPTION(strip_ansi) &&
91 (mode != TERM_COLOR_AUTO || caps.color_level != TERM_COLOR_NONE);
92 const char *blue = color ? ui_text_color_prefix(LOG_COLOR_DEBUG) : "";
94 frame_buffer_printf(buffer, "\033[1m%s ASCII-CHAT / %-20s LIVE STATS\033[0m\033[K\n", blue, view->mode);
95 frame_buffer_printf(buffer, " ");
96 frame_buffer_printf(buffer, "%s", blue);
97 ui_text_render_values(buffer, view->page ? "CONNECTIONS & MODE DETAILS" : "OVERVIEW / rates over ~1 second");
98 frame_buffer_printf(buffer, "\033[K\n");
99 frame_buffer_render_border(buffer, size.cols, blue);
100 ui_layout_t layout;
101 if (ui_layout_measure(size, 3, 1, 2, &layout) != ASCIICHAT_OK) {
102 frame_buffer_destroy(buffer);
103 return;
104 }
105 int available = layout.body_rows > 0 ? layout.body_rows : 1;
106 const ui_table_column_t column = {.heading = "Statistics", .width = size.cols - 4};
107 stats_table_context_t context = {.view = view};
108 ui_table_t table = {.columns = &column,
109 .column_count = 1,
110 .row_count = view->count,
111 .viewport = {.selected = view->selected, .first_visible = view->offset},
112 .cells = stats_cells,
113 .data = &context,
114 .selectable = true};
115 ui_table_render(buffer, &table, 4, 2, size.cols - 2, available);
116 frame_buffer_printf(buffer, "\033[%d;1H", size.rows - 2);
117 frame_buffer_render_border(buffer, size.cols, blue);
118 char footer[TUI_STATS_FOOTER_BUFFER_SIZE];
119 stats_footer(view, footer, sizeof(footer));
120 frame_buffer_printf(buffer, " ");
121 ui_text_render_values(buffer, footer);
122 frame_buffer_printf(buffer, "\033[K");
124 frame_buffer_destroy(buffer);
125}
int color_mode
Definition fft.c:25
void frame_buffer_cursor_home(frame_buffer_t *buf)
Append cursor home escape code (\033[H).
void frame_buffer_destroy(frame_buffer_t *buf)
Destroy a frame buffer.
size_t frame_buffer_get_length(const frame_buffer_t *buf)
Get the current content length of the frame buffer.
const char * frame_buffer_get_content(const frame_buffer_t *buf)
Get a pointer to the buffer content.
frame_buffer_t * frame_buffer_create(int rows, int cols)
Create a new frame buffer.
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
void frame_buffer_render_border(frame_buffer_t *buf, int cols, const char *color)
Render a horizontal border line (used by both splash and status screens).
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
@ ASCIICHAT_OK
Definition error_codes.h:46
@ LOG_COLOR_DEBUG
Definition log/log.h:149
#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
@ COLOR_SETTING_FALSE
Force colors OFF: disable all colors and logging colors.
Definition options.h:249
unsigned page
unsigned selected
unsigned offset
unsigned count
int body_rows
Definition ui/layout.h:5
The useful part of ui table column is in heading, width, alignment, ellipsis: together they describe ...
Definition table.h:22
void ui_table_render(frame_buffer_t *out, ui_table_t *t, int row, int col, int width, int height)
Definition table.c:76
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
void ui_text_render_values(frame_buffer_t *out, const char *text)
Definition text.c:146
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
int ui_controller_active_fd(void)
asciichat_error_t ui_layout_measure(terminal_size_t size, int header, int detail, int footer, ui_layout_t *out)
Definition ui/layout.c:3

References ASCIICHAT_OK, ui_layout_t::body_rows, color_mode, COLOR_SETTING_FALSE, stats_view_t::count, frame_buffer_create(), frame_buffer_cursor_home(), frame_buffer_destroy(), frame_buffer_get_content(), frame_buffer_get_length(), frame_buffer_printf(), frame_buffer_render_border(), GET_OPTION, LOG_COLOR_DEBUG, stats_view_t::mode, stats_view_t::offset, options_terminal_settings, stats_view_t::page, stats_view_t::selected, TUI_STATS_FOOTER_BUFFER_SIZE, ui_controller_active_fd(), ui_controller_write(), ui_layout_measure(), ui_table_render(), ui_text_color_prefix(), and ui_text_render_values().