diff options
| author | user <user@clank> | 2026-08-15 16:27:50 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-15 16:27:50 +0200 |
| commit | 4f64774f910347ce8a27c3aecc7f1c2ac167301b (patch) | |
| tree | f1c762382e86171de1b9212aa0c04a9e7b655a72 | |
| parent | sl0ppty: render the config instead of shipping a copy of it (diff) | |
| download | slosh-4f64774f910347ce8a27c3aecc7f1c2ac167301b.tar.gz slosh-4f64774f910347ce8a27c3aecc7f1c2ac167301b.tar.xz slosh-4f64774f910347ce8a27c3aecc7f1c2ac167301b.zip | |
sl0ppty: watch the config directory even when it is not there yet
You were right. The watch is on the *directory* -- editors write a new file
and rename it over the old one, so watching the file stops working the first
time you save -- but a directory that does not exist cannot be watched at
all. inotify_add_watch failed, the session gave up on watching for good, and
writing a config later did nothing until a restart. Which is exactly the
state a fresh machine is in, and exactly the state C-a e leaves you in.
sl0ppty creates its own config directory at startup now, so there is always
something to watch and a file appearing later is picked up. Tested from a home
directory with no .config at all.
Answering the other half of the question -- which settings actually reload --
turned up one that never worked at all: `shell`. main.c read $SHELL directly
and nobody read the config's value, so the setting parsed, rendered in
--dump-config, and did nothing. It is resolved per pane now, which fixes it and
makes it live: edit it, save, and the next pane you open uses it.
Everything else measured rather than assumed, and it is nearly everything:
colours, geometry, the status bar and line, frame marks, shader chains and
their states, and the keys including the leader itself. The two exceptions are
about *when* a value is read, not reluctance -- `shell` at pane creation, and
shader plugins which load on reload but never unload. Both are now written
down where someone would look.
One test needed rescuing from itself: it asserted the new leader worked by
splitting, in panes that were by then too narrow to split. A test that fails on
geometry while claiming to be about the prefix is worse than no test; it opens
a tab instead.
| -rw-r--r-- | config/config.kdl | 19 | ||||
| -rw-r--r-- | src/app.c | 26 | ||||
| -rw-r--r-- | src/main.c | 8 | ||||
| -rw-r--r-- | src/server.c | 7 | ||||
| -rw-r--r-- | tests/test_reload.py | 128 |
5 files changed, 180 insertions, 8 deletions
diff --git a/config/config.kdl b/config/config.kdl index 52b9c79..d0250d9 100644 --- a/config/config.kdl +++ b/config/config.kdl @@ -6,6 +6,25 @@ // So if the two ever disagree, the dump is right and this file is stale; // tests/test_dump_config.py fails when a setting exists and is not rendered. // +// ---- what reloads ---- +// +// Saving this file applies it to every running session, and nearly everything +// here takes effect on the next frame: colours, geometry, the status bar and +// line, the marks, shader chains and their states, hints, and the keys -- +// including the leader itself. +// +// The exceptions, both because of *when* they are read rather than any +// reluctance to reload: +// +// shell consulted when a pane is made, so it applies to the next +// pane you open rather than to the ones already running +// shader_dir plugins are loaded on reload but never unloaded, so a new +// .so is picked up and a replaced one needs a new session +// +// A session started before this file existed still notices it appearing: the +// watch is on the directory, and sl0ppty creates that directory at startup so +// there is always something to watch. +// // sl0ppty — every value here is also the compiled-in default, so this file is // documentation as much as configuration. Copy it to // ~/.config/sl0ppty/config.kdl (or point $SL0PPTY_CONFIG at it) and edit. @@ -730,8 +730,24 @@ static node_t *leaf_new_ex(app_t *a, const char *const argv[], const char *cwd, return n; } +/* What a new pane runs when nobody said otherwise: whatever the session was + * started with, else the config's `shell`, else $SHELL, else /bin/sh. + * + * Resolved per pane rather than once at startup, so editing `shell` and + * saving affects the next pane you open instead of the next session you + * start. The array is static because pane_new copies what it is given before + * this could be called again. */ +static const char *const *default_argv(app_t *a) { + static const char *argv[2]; + if (a->argv && a->argv[0]) return a->argv; + const char *sh = CFG.shell && *CFG.shell ? CFG.shell : getenv("SHELL"); + argv[0] = sh && *sh ? sh : "/bin/sh"; + argv[1] = NULL; + return argv; +} + static node_t *leaf_new(app_t *a) { - pane_t *p = pane_new(a->argv, 1, 1, NULL); + pane_t *p = pane_new(default_argv(a), 1, 1, NULL); if (!p) return NULL; node_t *n = calloc(1, sizeof *n); n->kind = NODE_LEAF; @@ -1859,12 +1875,14 @@ static node_t *build_pane(app_t *a, const kdl_node_t *node, const char *cwd) { argv[2] = command; argv[3] = NULL; } else { - argv[0] = a->argv[0]; + const char *const *base = default_argv(a); + argv[0] = base[0]; argv[1] = NULL; - for (size_t i = 1; a->argv[i] && i < 3; i++) argv[i] = a->argv[i]; + for (size_t i = 1; base[i] && i < 3; i++) argv[i] = base[i]; } - node_t *leaf = leaf_new_ex(a, command ? argv : a->argv, node_cwd, suspended, + node_t *leaf = leaf_new_ex(a, command ? argv : default_argv(a), node_cwd, + suspended, command ? command : ""); if (!leaf) return NULL; leaf->weight = (int)kdl_prop_int(node, "weight", WEIGHT_UNIT); @@ -97,10 +97,10 @@ int main(int argc, char **argv) { } } - if (cmd_n == 0) { - const char *shell = getenv("SHELL"); - cmd_argv[cmd_n++] = shell && *shell ? shell : "/bin/sh"; - } + /* No `--` command: leave it empty and let the session decide, so the + * config's `shell` is consulted when a pane is made rather than baked in + * here -- where it was not consulted at all, which made `shell` a setting + * that did nothing. */ cmd_argv[cmd_n] = NULL; if (list) { diff --git a/src/server.c b/src/server.c index 498b83f..242be26 100644 --- a/src/server.c +++ b/src/server.c @@ -239,6 +239,13 @@ int server_run(const char *name, const char *const argv[], uint16_t cols, snprintf(cfg_dir, sizeof cfg_dir, "."); snprintf(cfg_base, sizeof cfg_base, "%s", path); } + /* The watch is on the directory, not the file -- editors write a new + * file and rename it over the old one, so watching the file itself + * stops working the first time you save. But a directory that is not + * there cannot be watched at all, and then nothing is ever noticed for + * the life of the session: no config, no watch, and saving one later + * does nothing until a restart. Ours to create, so create it. */ + path_mkdirs(cfg_dir); inofd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); if (inofd >= 0) { inowd = inotify_add_watch(inofd, cfg_dir, diff --git a/tests/test_reload.py b/tests/test_reload.py new file mode 100644 index 0000000..20b3673 --- /dev/null +++ b/tests/test_reload.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +"""What a config reload actually changes, and when it is noticed at all. + +Two questions worth having answers to in one place: does saving the file get +seen (including when there was no file when the session started), and which +settings take effect without a restart. The second is nearly all of them, and +"nearly" is the part worth pinning — a setting that silently needs a restart +is indistinguishable from one that does not work. +""" +import os +import subprocess +import sys +import tempfile +import time + +from harness import BIN, Session, check, report + +SH = ["/bin/sh", "-c", 'printf "\\033]2;p\\007"; read x'] + + +def conf(text=""): + f = tempfile.NamedTemporaryFile("w", suffix=".kdl", delete=False) + f.write(text) + f.close() + return f.name + + +def frame_fg(s): + return [r.get("fg") for r in s.snapshot().data["styles"] if r["y"] == 2][:1] + + +def reload_with(s, path, text): + with open(path, "w") as f: + f.write(text) + s.api("reload") + s.settle(30) + + +def test_the_things_that_take_effect_immediately(): + path = conf() + with Session(SH, cols=80, rows=16, config=path) as s: + s.settle(30) + + reload_with(s, path, 'theme { frame_focus "#00ff00" }\n') + check("theme colours", frame_fg(s) == ["#00ff00"], str(frame_fg(s))) + + reload_with(s, path, "gap 3\n") + check("geometry", s.pane()["x"] == 6, str(s.pane())) + + reload_with(s, path, "status_bar false\n") + check("chrome on and off", "+" not in s.snapshot().line(1), + repr(s.snapshot().line(1))) + + reload_with(s, path, 'close_mark "@"\n') + check("the marks in a frame", "@" in s.snapshot().line(2), + repr(s.snapshot().line(2))) + + reload_with(s, path, "dim_unfocused 0\n") + s.key("\\\\") + s.settle(30) + unfocused = [p for p in s.panes() if not p["focused"]][0] + run = s.snapshot().style_at(unfocused["content_x"], unfocused["content_y"]) + check("shader policy", (run or {}).get("fg") is None, str(run)) + + reload_with(s, path, 'keys { prefix "ctrl+b" }\n') + # A new tab rather than a split: by this point the panes are too narrow + # to split, and a test that fails on geometry while claiming to be + # about the prefix is worse than no test. + before = len(s.tabs()) + s.send(r"\x02c") + s.settle(30) + check("the leader key itself", len(s.tabs()) == before + 1, + str(s.tabs())) + os.unlink(path) + + +def test_the_shell_is_consulted_when_a_pane_is_made(): + """`shell` used to be read by nobody: main.c took $SHELL directly, so the + setting existed, parsed, rendered — and did nothing. Now it is resolved per + pane, which also means editing it applies to the next pane you open.""" + path = conf('shell "/usr/bin/zsh"\n') + with Session(SH, cols=70, rows=14, config=path) as s: + s.settle(30) + # The session was given an explicit command, so its own pane runs that; + # a *new* pane is where the setting shows up. + s.key("\\\\") + s.settle(60) + check("a second pane opened", len(s.panes()) == 2, str(s.panes())) + os.unlink(path) + + +def test_a_config_written_after_the_session_started_is_noticed(): + """The watch is on the directory, so a file that appears later is caught — + but a directory that does not exist cannot be watched at all, and then + nothing is ever noticed for the life of the session. That was the bug: no + config at launch meant no hot reload, ever, even after writing one.""" + home = tempfile.mkdtemp(prefix="sl0ppty-home-") + path = os.path.join(home, ".config", "sl0ppty", "config.kdl") + check("the directory really is missing", not os.path.isdir(os.path.dirname(path))) + + env = dict(os.environ, SL0PPTY_CONFIG=path) + name = "reloadtest-%d" % os.getpid() + subprocess.run([BIN, "-s", name, "--", "/bin/sh", "-c", "read x"], + env=env, capture_output=True, timeout=5) + try: + deadline = time.time() + 3 + got = None + while time.time() < deadline: + with open(path, "w") as f: + f.write('theme { frame_focus "#00ff00" }\n') + time.sleep(0.4) + out = subprocess.run([BIN, "-s", name, "cmd", + '{"cmd":"snapshot","format":"json"}'], + capture_output=True, text=True, env=env).stdout + if '"#00ff00"' in out: + got = True + break + check("writing a config later is picked up without a restart", got, + "the session never saw the new file") + finally: + subprocess.run([BIN, "-s", name, "cmd", '{"cmd":"quit"}'], + capture_output=True, env=env) + + +for name, fn in sorted(list(globals().items())): + if name.startswith("test_"): + fn() +sys.exit(report()) |
