aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-15 13:13:34 +0200
committeruser <user@clank>2026-08-15 13:13:34 +0200
commitd9dee7f725b68332982f34ab6c52da0552b821d3 (patch)
tree950f8293a6d6b6fd5a39acee5625c64813ab0160 /src
parentsl0ppty: let the cheatsheet breathe (diff)
downloadslosh-d9dee7f725b68332982f34ab6c52da0552b821d3.tar.gz
slosh-d9dee7f725b68332982f34ab6c52da0552b821d3.tar.xz
slosh-d9dee7f725b68332982f34ab6c52da0552b821d3.zip
sl0ppty: write the session back out as a layout, and a loop that uses it
Dogfooding this while building it has one sharp edge: a running session keeps the binary it started with. `reload` re-reads the config, it cannot re-read the code, so every build you want to try means losing your screen. The only thing worth carrying across a restart is the shape, and sl0ppty already knows how to read one -- it just could not write one. `dump-layout` is the inverse of `--layout`: tabs with their names and purposes, how the panes are split, in what proportion, in which directory, running what, which tab was active and which pane had focus. Two details make it worth trusting. The directory is where the shell *is*, read from /proc, not where the pane was started -- after any amount of `cd` those differ and only one of them is useful. And a pane running the session's default shell is dumped with no `command`, so it comes back as a shell rather than as a shell wrapped in a shell, one layer deeper every restart. contrib/sl0ppty-dev is the loop: attach, and when the session ends because you asked for a restart, come straight back with the layout. It is two halves because it has to be -- `restart` runs inside the session, and the moment the session ends so does the shell it was typed in, so it leaves the layout and a note and the loop outside picks them up. What does not come back is what was running *inside* a program. A shell returns as a shell, in the right directory, with an empty history. That is the limit of restoring a layout rather than a session, and the script says so rather than implying otherwise. Three bugs found by the round-trip test, all the same kind -- a dump that loads without complaint and rebuilds something subtly different: - `split=` belongs on the node that *has* the children, not on the children. Written the other way it parses fine and gives the wrong tree. - a split's weight was never restored, because the edit meant to read it silently matched nothing and was never applied. - `active=true` landed on the wrong tab: the index is recorded while the new tabs sit after the old ones, and `replace` then moves them to the front. Which is why the test asserts that a dump of a reload is byte-identical to the first dump. Anything the dumper loses shows up there at once, where reading it and nodding would not.
Diffstat (limited to 'src')
-rw-r--r--src/app.c168
-rw-r--r--src/app.h8
-rw-r--r--src/cmd.c15
-rw-r--r--src/pane.c2
-rw-r--r--src/sl0ppty.h6
5 files changed, 196 insertions, 3 deletions
diff --git a/src/app.c b/src/app.c
index 0f80c75..c9ba927 100644
--- a/src/app.c
+++ b/src/app.c
@@ -4,6 +4,7 @@
#include <ghostty/vt.h>
#include <ctype.h>
+#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
@@ -276,6 +277,13 @@ struct app {
int64_t name_click_ms;
uint32_t name_click_id;
int name_click_kind;
+ /* Set while a layout is being built, by a `focus=true` pane, and consumed
+ * by the tab that contains it: focus belongs to a tab, and the tab does not
+ * exist yet when the pane declaring it is made. */
+ node_t *restore_focus;
+ /* Which tab a layout said was active, resolved after every tab exists --
+ * the index is only meaningful once the array has stopped growing. */
+ size_t restore_tab;
const char *const *argv;
/* What this session is called. The server knows it because it opened the
* socket under that name; the app only knows it because it is worth saying
@@ -1699,7 +1707,10 @@ static node_t *build_pane(app_t *a, const kdl_node_t *node, const char *cwd) {
node_t *sp = calloc(1, sizeof *sp);
sp->kind = NODE_SPLIT;
sp->id = ++a->next_id;
- sp->weight = WEIGHT_UNIT;
+ /* A dumped layout carries the proportions it had; a hand-written one says
+ * nothing and means "even", which is what WEIGHT_UNIT is. */
+ sp->weight = (int)kdl_prop_int(node, "weight", WEIGHT_UNIT);
+ if (sp->weight < WEIGHT_MIN) sp->weight = WEIGHT_UNIT;
sp->dir = strcmp(kdl_prop(node, "split", "cols"), "rows") == 0 ? SPLIT_ROWS
: SPLIT_COLS;
for (size_t i = 0; i < node->nkids; i++) {
@@ -1741,6 +1752,11 @@ static node_t *build_pane(app_t *a, const kdl_node_t *node, const char *cwd) {
node_t *leaf = leaf_new_ex(a, command ? argv : a->argv, node_cwd, suspended,
command ? command : "");
if (!leaf) return NULL;
+ leaf->weight = (int)kdl_prop_int(node, "weight", WEIGHT_UNIT);
+ if (leaf->weight < WEIGHT_MIN) leaf->weight = WEIGHT_UNIT;
+ /* `focus=true` restores which pane you were in. Recorded on the node and
+ * resolved once the tab exists, because focus belongs to the tab. */
+ if (kdl_prop_bool(node, "focus", false)) a->restore_focus = leaf;
const char *purpose = kdl_prop(node, "purpose", NULL);
if (purpose) {
sanitise_purpose(purpose, leaf->purpose, sizeof leaf->purpose);
@@ -1751,6 +1767,7 @@ static node_t *build_pane(app_t *a, const kdl_node_t *node, const char *cwd) {
bool app_apply_layout(app_t *a, const kdl_node_t *root, bool replace, char *err,
size_t errcap) {
+ a->restore_tab = (size_t)-1;
const kdl_node_t *lay = kdl_child(root, "layout");
if (!lay) lay = root; /* allow a bare list of tabs */
@@ -1761,6 +1778,7 @@ bool app_apply_layout(app_t *a, const kdl_node_t *root, bool replace, char *err,
if (strcmp(t->name, "tab") != 0) continue;
tab_t *tab = tab_add(a, kdl_prop(t, "name", ""));
+ bool active = kdl_prop_bool(t, "active", false);
const char *purpose = kdl_prop(t, "purpose", NULL);
if (purpose) {
sanitise_purpose(purpose, tab->purpose, sizeof tab->purpose);
@@ -1779,7 +1797,11 @@ bool app_apply_layout(app_t *a, const kdl_node_t *root, bool replace, char *err,
return false;
}
tab->root = tree;
- tab->focus = first_leaf_of(tree);
+ tab->focus = a->restore_focus ? a->restore_focus : first_leaf_of(tree);
+ a->restore_focus = NULL;
+ /* `active=true` restores which tab you were looking at. Resolved by index
+ * because the tab was appended to whatever was already there. */
+ if (active) a->restore_tab = (size_t)(tab - a->tabs);
made++;
}
@@ -1793,7 +1815,21 @@ bool app_apply_layout(app_t *a, const kdl_node_t *root, bool replace, char *err,
memmove(&a->tabs[0], &a->tabs[before], made * sizeof *a->tabs);
a->ntabs = made;
}
- a->cur = replace ? 0 : before;
+ /* Which tab to land on: the one a dump marked `active`, if it named one,
+ * and otherwise the first of what was just built. Setting it unconditionally
+ * here is what quietly undid the restore.
+ *
+ * The index was recorded while the new tabs sat *after* the old ones, and
+ * `replace` has just moved them to the front -- so it shifts by exactly the
+ * number that were dropped. Off by that, it lands on the wrong tab, which
+ * looks like the restore working badly rather than not at all. */
+ if (a->restore_tab != (size_t)-1 && replace && a->restore_tab >= before)
+ a->restore_tab -= before;
+ if (a->restore_tab != (size_t)-1 && a->restore_tab < a->ntabs)
+ a->cur = a->restore_tab;
+ else
+ a->cur = replace ? 0 : before;
+ a->restore_tab = (size_t)-1;
layout(a);
return true;
}
@@ -4173,6 +4209,132 @@ void app_event(app_t *a, const input_event_t *ev) {
}
}
+
+/* ---- dumping a session back out as a layout ------------------------------
+ *
+ * The inverse of apply-layout, and the thing that makes a restart survivable:
+ * build a fresh binary, dump what you have, quit, come back with `--layout`.
+ * contrib/sl0ppty-dev wraps exactly that.
+ *
+ * What can honestly be restored is the *shape*: tabs, their names and
+ * purposes, how the panes are split, in what proportion, in which directory,
+ * running what they were started with. What cannot is the state inside a
+ * program -- a shell's history, a running vim -- and this does not pretend
+ * otherwise. A pane running the session's default shell is dumped as a pane
+ * with no command, so it comes back as a shell rather than as a re-run of one.
+ */
+
+typedef struct {
+ char *buf;
+ size_t len, cap;
+} strbuf_t;
+
+static void sb_add(strbuf_t *b, const char *fmt, ...) {
+ va_list ap;
+ for (;;) {
+ va_start(ap, fmt);
+ int n = vsnprintf(b->buf + b->len, b->cap - b->len, fmt, ap);
+ va_end(ap);
+ if (n < 0) return;
+ if ((size_t)n < b->cap - b->len) {
+ b->len += (size_t)n;
+ return;
+ }
+ b->cap = b->cap ? b->cap * 2 : 1024;
+ while (b->cap - b->len <= (size_t)n) b->cap *= 2;
+ b->buf = realloc(b->buf, b->cap);
+ }
+}
+
+/* KDL strings are double-quoted, so the two characters that end or escape one
+ * have to be escaped themselves. A path can contain both. */
+static void sb_quoted(strbuf_t *b, const char *key, const char *val) {
+ if (!val || !*val) return;
+ sb_add(b, " %s=\"", key);
+ for (const char *p = val; *p; p++) {
+ if (*p == '"' || *p == '\\') sb_add(b, "\\%c", *p);
+ else if ((unsigned char)*p >= 0x20) sb_add(b, "%c", *p);
+ }
+ sb_add(b, "\"");
+}
+
+/* Where the program in the pane is *now*, which after any amount of `cd` is
+ * not where it was started. The kernel knows; nothing else does. */
+static const char *live_cwd(const pane_t *p, char *buf, size_t cap) {
+ pid_t pid = pane_pid(p);
+ if (pid > 0) {
+ char link[64];
+ snprintf(link, sizeof link, "/proc/%d/cwd", (int)pid);
+ ssize_t n = readlink(link, buf, cap - 1);
+ if (n > 0) {
+ buf[n] = 0;
+ return buf;
+ }
+ }
+ return pane_start_cwd(p); /* not running, or not Linux: what it was given */
+}
+
+/* `split=` belongs on the node that *has* the children -- that is where
+ * build_pane() reads it -- and a tab's own props are the props of its root,
+ * so a root split says so on the tab. Getting this backwards produces a file
+ * that loads without complaint and rebuilds the wrong tree. */
+static void dump_node(app_t *a, node_t *n, strbuf_t *b, int depth) {
+ char pad[64];
+ int p = depth * 4 < 60 ? depth * 4 : 60;
+ memset(pad, ' ', (size_t)p);
+ pad[p] = 0;
+
+ sb_add(b, "%spane", pad);
+ /* A weight is a share of the parent, so the root of a tab has none. */
+ if (n->parent) sb_add(b, " weight=%d", n->weight);
+
+ if (n->kind == NODE_SPLIT) {
+ sb_add(b, " split=\"%s\" {\n", n->dir == SPLIT_ROWS ? "rows" : "cols");
+ for (size_t i = 0; i < n->nkids; i++) dump_node(a, n->kids[i], b, depth + 1);
+ sb_add(b, "%s}\n", pad);
+ return;
+ }
+
+ char cwdbuf[4096];
+ sb_quoted(b, "cwd", live_cwd(n->pane, cwdbuf, sizeof cwdbuf));
+ /* The label is the command a layout gave it; a pane running the session's
+ * default shell has none, and gets none back. */
+ sb_quoted(b, "command", pane_label(n->pane));
+ sb_quoted(b, "purpose", n->purpose);
+ if (pane_suspended(n->pane)) sb_add(b, " suspended=true");
+ if (n == cur(a)->focus) sb_add(b, " focus=true");
+ sb_add(b, "\n");
+}
+
+char *app_dump_layout(app_t *a) {
+ strbuf_t b = {0};
+ sb_add(&b, "// sl0ppty session, dumped %s", "");
+ b.len = 0; /* no timestamp: a layout that differs every time is a bad diff */
+ sb_add(&b, "layout {\n");
+ for (size_t i = 0; i < a->ntabs; i++) {
+ tab_t *t = &a->tabs[i];
+ sb_add(&b, " tab");
+ sb_quoted(&b, "name", t->name);
+ sb_quoted(&b, "purpose", t->purpose);
+ if (i == a->cur) sb_add(&b, " active=true");
+ /* The tab's props are its root's props, so a root that is a split says
+ * which way it goes here rather than on a `pane` node of its own. */
+ if (t->root && t->root->kind == NODE_SPLIT)
+ sb_add(&b, " split=\"%s\"", t->root->dir == SPLIT_ROWS ? "rows" : "cols");
+ sb_add(&b, " {\n");
+ if (t->root) {
+ if (t->root->kind == NODE_SPLIT)
+ for (size_t k = 0; k < t->root->nkids; k++)
+ dump_node(a, t->root->kids[k], &b, 2);
+ else
+ dump_node(a, t->root, &b, 2);
+ }
+ sb_add(&b, " }\n");
+ }
+ sb_add(&b, "}\n");
+ return b.buf;
+}
+
/* ---- introspection ------------------------------------------------------ */
struct panes_json {
diff --git a/src/app.h b/src/app.h
index f220eb6..6150147 100644
--- a/src/app.h
+++ b/src/app.h
@@ -126,6 +126,14 @@ bool app_shade_add(app_t *a, uint32_t pane_id, const char *kind, color_t color,
void app_shade_clear(app_t *a, uint32_t pane_id);
size_t app_shade_count(app_t *a, uint32_t pane_id);
+/* The session written back out as a layout file: tabs, splits, proportions,
+ * directories and commands. The inverse of app_apply_layout_text(), and what
+ * makes "quit, rebuild, come back to the same screen" possible. Caller frees.
+ *
+ * The shape is restorable; what is running inside a program is not, and this
+ * does not pretend otherwise. */
+char *app_dump_layout(app_t *a);
+
/* State as JSON, for the control API and the harness. Caller frees. */
char *app_panes_json(app_t *a);
char *app_tabs_json(app_t *a);
diff --git a/src/cmd.c b/src/cmd.c
index 618669c..cddba75 100644
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -199,6 +199,17 @@ static char *cmd_json(app_t *a, screen_t *s, input_parser_t *in,
: app_set_pane_purpose(a, id, purpose, declared);
return ok ? jok_int(NULL, 0) : jerr("refused");
}
+ if (strcmp(cmd, "dump-layout") == 0) {
+ char *kdl = app_dump_layout(a);
+ json_t j;
+ json_init(&j);
+ json_obj_open(&j, NULL);
+ json_bool(&j, "ok", true);
+ json_str(&j, "kdl", kdl, strlen(kdl));
+ json_obj_close(&j);
+ free(kdl);
+ return j.buf;
+ }
if (strcmp(cmd, "apply-layout") == 0) {
const char *path = jv_gets(req, "path", NULL);
const char *text = jv_gets(req, "kdl", NULL);
@@ -327,6 +338,10 @@ char *cmd_exec(app_t *a, screen_t *s, input_parser_t *in, const char *line,
app_compose(a, s);
return app_tabs_json(a);
}
+ if (strcmp(verb, "dump-layout") == 0) {
+ app_compose(a, s); /* the layout is a function of the frame: compose first */
+ return app_dump_layout(a);
+ }
if (strcmp(verb, "reload") == 0) {
char err[256] = {0};
bool ok = app_reload_config(err, sizeof err);
diff --git a/src/pane.c b/src/pane.c
index e540049..4306844 100644
--- a/src/pane.c
+++ b/src/pane.c
@@ -352,6 +352,8 @@ void pane_free(pane_t *p) {
}
int pane_fd(const pane_t *p) { return p->pty.fd; }
+pid_t pane_pid(const pane_t *p) { return p->pty.pid; }
+const char *pane_start_cwd(const pane_t *p) { return p->cwd; }
bool pane_alive(const pane_t *p) { return p->alive; }
bool pane_dirty(pane_t *p) { return p->dirty; }
const char *pane_title(const pane_t *p) {
diff --git a/src/sl0ppty.h b/src/sl0ppty.h
index fb7ba2d..e117908 100644
--- a/src/sl0ppty.h
+++ b/src/sl0ppty.h
@@ -120,6 +120,12 @@ pane_t *pane_new_ex(const char *const argv[], uint16_t cols, uint16_t rows,
const char *cwd, bool suspended, const char *label);
bool pane_suspended(const pane_t *p);
const char *pane_label(const pane_t *p);
+/* The pid of the program in the pane, or -1. Used to ask the kernel where a
+ * shell has got to, which is the only honest answer to "what is this pane's
+ * directory" once somebody has typed `cd`. */
+pid_t pane_pid(const pane_t *p);
+/* Where it was started, which is not where it *is*: see pane_pid(). */
+const char *pane_start_cwd(const pane_t *p);
bool pane_start(pane_t *p);
void pane_free(pane_t *p);
int pane_fd(const pane_t *p);