1
0
Fork 0
forked from murx/murx

Show remote path in the status line when it is reported.

This commit is contained in:
Johannes Findeisen 2026-11-16 00:15:00 +01:00
commit 23b78850da
2 changed files with 74 additions and 10 deletions

View file

@ -66,11 +66,11 @@ older versions once before using this option.
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 ## SSH window names and directories
While an SSH pane is active, the outer terminal's window title shows While an SSH pane is active, the outer terminal's window title shows
`[moerx] remote-hostname:program`, for example `[moerx] server:vim`. `[moerx] remote-hostname:program`, for example `[moerx] server:vim`.
The moerx window list and pane title also show `server:vim`. Starting another The moerx window list also shows `server:vim`. Starting another
remote program updates the title, and returning to the prompt shows the remote program updates the title, and returning to the prompt shows the
remote shell name, for example `[moerx] server:zsh`. remote shell name, for example `[moerx] server:zsh`.
The remote shell must send an OSC 0 or OSC 2 terminal title in The remote shell must send an OSC 0 or OSC 2 terminal title in
@ -87,6 +87,13 @@ if [[ -n $SSH_CONNECTION ]]; then
moerx_ssh_title() { moerx_ssh_title() {
local program=${1:t} local program=${1:t}
printf '\033]2;%s:%s\007' "${HOST%%.*}" "$program" printf '\033]2;%s:%s\007' "${HOST%%.*}" "$program"
local directory=$PWD
if [[ $directory == "$HOME" ]]; then
directory='~'
elif [[ $directory == "$HOME/"* ]]; then
directory="~/${directory#"$HOME/"}"
fi
printf '\033]777;moerx-cwd;%s\007' "$directory"
} }
moerx_ssh_preexec() { moerx_ssh_preexec() {
local -a words local -a words
@ -103,6 +110,17 @@ This reports the command launched by the shell (the first command for a
pipeline), then `hostname:zsh` at the prompt. moerx retains the latest pipeline), then `hostname:zsh` at the prompt. moerx retains the latest
`hostname:program` for that SSH job when programs send other terminal titles, `hostname:program` for that SSH job when programs send other terminal titles,
so these cannot reset the window name to `local-hostname:ssh`. so these cannot reset the window name to `local-hostname:ssh`.
The hook also reports the remote working directory before each command and at
each prompt, so the status bar follows remote `cd` commands and shows `~` for
the remote home directory. Disconnecting restores the local path. Restart
existing moerx servers and reload the remote shell configuration after upgrading.
moerx accepts standard OSC 7 reports (`ESC ] 7 ; file://host/path BEL`) too,
including percent-encoded paths. These display the reported absolute path;
`OSC 777;moerx-cwd;path` accepts either an absolute path or a path starting with
`~` and preserves that abbreviation. Reports belong to the foreground job
that sent them, so an old SSH connection cannot override the local shell's path.
Other shells can use equivalent Other shells can use equivalent
hooks sending `printf '\033]2;server:vim\007'`; use a hostname containing only hooks sending `printf '\033]2;server:vim\007'`; use a hostname containing only
letters, digits, dots, underscores or hyphens, and a program basename without letters, digits, dots, underscores or hyphens, and a program basename without
@ -170,7 +188,7 @@ Hard-coded:
Paths in `$HOME` use `~`, for example `[~/code/moerx]`; `$HOME` itself Paths in `$HOME` use `~`, for example `[~/code/moerx]`; `$HOME` itself
appears as `[~]`. Long paths end in `...` to leave room for the window list and clock; the appears as `[~]`. Long paths end in `...` to leave room for the window list and clock; the
directory label is hidden when fewer than four columns remain. Remote SSH directory label is hidden when fewer than four columns remain. Remote SSH
directories are not available locally. directories require a shell report; see [SSH window names and directories](#ssh-window-names-and-directories).
- `HH:MM:SS` on the right in `colour12`, refreshed every second. - `HH:MM:SS` on the right in `colour12`, refreshed every second.
- Window list right-aligned: current name `colour11`, other names `colour12`, - Window list right-aligned: current name `colour11`, other names `colour12`,
separators/flags `colour14`. separators/flags `colour14`.

60
moerx.c
View file

@ -308,7 +308,7 @@ struct Pane {
Pen main_pen; Pen main_pen;
ParseState pstate; ParseState pstate;
char seq[256]; char seq[PATH_MAX * 3 + 256];
int seqlen; int seqlen;
int u8_need, u8_have; int u8_need, u8_have;
@ -318,6 +318,8 @@ struct Pane {
pid_t title_pgrp; /* foreground job that supplied the OSC title */ pid_t title_pgrp; /* foreground job that supplied the OSC title */
char remote_title[128]; char remote_title[128];
pid_t remote_title_pgrp; pid_t remote_title_pgrp;
char reported_cwd[PATH_MAX];
pid_t reported_cwd_pgrp;
int id; int id;
int tty_fd; int tty_fd;
@ -840,8 +842,46 @@ 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 int pane_remote_title(Pane *p, pid_t pid);
/* OSC 7 uses a percent-encoded file URI. The moerx-specific form also
accepts a shell-shortened path so remote $HOME can be displayed as ~. */
static void em_osc_cwd(Pane *p) {
const char *path = NULL;
int uri = 0;
if (strncmp(p->seq, "7;file://", 9) == 0) {
path = strchr(p->seq + 9, '/');
uri = 1;
} else if (strncmp(p->seq, "777;moerx-cwd;", 14) == 0) {
path = p->seq + 14;
}
if (!path || (*path != '/' &&
!(*path == '~' && (!path[1] || path[1] == '/')))) return;
char cwd[PATH_MAX];
size_t n = 0;
while (*path) {
unsigned char ch = (unsigned char)*path++;
if (uri && ch == '%') {
if (!path[0] || !path[1]) return;
const char *digits = "0123456789abcdef";
const char *hi = strchr(digits, tolower((unsigned char)path[0]));
const char *lo = strchr(digits, tolower((unsigned char)path[1]));
if (!hi || !lo) return;
ch = (unsigned char)((hi - digits) * 16 + lo - digits);
path += 2;
}
if (ch < 32 || ch == 127 || n >= sizeof cwd - 1) return;
cwd[n++] = (char)ch;
}
cwd[n] = '\0';
pid_t pid = tcgetpgrp(p->fd);
if (pid <= 0) pid = tcgetpgrp(p->tty_fd);
if (pid <= 0) return;
snprintf(p->reported_cwd, sizeof p->reported_cwd, "%s", cwd);
p->reported_cwd_pgrp = 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';
em_osc_cwd(p);
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] == ';') {
const char *t = p->seq + 2; const char *t = p->seq + 2;
size_t n = strlen(t); size_t n = strlen(t);
@ -1674,12 +1714,17 @@ static void status_right_draw(Grid *g, int rx, int y, const char *clock) {
/* Linux exposes the foreground job's cwd; fall back to the pane shell, /* Linux exposes the foreground job's cwd; fall back to the pane shell,
then the session's starting directory when it cannot be queried. */ then the session's starting directory when it cannot be queried. */
static void status_cwd(Session *s, char *out, size_t size) { static int status_cwd(Session *s, char *out, size_t size) {
#ifdef __linux__
Pane *p = s->cur ? s->cur->active : NULL; Pane *p = s->cur ? s->cur->active : NULL;
if (p) { if (p) {
pid_t foreground = tcgetpgrp(p->fd); pid_t foreground = tcgetpgrp(p->fd);
if (foreground <= 0) foreground = tcgetpgrp(p->tty_fd); if (foreground <= 0) foreground = tcgetpgrp(p->tty_fd);
if (foreground > 0 && p->reported_cwd_pgrp == foreground &&
p->reported_cwd[0]) {
snprintf(out, size, "%s", p->reported_cwd);
return 1;
}
#ifdef __linux__
pid_t pids[2] = { foreground, p->pid }; pid_t pids[2] = { foreground, p->pid };
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (pids[i] <= 0) continue; if (pids[i] <= 0) continue;
@ -1691,12 +1736,13 @@ static void status_cwd(Session *s, char *out, size_t size) {
for (char *q = out; *q; q++) { for (char *q = out; *q; q++) {
if ((unsigned char)*q < 32 || (unsigned char)*q == 127) *q = '?'; if ((unsigned char)*q < 32 || (unsigned char)*q == 127) *q = '?';
} }
return; return 0;
} }
} }
}
#endif #endif
}
snprintf(out, size, "%s", s->cwd); snprintf(out, size, "%s", s->cwd);
return 0;
} }
/* Match a complete home-directory component, not a similarly named sibling. */ /* Match a complete home-directory component, not a similarly named sibling. */
@ -1774,8 +1820,8 @@ static void status_draw(Grid *g, Session *s, Client *c) {
int need = s->w - right_len; int need = s->w - right_len;
int start = need - total; int start = need - total;
char cwd[PATH_MAX]; char cwd[PATH_MAX];
status_cwd(s, cwd, sizeof cwd); if (!status_cwd(s, cwd, sizeof cwd))
status_cwd_home(cwd, sizeof cwd); status_cwd_home(cwd, sizeof cwd);
int left_len = status_cwd_draw(g, y, cwd, start); int left_len = status_cwd_draw(g, y, cwd, start);
if (start < left_len) start = left_len; if (start < left_len) start = left_len;
if (start < 0) start = 0; if (start < 0) start = 0;