summaryrefslogtreecommitdiffhomepage
path: root/tui
diff options
context:
space:
mode:
Diffstat (limited to 'tui')
-rw-r--r--tui/Makefile2
-rw-r--r--tui/demo.c2
-rw-r--r--tui/test_tui.c149
-rw-r--r--tui/tui.c448
-rw-r--r--tui/tui.h265
5 files changed, 631 insertions, 235 deletions
diff --git a/tui/Makefile b/tui/Makefile
index 7304500..89479e3 100644
--- a/tui/Makefile
+++ b/tui/Makefile
@@ -14,7 +14,7 @@
.SUFFIXES:
CC = cc
-CFLAGS = -std=c17 -O2 -Wall -Wextra -Wshadow -Wpedantic \
+CFLAGS = -std=c23 -O2 -Wall -Wextra -Wshadow -Wpedantic \
-D_XOPEN_SOURCE=500 -D_POSIX_C_SOURCE=200112L
AR = ar
diff --git a/tui/demo.c b/tui/demo.c
index 6e2e081..c8cf45c 100644
--- a/tui/demo.c
+++ b/tui/demo.c
@@ -6,7 +6,7 @@
#include "tui.h"
int main(void) {
- struct tui t;
+ tui t;
int x = 4;
if (!tui_open(&t)) {
diff --git a/tui/test_tui.c b/tui/test_tui.c
index 63a5e86..0a4950b 100644
--- a/tui/test_tui.c
+++ b/tui/test_tui.c
@@ -1,8 +1,12 @@
-/* Buffer-level tests for libtui: no terminal, no clock, no signals.
+/* Buffer-level tests for libtui: no terminal, no signals. The tick test
+ * uses a pipe and a real clock, because that is where the bug was.
* `make test` builds and runs this next to the library. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/select.h>
+#include <sys/wait.h>
+#include <unistd.h>
#include "tui.h"
@@ -16,12 +20,12 @@ static int fails;
} \
} while (0)
-static unsigned char cell(const struct tui *t, int x, int y) {
+static unsigned char cell(const tui *t, int x, int y) {
return t->cur[(size_t)y * (size_t)t->w + (size_t)x].ch;
}
static void test_buffer(void) {
- struct tui t;
+ tui t;
memset(&t, 0, sizeof t);
CHECK(tui_alloc(&t, 20, 6));
@@ -55,7 +59,7 @@ static void test_buffer(void) {
}
static void test_viewport(void) {
- struct tui_viewport v = {0, 0};
+ tui_viewport v = {0, 0};
tui_viewport_clamp(&v, 10, 3);
CHECK(v.sel == 0 && v.top == 0);
@@ -85,23 +89,26 @@ static void test_tabs(void) {
CHECK(act == 2);
CHECK(!tui_tabs_key('x', &act, 3));
- struct tui t;
+ tui t;
memset(&t, 0, sizeof t);
CHECK(tui_alloc(&t, 30, 2));
static const char *const labels[3] = {"one", "two", "three"};
int x = tui_tabs_draw(&t, 0, 0, labels, 3, 1);
- CHECK(x == 5 + 1 + 5 + 1 + 7);
+ CHECK(x == 5 + 2 + 5 + 2 + 7);
CHECK(cell(&t, 0, 0) == ' ');
CHECK(cell(&t, 1, 0) == 'o');
CHECK(cell(&t, 6, 0) == ' ');
- CHECK(cell(&t, 7, 0) == 't');
+ CHECK(cell(&t, 7, 0) == ' ');
+ CHECK(cell(&t, 8, 0) == 't');
+ CHECK(tui_tabs_hit(8, labels, 3) == 1);
+ CHECK(tui_tabs_hit(6, labels, 3) == -1);
free(t.cur);
free(t.old);
}
static void test_input(void) {
- struct tui_input in;
+ tui_input in;
memset(&in, 0, sizeof in);
in.focus = true;
@@ -129,33 +136,149 @@ static void called(void *ctx, int row, int col, char *buf, size_t len) {
}
static void test_table(void) {
- static const struct tui_col cols[2] = {
+ static const tui_col cols[2] = {
{"A", 5, false},
{"B", 4, true},
};
- struct tui_table tb = {cols, 2, {0, 0}};
- struct tui t;
+ tui_table tb = {cols, 2, {0, 0}};
+ tui t;
memset(&t, 0, sizeof t);
CHECK(tui_alloc(&t, 20, 5));
- tui_table_draw(&t, (tui_rect){0, 0, 20, 5}, &tb, 3, called, NULL, NULL);
+ tui_table_draw(&t, (tui_rect){0, 0, 20, 5}, &tb, 3, called, nullptr,
+ nullptr);
CHECK(cell(&t, 0, 0) == 'A');
CHECK(cell(&t, 6, 0) == ' ');
CHECK(cell(&t, 6, 1) == 'r'); /* first data row, first column */
CHECK(cell(&t, 8, 1) == 'c');
tb.vp.sel = 2;
- tui_table_draw(&t, (tui_rect){0, 0, 20, 5}, &tb, 3, called, NULL, NULL);
+ tui_table_draw(&t, (tui_rect){0, 0, 20, 5}, &tb, 3, called, nullptr,
+ nullptr);
CHECK(cell(&t, 6, 3) == 'r'); /* third row is last visible */
free(t.cur);
free(t.old);
}
+/* Layout, sparkline and keymap: geometry is computed once and shared. */
+static void test_widgets(void) {
+ tui t;
+ tui_rect a, b, r = {0, 0, 80, 24};
+ static const unsigned char samples[] = {0, 50, 100};
+ static const tui_binding km[] = {
+ {"q", "quit", "screen", 0xFu, true},
+ };
+
+ tui_split_v(r, 3, &a, &b);
+ CHECK(a.y == 0 && a.h == 3 && b.y == 3 && b.h == 21);
+ tui_split_h(b, 50, &a, &b);
+ CHECK(a.x == 0 && a.w == 50 && b.x == 50 && b.w == 30);
+ a = tui_inset(r, 2, 1);
+ CHECK(a.x == 2 && a.y == 1 && a.w == 76 && a.h == 22);
+ CHECK(tui_rect_hit(a, 2, 1));
+ CHECK(!tui_rect_hit(a, 1, 1));
+ CHECK(!tui_rect_hit(a, 78, 1));
+
+ memset(&t, 0, sizeof t);
+ CHECK(tui_alloc(&t, 40, 6));
+ tui_clear(&t);
+ tui_sparkline(&t, 0, 0, samples, 3, TUI_ACCENT);
+ CHECK(cell(&t, 0, 0) == ' ');
+ CHECK(cell(&t, 1, 0) == '=');
+ CHECK(cell(&t, 2, 0) == '%');
+ tui_keymap_footer(&t, 0, 1, 40, km, 1, 0);
+ CHECK(cell(&t, 1, 1) == 'q');
+ free(t.cur);
+ free(t.old);
+}
+
+typedef struct tick_state {
+ int ticks;
+ int keys;
+} tick_state;
+
+static void nothing(void *model, tui *t) {
+ (void)model, (void)t;
+}
+
+static bool tick_update(tui_app *app, const tui_msg *m) {
+ tick_state *s = app->model;
+
+ if (m->type == TUI_MSG_TICK) {
+ s->ticks++;
+ app->quit = s->ticks >= 2;
+ return false;
+ }
+ if (m->type == TUI_MSG_KEY) {
+ s->keys++;
+ if (s->keys >= 100) /* the bug: keys all the way and not a tick */
+ app->quit = true;
+ }
+ return false;
+}
+
+/* The interval is a deadline, not a wait restarted by every key. A child
+ * holds a key down at 5 ms intervals; with the old code the 50 ms select
+ * never expired and no tick arrived. */
+static void test_tick(void) {
+ tui t;
+ tui_app app;
+ tick_state s;
+ int p[2];
+ pid_t pid;
+
+ memset(&t, 0, sizeof t);
+ memset(&app, 0, sizeof app);
+ memset(&s, 0, sizeof s);
+ CHECK(pipe(p) == 0);
+ CHECK(tui_alloc(&t, 20, 6));
+ t.fd_in = p[0];
+ t.fd_out = -1; /* nothing redraws, so nothing is flushed */
+ app.model = &s;
+ app.update = tick_update;
+ app.view = nothing;
+ app.tick_ms = 50;
+
+ pid = fork();
+ CHECK(pid >= 0);
+ if (pid == 0) {
+ int idle[2];
+
+ (void)close(p[0]);
+ if (pipe(idle) != 0)
+ _exit(1);
+ for (int i = 0; i < 200; i++) {
+ /* select is the sleep this build has: nanosleep is not
+ * declared on z/OS, and nfds 0 with no set returns at once. */
+ fd_set f;
+ struct timeval gap = {0, 5000};
+
+ if (write(p[1], "j", 1) != 1)
+ break;
+ FD_ZERO(&f);
+ FD_SET(idle[0], &f);
+ (void)select(idle[0] + 1, &f, nullptr, nullptr, &gap);
+ }
+ _exit(0);
+ }
+ (void)close(p[1]);
+ tui_run(&t, &app);
+ CHECK(s.ticks == 2);
+ CHECK(s.keys < 100);
+
+ (void)close(p[0]); /* the writer stops on the next write */
+ (void)waitpid(pid, nullptr, 0);
+ free(t.cur);
+ free(t.old);
+}
+
int main(void) {
test_buffer();
test_viewport();
test_tabs();
test_input();
test_table();
+ test_widgets();
+ test_tick();
(void)printf("test_tui: %s\n", fails == 0 ? "ok" : "FAILED");
return fails != 0;
}
diff --git a/tui/tui.c b/tui/tui.c
index 6ab7de4..f17e718 100644
--- a/tui/tui.c
+++ b/tui/tui.c
@@ -13,6 +13,7 @@
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
+#include <time.h>
#include <unistd.h>
#ifdef __MVS__
@@ -34,7 +35,7 @@
#define CLEAR ESCS "[H" ESCS "[2J"
#define RESET ESCS "[0m"
-static struct tui *g_tui;
+static tui *g_tui;
static volatile sig_atomic_t g_winch;
static void wr(int fd, const void *buf, size_t n) {
@@ -51,11 +52,12 @@ static void wr(int fd, const void *buf, size_t n) {
}
}
-static void restore(struct tui *t) {
+static void restore(tui *t) {
if (!t->open)
return;
- wr(t->fd_out, RESET CUR_ON MOUSE_OFF ALT_OFF,
- strlen(RESET CUR_ON MOUSE_OFF ALT_OFF));
+ /* Leave the graphics character set even if a signal cut a frame short. */
+ wr(t->fd_out, RESET ESCS "(B" CUR_ON MOUSE_OFF ALT_OFF,
+ strlen(RESET ESCS "(B" CUR_ON MOUSE_OFF ALT_OFF));
(void)tcsetattr(t->fd_in, TCSANOW, &t->orig);
}
@@ -65,19 +67,19 @@ static void on_winch(int sig) {
}
static void on_term(int sig) {
- if (g_tui != NULL)
+ if (g_tui != nullptr)
restore(g_tui);
_exit(128 + sig);
}
-static void out_flush(struct tui *t) {
+static void out_flush(tui *t) {
if (t->olen > 0) {
wr(t->fd_out, t->out, (size_t)t->olen);
t->olen = 0;
}
}
-static void out(struct tui *t, const char *s, size_t n) {
+static void out(tui *t, const char *s, size_t n) {
if (t->olen + (int)n > (int)sizeof t->out)
out_flush(t);
if (n > sizeof t->out) {
@@ -88,11 +90,11 @@ static void out(struct tui *t, const char *s, size_t n) {
t->olen += (int)n;
}
-static void outs(struct tui *t, const char *s) {
+static void outs(tui *t, const char *s) {
out(t, s, strlen(s));
}
-static void outf(struct tui *t, const char *fmt, ...) {
+static void outf(tui *t, const char *fmt, ...) {
char b[64];
va_list ap;
int n;
@@ -104,8 +106,8 @@ static void outf(struct tui *t, const char *fmt, ...) {
out(t, b, (size_t)n < sizeof b ? (size_t)n : sizeof b - 1);
}
-static void sgr(struct tui *t, int fg, int bg, int attr) {
- char b[40];
+static void sgr(tui *t, int fg, int bg, int attr) {
+ char b[64];
int n = snprintf(b, sizeof b, "%s[0", ESCS);
if (attr & TUI_BOLD)
@@ -116,7 +118,14 @@ static void sgr(struct tui *t, int fg, int bg, int attr) {
n += snprintf(b + n, sizeof b - (size_t)n, ";4");
if (attr & TUI_REVERSE)
n += snprintf(b + n, sizeof b - (size_t)n, ";7");
- n += snprintf(b + n, sizeof b - (size_t)n, ";3%d;4%dm", fg, bg);
+ if (fg == TUI_COLOR_DEFAULT)
+ n += snprintf(b + n, sizeof b - (size_t)n, ";39");
+ else
+ n += snprintf(b + n, sizeof b - (size_t)n, ";38;5;%d", fg);
+ if (bg == TUI_COLOR_DEFAULT)
+ n += snprintf(b + n, sizeof b - (size_t)n, ";49m");
+ else
+ n += snprintf(b + n, sizeof b - (size_t)n, ";48;5;%dm", bg);
out(t, b, (size_t)n);
}
@@ -135,7 +144,7 @@ static bool cpr_size(int in, int outfd, int *w, int *h) {
FD_ZERO(&f);
FD_SET(in, &f);
- int r = select(in + 1, &f, NULL, NULL, &tv);
+ int r = select(in + 1, &f, nullptr, nullptr, &tv);
if (r < 0 && errno == EINTR)
continue;
if (r <= 0)
@@ -180,7 +189,7 @@ static bool cpr_size(int in, int outfd, int *w, int *h) {
return true;
}
-static void probe_size(struct tui *t, int *w, int *h) {
+static void probe_size(tui *t, int *w, int *h) {
struct winsize ws;
*w = 80;
@@ -192,9 +201,9 @@ static void probe_size(struct tui *t, int *w, int *h) {
} else if (!cpr_size(t->fd_in, t->fd_out, w, h)) {
const char *c = getenv("COLUMNS");
const char *l = getenv("LINES");
- if (c != NULL && atoi(c) > 0)
+ if (c != nullptr && atoi(c) > 0)
*w = atoi(c);
- if (l != NULL && atoi(l) > 0)
+ if (l != nullptr && atoi(l) > 0)
*h = atoi(l);
}
if (*w > TUI_MAX_W)
@@ -207,8 +216,8 @@ static void probe_size(struct tui *t, int *w, int *h) {
*h = 24;
}
-bool tui_alloc(struct tui *t, int w, int h) {
- struct tui_cell *cur, *old;
+bool tui_alloc(tui *t, int w, int h) {
+ tui_cell *cur, *old;
if (w < 2)
w = 2;
@@ -220,7 +229,7 @@ bool tui_alloc(struct tui *t, int w, int h) {
h = TUI_MAX_H;
cur = calloc((size_t)w * (size_t)h, sizeof *cur);
old = calloc((size_t)w * (size_t)h, sizeof *old);
- if (cur == NULL || old == NULL) {
+ if (cur == nullptr || old == nullptr) {
free(cur);
free(old);
return false;
@@ -236,7 +245,7 @@ bool tui_alloc(struct tui *t, int w, int h) {
return true;
}
-bool tui_resize(struct tui *t) {
+bool tui_resize(tui *t) {
int w, h;
probe_size(t, &w, &h);
@@ -245,10 +254,39 @@ bool tui_resize(struct tui *t) {
return tui_alloc(t, w, h);
}
-bool tui_open(struct tui *t) {
+/* z/OS C runtime prefers sigaction(); plain signal() is not used. */
+static void install_signal(int sig, void (*fn)(int)) {
+ struct sigaction sa = {};
+
+ sa.sa_handler = fn;
+ (void)sigemptyset(&sa.sa_mask);
+ (void)sigaction(sig, &sa, nullptr);
+}
+
+/* DEC Special Graphics are VT100 core, but a terminal we do not recognise
+ * is not worth the risk: `lqqqk` where a corner should be is worse than
+ * `+----+`. This is the one place the glyph backend is chosen. */
+static bool term_glyphs(void) {
+ static const char *const ok[] = {
+ "xterm", "screen", "tmux", "rxvt", "vt100", "vt220",
+ "linux", "ansi", "cygwin", "alacrit", "foot", "wezterm",
+ "kitty", "konsole", "putty", "gnome", "iterm", "st",
+ };
+ const char *term = getenv("TERM");
+
+ if (term == nullptr || term[0] == '\0')
+ return false;
+ for (size_t i = 0; i < sizeof ok / sizeof ok[0]; i++)
+ if (strncmp(term, ok[i], strlen(ok[i])) == 0)
+ return true;
+ return false;
+}
+
+bool tui_open(tui *t) {
struct termios r;
memset(t, 0, sizeof *t);
+ t->glyphs = term_glyphs();
t->fd_in = 0;
t->fd_out = 1;
if (!isatty(t->fd_in) || !isatty(t->fd_out))
@@ -267,54 +305,92 @@ bool tui_open(struct tui *t) {
t->open = true;
g_tui = t;
g_winch = 0;
- (void)signal(SIGTERM, on_term);
- (void)signal(SIGINT, on_term);
- (void)signal(SIGHUP, on_term);
- (void)signal(SIGQUIT, on_term);
+ install_signal(SIGTERM, on_term);
+ install_signal(SIGINT, on_term);
+ install_signal(SIGHUP, on_term);
+ install_signal(SIGQUIT, on_term);
#ifdef SIGWINCH
- (void)signal(SIGWINCH, on_winch);
+ install_signal(SIGWINCH, on_winch);
#endif
outs(t, ALT_ON CUR_OFF CLEAR MOUSE_ON);
out_flush(t);
if (!tui_resize(t)) {
restore(t);
t->open = false;
- g_tui = NULL;
+ g_tui = nullptr;
return false;
}
return true;
}
-void tui_close(struct tui *t) {
+void tui_close(tui *t) {
if (!t->open)
return;
restore(t);
t->open = false;
- g_tui = NULL;
+ g_tui = nullptr;
free(t->cur);
free(t->old);
- t->cur = NULL;
- t->old = NULL;
+ t->cur = nullptr;
+ t->old = nullptr;
}
-void tui_size(struct tui *t, int *w, int *h) {
+void tui_size(tui *t, int *w, int *h) {
*w = t->w;
*h = t->h;
}
-void tui_clear(struct tui *t) {
+void tui_clear(tui *t) {
int n = t->w * t->h;
for (int i = 0; i < n; i++) {
t->cur[i].ch = ' ';
- t->cur[i].fg = 7;
- t->cur[i].bg = 0;
+ t->cur[i].fg = TUI_PLAIN.fg;
+ t->cur[i].bg = TUI_PLAIN.bg;
t->cur[i].attr = 0;
}
}
-void tui_fill(struct tui *t, int x, int y, int w, int h, char ch,
- tui_style st) {
+/* ---- layout primitives ----------------------------------------------- */
+
+void tui_split_v(tui_rect r, int top_h, tui_rect *top, tui_rect *bot) {
+ if (top_h < 0)
+ top_h = 0;
+ if (top_h > r.h)
+ top_h = r.h;
+ if (top != nullptr)
+ *top = (tui_rect){r.x, r.y, r.w, top_h};
+ if (bot != nullptr)
+ *bot = (tui_rect){r.x, r.y + top_h, r.w, r.h - top_h};
+}
+
+void tui_split_h(tui_rect r, int left_w, tui_rect *left, tui_rect *right) {
+ if (left_w < 0)
+ left_w = 0;
+ if (left_w > r.w)
+ left_w = r.w;
+ if (left != nullptr)
+ *left = (tui_rect){r.x, r.y, left_w, r.h};
+ if (right != nullptr)
+ *right = (tui_rect){r.x + left_w, r.y, r.w - left_w, r.h};
+}
+
+tui_rect tui_inset(tui_rect r, int dx, int dy) {
+ int nw = r.w - 2 * dx;
+ int nh = r.h - 2 * dy;
+
+ return (tui_rect){r.x + dx, r.y + dy, nw > 0 ? nw : 0, nh > 0 ? nh : 0};
+}
+
+bool tui_rect_hit(tui_rect r, int x, int y) {
+ if (x < r.x || x >= r.x + r.w)
+ return false;
+ if (y < r.y || y >= r.y + r.h)
+ return false;
+ return true;
+}
+
+void tui_fill(tui *t, int x, int y, int w, int h, char ch, tui_style st) {
if (x < 0) {
w += x;
x = 0;
@@ -330,7 +406,7 @@ void tui_fill(struct tui *t, int x, int y, int w, int h, char ch,
if (w <= 0 || h <= 0)
return;
for (int j = y; j < y + h; j++) {
- struct tui_cell *row = t->cur + (size_t)j * (size_t)t->w;
+ tui_cell *row = t->cur + (size_t)j * (size_t)t->w;
for (int i = x; i < x + w; i++) {
row[i].ch = (unsigned char)ch;
row[i].fg = st.fg;
@@ -340,11 +416,10 @@ void tui_fill(struct tui *t, int x, int y, int w, int h, char ch,
}
}
-void tui_put(struct tui *t, int x, int y, const char *s, int maxw,
- tui_style st) {
+void tui_put(tui *t, int x, int y, const char *s, int maxw, tui_style st) {
if (y < 0 || y >= t->h)
return;
- struct tui_cell *row = t->cur + (size_t)y * (size_t)t->w;
+ tui_cell *row = t->cur + (size_t)y * (size_t)t->w;
for (int i = 0; s[i] != '\0' && x < t->w; i++, x++) {
unsigned char c = (unsigned char)s[i];
if (maxw > 0 && i >= maxw)
@@ -360,8 +435,8 @@ void tui_put(struct tui *t, int x, int y, const char *s, int maxw,
}
}
-void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st,
- const char *fmt, ...) {
+void tui_printf(tui *t, int x, int y, int maxw, tui_style st, const char *fmt,
+ ...) {
char b[512];
va_list ap;
@@ -371,27 +446,49 @@ void tui_printf(struct tui *t, int x, int y, int maxw, tui_style st,
tui_put(t, x, y, b, maxw, st);
}
-void tui_box(struct tui *t, int x, int y, int w, int h, tui_style st) {
- tui_box_frame(t, x, y, w, h, st, NULL, TUI_PLAIN);
+void tui_box(tui *t, int x, int y, int w, int h, tui_style st) {
+ tui_box_frame(t, x, y, w, h, st, nullptr, TUI_PLAIN);
}
-void tui_box_frame(struct tui *t, int x, int y, int w, int h, tui_style st,
+/* ---- line drawing ---------------------------------------------------- */
+
+/* A cell holds one byte, so a line is either a DEC Special Graphics letter
+ * (the terminal draws the line) or the ASCII the frame has always used.
+ * The order is the order of the calls below and means nothing else. */
+enum { G_TL, G_TR, G_BL, G_BR, G_H, G_V, G_SHADE, G_COUNT };
+
+static char glyph(const tui *t, int g) {
+ static const char dec[] = {'l', 'k', 'm', 'j', 'q', 'x', 'a'};
+ static const char ascii[] = {'+', '+', '+', '+', '-', '|', '#'};
+
+ return t->glyphs ? dec[g] : ascii[g];
+}
+
+static void putg(tui *t, int x, int y, int g, tui_style st) {
+ char s[2] = {glyph(t, g), '\0'};
+
+ if (t->glyphs)
+ st.attr = (unsigned char)(st.attr | TUI_ACS);
+ tui_put(t, x, y, s, 1, 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) {
if (w < 2 || h < 2)
return;
for (int i = 1; i < w - 1; i++) {
- tui_put(t, x + i, y, "-", 1, st);
- tui_put(t, x + i, y + h - 1, "-", 1, st);
+ putg(t, x + i, y, G_H, st);
+ putg(t, x + i, y + h - 1, G_H, st);
}
for (int j = 1; j < h - 1; j++) {
- tui_put(t, x, y + j, "|", 1, st);
- tui_put(t, x + w - 1, y + j, "|", 1, st);
+ putg(t, x, y + j, G_V, st);
+ putg(t, x + w - 1, y + j, G_V, st);
}
- tui_put(t, x, y, "+", 1, st);
- tui_put(t, x + w - 1, y, "+", 1, st);
- tui_put(t, x, y + h - 1, "+", 1, st);
- tui_put(t, x + w - 1, y + h - 1, "+", 1, st);
- if (title != NULL && title[0] != '\0' && w > 6) {
+ putg(t, x, y, G_TL, st);
+ putg(t, x + w - 1, y, G_TR, st);
+ putg(t, x, y + h - 1, G_BL, st);
+ putg(t, x + w - 1, y + h - 1, G_BR, st);
+ if (title != nullptr && title[0] != '\0' && w > 6) {
char chip[64];
int n = (int)strlen(title);
int room = w - 6;
@@ -403,7 +500,7 @@ void tui_box_frame(struct tui *t, int x, int y, int w, int h, tui_style st,
}
}
-void tui_flush(struct tui *t) {
+void tui_flush(tui *t) {
int lfg = -1, lbg = -1, la = -1;
if (!t->open)
@@ -414,8 +511,8 @@ void tui_flush(struct tui *t) {
t->full = false;
}
for (int y = 0; y < t->h; y++) {
- struct tui_cell *row = t->cur + (size_t)y * (size_t)t->w;
- struct tui_cell *orow = t->old + (size_t)y * (size_t)t->w;
+ tui_cell *row = t->cur + (size_t)y * (size_t)t->w;
+ tui_cell *orow = t->old + (size_t)y * (size_t)t->w;
int x = 0;
while (x < t->w) {
@@ -432,12 +529,20 @@ void tui_flush(struct tui *t) {
x++;
outf(t, "%s[%d;%dH", ESCS, y + 1, start + 1);
for (int i = start; i < x; i++) {
+ bool acs = (row[i].attr & TUI_ACS) != 0;
+
if (row[i].fg != lfg || row[i].bg != lbg || row[i].attr != la) {
sgr(t, row[i].fg, row[i].bg, row[i].attr);
lfg = row[i].fg;
lbg = row[i].bg;
la = row[i].attr;
}
+ /* An SGR reset does not leave the graphics set, so this is
+ * tracked apart from the colours. */
+ if (acs != t->acs_on) {
+ outs(t, acs ? ESCS "(0" : ESCS "(B");
+ t->acs_on = acs;
+ }
char c = (char)(row[i].ch != 0 ? row[i].ch : ' ');
out(t, &c, 1);
}
@@ -455,12 +560,12 @@ int tui_tabs_hit(int x, const char *const *labels, int n) {
if (x >= at && x < at + len)
return i;
- at += len + 1;
+ at += len + 2; /* the gap drawn by tui_tabs_draw */
}
return -1;
}
-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) {
for (int i = 0; i < n; i++) {
int len = (int)strlen(labels[i]) + 2;
@@ -468,19 +573,17 @@ int tui_tabs_draw(struct tui *t, int x, int y, const char *const *labels, int n,
tui_printf(t, x, y, len, i == active ? TUI_TAB_ON : TUI_TAB_OFF, " %s ",
labels[i]);
x += len;
- if (i + 1 < n) {
- tui_put(t, x, y, "|", 1, TUI_META);
- x += 1;
- }
+ if (i + 1 < n)
+ x += 2; /* a gap, not a `|`: separators read as a DOS menu */
}
return x;
}
-void tui_rule(struct tui *t, int y, char ch, tui_style st) {
+void tui_rule(tui *t, int y, char ch, tui_style st) {
tui_fill(t, 0, y, t->w, 1, ch, st);
}
-static void shift_in(struct tui *t, int n) {
+static void shift_in(tui *t, int n) {
if (n >= t->inlen) {
t->inlen = 0;
return;
@@ -489,7 +592,7 @@ static void shift_in(struct tui *t, int n) {
t->inlen -= n;
}
-static int decode(struct tui *t) {
+static int decode(tui *t) {
unsigned char *b = t->in;
if (t->inlen == 0)
@@ -610,7 +713,7 @@ static int decode(struct tui *t) {
return TUI_KEY_NONE;
}
-int tui_key(struct tui *t, int timeout_ms) {
+int tui_key(tui *t, int timeout_ms) {
if (g_winch) {
g_winch = 0;
return TUI_KEY_RESIZE;
@@ -627,7 +730,7 @@ int tui_key(struct tui *t, int timeout_ms) {
}
int wait = (t->inlen > 0) ? 30 : timeout_ms;
fd_set f;
- struct timeval tv, *ptv = NULL;
+ struct timeval tv, *ptv = nullptr;
FD_ZERO(&f);
FD_SET(t->fd_in, &f);
@@ -636,7 +739,7 @@ int tui_key(struct tui *t, int timeout_ms) {
tv.tv_usec = (wait % 1000) * 1000;
ptv = &tv;
}
- int r = select(t->fd_in + 1, &f, NULL, NULL, ptv);
+ int r = select(t->fd_in + 1, &f, nullptr, nullptr, ptv);
if (r < 0) {
if (errno == EINTR)
continue;
@@ -665,21 +768,51 @@ int tui_key(struct tui *t, int timeout_ms) {
/* ---- messages and the application loop ------------------------------- */
-void tui_run(struct tui *t, struct tui_app *app) {
+/** @brief Set @p due to now + @p ms on the monotonic clock. */
+static void deadline(struct timespec *due, int ms) {
+ (void)clock_gettime(CLOCK_MONOTONIC, due);
+ due->tv_sec += ms / 1000;
+ due->tv_nsec += (long)(ms % 1000) * 1000000L;
+ if (due->tv_nsec >= 1000000000L) {
+ due->tv_nsec -= 1000000000L;
+ due->tv_sec++;
+ }
+}
+
+/** @brief Milliseconds from now until @p due; 0 once it has passed. */
+static int remaining(const struct timespec *due) {
+ struct timespec now;
+ long long ms;
+
+ (void)clock_gettime(CLOCK_MONOTONIC, &now);
+ ms = (long long)(due->tv_sec - now.tv_sec) * 1000 +
+ (long)(due->tv_nsec - now.tv_nsec) / 1000000L;
+ return ms < 0 ? 0 : (int)ms;
+}
+
+void tui_run(tui *t, tui_app *app) {
+ struct timespec due;
+ int tick = app->tick_ms;
+
app->quit = false;
app->view(app->model, t);
tui_flush(t);
+ if (tick > 0)
+ deadline(&due, tick);
for (;;) {
- int k = tui_key(t, app->tick_ms > 0 ? app->tick_ms : -1);
- struct tui_msg m;
+ /* The interval is a deadline, not a wait restarted by each key:
+ * a held key would otherwise starve the tick. */
+ int k = tui_key(t, tick > 0 ? remaining(&due) : -1);
+ tui_msg m;
m.key = 0;
m.w = t->w;
m.h = t->h;
if (k == TUI_KEY_NONE) {
- if (app->tick_ms <= 0)
+ if (tick <= 0)
continue;
m.type = TUI_MSG_TICK;
+ deadline(&due, tick);
} else if (k == TUI_KEY_RESIZE) {
(void)tui_resize(t);
m.type = TUI_MSG_RESIZE;
@@ -696,12 +829,17 @@ void tui_run(struct tui *t, struct tui_app *app) {
}
if (app->quit)
break;
+ if (app->tick_ms != tick) {
+ tick = app->tick_ms;
+ if (tick > 0)
+ deadline(&due, tick);
+ }
}
}
/* ---- viewport -------------------------------------------------------- */
-void tui_viewport_clamp(struct tui_viewport *v, int rows, int height) {
+void tui_viewport_clamp(tui_viewport *v, int rows, int height) {
if (rows < 0)
rows = 0;
if (height < 1)
@@ -720,13 +858,12 @@ void tui_viewport_clamp(struct tui_viewport *v, int rows, int height) {
v->top = 0;
}
-void tui_viewport_move(struct tui_viewport *v, int delta, int rows,
- int height) {
+void tui_viewport_move(tui_viewport *v, int delta, int rows, int height) {
v->sel += delta;
tui_viewport_clamp(v, rows, height);
}
-bool tui_viewport_key(struct tui_viewport *v, int key, int rows, int height) {
+bool tui_viewport_key(tui_viewport *v, int key, int rows, int height) {
switch (key) {
case 'j':
case TUI_KEY_DOWN:
@@ -759,9 +896,31 @@ bool tui_viewport_key(struct tui_viewport *v, int key, int rows, int height) {
/* ---- table ----------------------------------------------------------- */
-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) {
+/* A proportional thumb on the last free column, only when there is more to
+ * see than fits. */
+static void draw_scrollbar(tui *t, tui_rect r, int rows, int top, int height) {
+ int x = r.x + r.w;
+ int thumb_h, max_top, thumb_y;
+
+ if (rows <= height || r.w <= 2)
+ return;
+ if (x >= t->w)
+ x = r.x + r.w - 1;
+ thumb_h = height * height / rows;
+ if (thumb_h < 1)
+ thumb_h = 1;
+ max_top = rows - height;
+ thumb_y = max_top > 0 ? top * (height - thumb_h) / max_top : 0;
+ for (int i = 0; i < height; i++) {
+ int on = i >= thumb_y && i < thumb_y + thumb_h;
+
+ putg(t, x, r.y + 1 + i, on != 0 ? G_SHADE : G_V,
+ on != 0 ? TUI_ACCENT : TUI_META);
+ }
+}
+
+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) {
int x = r.x;
int height = r.h - 1;
char buf[TUI_CELL_MAX];
@@ -793,20 +952,21 @@ void tui_table_draw(struct tui *t, tui_rect r, const struct tui_table *tb,
if (height < 1)
return;
- struct tui_viewport vp = tb->vp;
+ tui_viewport vp = tb->vp;
tui_viewport_clamp(&vp, rows, height);
for (int i = 0; i < height && vp.top + i < rows; i++) {
int row = vp.top + i;
int y = r.y + 1 + i;
- tui_style st = rowstyle != NULL ? rowstyle(ctx, row) : TUI_PLAIN;
+ tui_style st = rowstyle != nullptr ? rowstyle(ctx, row) : TUI_PLAIN;
bool cur = row == vp.sel;
x = r.x;
if (cur) {
tui_style fs = st;
- fs.attr = (unsigned char)(fs.attr | TUI_REVERSE);
+
+ fs.bg = TUI_SEL_BG;
tui_fill(t, r.x, y, r.w, 1, ' ', fs);
- st.attr = fs.attr;
+ st = fs;
} else {
tui_fill(t, r.x, y, r.w, 1, ' ', TUI_PLAIN);
}
@@ -816,7 +976,7 @@ void tui_table_draw(struct tui *t, tui_rect r, const struct tui_table *tb,
if (w > r.x + r.w - x)
w = r.x + r.w - x;
buf[0] = '\0';
- if (cell != NULL)
+ if (cell != nullptr)
cell(ctx, row, c, buf, sizeof buf);
if (tb->cols[c].right)
tui_printf(t, x, y, w, st, "%*s", w, buf);
@@ -827,9 +987,10 @@ void tui_table_draw(struct tui *t, tui_rect r, const struct tui_table *tb,
x += 1;
}
}
+ draw_scrollbar(t, r, rows, vp.top, height);
}
-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) {
if (tb->ncols == 0 && key == 0)
return false;
return tui_viewport_key(&tb->vp, key, rows, height);
@@ -854,8 +1015,7 @@ bool tui_tabs_key(int key, int *active, int n) {
/* ---- text 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) {
if (r.w < 3 || r.h < 1)
return;
tui_fill(t, r.x, r.y, r.w, 1, ' ', st);
@@ -866,7 +1026,7 @@ void tui_input_draw(struct tui *t, tui_rect r, const struct tui_input *in,
start = in->pos - w + 1;
tui_printf(t, r.x + 1, r.y, w, st, "%.*s", w, in->buf + start);
if (in->focus) {
- tui_style cur = TUI_STYLE((unsigned char)(st.fg ^ 0), 7, 0);
+ tui_style cur = TUI_STYLE(16, 252, 0);
int cx = r.x + 1 + in->pos - start;
if (cx < r.x + 1 + w) {
@@ -876,7 +1036,7 @@ void tui_input_draw(struct tui *t, tui_rect r, const struct tui_input *in,
}
}
-bool tui_input_key(struct tui_input *in, int key) {
+bool tui_input_key(tui_input *in, int key) {
if (!in->focus)
return false;
if (key >= 32 && key < 127) {
@@ -930,50 +1090,110 @@ bool tui_input_key(struct tui_input *in, int key) {
/* ---- progress and status -------------------------------------------- */
-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) {
tui_style st;
- int fill, room, i;
+ int fill, i;
- if (width < 4)
+ if (width < 2)
return;
- room = width - 2;
- fill = pct < 0 ? 0 : pct * room / 100;
- if (fill > room)
- fill = room;
+ fill = pct < 0 ? 0 : pct * width / 100;
+ if (fill > width)
+ fill = width;
st = pct < 0 ? TUI_META
: pct < 70 ? TUI_GOOD
: pct < 85 ? TUI_WARN
: TUI_BAD;
- tui_put(t, x, y, "[", 1, TUI_META);
- for (i = 0; i < room; i++)
- tui_put(t, x + 1 + i, y, i < fill ? "=" : " ", 1,
- i < fill ? st : TUI_META);
- tui_put(t, x + width - 1, y, "]", 1, TUI_META);
+ /* A track with a shaded fill, no brackets: `[#### ]` is the other
+ * thing that read as curses. */
+ for (i = 0; i < width; i++)
+ putg(t, x + i, y, i < fill ? G_SHADE : G_H, i < fill ? st : TUI_META);
}
-void tui_hints(struct tui *t, int x, int y, int maxw,
- const struct tui_hint *hints, int n) {
+void tui_keymap_footer(tui *t, int x, int y, int maxw, const tui_binding *km,
+ int n, int tab) {
+ unsigned bit = 1u << (unsigned)tab;
+
for (int i = 0; i < n; i++) {
- int kl = (int)strlen(hints[i].key);
- int ll = (int)strlen(hints[i].label);
+ int kl, ll;
+ if (!km[i].in_footer || (km[i].tabs & bit) == 0)
+ continue;
+ kl = (int)strlen(km[i].key);
+ ll = (int)strlen(km[i].label);
if (x + kl + ll + 4 > maxw)
break;
- tui_put(t, x, y, " ", 1, TUI_META);
- tui_printf(t, x + 1, y, kl, TUI_KEYCAP, "%s", hints[i].key);
- tui_put(t, x + 1 + kl, y, " ", 1, TUI_META);
- tui_printf(t, x + 2 + kl, y, ll, TUI_META, "%s", hints[i].label);
+ tui_put(t, x, y, " ", 1, TUI_BAR);
+ tui_printf(t, x + 1, y, kl, TUI_KEYCAP, "%s", km[i].key);
+ tui_put(t, x + 1 + kl, y, " ", 1, TUI_BAR);
+ tui_printf(t, x + 2 + kl, y, ll, TUI_BAR, "%s", km[i].label);
x += kl + ll + 3;
}
}
-void tui_statusbar(struct tui *t, const char *left, const char *right) {
+void tui_keymap_help(tui *t, const tui_binding *km, int n, const char *title) {
+ int w = t->w < 68 ? t->w - 2 : 66;
+ int h = t->h < 20 ? t->h - 2 : 18;
+ int x = (t->w - w) / 2;
+ int y = (t->h - h) / 2;
+ const char *group = nullptr;
+ int row = y + 2;
+ int col_x = x + 2;
+
+ if (x < 0)
+ x = 0;
+ if (y < 0)
+ y = 0;
+ tui_fill(t, x, y, w, h, ' ', TUI_PLAIN);
+ tui_box_frame(t, x, y, w, h, TUI_ACCENT, title, TUI_ACCENT);
+ for (int i = 0; i < n && row < y + h - 2; i++) {
+ int kl, ll, need;
+
+ if (group == nullptr || strcmp(group, km[i].group) != 0) {
+ if (group != nullptr)
+ row += 2;
+ if (row >= y + h - 2)
+ break;
+ group = km[i].group;
+ tui_printf(t, x + 2, row++, w - 4, TUI_LABEL, "%s", group);
+ col_x = x + 2;
+ }
+ kl = (int)strlen(km[i].key);
+ ll = (int)strlen(km[i].label);
+ need = kl + ll + 4;
+ if (col_x + need > x + w - 2) {
+ row++;
+ col_x = x + 2;
+ if (row >= y + h - 2)
+ break;
+ }
+ tui_printf(t, col_x, row, kl, TUI_KEYCAP, "%s", km[i].key);
+ tui_printf(t, col_x + kl + 1, row, ll, TUI_META, "%s", km[i].label);
+ col_x += need;
+ }
+ tui_printf(t, x + 2, y + h - 2, w - 4, TUI_META, "press ? or ESC to close");
+}
+
+void tui_sparkline(tui *t, int x, int y, const unsigned char *vals, int n,
+ tui_style st) {
+ static const char ramp[] = " .:-=+*#%";
+
+ for (int i = 0; i < n; i++) {
+ unsigned v = vals[i] > 100u ? 100u : vals[i];
+ unsigned idx = (v * 8u + 50u) / 100u;
+ /* tui_put expects a terminated string, so the sample is one. */
+ char c[2] = {ramp[idx], '\0'};
+
+ tui_put(t, x + i, y, c, 1, st);
+ }
+}
+
+void tui_statusbar(tui *t, const char *left, const char *right) {
int y = t->h - 1;
tui_fill(t, 0, y, t->w, 1, ' ', TUI_BAR);
- if (left != NULL)
+ if (left != nullptr)
tui_printf(t, 1, y, t->w - 2, TUI_PLAIN, "%s", left);
- if (right != NULL) {
+ if (right != nullptr) {
int n = (int)strlen(right);
tui_printf(t, t->w - 1 - n, y, n, TUI_PLAIN, "%s", right);
}
diff --git a/tui/tui.h b/tui/tui.h
index c999eda..68404d4 100644
--- a/tui/tui.h
+++ b/tui/tui.h
@@ -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