repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

termux: nugu theme

e414b5d87335588a0012665f9e2e0740751a5349

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (10)

mise.toml

Modified
66 66 run("niri", "validate", "-c", "niri/config.kdl");
69 +[tasks.nugu-termux]
70 +description = "Generate both Nugu Termux palettes without selecting a mode"
71 +run = '''
72 +cd("nugu");
73 +run("just", "--shell", "nu", "--clear-shell-args", "--shell-arg=--no-config-file", "--shell-arg=--commands", "termux");
74 +'''
76 +[tasks.termux-test]
77 +description = "Test Nugu Termux generation and manual theme switching"
78 +run = '''
79 +run("nu", "--no-config-file", "-c", "for file in [nugu/termux.nu nushell/modules/termux-theme.nu] { if not (nu-check --as-module $file) { error make {msg: $'Parse failed: ($file)'} } }");
80 +run("node", "--test", "termux/theme.test.mjs");
81 +'''
69 83 [tasks.wezterm-test]
70 84 description = "Test WezTerm modernization and instance helpers"
71 85 run = '''

nugu/README.md

Modified
12 12 Layout, effects, and keybindings remain untouched.
13 13 Validate both modes from the repo root with `mise run niri-test`.
15 +## Termux
17 +`base.toml` → `nugu.nu` → `termux.nu` → `output/termux/nugu-{dark,light}.properties`.
18 +Run `mise run nugu-termux` from the dotfiles root to generate both variants into `../termux/themes/`.
19 +Generation does not select a theme.
20 +The Nushell command `termux-theme dark` or `termux-theme light` selects one manually and reloads Termux.
21 +See [Termux setup and recovery](../termux/README.md).
22 +Validate generation and switching with `mise run termux-test` from the dotfiles root.
15 24 ## TODO
17 26 * [x] helix

nugu/justfile

Modified
16 16 open aerc.nu | nu-check --as-module --debug aerc.nu
17 17 open neovim.nu | nu-check --as-module --debug neovim.nu
18 18 open niri.nu | nu-check --as-module --debug niri.nu
19 + open termux.nu | nu-check --as-module --debug termux.nu
20 21 fmt: lint
21 22 taplo fmt
79 80 mkdir ../zed/themes
80 81 cp {{output}}/zed/nugu.json ../zed/themes/nugu.json
83 +# Generate both modes without changing Termux's active colors.properties.
84 +termux:
85 + mkdir {{output}}/termux
86 + use termux.nu; termux theme dark | save --force --raw {{output}}/termux/nugu-dark.properties
87 + use termux.nu; termux theme light | save --force --raw {{output}}/termux/nugu-light.properties
88 + mkdir ../termux/themes
89 + cp {{output}}/termux/nugu-dark.properties ../termux/themes/nugu-dark.properties
90 + cp {{output}}/termux/nugu-light.properties ../termux/themes/nugu-light.properties
82 92 # Generate and install both modes; Niri's config selects one via include.
83 93 niri:
84 94 mkdir {{output}}/niri
128 138 cp {{output}}/aerc/nugu-dark ../aerc/stylesets/nugu-dark
129 139 cp {{output}}/aerc/nugu-light ../aerc/stylesets/nugu-light
131 -install: helix wezterm ghostty zed linux-console hypr aerc yazi nvim niri
141 +install: helix wezterm ghostty zed linux-console hypr aerc yazi nvim niri termux

nugu/termux.nu

Added
new file mode 100644
1 +use nugu.nu
3 +# Match WezTerm's Nugu mapping, using only Termux-supported properties.
4 +export def theme [mode: string] {
5 + let p = (match $mode {
6 + dark => (nugu dark)
7 + light => (nugu light)
8 + _ => { error make {msg: "Expected dark or light"} }
9 + } | get palette)
11 + let colors = [
12 + $p.content.backdrop
13 + $p.error
14 + $p.content.focus
15 + $p.warning
16 + $p.content.important_local
17 + $p.content.accent
18 + $p.info
19 + $p.content.normal
20 + $p.content.minor
21 + $p.error
22 + $p.ui.focus
23 + $p.warning
24 + $p.content.important_global
25 + $p.ui.accent
26 + $p.ui.important_local
27 + $p.ui.important_global
28 + # Nugu semantic extras, indexed 16-22.
29 + $p.debug
30 + $p.ui.normal
31 + $p.ui.backdrop
32 + $p.ui.minor
33 + $p.ui.focus
34 + $p.ui.unfocus
35 + $p.content.unfocus
36 + ]
38 + [
39 + "# Generated by nugu/termux.nu. Do not edit."
40 + $"foreground=($p.content.normal)"
41 + $"background=($p.content.backdrop)"
42 + $"cursor=($p.ui.accent)"
43 + ...($colors | enumerate | each {|c| $"color($c.index)=($c.item)" })
44 + ""
45 + ] | str join (char nl)

nushell/login.nu

Modified
12 12 use yazi.nu *
13 13 use akku.nu
14 14 use colors.nu
15 +use termux-theme.nu
15 16 use commands.nu *
16 17 use syncthing.nu
17 18 use minimax.nu

nushell/modules/termux-theme.nu

Added
new file mode 100644
1 +# Resolve the module itself first because ~/.config/nushell is a symlink.
2 +const themes = (path self | path expand | path dirname | path join ".." ".." "termux" "themes" | path expand)
4 +def modes [] { [dark light] }
6 +# Select a generated Nugu palette and reload Termux without restarting sessions.
7 +export def main [mode: string@modes] {
8 + if $mode not-in [dark light] {
9 + error make {msg: "Expected dark or light"}
10 + }
11 + if ($env.TERMUX_VERSION? | is-empty) or (which termux-reload-settings | is-empty) {
12 + error make {msg: "Run termux-theme inside Termux with termux-reload-settings available"}
13 + }
15 + let source = ($themes | path join $"nugu-($mode).properties")
16 + # Read before touching the active configuration, failing on missing palettes.
17 + let colors = (open --raw $source)
18 + let directory = ($nu.home-dir | path join ".termux")
19 + let active = ($directory | path join "colors.properties")
20 + let backup = ($directory | path join "colors.properties.before-nugu")
22 + # Do not follow links into managed palettes or outside HOME.
23 + if ($directory | path type) not-in [null dir] {
24 + error make {msg: "~/.termux must be a regular directory, not a symlink"}
25 + }
26 + for file in [$active $backup] {
27 + if ($file | path type) not-in [null file] {
28 + error make {msg: $"Refusing non-regular file: ($file)"}
29 + }
30 + }
31 + mkdir $directory
32 + if ($active | path exists) and not ($backup | path exists) {
33 + cp --preserve [mode timestamps] $active $backup
34 + }
35 + $colors | save --raw --force $active
37 + let result = (^termux-reload-settings | complete)
38 + if $result.exit_code != 0 {
39 + error make {msg: $"Nugu ($mode) saved, but Termux reload failed with exit code ($result.exit_code)"}
40 + }
41 + print $"Termux: Nugu ($mode)"

termux/README.md

Added
new file mode 100644
1 +# Termux Nugu themes
3 +`nugu/termux.nu` generates dark and light palettes from `nugu/nugu.nu` and `nugu/base.toml`.
4 +Foreground, background, cursor, ANSI colors, and indexed colors 16-22 match WezTerm's Nugu mapping.
5 +Only properties supported by Termux are emitted; WezTerm's selection and window styling are not portable.
7 +From the dotfiles root:
9 +```sh
10 +mise run nugu-termux
11 +mise run termux-test
12 +```
14 +Generation writes both variants to `nugu/output/termux/` and `termux/themes/` without selecting an active theme.
15 +The command reads the tracked palettes relative to its module, so no new HOME symlink is needed.
17 +## Manual selection
19 +`nushell/login.nu` loads `nushell/modules/termux-theme.nu` in new login shells.
20 +For an already-running Nushell, import it once:
22 +```nu
23 +use ~/.config/nushell/modules/termux-theme.nu
24 +```
26 +Choose a palette:
28 +```nu
29 +termux-theme dark
30 +termux-theme light
31 +```
33 +The command copies the selected palette into `~/.termux/colors.properties` and runs `termux-reload-settings`.
34 +It requires Termux and its existing reload command, not an extra APK.
35 +There is no schedule, background process, startup selection, or system-theme integration.
36 +Keyboard settings, font, fullscreen, and app UI settings remain untouched.
38 +## Backup and recovery
40 +Before the first switch, an existing palette is copied to `~/.termux/colors.properties.before-nugu`.
41 +Later switches preserve that backup.
42 +If reload fails, the command reports failure, but the selected palette remains saved.
43 +Symlinked settings directories, active palettes, and backups are refused rather than modifying their targets.
45 +To restore the original palette, if the backup exists:
47 +```nu
48 +cp ~/.termux/colors.properties.before-nugu ~/.termux/colors.properties
49 +termux-reload-settings
50 +```

termux/theme.test.mjs

Added
new file mode 100644
1 +import assert from "node:assert/strict";
2 +import { spawnSync } from "node:child_process";
3 +import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
4 +import { tmpdir } from "node:os";
5 +import { dirname, join, resolve } from "node:path";
6 +import { fileURLToPath } from "node:url";
7 +import test from "node:test";
9 +const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
10 +const justArgs = ["--shell", "nu", "--clear-shell-args", "--shell-arg=--no-config-file", "--shell-arg=--commands", "termux"];
12 +function run(command, args, options = {}) {
13 + const result = spawnSync(command, args, { encoding: "utf8", timeout: 120_000, ...options });
14 + assert.equal(result.error, undefined);
15 + return result;
18 +function success(result) {
19 + assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`);
20 + return result.stdout;
23 +function failure(result, message) {
24 + assert.notEqual(result.status, 0);
25 + assert.match(result.stderr, message);
28 +const nu = success(run("nu", ["--no-config-file", "-c", "$nu.current-exe"])).trim();
30 +function weztermColors(palette) {
31 + const lua = readFileSync(join(root, "wezterm/bugabinga/nugu/init.lua"), "utf8");
32 + const lookup = (role) => role.split(".").reduce((value, key) => value[key], palette);
33 + const result = {};
34 + for (const [termux, wezterm] of [["foreground", "foreground"], ["background", "background"], ["cursor", "cursor_bg"]]) {
35 + result[termux] = lookup(lua.match(new RegExp(`\\b${wezterm} = palette\\.([\\w.]+),`))[1]);
36 + }
37 + for (const [section, offset, count] of [["ansi", 0, 8], ["brights", 8, 8], ["indexed", 16, 7]]) {
38 + const body = lua.match(new RegExp(`\\b${section} = \\{([\\s\\S]*?)\\},`))[1];
39 + const roles = [...body.matchAll(/palette\.([\w.]+),/g)].map((match) => match[1]);
40 + assert.equal(roles.length, count);
41 + roles.forEach((role, index) => { result[`color${offset + index}`] = lookup(role); });
42 + }
43 + return result;
46 +test("Termux palettes and manual switching", async (t) => {
47 + const workspace = mkdtempSync(join(tmpdir(), "termux-theme-test-"));
48 + t.after(() => rmSync(workspace, { recursive: true, force: true }));
49 + const nugu = join(workspace, "nugu");
50 + const modules = join(workspace, "nushell/modules");
51 + const home = join(workspace, "home");
52 + const settings = join(home, ".termux");
53 + const bin = join(workspace, "bin");
54 + for (const directory of [nugu, modules, settings, bin, join(home, ".config")]) mkdirSync(directory, { recursive: true });
55 + for (const file of ["base.toml", "nugu.nu", "termux.nu", "justfile"]) cpSync(join(root, "nugu", file), join(nugu, file));
56 + cpSync(join(root, "nushell/modules/termux-theme.nu"), join(modules, "termux-theme.nu"));
57 + symlinkSync(join(workspace, "nushell"), join(home, ".config/nushell"), "junction");
58 + const module = join(home, ".config/nushell/modules/termux-theme.nu");
59 + const active = join(settings, "colors.properties");
60 + const backup = `${active}.before-nugu`;
61 + const keyboard = join(settings, "termux.properties");
62 + const log = join(workspace, "reload.log");
63 + const original = "# original palette\nbackground=#20201d\n";
64 + writeFileSync(active, original);
65 + writeFileSync(keyboard, "extra-keys = []\nfullscreen = true\n");
66 + const env = { ...process.env, HOME: home, USERPROFILE: home, TERMUX_VERSION: "test", PATH: bin, RELOAD_LOG: log };
67 + const invoke = (mode, extraEnv = {}) => run(nu, ["--no-config-file", "-c", `use ${JSON.stringify(module)}; termux-theme ${mode}`], { cwd: home, env: { ...env, ...extraEnv } });
68 + const themes = {};
70 + await t.test("generate both variants from source, matching WezTerm, without selecting a mode", () => {
71 + success(run("just", justArgs, { cwd: nugu }));
72 + assert.equal(readFileSync(active, "utf8"), original);
73 + assert.equal(existsSync(backup), false);
74 + assert.equal(existsSync(join(workspace, "termux/colors.properties")), false);
75 + for (const mode of ["dark", "light"]) {
76 + const filename = `nugu-${mode}.properties`;
77 + const text = readFileSync(join(nugu, "output/termux", filename), "utf8");
78 + themes[mode] = text;
79 + assert.equal(readFileSync(join(workspace, "termux/themes", filename), "utf8"), text);
80 + assert.equal(readFileSync(join(root, "termux/themes", filename), "utf8"), text);
81 + const lines = text.trim().split(/\r?\n/).filter((line) => !line.startsWith("#"));
82 + for (const line of lines) assert.match(line, /^(foreground|background|cursor|color\d+)=#[0-9a-f]{6}$/);
83 + const properties = Object.fromEntries(lines.map((line) => line.split("=")));
84 + assert.equal(Object.keys(properties).length, lines.length, "no duplicate properties");
85 + const palette = JSON.parse(success(run(nu, ["--no-config-file", "-c", `use nugu.nu; nugu ${mode} | get palette | to json`], { cwd: nugu })));
86 + assert.deepEqual(properties, weztermColors(palette));
87 + }
88 + assert.notEqual(themes.dark, themes.light);
89 + failure(run(nu, ["--no-config-file", "-c", "use termux.nu; termux theme invalid"], { cwd: nugu }), /Expected dark or light/);
90 + });
92 + await t.test("login.nu loads the command without a config.nu import", () => {
93 + const login = readFileSync(join(root, "nushell/login.nu"), "utf8");
94 + const imports = login.match(/^use termux-theme\.nu$/gm);
95 + assert.equal(imports?.length, 1);
96 + assert.doesNotMatch(readFileSync(join(root, "nushell/config.nu"), "utf8"), /^use termux-theme\.nu$/m);
97 + const code = `const NU_LIB_DIRS = [${JSON.stringify(join(home, ".config/nushell/modules"))}]; ${imports[0]}; help termux-theme`;
98 + assert.match(success(run(nu, ["--no-config-file", "-c", code], { env })), /termux-theme/);
99 + assert.equal(existsSync(log), false);
100 + });
102 + await t.test("invalid modes and missing Termux support do not write anything", () => {
103 + failure(invoke("invalid"), /Expected dark or light/);
104 + failure(invoke("dark"), /inside Termux/);
105 + assert.equal(readFileSync(active, "utf8"), original);
106 + assert.equal(existsSync(backup), false);
107 + });
109 + const fake = join(bin, "reload.cjs");
110 + writeFileSync(fake, 'require("node:fs").appendFileSync(process.env.RELOAD_LOG, "reload\\n"); process.exit(Number(process.env.RELOAD_EXIT ?? 0));\n');
111 + if (process.platform === "win32") {
112 + writeFileSync(join(bin, "termux-reload-settings.cmd"), `@"${process.execPath}" "${fake}"\r\n`);
113 + } else {
114 + writeFileSync(join(bin, "termux-reload-settings"), `#!${process.execPath}\nrequire(${JSON.stringify(fake)});\n`, { mode: 0o755 });
117 + await t.test("import is inert and non-Termux use is rejected even with a reload executable", () => {
118 + success(run(nu, ["--no-config-file", "-c", `use ${JSON.stringify(module)}`], { env }));
119 + failure(invoke("dark", { TERMUX_VERSION: "" }), /inside Termux/);
120 + assert.equal(existsSync(log), false);
121 + assert.equal(readFileSync(active, "utf8"), original);
122 + });
124 + await t.test("switch both ways, retain the original backup, and preserve touch settings", () => {
125 + for (const mode of ["dark", "light", "dark"]) {
126 + assert.match(success(invoke(mode)), new RegExp(`Termux: Nugu ${mode}`));
127 + assert.equal(readFileSync(active, "utf8"), themes[mode]);
128 + assert.equal(readFileSync(backup, "utf8"), original);
130 + assert.equal(readFileSync(log, "utf8"), "reload\nreload\nreload\n");
131 + assert.equal(readFileSync(keyboard, "utf8"), "extra-keys = []\nfullscreen = true\n");
132 + success(run("just", justArgs, { cwd: nugu }));
133 + assert.equal(readFileSync(active, "utf8"), themes.dark, "regeneration must not select a mode");
134 + });
136 + await t.test("missing palette fails before touching the current theme", () => {
137 + const source = join(workspace, "termux/themes/nugu-light.properties");
138 + rmSync(source);
139 + failure(invoke("light"), /nugu-light/);
140 + assert.equal(readFileSync(active, "utf8"), themes.dark);
141 + writeFileSync(source, themes.light);
142 + });
144 + await t.test("reload failures report that the file was saved, preserving the backup", () => {
145 + failure(invoke("light", { RELOAD_EXIT: "7" }), /saved, but Termux reload failed/);
146 + assert.equal(readFileSync(active, "utf8"), themes.light);
147 + assert.equal(readFileSync(backup, "utf8"), original);
148 + });
150 + await t.test("refuse non-regular active files and backup paths", () => {
151 + for (const file of [active, backup]) {
152 + const content = readFileSync(file, "utf8");
153 + rmSync(file);
154 + mkdirSync(file);
155 + failure(invoke("dark"), /Refusing non-regular file/);
156 + rmSync(file, { recursive: true });
157 + writeFileSync(file, content);
159 + assert.equal(readFileSync(active, "utf8"), themes.light);
160 + });
162 + await t.test("refuse an active symlink instead of overwriting a palette source", { skip: process.platform === "win32" }, () => {
163 + rmSync(active);
164 + const target = join(workspace, "termux/themes/nugu-light.properties");
165 + symlinkSync(target, active);
166 + failure(invoke("dark"), /Refusing non-regular file/);
167 + assert.equal(readFileSync(target, "utf8"), themes.light);
168 + rmSync(active);
169 + });
171 + await t.test("refuse a symlinked settings directory without touching its target", () => {
172 + rmSync(settings, { recursive: true });
173 + const target = join(workspace, "other-settings");
174 + mkdirSync(target);
175 + writeFileSync(join(target, "colors.properties"), original);
176 + symlinkSync(target, settings, "junction");
177 + failure(invoke("dark"), /regular directory/);
178 + assert.equal(readFileSync(join(target, "colors.properties"), "utf8"), original);
179 + rmSync(settings);
180 + });
182 + await t.test("initialize absent settings without inventing a backup", () => {
183 + success(invoke("light"));
184 + assert.equal(readFileSync(active, "utf8"), themes.light);
185 + assert.equal(existsSync(backup), false);
186 + });
187 +});

termux/themes/nugu-dark.properties

Added
new file mode 100644
1 +# Generated by nugu/termux.nu. Do not edit.
2 +foreground=#ededed
3 +background=#121212
4 +cursor=#d86dd8
5 +color0=#121212
6 +color1=#ec6376
7 +color2=#7ba1aa
8 +color3=#f0ee69
9 +color4=#b59ae4
10 +color5=#d151d1
11 +color6=#3ec5e3
12 +color7=#ededed
13 +color8=#8b8b8b
14 +color9=#ec6376
15 +color10=#9aabae
16 +color11=#f0ee69
17 +color12=#dfd3f3
18 +color13=#d86dd8
19 +color14=#c9b6eb
20 +color15=#f3effb
21 +color16=#ed5eed
22 +color17=#dbdbdb
23 +color18=#242424
24 +color19=#9d9d9d
25 +color20=#9aabae
26 +color21=#6f6f6f
27 +color22=#5d5d5d

termux/themes/nugu-light.properties

Added
new file mode 100644
1 +# Generated by nugu/termux.nu. Do not edit.
2 +foreground=#121212
3 +background=#ededed
4 +cursor=#922792
5 +color0=#ededed
6 +color1=#bd172e
7 +color2=#184e5b
8 +color3=#6e6c10
9 +color4=#715696
10 +color5=#ae2eae
11 +color6=#116477
12 +color7=#121212
13 +color8=#3b3b3b
14 +color9=#bd172e
15 +color10=#15333b
16 +color11=#6e6c10
17 +color12=#4f3c69
18 +color13=#922792
19 +color14=#604980
20 +color15=#3e2f53
21 +color16=#a112a1
22 +color17=#242424
23 +color18=#dbdbdb
24 +color19=#292929
25 +color20=#15333b
26 +color21=#5d5d5d
27 +color22=#6f6f6f