ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/tui/stats.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define TUI_STATS_TITLE_BUFFER_SIZE 160
4#define TUI_STATS_FOOTER_BUFFER_SIZE 160
5#define TUI_STATS_TEXT_BUFFER_SIZE 4096
6
7#include <ascii-chat/common.h>
11#include <ascii-chat/otel.h>
18#include <ascii-chat/ui/table.h>
19#include <ascii-chat/ui/text.h>
22#include <ctype.h>
23#include <libplatform/atomic.h>
24#include <libplatform/compiler.h>
25#include <libplatform/system.h>
26#include <libplatform/terminal.h>
27#include <libplatform/thread.h>
28#include <math.h>
29#include <options/options.h>
30#include <stdarg.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
36static size_t stats_body_width(const stats_view_t *view) {
37 size_t width = 0;
38 for (unsigned i = 0; i < view->count; ++i) {
39 size_t length = strlen(view->lines[i]);
40 if (length > width)
41 width = length;
42 }
43 return width;
44}
45
46static void stats_footer(const stats_view_t *view, char *footer,
47 size_t capacity) {
48 snprintf(footer, capacity,
49 "= / Esc close | Tab page | Up/Down select | %u rows", view->count);
50}
51
52terminal_size_t tui_stats_minimum(const stats_view_t *view) {
54 snprintf(title, sizeof(title), " ASCII-CHAT / %-20s LIVE STATS",
55 view->mode);
56 stats_footer(view, footer, sizeof(footer));
57 size_t width = stats_body_width(view) + 4;
58 if (strlen(title) + 1 > width)
59 width = strlen(title) + 1;
60 if (strlen(footer) + 3 > width)
61 width = strlen(footer) + 3;
62 // Both subtitles are shorter than the title. Keep one spare row and column
63 // to avoid terminal auto-wrap/scroll, in addition to the fixed header/footer.
64 return (terminal_size_t){.cols = (int)width, .rows = 10};
65}
66
77static void stats_cells(size_t row, const char **cells, size_t count,
78 void *data) {
79 (void)count;
80 stats_table_context_t *context = data;
81 if (context->view->headings[row])
82 snprintf(context->text, sizeof(context->text), "%s",
83 colored_string(LOG_COLOR_DEBUG, context->view->lines[row]));
84 else
85 ui_text_values_style(context->text, sizeof(context->text),
86 context->view->lines[row], LOG_COLOR_FATAL);
87 cells[0] = context->text;
88}
89
90void tui_stats_render(terminal_size_t size, const void *data) {
91 const stats_view_t *view = data;
92 frame_buffer_t *buffer = frame_buffer_create(size.rows, size.cols + 64);
93 if (!buffer)
94 return;
95 terminal_capabilities_t caps =
97 terminal_color_mode_t mode = GET_OPTION(color_mode);
98 bool color = mode != TERM_COLOR_NONE &&
100 !GET_OPTION(strip_ansi) &&
101 (mode != TERM_COLOR_AUTO || caps.color_level != TERM_COLOR_NONE);
102 const char *blue = color ? ui_text_color_prefix(LOG_COLOR_DEBUG) : "";
105 buffer, "\033[1m%s ASCII-CHAT / %-20s LIVE STATS\033[0m\033[K\n", blue,
106 view->mode);
107 frame_buffer_printf(buffer, " ");
108 frame_buffer_printf(buffer, "%s", blue);
109 ui_text_render_values(buffer, view->page ? "CONNECTIONS & MODE DETAILS"
110 : "OVERVIEW / rates over ~1 second");
111 frame_buffer_printf(buffer, "\033[K\n");
112 frame_buffer_render_border(buffer, size.cols, blue);
113 ui_layout_t layout;
114 if (ui_layout_measure(size, 3, 1, 2, &layout) != ASCIICHAT_OK) {
115 frame_buffer_destroy(buffer);
116 return;
117 }
118 int available = layout.body_rows > 0 ? layout.body_rows : 1;
119 const ui_table_column_t column = {.heading = "Statistics",
120 .width = size.cols - 4};
121 stats_table_context_t context = {.view = view};
122 ui_table_t table = {
123 .columns = &column,
124 .column_count = 1,
125 .row_count = view->count,
126 .viewport = {.selected = view->selected, .first_visible = view->offset},
127 .cells = stats_cells,
128 .data = &context,
129 .selectable = true};
130 ui_table_render(buffer, &table, 4, 2, size.cols - 2, available);
131 frame_buffer_printf(buffer, "\033[%d;1H", size.rows - 2);
132 frame_buffer_render_border(buffer, size.cols, blue);
133 char footer[TUI_STATS_FOOTER_BUFFER_SIZE];
134 stats_footer(view, footer, sizeof(footer));
135 frame_buffer_printf(buffer, " ");
136 ui_text_render_values(buffer, footer);
137 frame_buffer_printf(buffer, "\033[K");
141 frame_buffer_destroy(buffer);
142}
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_DEBUG
Definition color.h:8
int color_mode
Definition fft.c:26
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int width
Definition fft.c:24
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).
Buffered terminal frame rendering.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
@ ASCIICHAT_OK
Definition error_codes.h:50
#define GET_OPTION(field)
Definition options.h:1339
#define options_terminal_settings()
Definition options.h:1287
@ COLOR_SETTING_FALSE
Force colors OFF: disable all colors and logging colors.
Definition options.h:250
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
void tui_stats_render(terminal_size_t size, const void *data)
#define TUI_STATS_FOOTER_BUFFER_SIZE
#define TUI_STATS_TEXT_BUFFER_SIZE
terminal_size_t tui_stats_minimum(const stats_view_t *view)
#define TUI_STATS_TITLE_BUFFER_SIZE
🔢 Mathematical Utility Functions
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Interactive search filtering for terminal screens.
Shutdown check system for clean library/application separation.
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)
🔤 String Manipulation and Shell Escaping Utilities
const stats_view_t * view
char mode[32]
bool headings[128]
unsigned page
char lines[128][160]
unsigned selected
unsigned offset
unsigned count
int body_rows
Definition ui/layout.h:9
The useful part of ui table column is in heading, width, alignment, ellipsis: together they describe ...
Definition table.h:35
void ui_table_render(frame_buffer_t *out, ui_table_t *t, int row, int col, int width, int height)
Definition table.c:81
Reusable "fixed header + scrolling logs" terminal screen abstraction.
const char * ui_text_color_prefix(log_color_t color)
Definition text.c:18
void ui_text_values_style(char *out, size_t cap, const char *text, log_color_t zero_color)
Definition text.c:101
void ui_text_render_values(frame_buffer_t *out, const char *text)
Definition text.c:158
⏱️ High-precision timing utilities using sokol_time.h and uthash
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