aboutsummaryrefslogtreecommitdiffstats
path: root/src/path.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-17 12:56:42 +0200
committeruser <user@clank>2026-08-17 12:56:42 +0200
commit6a27f7b365d71ab853931fd5ab3c0f2f7ac83d57 (patch)
treeaf17513ab62107059d8044eb416b5353e66ab528 /src/path.c
parentsl0ppty: `C-a Enter` splits the longer way (diff)
downloadslosh-6a27f7b365d71ab853931fd5ab3c0f2f7ac83d57.tar.gz
slosh-6a27f7b365d71ab853931fd5ab3c0f2f7ac83d57.tar.xz
slosh-6a27f7b365d71ab853931fd5ab3c0f2f7ac83d57.zip
sl0ppty: workspaces, and layouts that write themselves
A **project** is a directory on disk. A **workspace** is the tab it occupies in a session. Two words because they are two things, and conflating them is how a session grows a second source of truth about what it is looking at. `project_roots "~/dev" "~/work" depth=2` is scanned for subdirectories holding a `sl0ppty.layout.kdl` -- or a `.git`, which is the project that has not said what it needs yet. Both markers, because a `~/dev` of forty checkouts and three layout files is a picker with three rows, and `C-a W` is how a project stops being the second kind. A directory that *is* a project is never descended into, so the walk stays off `node_modules` without anyone writing a rule about `node_modules`. `C-a w` is the third subject of the one picker (D19): the finder searches what this session has, this lists what is on disk, including the projects with no workspace open yet, which is most of them and the whole point. **Identity is the tab's `purpose`** -- `project:<name>.<hash8>` of the resolved path, the shape D3 already published -- so there is no second field to disagree with it, membership is a string comparison, and a dumped session restores it for free because a dump already writes tab purposes and applying one already locks them (D8). Hashed on the *path* rather than the name, so two worktrees of one repo are two workspaces, which is what they are. A tab whose layout gave it a purpose of its own keeps it and is simply not a member: overwriting a declared purpose is the one thing D8 forbids, and the reply counts the ones it left alone. **Nothing watches the filesystem.** The list is derived when it is asked for -- one `readdir` per root and two `stat`s per entry -- and an answer nobody remembered cannot be stale, which is a promise no watcher can make on a bind mount. The picker scans once as it opens, not per frame, because `draw_picker` asks for its rows every frame and a `readdir` per repaint is a filesystem walk at 120Hz. Opening is idempotent (`sl0ppi up`'s property): the same request twice is one workspace, focused, and the answer says which branch it took. Four things had to be true before `save-workspace` could exist, and none of them were. **A relative `cwd=` in a layout file now means that file's own directory** -- the rule `include` already followed for configs, applied to the other half of the same syntax. A project's layout could previously only name absolute directories, which are right on exactly one machine and wrong in a worktree of the same repo. **A dump can be asked for one tab, against a base.** `dump-layout` takes `tab`, `relative_to` and `suspend`. Writing it also turned up a bug: `dump_node` asked `cur(a)->focus` whichever tab it was writing, so a dump of three tabs restored the focus of one. **A dump records what a pane is *running*.** `label` only ever knew what a layout said, so a pane you split and typed `npm run dev` into had nothing to say for itself and came back as a bare shell -- which made arranging a project and writing it down two jobs instead of one. The kernel knows what the label does not: a pty has a foreground process group, and that group is by definition the job that owns the terminal, so the answer is read the way `live_cwd` already reads `/proc/PID/cwd`. Three things are deliberately not commands: a shell at a prompt (its group is the pane's own, and `command="zsh"` on every idle pane restores a session where each shell runs inside a shell), a background job (it does not own the terminal), and an ephemeral pane. A declared label still outranks a live process, because it survives the program exiting (D14) and re-saving must not degrade `npm run dev` into whatever the process tree looks like this minute. argv is joined with shell quoting, since `command=` goes through `/bin/sh -c`. **A pane from a split starts where the pane it came out of stands**, not where the server was launched. Wrong on its own, and it had been quietly writing an absolute path to somebody else's directory into every saved project. A saved project defaults to `suspend="commands"`: the pane running this morning's dev server is written asleep, because a *project's* layout is a statement of shape and a *session's* dump is a statement of fact. Over the API, replacing a layout that is already there is refused without `force`; from the keyboard `C-a W` replaces and says `replaced`, because a key cannot pass a flag and the file is committed text whose diff is the review. `--check` now holds a layout to the layout schema instead of answering with the name of the flag that reads one. `unknown pane property: cmd`, `split is cols or rows`, `purpose is ignored on a pane with panes in it` -- the mistakes a layout can make that still parses. The lists live beside `build_pane` and a test greps for every prop the loader reads, the arrangement that keeps `KNOWN_TOP` honest. **`C-a P` sets a pane's purpose**, a third subject on the editor the pane title and the tab label already share. A purpose is what tooling finds a pane by, and it could only be declared by a layout or set over the socket -- so every pane anybody arranged by hand had none, and a layout dumped from one was a shape with no tags in it. Typed by an operator it counts as declared (D8: "a layout or an operator"). Clearing one now *unlocks* it: a lock held over an empty string was the one state nothing could escape. `set-purpose` also reads `id: 0` as the focused pane now, which every sibling verb already did. Two gaps found on the way, both reachable only over the socket before: **`C-a X` closes a tab.** It used to be a side effect -- press `x` once per pane and the tab evaporates when the last one goes -- so four panes meant four presses and counting. It says how many panes went, since most of them were not the one you were looking at, and on the last tab it refuses and points at `q`: a key that closes a tab four times and ends your session the fifth is one you cannot press without counting first. No `✕` on the tabs, for the arithmetic that took the per-pane `+` away. **`scrollback` is a number we choose.** D4's "free from lib-vt" was true of the machinery and quietly false of the size: handed no limit, lib-vt picks its own -- 10,000 *bytes*, which measured at 622 lines of an 80-column pane, less than one `make` run. `scrollback` is lines, defaulting to the 10,000 every other multiplexer settled on, and `scrollback_bytes` is the ceiling it runs into, because either limit can be reached first and neither can see what the other depends on. Measured at 10,000 lines of styled output: ~6 MB at 80 columns (the ceiling never bites), ~15 MB at 200 (2% of the lines), ~32 MB at 400 (half of them). No `unlimited`, deliberately: the ceiling is per pane, and a negative is refused rather than wrapped into eighteen quintillion lines. Read when a pane is created, so it applies to the next pane you open -- same rule as `shell`, because shrinking a pane's history retroactively throws away output somebody is reading. `--project-root` was considered and dropped: the config is the one place projects are declared, and tests pin a config like every other config-driven feature. Verified: 1752 checks, all green. 90 in `tests/test_workspaces.py` (the two markers, depth without descending into a project, two worktrees as two workspaces, idempotent open, the honoured-purpose count, the save/close/open round trip byte-for-byte, the set-up-by-hand-and-write-it-down flow, the picker, and every verb's bare form); 53 in `tests/test_portable_layouts.py` (relative and absolute `cwd=`, text having no base, per-tab focus, the four `suspend` words, captured commands, an idle shell and a background job captured as nothing, spaces surviving the round trip, label precedence, and the checker's lists not going stale); 21 in `tests/test_purpose_editor.py`; 14 in `tests/test_scrollback.py`; 5 more in `tests/test_tabs.py`. `tests/test_osc5577.py` had a vacuous check: `\033` through the driver's unescaper is NUL + "33", so "a pane cannot relabel a declared purpose" passed without the sequence ever arriving. It uses `emit_cmd` now and has a positive control that the same sequence lands once the purpose is cleared. Three cheatsheet tests were pinned to terminal sizes that exactly fitted the old sheet, which four new bindings pushed past. They now ask for room; truncation has its own test.
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c
index 79ca119..edacda2 100644
--- a/src/path.c
+++ b/src/path.c
@@ -27,6 +27,63 @@ const char *path_expand(const char *path, char *buf, size_t cap) {
return buf;
}
+/* A path as a layout file meant it, against the directory that file lives in.
+ *
+ * A project's layout is checked in with the project, so it cannot name absolute
+ * directories: `cwd="/home/user/dev/api"` is right on exactly one machine and
+ * wrong in a worktree of the same repo. A relative path therefore resolves
+ * against the file that wrote it -- the rule `include` already follows for
+ * configs, applied to the other half of the same syntax.
+ *
+ * `base` NULL is the old behaviour on purpose: a layout arriving as text over
+ * the control socket was written by whoever sent it and has no directory to be
+ * relative to, so it keeps meaning what it meant. */
+const char *path_resolve(const char *path, const char *base, char *buf,
+ size_t cap) {
+ if (!path) return NULL;
+ if (path[0] == '~') return path_expand(path, buf, cap);
+ if (path[0] == '/' || !base || !*base) return path;
+ /* `.` is the project directory itself, which is what a dump writes for the
+ * pane that started there -- joined naively it would be `/home/you/dev/api/.`,
+ * which works and reads like a bug. */
+ if (strcmp(path, ".") == 0) return base;
+ if (strncmp(path, "./", 2) == 0) path += 2;
+ snprintf(buf, cap, "%s/%s", base, path);
+ return buf;
+}
+
+/* The directory a file lives in: what `path_resolve` wants as its base. A path
+ * with no `/` in it is in the current one, which is `.` rather than `""` --
+ * an empty base means "no base" to everything downstream. */
+const char *path_dir(const char *path, char *buf, size_t cap) {
+ if (!path) return NULL;
+ const char *slash = strrchr(path, '/');
+ if (!slash) return ".";
+ if (slash == path) return "/";
+ size_t n = (size_t)(slash - path);
+ if (n >= cap) n = cap - 1;
+ memcpy(buf, path, n);
+ buf[n] = 0;
+ return buf;
+}
+
+/* The inverse, for writing a layout back out: a path under `base` becomes what
+ * it is relative to it, and anything else is left absolute. Left absolute
+ * rather than climbed out of with `../..`, because a pane whose directory is
+ * outside the project is not a fact about the project and a file full of `..`
+ * is a file nobody can move. Returns a pointer into `path` or a literal, so
+ * there is nothing to free and nothing to size. */
+const char *path_relative(const char *path, const char *base) {
+ if (!path || !base || !*base) return path;
+ size_t n = strlen(base);
+ while (n && base[n - 1] == '/') n--; /* a trailing slash is not a component */
+ if (strncmp(path, base, n) != 0) return path;
+ if (path[n] == 0) return ".";
+ if (path[n] != '/') return path; /* /dev/apiary is not under /dev/api */
+ const char *rest = path + n + 1;
+ return *rest ? rest : ".";
+}
+
/* Create a directory and everything above it. `mkdir` of one level is enough
* on a machine where ~/.config already exists, which is most of them and not
* all of them -- a fresh container has neither, and "could not write your