diff options
| author | user <user@clank> | 2026-09-22 12:42:15 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-09-22 12:42:15 +0200 |
| commit | faa3fb6ee575a900abc574b0f9ed5ff291761837 (patch) | |
| tree | ce644998c144687e205d1f4e97e6f999de98723b /tests/test_themes.py | |
| parent | tabs: idle tabs you can actually read across the room (diff) | |
| download | slosh-faa3fb6ee575a900abc574b0f9ed5ff291761837.tar.gz slosh-faa3fb6ee575a900abc574b0f9ed5ff291761837.tar.xz slosh-faa3fb6ee575a900abc574b0f9ed5ff291761837.zip | |
theme: a name and a directory, not sixty colours in your config
Wearing a theme meant pasting its `theme { }` block into config.kdl, or
including a path to a file you had copied there yourself. Both make the
palette a literal in your config, and a literal is the one thing you cannot
change cheaply: not from a script, not from a running session, not by a list
of what is available, and never again from upstream -- the colours you pasted
are the colours that theme had the day you pasted them.
So a theme is a file in a directory now, and a config names it:
theme_name "phosphor"
theme { frame_focus "#00ff88" } // ...but that one colour is mine
theme_name is applied after the includes and before everything the file says
itself, which is the rule includes already follow and for the same reason: the
loader reads a document by asking it for names rather than walking it in order,
so "here" is not a position it could honour, and "what you asked for is the
base, what you wrote is yours" is the rule worth having.
Two directories, yours first: `themes/` beside the root config -- beside the
file that was loaded, so a config somewhere else (SLOSH_CONFIG, or one handed
to --check) takes its themes with it -- and then <prefix>/share/slosh/themes,
compiled in from PREFIX. install(1) has been putting themes there all along
and nothing could read them; now a shipped theme is nameable without being
copied, and a file of yours with the same name shadows it, which is how a
root-owned theme gets edited.
A name, not a path. The directory is what makes a theme switchable, listable
and shadowable, and a path silently does none of those, so `theme_name
"../x.kdl"` is refused with a line pointing at `include`, which still works
and is still the answer for a file somewhere a directory of names cannot
reach. A theme that is not there is one complaint and no more (D9): the rest
of the config applies, because losing your keybindings over a mistyped colour
scheme would be the wrong trade. A theme file is read by the same loader, so
it is watched like any other: saving one reloads the session wearing it, and
the derivations (tab_status_fg, tab_idle_bg) run over the theme's own colours
exactly as before.
Switching without editing anything, over the socket a session already has:
slosh -s main cmd theme # amber default* ...
slosh -s main cmd '{"cmd":"theme","name":"amber"}' # now
slosh -s main cmd '{"cmd":"theme","name":"amber","save":true}'
The session holds the name over later reloads, so saving your config for an
unrelated reason does not put the old colours back; `save` writes the
theme_name line into the file, replacing the one already there and leaving
every other line and comment alone -- dumping the whole config back over
itself would reformat a hand-written file and drop its comments, which is not
a fair price for changing a colour scheme. A name is tried on a throwaway
config first, so a typo costs nothing. An empty name gives the answer back to
the file.
--dump-config stops writing colours: it writes the theme's name and the
colours a config overrode by hand, so a dumped config keeps following its
theme instead of freezing it. --dump-theme is the other half and is new: the
resolved palette as a theme file, which is how you start one from the session
in front of you rather than from an empty file. It is also what keeps
contrib/themes/default.kdl honest -- that file is now the annotated reference
(the commented palette moved there out of config.kdl, where it was 119 lines
of colour in the middle of the prose), and a test compares it against the
compiled-in one.
While in there: tests/test_themes.py's "every theme sets every colour" guard
had been grepping for `&c->`, the spelling before the colour table moved to
offsetof, so it had been finding zero colours and passing for months. Fixed,
with the derived and opt-in colours exempted for the reasons they are derived
and opt-in, and with a check that the guard found a table at all.
Diffstat (limited to 'tests/test_themes.py')
| -rw-r--r-- | tests/test_themes.py | 86 |
1 files changed, 82 insertions, 4 deletions
diff --git a/tests/test_themes.py b/tests/test_themes.py index 9843d89..6710477 100644 --- a/tests/test_themes.py +++ b/tests/test_themes.py @@ -9,18 +9,41 @@ theme rather than a missing line in it. import glob import os import re +import subprocess import sys +import tempfile -from harness import Session, check, report +from harness import BIN, Session, check, report SH = ["/bin/sh", "-c", 'printf "\\033]2;p\\007"; stty raw -echo; cat'] HERE = os.path.dirname(os.path.abspath(__file__)) -THEMES = sorted(glob.glob(os.path.join(HERE, "..", "contrib", "themes", "*.kdl"))) +THEMEDIR = os.path.join(HERE, "..", "contrib", "themes") +THEMES = sorted(glob.glob(os.path.join(THEMEDIR, "*.kdl"))) + + +# Colours a theme is *not* expected to name. Two kinds, and both are the +# point of the feature rather than an oversight: the derived ones are mixed +# from colours the theme does define (so a theme gets them in its own family +# without a line), and the opt-in ones are unset on purpose, because a band +# behind a row takes a translucent terminal's own background away. +DERIVED = {"tab_status_fg", "tab_status_stripe", "tab_idle_bg"} +OPT_IN = {"tab_status_bg", "attach_bg"} +# The scrollback search tints: one theme in contrib names them and the rest +# take the compiled-in pair, which reads the same on every palette here. +SHARED = {"search_fg", "search_bg", "search_cur_fg", "search_cur_bg"} def declared_in_config(): + """Every colour the loader knows, from the table it walks. + + Matched against `offsetof`, which is how the table is written. It used to + grep for `&c->` -- the spelling before the table moved to offsets -- and + matched nothing at all for however many months, so this guard was passing + by finding no colours to check. A guard that cannot fail is worse than no + guard, so the count is asserted too. + """ src = open(os.path.join(HERE, "..", "src", "config.c")).read() - return set(re.findall(r'\{"([a-z_]+)", &c->', src)) + return set(re.findall(r'\{"([a-z_]+)", offsetof\(config_t', src)) def set_by(path): @@ -31,8 +54,13 @@ def test_there_are_themes(): check("contrib ships some", len(THEMES) >= 3, str(THEMES)) -def test_each_sets_every_colour_the_config_knows(): +def test_the_guard_has_something_to_guard(): want = declared_in_config() + check("the colour table was found", len(want) > 40, str(len(want))) + + +def test_each_sets_every_colour_the_config_knows(): + want = declared_in_config() - DERIVED - OPT_IN - SHARED for t in THEMES: missing = want - set_by(t) check( @@ -42,6 +70,28 @@ def test_each_sets_every_colour_the_config_knows(): ) +def test_default_is_the_compiled_palette_written_out(): + """`theme_name "default"` and naming no theme at all have to be the same + session -- that claim is the whole reason the file exists, and since the + config dump stopped writing colours, --dump-theme is what can check it.""" + path = os.path.join(HERE, "..", "contrib", "themes", "default.kdl") + theirs = dict( + re.findall(r'^ ([a-z_]+) "(#[0-9a-fA-F]{6})"', open(path).read(), re.M) + ) + env = dict(os.environ, SLOSH_CONFIG="/nonexistent/slosh.kdl") + dumped = subprocess.run( + [BIN, "--dump-theme"], capture_output=True, text=True, env=env + ).stdout + ours = dict(re.findall(r'^ ([a-z_]+)\s+"(#[0-9a-fA-F]{6})"', dumped, re.M)) + check("the dump has a palette in it", len(ours) > 40, str(len(ours))) + wrong = {k: (v, ours.get(k)) for k, v in theirs.items() if ours.get(k) != v} + check( + "default.kdl matches the compiled-in palette", + not wrong, + "; ".join(f"{k}: file {a}, code {b}" for k, (a, b) in sorted(wrong.items())), + ) + + def test_each_one_parses_and_reaches_the_screen(): for t in THEMES: declared = dict( @@ -59,8 +109,36 @@ def test_each_one_parses_and_reaches_the_screen(): check(f"{os.path.basename(t)} leaves the session alive", s.alive(), "") +def test_each_one_can_be_worn_by_name(): + """What a person actually types: `theme_name "amber"`, not a path to a file + in a checkout. Every shipped theme has to answer to its own filename.""" + d = tempfile.mkdtemp() + cfg = os.path.join(d, "config.kdl") + for t in THEMES: + name = os.path.basename(t)[:-4] + declared = dict( + re.findall(r'^ ([a-z_]+) "(#[0-9a-fA-F]{6})"', open(t).read(), re.M) + ) + with open(cfg, "w") as f: + f.write( + 'theme_dir "%s"\ntheme_name "%s"\n' % (os.path.abspath(THEMEDIR), name) + ) + with Session(SH, cols=50, rows=10, config=cfg) as s: + s.settle(30) + snap, p = s.snapshot(), s.pane() + drawn = (snap.style_at(p["x"], p["y"] + 1) or {}).get("fg") + check( + f"theme_name {name!r} is the theme that gets drawn", + drawn == declared["frame_focus"], + f"{drawn} != {declared['frame_focus']}", + ) + + if __name__ == "__main__": test_there_are_themes() + test_the_guard_has_something_to_guard() test_each_sets_every_colour_the_config_knows() + test_default_is_the_compiled_palette_written_out() test_each_one_parses_and_reaches_the_screen() + test_each_one_can_be_worn_by_name() sys.exit(report()) |
