1
0
Fork 0
forked from murx/murx

Added window names update for remote hosts.

This commit is contained in:
Johannes Findeisen 2026-11-15 01:27:55 +01:00
commit d584a2a309
2 changed files with 90 additions and 4 deletions

View file

@ -29,11 +29,54 @@ sent straight to the pane.
centred in its terminal, while a smaller client shows the top-left part. centred in its terminal, while a smaller client shows the top-left part.
`Ctrl-A R` forces the session back to the current client's terminal size. `Ctrl-A R` forces the session back to the current client's terminal size.
- Panes run `$SHELL` as a login shell with `TERM=screen-256color`. - Panes run `$SHELL` as a login shell with `TERM=screen-256color`.
- Window names follow the foreground program in their active pane, refreshed - Window names show `hostname:program` for the foreground program in their
active pane (for example `laptop:zsh` or `laptop:vim`), refreshed
every second, including in inactive windows. When a program exits, the name every second, including in inactive windows. When a program exits, the name
returns to the shell. For pipelines, the foreground process group leader returns to the shell. For pipelines, the foreground process group leader
supplies the name. Linux uses `/proc`; other systems use `ps`. supplies the name. Linux uses `/proc`; other systems use `ps`.
## SSH window names
While an SSH pane is active, the outer terminal's window title shows
`[moerx] remote-hostname:program`, for example `[moerx] server:vim`.
The moerx window list and pane title also show `server:vim`. Starting another
remote program updates the title, and returning to the prompt shows the
remote shell name, for example `[moerx] server:zsh`.
The remote shell must send an OSC 0 or OSC 2 terminal title in
that format: SSH itself does not report the remote foreground program.
Without a matching title, the window name remains `local-hostname:ssh`.
Names refresh within one second; disconnecting restores the local hostname
and shell name in the terminal title and window list.
For **zsh**, add this to `~/.zshrc` on the **remote host**:
```zsh
if [[ -n $SSH_CONNECTION ]]; then
autoload -Uz add-zsh-hook
moerx_ssh_title() {
local program=${1:t}
printf '\033]2;%s:%s\007' "${HOST%%.*}" "$program"
}
moerx_ssh_preexec() {
local -a words
words=(${(z)1})
moerx_ssh_title "${(Q)words[1]}"
}
moerx_ssh_precmd() { moerx_ssh_title zsh; }
add-zsh-hook preexec moerx_ssh_preexec
add-zsh-hook precmd moerx_ssh_precmd
fi
```
This reports the command launched by the shell (the first command for a
pipeline), then `hostname:zsh` at the prompt. moerx retains the latest
`hostname:program` for that SSH job when programs send other terminal titles,
so these cannot reset the window name to `local-hostname:ssh`.
Other shells can use equivalent
hooks sending `printf '\033]2;server:vim\007'`; use a hostname containing only
letters, digits, dots, underscores or hyphens, and a program basename without
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. Prefix is `Ctrl-A`. Every shortcut below is `Ctrl-A`, then the key(s) shown.
@ -91,7 +134,8 @@ Colours are written to the outer terminal in 256-colour form (`38;5;n` /
unless the outer terminal advertises `COLORTERM=truecolor` and is not unless the outer terminal advertises `COLORTERM=truecolor` and is not
screen/tmux/linux. screen/tmux/linux.
The title is set to `[moerx] <window name>`. The terminal window title is set to `[moerx] hostname:program`, following the
active moerx window. Hostnames use their short form (before the first dot).
## Deliberate limitations ## Deliberate limitations

46
moerx.c
View file

@ -311,6 +311,9 @@ struct Pane {
uint32_t u8_cp; uint32_t u8_cp;
char title[128]; char title[128];
pid_t title_pgrp; /* foreground job that supplied the OSC title */
char remote_title[128];
pid_t remote_title_pgrp;
int id; int id;
int tty_fd; int tty_fd;
@ -816,6 +819,8 @@ static void em_csi(Pane *p, const char *raw, int len, unsigned char final) {
} }
} }
static int pane_remote_title(Pane *p, pid_t pid);
static void em_osc_done(Pane *p) { static void em_osc_done(Pane *p) {
p->seq[p->seqlen] = '\0'; p->seq[p->seqlen] = '\0';
if (p->seqlen > 2 && (p->seq[0] == '0' || p->seq[0] == '2') && p->seq[1] == ';') { if (p->seqlen > 2 && (p->seq[0] == '0' || p->seq[0] == '2') && p->seq[1] == ';') {
@ -827,6 +832,14 @@ static void em_osc_done(Pane *p) {
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
if ((unsigned char)p->title[i] < 32) p->title[i] = ' '; if ((unsigned char)p->title[i] < 32) p->title[i] = ' ';
} }
p->title_pgrp = tcgetpgrp(p->fd);
if (p->title_pgrp <= 0) p->title_pgrp = tcgetpgrp(p->tty_fd);
/* Save structured remote names as they arrive, even when a program
immediately follows them with its own unstructured OSC title. */
if (p->title_pgrp > 0 && pane_remote_title(p, p->title_pgrp)) {
snprintf(p->remote_title, sizeof p->remote_title, "%s", p->title);
p->remote_title_pgrp = p->title_pgrp;
}
} }
p->seqlen = 0; p->seqlen = 0;
} }
@ -1060,8 +1073,25 @@ struct Session {
long long last_name_check; long long last_name_check;
}; };
/* Remote jobs are invisible locally. Accept host:program OSC titles from
the current ssh job; a previous job's title must not name a new connection. */
static int pane_remote_title(Pane *p, pid_t pid) {
if (p->title_pgrp != pid) return 0;
const char *colon = strchr(p->title, ':');
if (!colon || colon == p->title || !colon[1]) return 0;
for (const char *s = p->title; s < colon; s++) {
unsigned char c = (unsigned char)*s;
if (!isalnum(c) && c != '.' && c != '-' && c != '_') return 0;
}
for (const char *s = colon + 1; *s; s++) {
unsigned char c = (unsigned char)*s;
if (c <= 32 || c == 127 || c == '/' || c == ':') return 0;
}
return 1;
}
/* Use the active pane's foreground process group, so returning from a job /* Use the active pane's foreground process group, so returning from a job
restores the shell name. Titles set with OSC are independent of this. */ restores the shell name. */
static int window_update_name(Window *w) { static int window_update_name(Window *w) {
Pane *p = w->active; Pane *p = w->active;
if (!p || !p->alive) return 0; if (!p || !p->alive) return 0;
@ -1099,7 +1129,19 @@ static int window_update_name(Window *w) {
if ((unsigned char)*s < 32 || (unsigned char)*s == 127) *s = '?'; if ((unsigned char)*s < 32 || (unsigned char)*s == 127) *s = '?';
} }
char next[sizeof w->name]; char next[sizeof w->name];
snprintf(next, sizeof next, "%.*s", (int)sizeof next - 1, base); if (strcmp(base, "ssh") == 0 && p->remote_title_pgrp == pid && p->remote_title[0]) {
snprintf(next, sizeof next, "%s", p->remote_title);
} else {
char host[256];
if (gethostname(host, sizeof host) < 0) snprintf(host, sizeof host, "localhost");
host[sizeof host - 1] = '\0';
char *dot = strchr(host, '.');
if (dot) *dot = '\0';
for (char *s = host; *s; s++) {
if ((unsigned char)*s < 32 || (unsigned char)*s == 127) *s = '?';
}
snprintf(next, sizeof next, "%.63s:%.63s", host, base);
}
if (strcmp(w->name, next) == 0) return 0; if (strcmp(w->name, next) == 0) return 0;
snprintf(w->name, sizeof w->name, "%s", next); snprintf(w->name, sizeof w->name, "%s", next);
return 1; return 1;