1
0
Fork 0
forked from murx/murx

Add a table-driven main menu with live configuration

Ctrl-A m opens a boxed menu with window, pane, session and
configuration actions. Items live in static tables, so new entries
need no changes to drawing or key handling. Configuration pages set
status line position and background, the per-client prefix and the
display toggles at runtime; Ctrl-A w opens the window list in the
same style.
This commit is contained in:
Armin 2026-11-22 04:04:58 +01:00
commit a0279bd633
3 changed files with 633 additions and 103 deletions

View file

@ -65,8 +65,9 @@ and `--clock` to show the clock on the right. Use `--murx` to show `[murx]`
on the left when `--cwd` is not enabled; the directory takes precedence if on the left when `--cwd` is not enabled; the directory takes precedence if
both are supplied. Use `--hostname` to show the `hostname:` prefix in window both are supplied. Use `--hostname` to show the `hostname:` prefix in window
names and the outer terminal title. All four features are disabled by default. names and the outer terminal title. All four features are disabled by default.
These options apply when starting a new session and do not change an existing These options set the initial values for a new session; the
session. To restore the previous default display, use `Configuration...` menu can change them in a running session (see
[Menu](#menu)). To restore the previous default display, use
`murx --cwd --clock --hostname session`. `murx --cwd --clock --hostname session`.
## Upgrading from moerx ## Upgrading from moerx
@ -262,11 +263,15 @@ spaces or colons. With splits, the window follows its active pane.
## Keys ## Keys
Prefix is `Ctrl-A`. Every shortcut below is `Ctrl-A`, then the key(s) shown. The default prefix is `Ctrl-A`. Every shortcut below shows the default prefix;
if you chose a different prefix with `-p`, `--prefix`, or the Configuration
menu, press that prefix instead. Doubling the prefix switches to the last
window.
| Chain | Action | | Chain | Action |
| --- | --- | | --- | --- |
| `Ctrl-A a` | send a literal `Ctrl-A` to the pane | | `Ctrl-A m` | open the main menu |
| `Ctrl-A a` | send the prefix literally to the pane |
| `Ctrl-A c`, `Ctrl-A Ctrl-C` | new window | | `Ctrl-A c`, `Ctrl-A Ctrl-C` | new window |
| `Ctrl-A n`, `Ctrl-A Space`, `Ctrl-A Ctrl-N`, `Ctrl-A Ctrl-@` | next window | | `Ctrl-A n`, `Ctrl-A Space`, `Ctrl-A Ctrl-N`, `Ctrl-A Ctrl-@` | next window |
| `Ctrl-A p`, `Ctrl-A Ctrl-P`, `Ctrl-A Backspace` | previous window | | `Ctrl-A p`, `Ctrl-A Ctrl-P`, `Ctrl-A Backspace` | previous window |
@ -294,6 +299,34 @@ Two bindings worth noting: `Ctrl-A Ctrl-H`/`Ctrl-A Ctrl-L` resize the pane
(they do not switch windows), and `Ctrl-A k` selects the pane above while (they do not switch windows), and `Ctrl-A k` selects the pane above while
`Ctrl-A K` kills it. `Ctrl-A K` kills it.
## Menu
`Ctrl-A m` opens the main menu: a boxed overlay in murx blues, cyan and
yellow on black. Move with ↑/↓ (or `j`/`k`), press Enter to select, use
←/→ (or `h`/`l`) to enter or leave submenus, and press Escape, `q`, or
Ctrl-C to close. Long menus scroll when the terminal is short.
The main menu offers new window (tab), new pane (horizontal and vertical
splits), close pane/window, rename, the window list, pane selection and
resizing, and a `Session...` submenu with detach, lock, client list,
scrollback, forced resize and kill server. `Configuration...` contains:
- `Status line...` — position (top/bottom) and background (default, black
`000000`, grey `191a1c`).
- `Prefix...` — this client's Ctrl-key prefix, `a` through `z`.
- Toggles for the current directory, clock, `[murx]` label and hostname.
- `Reset display/status defaults`.
Settings changes apply immediately. Session settings affect all attached
clients; the prefix is per-client. Nothing is written to disk, so changes
last until the session ends or you change them again. The command-line
options still decide the initial values when a session starts.
`Ctrl-A w` still opens the window list directly, now in the same menu style;
Left returns from it to the main menu. The menu items live in one table near
the top of `murx.c`, so adding entries does not mean changing the drawing or
key-handling code.
## Scrollback ## Scrollback
Press `Ctrl-A`, then `Page Up` to scroll back in the active pane. Use Press `Ctrl-A`, then `Page Up` to scroll back in the active pane. Use
@ -341,6 +374,8 @@ Hard-coded:
`colour8`, drawn on the terminal's default background. Only splits have `colour8`, drawn on the terminal's default background. Only splits have
borders; panes have no title row, leaving more space for terminal content. borders; panes have no title row, leaving more space for terminal content.
- Messages use `fg=colour6,bg=colour8`. - Messages use `fg=colour6,bg=colour8`.
- Menus use a black background with a `colour12` frame, `colour11` titles and
selection, `colour14` accents and dim `colour8` hints and separators.
Use `-c RRGGBB` or `--color RRGGBB` to set the session's status bar Use `-c RRGGBB` or `--color RRGGBB` to set the session's status bar
background for all attached clients. Supply six hexadecimal digits, such as background for all attached clients. Supply six hexadecimal digits, such as

27
murx.1
View file

@ -81,8 +81,8 @@ Show the hostname: prefix in window names and the outer terminal title.
.BR \-h ", " \-\-help .BR \-h ", " \-\-help
Print usage and keyboard shortcuts, then exit. Print usage and keyboard shortcuts, then exit.
.PP .PP
These four display features are disabled by default. Their options apply only These four display features are disabled by default and set a new session's
when creating a session; they do not change a running server. values; the Configuration menu can change them in a running session.
To restore the previous default display, use To restore the previous default display, use
.BR "murx \-\-cwd \-\-clock \-\-hostname session" . .BR "murx \-\-cwd \-\-clock \-\-hostname session" .
.SH KEY BINDINGS .SH KEY BINDINGS
@ -95,9 +95,14 @@ other control-key bindings. With Ctrl-X as prefix, Ctrl-X a sends Ctrl-X,
Ctrl-X Ctrl-X switches windows and Ctrl-X x locks the client. Ctrl-X Ctrl-X switches windows and Ctrl-X x locks the client.
Other Ctrl-A input is passed to the pane when a different prefix is selected. Other Ctrl-A input is passed to the pane when a different prefix is selected.
.TP
.B Ctrl\-A m
open the main menu.
The menu has submenus for panes, sessions and configuration;
Ctrl\-A w opens the window list in the same style.
.TP .TP
.B Ctrl\-A a .B Ctrl\-A a
send a literal Ctrl\-A to the pane. send the configured prefix literally to the pane.
.TP .TP
.B Ctrl\-A c, Ctrl\-A Ctrl\-C .B Ctrl\-A c, Ctrl\-A Ctrl\-C
@ -195,8 +200,18 @@ Ctrl-A k selects the pane above; Ctrl-A K kills a pane.
Renaming a window is temporary until the next automatic name refresh. Renaming a window is temporary until the next automatic name refresh.
Ctrl-A r only displays a message: there is no configuration file to reload. Ctrl-A r only displays a message: there is no configuration file to reload.
.SS Menus, prompts and confirmations .SS Menus, prompts and confirmations
In menus use Up/Down or j/k and Enter; q or Escape closes the menu. The main menu offers new windows and panes, closing and renaming, the window
In prompts use Enter to accept, Escape to cancel and Ctrl-U to clear. list, pane selection and resizing, and Session and Configuration submenus.
Use Up/Down or j/k to move and Enter to select. Left/Right or h/l enter and
leave submenus; q, Escape, or Ctrl-C closes the menu. Long menus scroll.
The Configuration submenu sets the status line position and background
(default, black or grey), the client prefix, display toggles for the directory,
clock, [murx] label and hostname, and resets display/status defaults (display toggles, status position and
background). Session
settings affect all attached clients; the prefix is per-client. Changes take
effect immediately and are not saved to disk.
In prompts use Enter to accept, Escape or Ctrl-C to cancel, Backspace or Ctrl-H
to delete, and Ctrl-U to clear.
Confirm destructive actions with y, Y or Enter; n, N, Escape or Ctrl-C cancels. Confirm destructive actions with y, Y or Enter; n, N, Escape or Ctrl-C cancels.
The client lock is cosmetic: Enter unlocks it without a password. The client lock is cosmetic: Enter unlocks it without a password.
.SH SCROLLBACK .SH SCROLLBACK
@ -250,6 +265,8 @@ Over SSH the shell must report root status; install the hooks in root's shell
as well as the regular user's. This is a visual hint, not a security indicator. as well as the regular user's. This is a visual hint, not a security indicator.
Borders use colour8 on the terminal's default background. Borders use colour8 on the terminal's default background.
Messages use foreground colour6 and background colour8. Messages use foreground colour6 and background colour8.
Menus use a black background with a colour12 frame, colour11 titles and
selection, colour14 accents and dim colour8 separators and help text.
.PP .PP
UTF-8, wide characters, application cursor keys, 256 colors and alternate UTF-8, wide characters, application cursor keys, 256 colors and alternate
screens are supported. Output uses 256-color escapes, or truecolor when screens are supported. Output uses 256-color escapes, or truecolor when

650
murx.c
View file

@ -123,6 +123,15 @@ static int send_msg(int fd, uint32_t type, const void *data, size_t len);
#define P_MSG C_CYAN #define P_MSG C_CYAN
#define P_MSG_BG C_GREY #define P_MSG_BG C_GREY
/* Menu palette: murx blues/cyan/yellow on a hard black background. */
#define MENU_BG C_BLACK
#define MENU_FRAME C_BLUE
#define MENU_TITLE P_CURRENT
#define MENU_ITEM P_STATUS
#define MENU_SELECT P_CURRENT
#define MENU_ACCENT P_ACCENT
#define MENU_DIM C_GREY
#define DEFAULT_COLOR (-1) #define DEFAULT_COLOR (-1)
#define RGB_FLAG 0x01000000 #define RGB_FLAG 0x01000000
#define WIDE_CONT 0xFFFFFFFFu #define WIDE_CONT 0xFFFFFFFFu
@ -136,6 +145,7 @@ static int send_msg(int fd, uint32_t type, const void *data, size_t len);
#define STATUS_INTERVAL 1 #define STATUS_INTERVAL 1
#define SIZE_CHECK_MS 250 #define SIZE_CHECK_MS 250
#define NAME_CHECK_MS 1000 #define NAME_CHECK_MS 1000
#define MENU_MAX_DEPTH 8
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* small helpers */ /* small helpers */
@ -1241,8 +1251,16 @@ struct Client {
Scrollback *scrollbacks; Scrollback *scrollbacks;
/* Menu navigation. Pages and items live in a static table; only the
current page, selection and parent stack are per-client state. */
int menu_page;
int menu_top;
int menu_sel; int menu_sel;
int confirm_action; /* 1 kill pane, 2 kill server */ int menu_depth;
int menu_parent[MENU_MAX_DEPTH];
int menu_parent_sel[MENU_MAX_DEPTH];
int confirm_action; /* 1 kill pane, 2 kill server, 3 kill window */
char msg[256]; char msg[256];
long long msg_until; long long msg_until;
@ -1253,7 +1271,8 @@ struct Client {
Client *next; Client *next;
}; };
/* Display choices are set when a session starts. */ /* Display choices start from the session's command-line options and can be
* changed live from the Configuration menu. */
typedef struct { typedef struct {
int show_cwd; int show_cwd;
int show_clock; int show_clock;
@ -1802,14 +1821,17 @@ static int text_width(const char *s) {
return n; return n;
} }
static int draw_text(Grid *g, int x, int y, const char *s, int fg, int bg, int attr) { /* limit is the first column that must stay untouched (for clipping labels
* inside menu boxes). Passing INT_MAX gives the ordinary unbounded behaviour. */
static int draw_text_limited(Grid *g, int x, int y, const char *s, int fg, int bg,
int attr, int limit) {
const unsigned char *p = (const unsigned char *)s; const unsigned char *p = (const unsigned char *)s;
while (*p) { while (*p) {
uint32_t cp; uint32_t cp;
int used = utf8_decode(p, strlen((const char *)p), &cp); int used = utf8_decode(p, strlen((const char *)p), &cp);
int w = cp_width(cp); int w = cp_width(cp);
if (w < 0) w = 1; if (w < 0) w = 1;
if (x > INT_MAX - w) return INT_MAX; if (x > INT_MAX - w || x + w > limit) return x;
if (cp >= 32 && cp != 0x7f) scr_put_wide(g, x, y, cp, fg, bg, attr); if (cp >= 32 && cp != 0x7f) scr_put_wide(g, x, y, cp, fg, bg, attr);
x += w; x += w;
p += used; p += used;
@ -1817,6 +1839,10 @@ static int draw_text(Grid *g, int x, int y, const char *s, int fg, int bg, int a
return x; return x;
} }
static int draw_text(Grid *g, int x, int y, const char *s, int fg, int bg, int attr) {
return draw_text_limited(g, x, y, s, fg, bg, attr, INT_MAX);
}
/* Pane borders are thin box-drawing lines drawn over the terminal's own /* Pane borders are thin box-drawing lines drawn over the terminal's own
* background. Borders occupy only the separators between panes and are * background. Borders occupy only the separators between panes and are
* collected into a mask first so that crossing lines get the right junction * collected into a mask first so that crossing lines get the right junction
@ -2147,43 +2173,341 @@ static void status_draw(Grid *g, Session *s, Client *c) {
} }
} }
static void menu_draw(Grid *g, Session *s, Client *c) { /* ------------------------------------------------------------------ */
Window *list[256]; /* extensible menu */
int n = 0; /* ------------------------------------------------------------------ */
for (Window *w = s->windows; w && n < 256; w = w->next) list[n++] = w;
if (n == 0) return;
if (c->menu_sel < 0) c->menu_sel = 0;
if (c->menu_sel >= n) c->menu_sel = n - 1;
int width = 24; /* To add a menu entry, append an item to one of the static tables below.
for (int i = 0; i < n; i++) { * New submenus need a page in menu_pages; new commands need a case in
int wl = text_width(list[i]->name) + 8; * menu_action(). Drawing, navigation and key handling are table-driven. */
enum {
MENU_MAIN = 0,
MENU_WINDOWS, /* dynamic: one item per window */
MENU_SELECT_PANE,
MENU_RESIZE_PANE,
MENU_SESSION,
MENU_CONFIG,
MENU_CONFIG_STATUS,
MENU_CONFIG_PREFIX,
MENU_COUNT
};
enum { MI_ACTION, MI_SUBMENU, MI_TOGGLE, MI_CHOICE, MI_SEPARATOR };
#define MIF_NONE 0
#define MIF_PANES 0x01 /* only meaningful with more than one pane */
enum {
ACT_NONE = 0,
ACT_NEW_WINDOW,
ACT_SPLIT_ROWS,
ACT_SPLIT_COLS,
ACT_CLOSE_PANE,
ACT_CLOSE_WINDOW,
ACT_RENAME_WINDOW,
ACT_PANE_LEFT,
ACT_PANE_DOWN,
ACT_PANE_UP,
ACT_PANE_RIGHT,
ACT_PANE_NEXT,
ACT_PANE_PREV,
ACT_RESIZE_LEFT,
ACT_RESIZE_DOWN,
ACT_RESIZE_UP,
ACT_RESIZE_RIGHT,
ACT_SCROLLBACK,
ACT_DETACH,
ACT_LOCK,
ACT_CLIENTS,
ACT_FIT_SIZE,
ACT_KILL_SERVER,
ACT_RESET_DISPLAY
};
enum {
SET_NONE = 0,
SET_STATUS_BOTTOM,
SET_STATUS_BG,
SET_SHOW_CWD,
SET_SHOW_CLOCK,
SET_SHOW_MURX,
SET_SHOW_HOSTNAME,
SET_PREFIX
};
typedef struct {
const char *label;
int type; /* MI_* */
int id; /* ACT_*, MENU_* or SET_* */
int arg; /* setting value */
int flags; /* MIF_* */
} MenuItem;
typedef struct {
const char *title;
const MenuItem *items;
int count;
} MenuPage;
#define MENU_ITEMS(a) ((int)(sizeof(a) / sizeof((a)[0])))
#define MENU_SEP { "", MI_SEPARATOR, 0, 0, 0 }
static const MenuItem menu_main_items[] = {
{ "New window (tab)", MI_ACTION, ACT_NEW_WINDOW, 0, 0 },
{ "New pane (h. split)", MI_ACTION, ACT_SPLIT_ROWS, 0, 0 },
{ "New pane (v. split)", MI_ACTION, ACT_SPLIT_COLS, 0, 0 },
MENU_SEP,
{ "Close pane", MI_ACTION, ACT_CLOSE_PANE, 0, 0 },
{ "Close window (tab)", MI_ACTION, ACT_CLOSE_WINDOW, 0, 0 },
{ "Rename window", MI_ACTION, ACT_RENAME_WINDOW, 0, 0 },
MENU_SEP,
{ "Windows...", MI_SUBMENU, MENU_WINDOWS, 0, 0 },
{ "Select pane...", MI_SUBMENU, MENU_SELECT_PANE, 0, MIF_PANES },
{ "Resize pane...", MI_SUBMENU, MENU_RESIZE_PANE, 0, MIF_PANES },
MENU_SEP,
{ "Session...", MI_SUBMENU, MENU_SESSION, 0, 0 },
{ "Configuration...", MI_SUBMENU, MENU_CONFIG, 0, 0 }
};
static const MenuItem menu_select_items[] = {
{ "Pane left", MI_ACTION, ACT_PANE_LEFT, 0, MIF_PANES },
{ "Pane down", MI_ACTION, ACT_PANE_DOWN, 0, MIF_PANES },
{ "Pane up", MI_ACTION, ACT_PANE_UP, 0, MIF_PANES },
{ "Pane right", MI_ACTION, ACT_PANE_RIGHT, 0, MIF_PANES },
MENU_SEP,
{ "Next pane", MI_ACTION, ACT_PANE_NEXT, 0, MIF_PANES },
{ "Previous pane", MI_ACTION, ACT_PANE_PREV, 0, MIF_PANES }
};
static const MenuItem menu_resize_items[] = {
{ "Resize left", MI_ACTION, ACT_RESIZE_LEFT, 0, MIF_PANES },
{ "Resize right", MI_ACTION, ACT_RESIZE_RIGHT, 0, MIF_PANES },
MENU_SEP,
{ "Resize up", MI_ACTION, ACT_RESIZE_UP, 0, MIF_PANES },
{ "Resize down", MI_ACTION, ACT_RESIZE_DOWN, 0, MIF_PANES }
};
static const MenuItem menu_session_items[] = {
{ "Detach", MI_ACTION, ACT_DETACH, 0, 0 },
{ "Lock client", MI_ACTION, ACT_LOCK, 0, 0 },
{ "List attached clients", MI_ACTION, ACT_CLIENTS, 0, 0 },
{ "Resize session to terminal", MI_ACTION, ACT_FIT_SIZE, 0, 0 },
MENU_SEP,
{ "Enter scrollback", MI_ACTION, ACT_SCROLLBACK, 0, 0 },
MENU_SEP,
{ "Kill server", MI_ACTION, ACT_KILL_SERVER, 0, 0 }
};
static const MenuItem menu_config_items[] = {
{ "Status line...", MI_SUBMENU, MENU_CONFIG_STATUS, 0, 0 },
{ "Prefix...", MI_SUBMENU, MENU_CONFIG_PREFIX, 0, 0 },
MENU_SEP,
{ "Show current directory", MI_TOGGLE, SET_SHOW_CWD, 1, 0 },
{ "Show clock", MI_TOGGLE, SET_SHOW_CLOCK, 1, 0 },
{ "Show [murx] label", MI_TOGGLE, SET_SHOW_MURX, 1, 0 },
{ "Show hostname", MI_TOGGLE, SET_SHOW_HOSTNAME, 1, 0 },
MENU_SEP,
{ "Reset display/status defaults", MI_ACTION, ACT_RESET_DISPLAY, 0, 0 }
};
static const MenuItem menu_config_status_items[] = {
{ "Position: top", MI_CHOICE, SET_STATUS_BOTTOM, 0, 0 },
{ "Position: bottom", MI_CHOICE, SET_STATUS_BOTTOM, 1, 0 },
MENU_SEP,
{ "Background: default", MI_CHOICE, SET_STATUS_BG, 0, 0 },
{ "Background: black", MI_CHOICE, SET_STATUS_BG, 1, 0 },
{ "Background: grey", MI_CHOICE, SET_STATUS_BG, 2, 0 }
};
#define PREFIX_ITEM(ch) { "Ctrl-" #ch, MI_CHOICE, SET_PREFIX, (#ch)[0] - 'a' + 1, 0 }
static const MenuItem menu_config_prefix_items[] = {
PREFIX_ITEM(a), PREFIX_ITEM(b), PREFIX_ITEM(c), PREFIX_ITEM(d),
PREFIX_ITEM(e), PREFIX_ITEM(f), PREFIX_ITEM(g), PREFIX_ITEM(h),
PREFIX_ITEM(i), PREFIX_ITEM(j), PREFIX_ITEM(k), PREFIX_ITEM(l),
PREFIX_ITEM(m), PREFIX_ITEM(n), PREFIX_ITEM(o), PREFIX_ITEM(p),
PREFIX_ITEM(q), PREFIX_ITEM(r), PREFIX_ITEM(s), PREFIX_ITEM(t),
PREFIX_ITEM(u), PREFIX_ITEM(v), PREFIX_ITEM(w), PREFIX_ITEM(x),
PREFIX_ITEM(y), PREFIX_ITEM(z)
};
#undef PREFIX_ITEM
static const MenuPage menu_pages[MENU_COUNT] = {
[MENU_MAIN] = { "murx", menu_main_items, MENU_ITEMS(menu_main_items) },
[MENU_WINDOWS] = { "Windows", NULL, 0 },
[MENU_SELECT_PANE] = { "Select pane", menu_select_items, MENU_ITEMS(menu_select_items) },
[MENU_RESIZE_PANE] = { "Resize pane", menu_resize_items, MENU_ITEMS(menu_resize_items) },
[MENU_SESSION] = { "Session", menu_session_items, MENU_ITEMS(menu_session_items) },
[MENU_CONFIG] = { "Configuration", menu_config_items, MENU_ITEMS(menu_config_items) },
[MENU_CONFIG_STATUS] = { "Status line", menu_config_status_items, MENU_ITEMS(menu_config_status_items) },
[MENU_CONFIG_PREFIX] = { "Client prefix", menu_config_prefix_items, MENU_ITEMS(menu_config_prefix_items) }
};
static const MenuPage *menu_page_of(Client *c) {
if (c->menu_page < 0 || c->menu_page >= MENU_COUNT) return NULL;
return &menu_pages[c->menu_page];
}
static int menu_window_count(Session *s) {
int n = 0;
for (Window *w = s->windows; w; w = w->next) n++;
return n;
}
static int menu_count(Client *c, Session *s) {
if (c->menu_page == MENU_WINDOWS) return menu_window_count(s);
const MenuPage *page = menu_page_of(c);
return page ? page->count : 0;
}
static const MenuItem *menu_item_at(Client *c, int i) {
const MenuPage *page = menu_page_of(c);
if (!page || c->menu_page == MENU_WINDOWS || i < 0 || i >= page->count) return NULL;
return &page->items[i];
}
static const char *menu_item_label(Client *c, Session *s, int i) {
if (c->menu_page == MENU_WINDOWS) {
/* Single-threaded server: a scratch buffer is safe for drawing. */
static char label[192];
int k = 0;
for (Window *w = s->windows; w; w = w->next, k++) {
if (k == i) {
snprintf(label, sizeof label, "%d: %s", w->index, w->name);
return label;
}
}
return "";
}
const MenuItem *it = menu_item_at(c, i);
return it ? it->label : "";
}
static int menu_item_enabled(Session *s, const MenuItem *it) {
if (!it || it->type == MI_SEPARATOR) return 0;
if (!(it->flags & MIF_PANES)) return 1;
Window *w = s->cur;
return w && w->root && layout_pane_count(w->root) >= 2;
}
static int menu_setting_on(Session *s, Client *c, const MenuItem *it) {
switch (it->id) {
case SET_STATUS_BOTTOM: return (s->status_bottom ? 1 : 0) == it->arg;
case SET_STATUS_BG: {
int32_t want = it->arg == 1 ? (int32_t)RGB_FLAG :
it->arg == 2 ? (int32_t)(RGB_FLAG | 0x191a1c) : 0;
return s->status_bg == want;
}
case SET_SHOW_CWD: return s->display.show_cwd == it->arg;
case SET_SHOW_CLOCK: return s->display.show_clock == it->arg;
case SET_SHOW_MURX: return s->display.show_murx == it->arg;
case SET_SHOW_HOSTNAME: return s->display.show_hostname == it->arg;
case SET_PREFIX: return c->prefix == it->arg;
default: return 0;
}
}
static void menu_draw(Grid *g, Session *s, Client *c) {
const MenuPage *page = menu_page_of(c);
if (!page) { c->mode = 0; return; }
int count = menu_count(c, s);
if (count < 1) { c->mode = 0; return; }
if (c->menu_sel < 0) c->menu_sel = 0;
if (c->menu_sel >= count) c->menu_sel = count - 1;
int bottom = s->h - (s->status_bottom ? 1 : 0);
if (bottom < 1) bottom = 1;
const char *hint_text = "Up/Down move Enter select Esc close";
int hint = bottom >= 6;
int maxrows = bottom - 2 - (hint ? 2 : 0);
if (maxrows < 1) maxrows = 1;
if (c->menu_top < 0) c->menu_top = 0;
if (c->menu_top > count - maxrows) c->menu_top = count - maxrows;
if (c->menu_top < 0) c->menu_top = 0;
if (c->menu_sel < c->menu_top) c->menu_top = c->menu_sel;
if (c->menu_sel >= c->menu_top + maxrows) c->menu_top = c->menu_sel - maxrows + 1;
int shown = count - c->menu_top;
if (shown > maxrows) shown = maxrows;
if (shown < 1) shown = 1;
int width = text_width(page->title) + 8;
for (int i = 0; i < count; i++) {
int wl = text_width(menu_item_label(c, s, i)) + 10;
if (wl > width) width = wl; if (wl > width) width = wl;
} }
if (width > s->w - 2) width = s->w - 2; if (hint) {
if (width < 10) width = 10; int hl = text_width(hint_text) + 4;
int x0 = 1, y0 = 1; if (hl > width) width = hl;
int h = n + 2;
int bottom = s->h - (s->status_bottom ? 1 : 0);
if (y0 + h > bottom) h = bottom - y0;
for (int y = y0; y < y0 + h; y++) {
for (int x = x0; x < x0 + width; x++) {
scr_put(g, x, y, ' ', C_BLUE, C_GREY, 0);
} }
if (width > s->w) width = s->w;
if (width < 8) width = s->w < 8 ? s->w : 8;
int height = shown + 2 + (hint ? 2 : 0);
int x0 = (s->w - width) / 2;
if (x0 < 0) x0 = 0;
int y0 = (bottom - height) / 2;
if (y0 < 0) y0 = 0;
int x1 = x0 + width - 1;
for (int y = y0; y < y0 + height; y++) {
for (int x = x0; x <= x1; x++) scr_put(g, x, y, ' ', MENU_ITEM, MENU_BG, 0);
} }
int rows = h - 2;
for (int i = 0; i < rows && i < n; i++) { scr_put(g, x0, y0, 0x256D, MENU_FRAME, MENU_BG, 0); /* ╭ */
char line[320]; scr_put(g, x1, y0, 0x256E, MENU_FRAME, MENU_BG, 0); /* ╮ */
snprintf(line, sizeof line, " %d: %s ", list[i]->index, list[i]->name); for (int x = x0 + 1; x < x1; x++) scr_put(g, x, y0, 0x2500, MENU_FRAME, MENU_BG, 0);
int sel = (i == c->menu_sel); char title[160];
int fg = sel ? C_BLACK : C_BLUE; snprintf(title, sizeof title, " %s ", page->title);
int bg = sel ? C_BLUE : C_GREY; draw_text_limited(g, x0 + 2, y0, title, MENU_TITLE, MENU_BG, A_BOLD, x1);
int x = x0;
for (const char *p = line; *p && x < x0 + width; p++) { for (int i = 0; i < shown; i++) {
scr_put(g, x++, y0 + 1 + i, (unsigned char)*p, fg, bg, 0); int idx = c->menu_top + i;
int y = y0 + 1 + i;
const MenuItem *it = menu_item_at(c, idx);
int selected = idx == c->menu_sel;
if (it && it->type == MI_SEPARATOR) {
scr_put(g, x0, y, 0x251C, MENU_FRAME, MENU_BG, 0);
scr_put(g, x1, y, 0x2524, MENU_FRAME, MENU_BG, 0);
for (int x = x0 + 1; x < x1; x++)
scr_put(g, x, y, 0x2500, MENU_DIM, MENU_BG, A_DIM);
continue;
} }
while (x < x0 + width) scr_put(g, x++, y0 + 1 + i, ' ', fg, bg, 0); scr_put(g, x0, y, 0x2502, MENU_FRAME, MENU_BG, 0);
scr_put(g, x1, y, 0x2502, MENU_FRAME, MENU_BG, 0);
int enabled = menu_item_enabled(s, it);
int fg = !enabled ? MENU_DIM : selected ? MENU_SELECT : MENU_ITEM;
int attr = !enabled ? A_DIM : selected ? A_BOLD : 0;
scr_put(g, x0 + 2, y, selected ? '>' : ' ', MENU_ACCENT, MENU_BG, A_BOLD);
draw_text_limited(g, x0 + 4, y, menu_item_label(c, s, idx),
fg, MENU_BG, attr, x1 - 5);
if (it && (it->type == MI_TOGGLE || it->type == MI_CHOICE)) {
int on = menu_setting_on(s, c, it);
draw_text_limited(g, x1 - 4, y, on ? "[x]" : "[ ]",
on ? MENU_ACCENT : MENU_DIM, MENU_BG, 0, x1);
} else if (it && it->type == MI_SUBMENU) {
scr_put(g, x1 - 2, y, 0x25B8, MENU_ACCENT, MENU_BG, 0); /* ▸ */
} }
if (c->menu_top > 0 && i == 0)
scr_put(g, x1 - 1, y, 0x25B2, MENU_ACCENT, MENU_BG, 0); /* ▲ */
else if (c->menu_top + shown < count && i == shown - 1)
scr_put(g, x1 - 1, y, 0x25BC, MENU_ACCENT, MENU_BG, 0); /* ▼ */
}
if (hint) {
int y = y0 + 1 + shown;
scr_put(g, x0, y, 0x251C, MENU_FRAME, MENU_BG, 0);
scr_put(g, x1, y, 0x2524, MENU_FRAME, MENU_BG, 0);
for (int x = x0 + 1; x < x1; x++) scr_put(g, x, y, 0x2500, MENU_FRAME, MENU_BG, 0);
int hx = x0 + (width - text_width(hint_text)) / 2;
scr_put(g, x0, y + 1, 0x2502, MENU_FRAME, MENU_BG, 0);
scr_put(g, x1, y + 1, 0x2502, MENU_FRAME, MENU_BG, 0);
draw_text_limited(g, hx, y + 1, hint_text, MENU_DIM, MENU_BG, A_DIM, x1 - 1);
}
int by = y0 + height - 1;
scr_put(g, x0, by, 0x2570, MENU_FRAME, MENU_BG, 0); /* ╰ */
scr_put(g, x1, by, 0x256F, MENU_FRAME, MENU_BG, 0); /* ╯ */
for (int x = x0 + 1; x < x1; x++) scr_put(g, x, by, 0x2500, MENU_FRAME, MENU_BG, 0);
} }
static void compose(Session *s, Client *c, Grid *out) { static void compose(Session *s, Client *c, Grid *out) {
@ -2915,14 +3239,50 @@ static void client_unlock(Client *c) {
client_invalidate(c); client_invalidate(c);
} }
static void start_menu(Client *c, Session *s) { static void menu_open_page(Client *c, int page, int sel) {
c->mode = 2; c->mode = 2;
c->menu_sel = 0; c->menu_page = page;
int i = 0; c->menu_sel = sel < 0 ? 0 : sel;
for (Window *w = s->windows; w; w = w->next) { c->menu_top = 0;
if (w == s->cur) c->menu_sel = i; c->menu_depth = 0;
i++; }
static void menu_open(Client *c) {
menu_open_page(c, MENU_MAIN, 0);
}
/* Ctrl-A w still opens the window list directly; Left returns to the main
* menu, Escape closes it. */
static void start_menu(Client *c, Session *s) {
int sel = 0, i = 0;
for (Window *w = s->windows; w; w = w->next, i++) {
if (w == s->cur) sel = i;
} }
menu_open_page(c, MENU_WINDOWS, sel);
c->menu_parent[0] = MENU_MAIN;
c->menu_parent_sel[0] = 0;
c->menu_depth = 1;
}
static void menu_enter(Client *c, int page) {
if (c->menu_depth >= MENU_MAX_DEPTH || page < 0 || page >= MENU_COUNT) return;
c->menu_parent[c->menu_depth] = c->menu_page;
c->menu_parent_sel[c->menu_depth] = c->menu_sel;
c->menu_depth++;
c->menu_page = page;
c->menu_sel = 0;
c->menu_top = 0;
}
static void menu_back(Client *c) {
if (c->menu_depth <= 0) {
c->mode = 0;
return;
}
c->menu_depth--;
c->menu_page = c->menu_parent[c->menu_depth];
c->menu_sel = c->menu_parent_sel[c->menu_depth];
c->menu_top = 0;
} }
static void start_rename_prompt(Client *c, Session *s) { static void start_rename_prompt(Client *c, Session *s) {
@ -3003,6 +3363,111 @@ static void backscroll_start(Client *c, Session *s, int page_up) {
if (view->offset > count) view->offset = count; if (view->offset > count) view->offset = count;
} }
static void report_clients(Client *c, Session *s) {
int n = 0;
for (Client *it = s->clients; it; it = it->next) if (it->attached) n++;
client_message(c, "%d client%s attached", n, n == 1 ? "" : "s");
}
static void fit_session_to_client(Client *c, Session *s) {
/* Make this client's terminal the size source again, even if another
client attached or resized more recently. */
c->order = client_next_order(s);
session_recalc_size(s);
invalidate_clients(s);
s->redraw = 1;
client_message(c, "Session resized to %dx%d", s->w, s->h);
}
static void menu_apply_setting(Client *c, Session *s, const MenuItem *it) {
switch (it->id) {
case SET_STATUS_BOTTOM:
s->status_bottom = it->arg ? 1 : 0;
session_layout(s);
s->redraw = 1;
break;
case SET_STATUS_BG:
s->status_bg = it->arg == 1 ? (int32_t)RGB_FLAG :
it->arg == 2 ? (int32_t)(RGB_FLAG | 0x191a1c) : 0;
s->redraw = 1;
break;
case SET_SHOW_CWD: s->display.show_cwd = it->arg ? 1 : 0; break;
case SET_SHOW_CLOCK: s->display.show_clock = it->arg ? 1 : 0; break;
case SET_SHOW_MURX: s->display.show_murx = it->arg ? 1 : 0; break;
case SET_SHOW_HOSTNAME: s->display.show_hostname = it->arg ? 1 : 0; break;
case SET_PREFIX: c->prefix = (unsigned char)it->arg; break;
default: return;
}
/* Window names carry the hostname setting, so refresh them at once. */
s->last_name_check = 0;
invalidate_clients(s);
}
static void menu_action(Client *c, Session *s, int action) {
c->mode = 0;
switch (action) {
case ACT_NEW_WINDOW: new_window(s); break;
case ACT_SPLIT_ROWS: split_pane(s, L_ROWS); break;
case ACT_SPLIT_COLS: split_pane(s, L_COLS); break;
case ACT_CLOSE_PANE: start_confirm(c, 1, "kill-pane? (y/n) "); break;
case ACT_CLOSE_WINDOW: start_confirm(c, 3, "kill-window? (y/n) "); break;
case ACT_RENAME_WINDOW: start_rename_prompt(c, s); break;
case ACT_PANE_LEFT: select_pane_dir(s, -1, 0); break;
case ACT_PANE_DOWN: select_pane_dir(s, 0, 1); break;
case ACT_PANE_UP: select_pane_dir(s, 0, -1); break;
case ACT_PANE_RIGHT: select_pane_dir(s, 1, 0); break;
case ACT_PANE_NEXT: cycle_pane(s, 1); break;
case ACT_PANE_PREV: cycle_pane(s, -1); break;
case ACT_RESIZE_LEFT: resize_pane_dir(s, -1, 0, 8); break;
case ACT_RESIZE_RIGHT: resize_pane_dir(s, 1, 0, 8); break;
case ACT_RESIZE_UP: resize_pane_dir(s, 0, -1, 5); break;
case ACT_RESIZE_DOWN: resize_pane_dir(s, 0, 1, 5); break;
case ACT_SCROLLBACK: backscroll_start(c, s, 0); break;
case ACT_DETACH: {
char m[128];
snprintf(m, sizeof m, "[detached (from session %s)]", s->name);
client_detach(c, m);
break;
}
case ACT_LOCK: client_lock(c); break;
case ACT_CLIENTS: report_clients(c, s); break;
case ACT_FIT_SIZE: fit_session_to_client(c, s); break;
case ACT_KILL_SERVER: start_confirm(c, 2, "kill-server? (y/n) "); break;
case ACT_RESET_DISPLAY:
memset(&s->display, 0, sizeof s->display);
s->status_bottom = 0;
s->status_bg = 0;
session_layout(s);
s->last_name_check = 0;
s->redraw = 1;
invalidate_clients(s);
client_message(c, "Display and status defaults restored");
break;
default: break;
}
}
static void menu_activate(Client *c, Session *s) {
int count = menu_count(c, s);
if (c->menu_sel < 0 || c->menu_sel >= count) return;
if (c->menu_page == MENU_WINDOWS) {
int i = 0;
for (Window *w = s->windows; w; w = w->next, i++) {
if (i == c->menu_sel) {
select_window(s, w);
break;
}
}
c->mode = 0;
return;
}
const MenuItem *it = menu_item_at(c, c->menu_sel);
if (!it || it->type == MI_SEPARATOR || !menu_item_enabled(s, it)) return;
if (it->type == MI_ACTION) menu_action(c, s, it->id);
else if (it->type == MI_SUBMENU) menu_enter(c, it->id);
else menu_apply_setting(c, s, it);
}
/* Return zero for an incomplete page-key sequence. */ /* Return zero for an incomplete page-key sequence. */
static size_t backscroll_key(Client *c, Scrollback *view, const unsigned char *buf, size_t n) { static size_t backscroll_key(Client *c, Scrollback *view, const unsigned char *buf, size_t n) {
if (buf[0] == 27 && n == 1) return 0; if (buf[0] == 27 && n == 1) return 0;
@ -3067,12 +3532,12 @@ static void prefix_command(Client *c, Session *s, int key) {
client_detach(c, m); client_detach(c, m);
break; break;
} }
case '*': { case 'm':
int n = 0; menu_open(c);
for (Client *it = s->clients; it; it = it->next) if (it->attached) n++; break;
client_message(c, "%d client%s attached", n, n == 1 ? "" : "s"); case '*':
report_clients(c, s);
break; break;
}
case 0x00: case 0x00:
case 0x0e: case 0x0e:
case ' ': case ' ':
@ -3128,13 +3593,7 @@ static void prefix_command(Client *c, Session *s, int key) {
client_message(c, "Configuration Reloaded!"); client_message(c, "Configuration Reloaded!");
break; break;
case 'R': case 'R':
/* Make this client's terminal the size source again, even if another fit_session_to_client(c, s);
client attached or resized more recently. */
c->order = client_next_order(s);
session_recalc_size(s);
invalidate_clients(s);
s->redraw = 1;
client_message(c, "Session resized to %dx%d", s->w, s->h);
break; break;
case '-': case '-':
split_pane(s, L_ROWS); split_pane(s, L_ROWS);
@ -3257,57 +3716,68 @@ static void confirm_key(Client *c, Session *s, unsigned char b) {
} }
} }
/* Move the selection, skipping separators and entries that cannot be used
* in the current state (for example pane commands with a single pane). */
static void menu_move(Client *c, Session *s, int dir) {
int count = menu_count(c, s);
if (count < 1) return;
for (int step = 0; step < count; step++) {
c->menu_sel += dir;
if (c->menu_sel < 0) c->menu_sel = count - 1;
else if (c->menu_sel >= count) c->menu_sel = 0;
const MenuItem *it = menu_item_at(c, c->menu_sel);
if (c->menu_page == MENU_WINDOWS || (it && menu_item_enabled(s, it))) return;
}
}
static void menu_key(Client *c, Session *s, const unsigned char *buf, size_t n, size_t *used) { static void menu_key(Client *c, Session *s, const unsigned char *buf, size_t n, size_t *used) {
*used = 1; *used = 1;
unsigned char b = buf[0]; if (menu_count(c, s) < 1) {
int count = 0;
for (Window *w = s->windows; w; w = w->next) count++;
if (count < 1) {
c->mode = 0; c->mode = 0;
return; return;
} }
unsigned char b = buf[0];
if (b == 0x1b) { int key = b;
int key = 0;
if (b == 0x1b) { if (b == 0x1b) {
if (n < 2) { *used = 0; return; } if (n < 2) { *used = 0; return; }
if (buf[1] == '[' || buf[1] == 'O') { if (buf[1] == '[' || buf[1] == 'O') {
if (n < 3) { *used = 0; return; } if (n < 3) { *used = 0; return; }
if (buf[2] == 'A') key = K_UP; if (buf[2] == 'A') key = K_UP;
else if (buf[2] == 'B') key = K_DOWN; else if (buf[2] == 'B') key = K_DOWN;
else if (buf[2] == 'C') key = K_RIGHT;
else if (buf[2] == 'D') key = K_LEFT;
else key = 0;
*used = 3; *used = 3;
if (!key) { c->mode = 0; return; } if (!key) return;
} else { } else {
c->mode = 0; c->mode = 0;
*used = 1;
return; return;
} }
} else {
key = b;
} }
if (key == K_UP) {
c->menu_sel--; switch (key) {
if (c->menu_sel < 0) c->menu_sel = count - 1; case K_UP: case 'k':
} else { menu_move(c, s, -1);
c->menu_sel++; break;
if (c->menu_sel >= count) c->menu_sel = 0; case K_DOWN: case 'j':
} menu_move(c, s, 1);
return; break;
} case K_LEFT: case 'h':
if (b == 'k') { c->menu_sel = (c->menu_sel + count - 1) % count; return; } menu_back(c);
if (b == 'j') { c->menu_sel = (c->menu_sel + 1) % count; return; } break;
if (b == 'q') { c->mode = 0; return; } case K_RIGHT: case 'l': {
if (b == '\r' || b == '\n') { const MenuItem *it = menu_item_at(c, c->menu_sel);
int i = 0; if (it && it->type == MI_SUBMENU && menu_item_enabled(s, it)) menu_enter(c, it->id);
for (Window *w = s->windows; w; w = w->next) {
if (i == c->menu_sel) {
select_window(s, w);
break; break;
} }
i++; case '\r': case '\n':
} menu_activate(c, s);
break;
case 'q': case 0x03:
c->mode = 0; c->mode = 0;
return; break;
default:
break;
} }
} }
@ -3317,6 +3787,7 @@ static void client_feed(Client *c, Session *s, const unsigned char *buf, size_t
now_ms() - c->escape_since >= 100) { now_ms() - c->escape_since >= 100) {
Scrollback *view = scrollback_current(c, s); Scrollback *view = scrollback_current(c, s);
if (view) scrollback_remove(scrollback_slot(c, view->pane)); if (view) scrollback_remove(scrollback_slot(c, view->pane));
if (c->mode == 2) c->mode = 0; /* a lone Escape closes menus */
c->prefix_pending = 0; c->prefix_pending = 0;
c->inlen = 0; c->inlen = 0;
c->escape_since = 0; c->escape_since = 0;
@ -3393,7 +3864,8 @@ static void client_feed(Client *c, Session *s, const unsigned char *buf, size_t
memmove(c->in, c->in + i, c->inlen - i); memmove(c->in, c->in + i, c->inlen - i);
c->inlen -= i; c->inlen -= i;
} }
if (c->inlen == 1 && c->in[0] == 27 && (scrollback_current(c, s) || c->prefix_pending)) { if (c->inlen == 1 && c->in[0] == 27 &&
(c->mode == 2 || scrollback_current(c, s) || c->prefix_pending)) {
if (!c->escape_since) c->escape_since = now_ms(); if (!c->escape_since) c->escape_since = now_ms();
} else c->escape_since = 0; } else c->escape_since = 0;
} }
@ -4350,7 +4822,8 @@ static void print_help(FILE *out) {
"Use --clock to show the clock in the status bar.\n" "Use --clock to show the clock in the status bar.\n"
"Use --murx to show [murx] in the status bar when --cwd is not enabled.\n" "Use --murx to show [murx] in the status bar when --cwd is not enabled.\n"
"Use --hostname to show the hostname: prefix in window titles.\n" "Use --hostname to show the hostname: prefix in window titles.\n"
"These four features default to off and apply only to new sessions.\n" "These four features default to off and set a new session's values;\n"
"the Configuration menu can change them in a running session.\n"
"\n" "\n"
"Use -p x (or --prefix x) to choose Ctrl-X as this client's prefix.\n" "Use -p x (or --prefix x) to choose Ctrl-X as this client's prefix.\n"
"Letters a through z are supported (case-insensitive); the default is a (Ctrl-A).\n" "Letters a through z are supported (case-insensitive); the default is a (Ctrl-A).\n"
@ -4358,6 +4831,9 @@ static void print_help(FILE *out) {
"your chosen prefix, including both keys in the last-window shortcut.\n" "your chosen prefix, including both keys in the last-window shortcut.\n"
"Prefix then a sends the chosen prefix literally to the pane.\n" "Prefix then a sends the chosen prefix literally to the pane.\n"
"\n" "\n"
"Menu\n"
" Ctrl-A m open the main menu\n"
"\n"
"Windows\n" "Windows\n"
" Ctrl-A c new window\n" " Ctrl-A c new window\n"
" Ctrl-A Ctrl-C new window\n" " Ctrl-A Ctrl-C new window\n"
@ -4407,7 +4883,9 @@ static void print_help(FILE *out) {
" Ctrl-A Ctrl-D detach\n" " Ctrl-A Ctrl-D detach\n"
" Ctrl-A \\ kill server (confirm)\n" " Ctrl-A \\ kill server (confirm)\n"
"\n" "\n"
"Inside menus use Up/Down or j/k and Enter; q or Escape closes.\n" "Inside menus use Up/Down or j/k, Left/Right or h/l for submenus,\n"
"Enter to select and q or Escape to close. The Configuration menu holds\n"
"status line, prefix and display settings.\n"
"Inside prompts use Enter to accept, Escape to cancel, Ctrl-U to clear.\n"); "Inside prompts use Enter to accept, Escape to cancel, Ctrl-U to clear.\n");
} }