summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-25 21:55:37 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-25 21:55:37 -0600
commit5f8909cf7beeefa6318c795f8bf52b4b9f4361ae (patch)
tree5a29d916b67da9a73be318c90271cb5656a5431d
parent3cec5cdebb48973b58e9131b53d4fdbb08cf98b6 (diff)
downloadztop-5f8909cf7beeefa6318c795f8bf52b4b9f4361ae.tar.gz
tui: mouse messages; ztop: clickable tabs, wheel, better help
Enable xterm SGR mouse reporting and decode it into TUI_MSG_MOUSE. ztop uses it for the view tabs, the storage sub-tabs, table rows and the wheel. The help panel now lists navigation explicitly, and the status line shows the keys for the current view.
-rw-r--r--DESIGN.md5
-rw-r--r--README.md3
-rw-r--r--src/ztop.c107
-rw-r--r--tui/tui.c54
-rw-r--r--tui/tui.h19
5 files changed, 166 insertions, 22 deletions
diff --git a/DESIGN.md b/DESIGN.md
index 23cfa5a..bf0a6e0 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -148,7 +148,10 @@ the terminal code out of the application and gives every view the same
keys. The widgets are a viewport with its own selection and scroll
offset, a table built from columns and a cell callback, tabs, a single
line input, a progress bar and a status line; an application that wants no
-tabs simply never draws them. Commands and subscriptions are not modelled:
+tabs simply never draws them. The mouse is part of the message loop: SGR
+mouse reporting (1006) turns a click into a `TUI_MSG_MOUSE` with 0-based
+cells, and `tui_tabs_hit` answers which tab a column belongs to, so the
+application decides what a click means. Commands and subscriptions are not modelled:
a tick message and a return value saying "redraw" are enough here, and a
`Cmd` type in C would be a function pointer by another name.
diff --git a/README.md b/README.md
index 3f57e76..b1eb2bb 100644
--- a/README.md
+++ b/README.md
@@ -77,12 +77,13 @@ prompt. There are three views, selected with `TAB`:
| key | |
| --- | --- |
-| `TAB`, `Shift-TAB` | next / previous view |
+| `TAB`, `Shift-TAB`, click a tab | next / previous view |
| `1`..`4` | inside STORAGE: CSA, SQA, LSQA, private |
| `p` | pause/resume refresh |
| `r` | collect now |
| `+`/`-` | refresh 1..60 s |
| `j`/`k`, arrows, `PgUp`/`PgDn`, `g`/`G` | move and scroll the selected table |
+| wheel, click a row | scroll, select a row |
| `?` | help |
| `q` | quit |
diff --git a/src/ztop.c b/src/ztop.c
index 9078739..23c1512 100644
--- a/src/ztop.c
+++ b/src/ztop.c
@@ -448,6 +448,8 @@ static void draw_storage(struct tui *t, struct app *a) {
tui_printf(t, 1, 4, t->w - 2, TUI_PLAIN,
"used %s free %s mapped %s total %s use %s blocks %u", used,
freed, mapped, total, pcbuf, v->blocks);
+ if (p >= 0)
+ tui_progress(t, t->w - 14, 4, 12, p);
tui_table_draw(t, r, &a->storage, v->nsub, storage_cell, NULL, a);
if (a->err[a->sub][0] != '\0')
@@ -493,27 +495,38 @@ static void draw_jobs(struct tui *t, struct app *a) {
}
static void draw_help(struct tui *t) {
- int w = t->w < 56 ? t->w - 2 : 54;
- int h = 16;
+ int w = t->w < 64 ? t->w - 2 : 62;
+ int h = 19;
int x = (t->w - w) / 2;
int y = (t->h - h) / 2;
+ if (y < 0)
+ y = 0;
tui_fill(t, x, y, w, h, ' ', TUI_PLAIN);
tui_box(t, x, y, w, h, TUI_LABEL);
tui_printf(t, x + 2, y + 1, w - 4, TUI_TITLE, "ztop keys");
- tui_printf(t, x + 2, y + 3, w - 4, TUI_PLAIN, "q quit");
- tui_printf(t, x + 2, y + 4, w - 4, TUI_PLAIN, "TAB next view");
+
+ tui_printf(t, x + 2, y + 3, w - 4, TUI_LABEL, "views");
+ tui_printf(t, x + 2, y + 4, w - 4, TUI_PLAIN,
+ "TAB / Shift-TAB next / previous view");
tui_printf(t, x + 2, y + 5, w - 4, TUI_PLAIN,
- "1-3 storage, system, jobs");
- tui_printf(t, x + 2, y + 6, w - 4, TUI_PLAIN, "1-4 CSA, SQA, LSQA, PVT");
- tui_printf(t, x + 2, y + 7, w - 4, TUI_PLAIN, "p pause refresh");
- tui_printf(t, x + 2, y + 8, w - 4, TUI_PLAIN, "r refresh now");
- tui_printf(t, x + 2, y + 9, w - 4, TUI_PLAIN, "+ - interval up / down");
- tui_printf(t, x + 2, y + 10, w - 4, TUI_PLAIN, "j k move the selection");
- tui_printf(t, x + 2, y + 11, w - 4, TUI_PLAIN, "up dn same");
- tui_printf(t, x + 2, y + 12, w - 4, TUI_PLAIN, "g G first / last");
- tui_printf(t, x + 2, y + 13, w - 4, TUI_PLAIN, "? close help");
- tui_printf(t, x + 2, y + 14, w - 4, TUI_PLAIN, "1-4 inside storage");
+ "1 2 3 4 inside STORAGE: CSA SQA LSQA PVT");
+ tui_printf(t, x + 2, y + 6, w - 4, TUI_PLAIN,
+ "click a tab same, with the mouse");
+
+ tui_printf(t, x + 2, y + 8, w - 4, TUI_LABEL, "moving");
+ tui_printf(t, x + 2, y + 9, w - 4, TUI_PLAIN, "j k / arrows one row");
+ tui_printf(t, x + 2, y + 10, w - 4, TUI_PLAIN, "PgUp PgDn a page");
+ tui_printf(t, x + 2, y + 11, w - 4, TUI_PLAIN,
+ "g G / Home End first / last");
+ tui_printf(t, x + 2, y + 12, w - 4, TUI_PLAIN,
+ "wheel, click scroll, select a row");
+
+ tui_printf(t, x + 2, y + 14, w - 4, TUI_LABEL, "the screen");
+ tui_printf(t, x + 2, y + 15, w - 4, TUI_PLAIN,
+ "p pause r refresh now + - interval 1..60 s");
+ tui_printf(t, x + 2, y + 16, w - 4, TUI_PLAIN,
+ "? close this help q quit");
}
static void view(void *model, struct tui *t) {
@@ -535,7 +548,10 @@ static void view(void *model, struct tui *t) {
}
if (a->help)
draw_help(t);
- tui_statusbar(t, "q quit TAB view j/k move r refresh p pause ? help",
+ tui_statusbar(t,
+ a->tab == 0 ? "TAB view 1-4 area j/k move wheel ? help"
+ : a->tab == 1 ? "TAB view r refresh ? help"
+ : "TAB view j/k move PgUp/PgDn ? help",
a->paused ? "PAUSED" : "");
}
@@ -577,6 +593,65 @@ static void usage(FILE *f) {
" -h this text\n");
}
+static void wheel(struct app *a, int dir, int height) {
+ if (a->tab == 0)
+ tui_viewport_move(&a->storage.vp, dir, a->info[a->sub].nsub, height);
+ else if (a->tab == 2)
+ tui_viewport_move(&a->jobs.vp, dir, a->sys.njobs, height);
+}
+
+/** @brief Tabs, sub-tabs, table rows and the wheel. */
+static bool mouse_event(struct tui_app *app, const struct tui_msg *m) {
+ static const char *const views[3] = {"1 STORAGE", "2 SYSTEM", "3 JOBS"};
+ struct app *a = app->model;
+ const struct tui_mouse *mo = &m->mouse;
+ int idx, row, height;
+
+ height = (a->tab == 0 ? m->h - 8 : m->h - 6);
+ if (height < 1)
+ height = 1;
+ if (mo->button == 64 || mo->button == 65) {
+ wheel(a, mo->button == 64 ? -1 : 1, height);
+ return true;
+ }
+ if (mo->release || mo->button != 0)
+ return false;
+ if (mo->y == 1) {
+ idx = tui_tabs_hit(mo->x, views, 3);
+ if (idx < 0)
+ return false;
+ a->tab = idx;
+ return true;
+ }
+ if (a->tab == 0 && mo->y == 3) {
+ if (mo->x >= 1 && mo->x <= 5)
+ a->sub = 0;
+ else if (mo->x >= 7 && mo->x <= 11)
+ a->sub = 1;
+ else if (mo->x >= 13 && mo->x <= 18)
+ a->sub = 2;
+ else if (mo->x >= 20 && mo->x <= 24)
+ a->sub = 3;
+ else
+ return false;
+ return true;
+ }
+ if (a->tab == 0 && mo->y >= 6) {
+ row = a->storage.vp.top + (mo->y - 6);
+ if (row < a->info[a->sub].nsub) {
+ a->storage.vp.sel = row;
+ return true;
+ }
+ } else if (a->tab == 2 && mo->y >= 4) {
+ row = a->jobs.vp.top + (mo->y - 4);
+ if (row < a->sys.njobs) {
+ a->jobs.vp.sel = row;
+ return true;
+ }
+ }
+ return false;
+}
+
static bool update(struct tui_app *app, const struct tui_msg *m) {
struct app *a = app->model;
int rows, height;
@@ -594,6 +669,8 @@ static bool update(struct tui_app *app, const struct tui_msg *m) {
}
if (m->type == TUI_MSG_RESIZE)
return true;
+ if (m->type == TUI_MSG_MOUSE)
+ return mouse_event(app, m);
if (m->type != TUI_MSG_KEY)
return false;
diff --git a/tui/tui.c b/tui/tui.c
index 34bd7f0..3027001 100644
--- a/tui/tui.c
+++ b/tui/tui.c
@@ -24,6 +24,10 @@
#define ALT_ON ESCS "[?1049h"
#define ALT_OFF ESCS "[?1049l"
+/* 1000 reports button presses and releases, 1006 gives SGR coordinates
+ * (1-based, no 223 limit) so clicks beyond column 223 still work. */
+#define MOUSE_ON ESCS "[?1000h" ESCS "[?1006h"
+#define MOUSE_OFF ESCS "[?1006l" ESCS "[?1000l"
#define CUR_OFF ESCS "[?25l"
#define CUR_ON ESCS "[?25h"
#define CLEAR ESCS "[H" ESCS "[2J"
@@ -49,7 +53,8 @@ static void wr(int fd, const void *buf, size_t n) {
static void restore(struct tui *t) {
if (!t->open)
return;
- wr(t->fd_out, RESET CUR_ON ALT_OFF, strlen(RESET CUR_ON ALT_OFF));
+ wr(t->fd_out, RESET CUR_ON MOUSE_OFF ALT_OFF,
+ strlen(RESET CUR_ON MOUSE_OFF ALT_OFF));
(void)tcsetattr(t->fd_in, TCSANOW, &t->orig);
}
@@ -268,7 +273,7 @@ bool tui_open(struct tui *t) {
#ifdef SIGWINCH
(void)signal(SIGWINCH, on_winch);
#endif
- outs(t, ALT_ON CUR_OFF CLEAR);
+ outs(t, ALT_ON CUR_OFF CLEAR MOUSE_ON);
out_flush(t);
if (!tui_resize(t)) {
restore(t);
@@ -426,6 +431,19 @@ void tui_flush(struct tui *t) {
out_flush(t);
}
+int tui_tabs_hit(int x, const char *const *labels, int n) {
+ int at = 0;
+
+ for (int i = 0; i < n; i++) {
+ int len = (int)strlen(labels[i]) + 2;
+
+ if (x >= at && x < at + len)
+ return i;
+ at += len + 1;
+ }
+ return -1;
+}
+
int tui_tabs_draw(struct tui *t, int x, int y, const char *const *labels, int n,
int active) {
for (int i = 0; i < n; i++) {
@@ -504,6 +522,35 @@ static int decode(struct tui *t) {
shift_in(t, 3);
return k;
}
+ if (b[2] == '<') {
+ int v[3] = {0, 0, 0};
+ int n = 0;
+ int i = 3;
+
+ while (i < t->inlen && n < 3) {
+ if (b[i] >= '0' && b[i] <= '9') {
+ v[n] = v[n] * 10 + (b[i] - '0');
+ i++;
+ } else if (b[i] == ';') {
+ n++;
+ i++;
+ } else {
+ break;
+ }
+ }
+ if (i >= t->inlen)
+ return TUI_KEY_NONE; /* wait for the final byte */
+ if (b[i] != 'M' && b[i] != 'm') {
+ shift_in(t, i + 1);
+ return TUI_KEY_NONE;
+ }
+ t->mouse.button = v[0];
+ t->mouse.x = v[1] > 0 ? v[1] - 1 : 0;
+ t->mouse.y = v[2] > 0 ? v[2] - 1 : 0;
+ t->mouse.release = b[i] == 'm';
+ shift_in(t, i + 1);
+ return TUI_KEY_MOUSE;
+ }
if (b[2] >= '1' && b[2] <= '9') {
int v = 0, i = 2;
while (i < t->inlen && b[i] >= '0' && b[i] <= '9') {
@@ -620,6 +667,9 @@ void tui_run(struct tui *t, struct tui_app *app) {
} else if (k == TUI_KEY_RESIZE) {
(void)tui_resize(t);
m.type = TUI_MSG_RESIZE;
+ } else if (k == TUI_KEY_MOUSE) {
+ m.type = TUI_MSG_MOUSE;
+ m.mouse = t->mouse;
} else {
m.type = TUI_MSG_KEY;
m.key = k;
diff --git a/tui/tui.h b/tui/tui.h
index 9046620..90d569b 100644
--- a/tui/tui.h
+++ b/tui/tui.h
@@ -66,11 +66,19 @@ enum {
TUI_KEY_RESIZE,
TUI_KEY_TAB,
TUI_KEY_BACKTAB,
+ TUI_KEY_MOUSE,
};
#define TUI_MAX_W 500
#define TUI_MAX_H 200
+/** @brief One mouse event; coordinates are 0-based cells. */
+struct tui_mouse {
+ int x, y;
+ int button; /**< 0 left, 1 middle, 2 right, 64 wheel up, 65 wheel down */
+ bool release;
+};
+
struct tui {
int w, h;
int fd_in, fd_out;
@@ -83,6 +91,7 @@ struct tui {
int inlen;
char out[32768];
int olen;
+ struct tui_mouse mouse;
};
bool tui_alloc(struct tui *t, int w, int h);
@@ -109,12 +118,13 @@ int tui_key(struct tui *t, int timeout_ms);
/* ---- messages and the application loop ------------------------------- */
-enum tui_msg_type { TUI_MSG_KEY, TUI_MSG_RESIZE, TUI_MSG_TICK };
+enum tui_msg_type { TUI_MSG_KEY, TUI_MSG_RESIZE, TUI_MSG_TICK, TUI_MSG_MOUSE };
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 */
+ 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 */
};
struct tui_app {
@@ -173,6 +183,9 @@ bool tui_table_key(struct tui_table *tb, int key, int rows, int height);
int tui_tabs_draw(struct 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. */
+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);