diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-27 18:15:26 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-27 18:15:26 -0600 |
| commit | 83e8fbc692b36be5ac9011ccf26b1b232d560008 (patch) | |
| tree | ab62a06c6a49b343b33d41b502487b233128ad80 /tui/tui.h | |
| parent | 4c95e6490d189b47cad050744de9768465e0307b (diff) | |
| parent | b5bb63d4760f6f6e0282fc9f7eec36b41705458c (diff) | |
| download | ztop-main.tar.gz | |
views: PS, FS and LOG, and a DEC-graphics frame
Diffstat (limited to 'tui/tui.h')
| -rw-r--r-- | tui/tui.h | 265 |
1 files changed, 159 insertions, 106 deletions
@@ -7,187 +7,226 @@ * anything that copies it), input is decoded by hand. The shape follows the * Elm architecture as popularised by Bubble Tea: an application owns a model, * receives messages (keys, resize, ticks), and draws from the model. Only - * bytes are stored, so a cell is one character wide: ASCII art, not UTF-8 box - * drawing. On z/OS the C compiler's EBCDIC literals are converted by the - * tty, which is why TUI_ESC below is 0x27 there and 0x1b everywhere else. + * bytes are stored, so a cell is one character wide: no UTF-8. The frame + * uses VT100 DEC Special Graphics when the terminal's TERM says it speaks + * them, and ASCII `+ - |` when it does not (or after tui_alloc alone). On + * z/OS the C compiler's EBCDIC literals are converted by the tty, which is + * why the escape byte is 0x27 there and 0x1b everywhere else. */ #ifndef ZTOP_TUI_H #define ZTOP_TUI_H -#include <stdbool.h> #include <stddef.h> #include <termios.h> -struct tui_cell { +/* The library is C, but a C++ program may include this header. */ +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct tui_cell { unsigned char ch; unsigned char fg; unsigned char bg; unsigned char attr; -}; +} tui_cell; -typedef struct { - unsigned char fg; /**< 0-7, 7 is white */ - unsigned char bg; /**< 0-7, 0 is black */ +typedef struct tui_style { + unsigned char + fg; /**< xterm-256 index, TUI_COLOR_DEFAULT for the tty's own */ + unsigned char + bg; /**< xterm-256 index, TUI_COLOR_DEFAULT for the tty's own */ unsigned char attr; /**< TUI_BOLD / TUI_REVERSE / TUI_DIM / TUI_UNDERLINE */ } tui_style; -typedef struct { +typedef struct tui_rect { int x, y, w, h; } tui_rect; -#define TUI_BOLD 1u -#define TUI_REVERSE 2u -#define TUI_DIM 4u -#define TUI_UNDERLINE 8u +/** Attribute bits, packed into tui_cell.attr. */ +enum tui_attr : unsigned char { + TUI_BOLD = 1, + TUI_REVERSE = 2, + TUI_DIM = 4, + TUI_UNDERLINE = 8, + /** The byte is a DEC Special Graphics letter, drawn as a line glyph. */ + TUI_ACS = 16, +}; + +/** The terminal's own foreground/background, as "SGR 39" and "SGR 49". */ +enum { TUI_COLOR_DEFAULT = 255 }; + +/** Background of the selected table row. A soft surface, because reverse + * video is the thing that made this look like a curses application. */ +enum { TUI_SEL_BG = 236 }; #define TUI_STYLE(f, b, a) \ ((tui_style){(unsigned char)(f), (unsigned char)(b), (unsigned char)(a)}) -#define TUI_PLAIN TUI_STYLE(7, 0, 0) -#define TUI_TITLE TUI_STYLE(7, 4, TUI_BOLD) -#define TUI_LABEL TUI_STYLE(6, 0, 0) -#define TUI_GOOD TUI_STYLE(2, 0, 0) -#define TUI_WARN TUI_STYLE(3, 0, 0) -#define TUI_BAD TUI_STYLE(1, 0, TUI_BOLD) -#define TUI_TAB_ON TUI_STYLE(0, 7, TUI_BOLD) -#define TUI_TAB_OFF TUI_STYLE(7, 0, TUI_DIM) -#define TUI_ACCENT TUI_STYLE(6, 0, TUI_BOLD) -#define TUI_META TUI_STYLE(7, 0, TUI_DIM) -#define TUI_KEYCAP TUI_STYLE(0, 6, 0) -#define TUI_BAR TUI_STYLE(7, 4, 0) +/* Blue, in xterm-256 indices: 25 navy, 18 a shade darker, 75 bright, 111 + * pale, 61 slate, 189 a blue-grey body. The greens and reds are kept for + * meaning: free and used, healthy and broken. */ +#define TUI_PLAIN TUI_STYLE(189, TUI_COLOR_DEFAULT, 0) +#define TUI_TITLE TUI_STYLE(252, 235, TUI_BOLD) +/* Frames are quiet: a muted grey so the data, not the box, is what the eye + * lands on. Accent stays for the focused title and the selection. */ +#define TUI_FRAME TUI_STYLE(240, TUI_COLOR_DEFAULT, 0) +#define TUI_LABEL TUI_STYLE(111, TUI_COLOR_DEFAULT, TUI_BOLD) +#define TUI_GOOD TUI_STYLE(84, TUI_COLOR_DEFAULT, 0) +#define TUI_WARN TUI_STYLE(222, TUI_COLOR_DEFAULT, 0) +#define TUI_BAD TUI_STYLE(203, TUI_COLOR_DEFAULT, TUI_BOLD) +#define TUI_TAB_ON TUI_STYLE(16, 75, TUI_BOLD) +#define TUI_TAB_OFF TUI_STYLE(110, TUI_COLOR_DEFAULT, 0) +#define TUI_ACCENT TUI_STYLE(75, TUI_COLOR_DEFAULT, TUI_BOLD) +#define TUI_META TUI_STYLE(61, TUI_COLOR_DEFAULT, 0) +#define TUI_KEYCAP TUI_STYLE(16, 111, TUI_BOLD) +#define TUI_BAR TUI_STYLE(252, 235, 0) +/* Above the byte range, so a key code never collides with a character. + * Every value is written out: the numbers are not a sequence to maintain. */ enum { TUI_KEY_NONE = -1, TUI_KEY_ESC = 256, - TUI_KEY_UP, - TUI_KEY_DOWN, - TUI_KEY_LEFT, - TUI_KEY_RIGHT, - TUI_KEY_HOME, - TUI_KEY_END, - TUI_KEY_PGUP, - TUI_KEY_PGDN, - TUI_KEY_DEL, - TUI_KEY_RESIZE, - TUI_KEY_TAB, - TUI_KEY_BACKTAB, - TUI_KEY_MOUSE, + TUI_KEY_UP = 257, + TUI_KEY_DOWN = 258, + TUI_KEY_LEFT = 259, + TUI_KEY_RIGHT = 260, + TUI_KEY_HOME = 261, + TUI_KEY_END = 262, + TUI_KEY_PGUP = 263, + TUI_KEY_PGDN = 264, + TUI_KEY_DEL = 265, + TUI_KEY_RESIZE = 266, + TUI_KEY_TAB = 267, + TUI_KEY_BACKTAB = 268, + TUI_KEY_MOUSE = 269, }; -#define TUI_MAX_W 500 -#define TUI_MAX_H 200 +enum { TUI_MAX_W = 500 }; +enum { TUI_MAX_H = 200 }; + +/* ---- layout primitives ----------------------------------------------- */ + +/** @brief Cut @p r into a top band of @p top_h rows and the rest. */ +void tui_split_v(tui_rect r, int top_h, tui_rect *top, tui_rect *bot); +/** @brief Cut @p r into a left band of @p left_w columns and the rest. */ +void tui_split_h(tui_rect r, int left_w, tui_rect *left, tui_rect *right); +/** @brief Shrink @p r by @p dx columns and @p dy rows on every side. */ +tui_rect tui_inset(tui_rect r, int dx, int dy); +/** @brief True when cell (@p x, @p y) is inside @p r. */ +bool tui_rect_hit(tui_rect r, int x, int y); /** @brief One mouse event; coordinates are 0-based cells. */ -struct tui_mouse { +typedef struct tui_mouse { int x, y; int button; /**< 0 left, 1 middle, 2 right, 64 wheel up, 65 wheel down */ bool release; -}; +} tui_mouse; -struct tui { +typedef struct tui { int w, h; int fd_in, fd_out; - struct tui_cell *cur; - struct tui_cell *old; + tui_cell *cur; + tui_cell *old; bool full; bool open; + bool glyphs; /**< terminal speaks DEC Special Graphics */ + bool acs_on; /**< G0 is currently the DEC graphics set */ struct termios orig; unsigned char in[64]; int inlen; char out[32768]; int olen; - struct tui_mouse mouse; -}; + tui_mouse mouse; +} tui; -bool tui_alloc(struct tui *t, int w, int h); -bool tui_open(struct tui *t); -void tui_close(struct tui *t); +bool tui_alloc(tui *t, int w, int h); +bool tui_open(tui *t); +void tui_close(tui *t); /** @brief Re-query the size; returns true when it changed and the buffers * moved. */ -bool tui_resize(struct tui *t); -void tui_size(struct tui *t, int *w, int *h); +bool tui_resize(tui *t); +void tui_size(tui *t, int *w, int *h); -void tui_clear(struct tui *t); -void tui_fill(struct tui *t, int x, int y, int w, int h, char ch, tui_style st); -void tui_put(struct tui *t, int x, int y, const char *s, int maxw, - tui_style st); -void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st, - const char *fmt, ...); -void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st); +void tui_clear(tui *t); +void tui_fill(tui *t, int x, int y, int w, int h, char ch, tui_style st); +void tui_put(tui *t, int x, int y, const char *s, int maxw, tui_style st); +void tui_printf(tui *t, int x, int y, int maxw, tui_style st, const char *fmt, + ...); +void tui_box(tui *t, int x, int y, int w, int h, tui_style st); /** @brief A box with a title set into its top border. */ -void tui_box_frame(struct tui *t, int x, int y, int w, int h, tui_style st, +void tui_box_frame(tui *t, int x, int y, int w, int h, tui_style st, const char *title, tui_style title_st); -void tui_rule(struct tui *t, int y, char ch, tui_style st); -void tui_flush(struct tui *t); +void tui_rule(tui *t, int y, char ch, tui_style st); +void tui_flush(tui *t); /** @brief Wait up to @p timeout_ms (-1 = forever) for a key. */ -int tui_key(struct tui *t, int timeout_ms); +int tui_key(tui *t, int timeout_ms); /* ---- messages and the application loop ------------------------------- */ enum tui_msg_type { TUI_MSG_KEY, TUI_MSG_RESIZE, TUI_MSG_TICK, TUI_MSG_MOUSE }; -struct tui_msg { +typedef struct tui_msg { enum tui_msg_type type; - int key; /**< TUI_MSG_KEY: a byte or one of TUI_KEY_* */ - int w, h; /**< TUI_MSG_RESIZE: the new size */ - struct tui_mouse mouse; /**< TUI_MSG_MOUSE */ -}; + int key; /**< TUI_MSG_KEY: a byte or one of TUI_KEY_* */ + int w, h; /**< TUI_MSG_RESIZE: the new size */ + tui_mouse mouse; /**< TUI_MSG_MOUSE */ +} tui_msg; -struct tui_app { +typedef struct tui_app { void *model; /** Handle a message. Returns true when the screen must be redrawn. * Set @c quit to leave the loop. */ - bool (*update)(struct tui_app *app, const struct tui_msg *m); + bool (*update)(struct tui_app *app, const tui_msg *m); /** Draw the model into the buffer. */ - void (*view)(void *model, struct tui *t); + void (*view)(void *model, tui *t); int tick_ms; /**< 0 = no tick messages */ bool quit; -}; +} tui_app; /** @brief Run until the app sets @c quit (or the terminal goes away). */ -void tui_run(struct tui *t, struct tui_app *app); +void tui_run(tui *t, tui_app *app); /* ---- widgets --------------------------------------------------------- */ /** One scroll window: selected row, first visible row. */ -struct tui_viewport { +typedef struct tui_viewport { int sel; int top; -}; +} tui_viewport; -void tui_viewport_clamp(struct tui_viewport *v, int rows, int height); -void tui_viewport_move(struct tui_viewport *v, int delta, int rows, int height); -bool tui_viewport_key(struct tui_viewport *v, int key, int rows, int height); +void tui_viewport_clamp(tui_viewport *v, int rows, int height); +void tui_viewport_move(tui_viewport *v, int delta, int rows, int height); +bool tui_viewport_key(tui_viewport *v, int key, int rows, int height); /** A column of a table. Width counts cells; right aligns numbers. */ -struct tui_col { +typedef struct tui_col { const char *title; int width; bool right; -}; +} tui_col; -struct tui_table { - const struct tui_col *cols; +typedef struct tui_table { + const tui_col *cols; int ncols; - struct tui_viewport vp; -}; + tui_viewport vp; +} tui_table; /** Render one cell into @p buf; the buffer is TUI_CELL_MAX bytes. */ -#define TUI_CELL_MAX 128 +enum { TUI_CELL_MAX = 128 }; typedef void (*tui_cell_fn)(void *ctx, int row, int col, char *buf, size_t len); typedef tui_style (*tui_rowstyle_fn)(void *ctx, int row); /** Draw the header and the visible rows inside @p r. @p rows may be 0. */ -void tui_table_draw(struct tui *t, tui_rect r, const struct tui_table *tb, - int rows, tui_cell_fn cell, tui_rowstyle_fn rowstyle, - void *ctx); +void tui_table_draw(tui *t, tui_rect r, const tui_table *tb, int rows, + tui_cell_fn cell, tui_rowstyle_fn rowstyle, void *ctx); /** Apply a key to the viewport; true when it was consumed. */ -bool tui_table_key(struct tui_table *tb, int key, int rows, int height); +bool tui_table_key(tui_table *tb, int key, int rows, int height); /** A row of tabs; returns the x just past the last one. */ -int tui_tabs_draw(struct tui *t, int x, int y, const char *const *labels, int n, +int tui_tabs_draw(tui *t, int x, int y, const char *const *labels, int n, int active); /** @brief Which tab covers cell @p x, or -1. Tabs start at column 0. */ @@ -196,33 +235,47 @@ int tui_tabs_hit(int x, const char *const *labels, int n); /** Cycle tabs with left/right/tab/backtab; true when it was consumed. */ bool tui_tabs_key(int key, int *active, int n); -/** @brief A key cap and its label, for the status bar. */ -struct tui_hint { - const char *key; - const char *label; -}; +/** @brief One key binding, for the footer and the help overlay. */ +typedef struct tui_binding { + const char *key; /**< key cap text, e.g. "TAB", "s S", "/" */ + const char *label; /**< short description */ + const char *group; /**< section title in the help overlay */ + unsigned tabs; /**< mask of tabs (1u << tab) where it applies */ + bool in_footer; /**< show in the bottom status bar */ +} tui_binding; -/** @brief Draw key caps and their labels along one row. */ -void tui_hints(struct tui *t, int x, int y, int maxw, - const struct tui_hint *hints, int n); +/** @brief Draw the footer hints of @p km that apply to @p tab. */ +void tui_keymap_footer(tui *t, int x, int y, int maxw, const tui_binding *km, + int n, int tab); + +/** @brief Draw the grouped help overlay from @p km. */ +void tui_keymap_help(tui *t, const tui_binding *km, int n, const char *title); + +/** @brief A sparkline of @p n samples, each 0-100, on row @p y. */ +enum { TUI_SPARK_LEN = 9 }; /* " .:-=+*#%" */ +void tui_sparkline(tui *t, int x, int y, const unsigned char *vals, int n, + tui_style st); /** A single-line text input. */ -struct tui_input { +typedef struct tui_input { char buf[256]; int len; int pos; bool focus; -}; +} tui_input; -void tui_input_draw(struct tui *t, tui_rect r, const struct tui_input *in, - tui_style st); +void tui_input_draw(tui *t, tui_rect r, const tui_input *in, tui_style st); /** Insert, erase, move, home/end; true when the key was consumed. */ -bool tui_input_key(struct tui_input *in, int key); +bool tui_input_key(tui_input *in, int key); /** A percentage bar, 0-100 or -1 for "no total". */ -void tui_progress(struct tui *t, int x, int y, int width, int pct); +void tui_progress(tui *t, int x, int y, int width, int pct); /** Fill the bottom row with a help line, left and right halves. */ -void tui_statusbar(struct tui *t, const char *left, const char *right); +void tui_statusbar(tui *t, const char *left, const char *right); + +#ifdef __cplusplus +} +#endif #endif |
