skip to content
==> include/mesh/top_ui_kit.h <==
1/* @ajax/mesh-top-ui-kit: what every mesh-top pane shares (crates/mesh-top src/ui.rs's palette and
2 * helpers): the dark palette, label/value spans, the rounded panel, machine titles, the number
3 * formats (Rust's, through rsfmt) and the "no snapshot" / "not loaded" fallbacks. A frame is an
4 * arena (every string, span and line of one draw) and the buffer it draws into. */
5#ifndef MESH_TOP_UI_KIT_H
6#define MESH_TOP_UI_KIT_H
7
8#include "mesh/top_app.h"
9#include "rsfmt.h"
10#include "tui/block.h"
11#include "tui/layout.h"
12#include "tui/paragraph.h"
13#include "tui/table.h"
14
15#include <stdbool.h>
16#include <stddef.h>
17#include <stdint.h>
18
19typedef struct {
20    tui_arena *a;
21    tui_buffer *buf;
22} mt_frame;
23
24/* ---- palette */
25#define MT_BG TUI_RGB(12, 14, 20)
26#define MT_FG TUI_RGB(205, 210, 220)
27#define MT_DIM TUI_RGB(105, 110, 128)
28#define MT_FAINT TUI_RGB(45, 48, 60)
29#define MT_ACCENT TUI_RGB(110, 190, 255)
30#define MT_GOOD TUI_RGB(90, 220, 120)
31#define MT_WARN TUI_RGB(240, 200, 70)
32#define MT_BAD TUI_RGB(240, 70, 70)
33/* One colour per system (charts, doom's part markers). */
34extern const tui_color MT_MACHINE[8];
35tui_color mt_sys_color(uint32_t k);
36/* Doom wall kinds 1..7. */
37extern const uint8_t MT_WALL_RGB[7][3];
38/* The four original programs keep their colours; others take one by slot. */
39void mt_prog_rgb(const char *name, uint32_t slot, uint8_t out[3]);
40tui_color mt_rgb(const uint8_t c[3], float k);
41tui_color mt_lerp(const uint8_t a[3], const uint8_t b[3], float t);
42tui_color mt_prog_color(const char *name, uint32_t slot, float k);
43
44/* ---- Rust's saturating float casts (`as u8` etc.: NaN is 0) */
45uint8_t mt_sat_u8(float v);
46uint16_t mt_sat_u16(double v);
47uint32_t mt_sat_u32(double v);
48uint64_t mt_sat_u64(double v);
49int32_t mt_sat_i32(double v);
50
51/* ---- text in the frame's arena */
52#define MT_FMT(f, ...) tui_arena_fmt((f)->a, __VA_ARGS__)
53/* A line of spans: MT_LINE(f, span, span, ...). */
54#define MT_LINE(f, ...) \
55    tui_line_of((f)->a, sizeof((tui_span[]){__VA_ARGS__}) / sizeof(tui_span), (tui_span[]){__VA_ARGS__})
56/* {:.prec} of v */
57const char *mt_ff(mt_frame *f, double v, int prec);
58/* {:+.prec} */
59const char *mt_ffp(mt_frame *f, double v, int prec);
60/* {:.prec e} */
61const char *mt_fe(mt_frame *f, double v, int prec);
62/* {:<w} '<', {:>w} '>', {:^w} '^' of a string, by characters */
63const char *mt_pad(mt_frame *f, const char *s, int w, char align);
64/* The first n characters (Rust's s.chars().take(n)). */
65const char *mt_take(mt_frame *f, const char *s, size_t n);
66const char *mt_name(mt_frame *f, const mt_machine *m);
67
68static inline tui_span mt_label(const char *s) { return tui_span_styled(s, tui_fg(MT_DIM)); }
69static inline tui_span mt_val(const char *s) { return tui_span_styled(s, tui_fg_bold(MT_FG)); }
70static inline tui_span mt_styled(const char *s, tui_style st) { return tui_span_styled(s, st); }
71static inline tui_span mt_fgs(const char *s, tui_color c) { return tui_span_styled(s, tui_fg(c)); }
72static inline tui_span mt_fgb(const char *s, tui_color c) { return tui_span_styled(s, tui_fg_bold(c)); }
73static inline tui_style mt_fg_bg_bold(tui_color fg, tui_color bg) { return tui_style_bold(tui_fg_bg(fg, bg)); }
74
75/* A table cell: from a string (Text::from), a span, a line; with a cell style. */
76tui_table_cell mt_cell_str(mt_frame *f, const char *s);
77tui_table_cell mt_cell_span(mt_frame *f, tui_span s);
78tui_table_cell mt_cell_line(mt_frame *f, tui_line l);
79tui_table_cell mt_cell_styled(mt_frame *f, const char *s, tui_style st); /* Cell::from(s).style(st) */
80/* A row from cells; a header row of DIM labels. */
81tui_table_row mt_row(mt_frame *f, const tui_table_cell *cells, size_t n);
82tui_table_row mt_header(mt_frame *f, const char *const *names, size_t n);
83/* A growable row list in the arena. */
84typedef struct {
85    tui_table_row *rows;
86    size_t n, cap;
87} mt_rows;
88void mt_rows_push(mt_frame *f, mt_rows *r, tui_table_row row);
89/* A growable cell list in the arena. */
90typedef struct {
91    tui_table_cell *c;
92    size_t n, cap;
93} mt_cells;
94void mt_cells_push(mt_frame *f, mt_cells *c, tui_table_cell cell);
95/* A growable line list. */
96typedef struct {
97    tui_line *l;
98    size_t n, cap;
99} mt_lines;
100void mt_lines_push(mt_frame *f, mt_lines *l, tui_line line);
101tui_text mt_text(mt_frame *f, const mt_lines *l);
102/* Spans gathered into a line. */
103typedef struct {
104    tui_line l;
105} mt_spans;
106
107/* The rounded panel every pane uses. */
108tui_block mt_panel(mt_frame *f, tui_line title, tui_color color);
109/* Renders a widget-less paragraph of lines / one line / one span into area. */
110void mt_par(mt_frame *f, tui_text t, tui_rect area);
111void mt_par_line(mt_frame *f, tui_line l, tui_rect area);
112void mt_block(mt_frame *f, const tui_block *b, tui_rect area);
113void mt_table(mt_frame *f, const tui_table *t, tui_rect area);
114
115/* ---- formats */
116const char *mt_fmt_uptime(mt_frame *f, uint64_t ms);
117const char *mt_fmt_rate(mt_frame *f, float r);
118const char *mt_fmt_us(mt_frame *f, float us);
119const char *mt_fmt_bytes(mt_frame *f, uint64_t b);
120const char *mt_pct(mt_frame *f, float x);
121/* Systems named in a link mask: "1,2". */
122const char *mt_mask_list(mt_frame *f, uint32_t mask);
123const char *mt_short_gpu(mt_frame *f, const char *g);
124
125/* ---- styles */
126tui_style mt_state_style(const mt_section *s);
127tui_style mt_share_style(float x);
128tui_style mt_busy_style(float x);
129tui_span mt_share_span(mt_frame *f, mt_opt_f32 s);
130tui_span mt_peer_span(mt_frame *f, const mt_snapshot *s);
131
132/* A machine's panel title, with focus and OFFLINE marks; *color is the border's. */
133tui_line mt_machine_title(mt_frame *f, const mt_machine *m, bool focused, const char *extra, tui_color *color);
134void mt_no_data(mt_frame *f, tui_rect area, const mt_machine *m);
135/* A custom tab whose program is absent from the machine's table. */
136void mt_not_loaded(mt_frame *f, tui_rect area, const mt_machine *m, const char *name);
137/* The shared head of a custom tab: its program on the machine, or NULL after drawing why not. */
138const mt_section *mt_bound(mt_frame *f, tui_rect inner, const mt_machine *m, const char *name);
139
140/* The member this machine waits for most per exchange round (false when none). */
141bool mt_slowest_wait(const mt_snapshot *s, uint32_t *k, float *us);
142/* The section's name in the arena. */
143const char *mt_sec_name(mt_frame *f, const mt_section *p);
144/* Rect helpers */
145static inline tui_rect mt_rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h) { return (tui_rect){x, y, w, h}; }
146
147#endif