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)

+414 / −1 text lines

Diff layout

mise.toml

Modified

+14 / −0 text lines

6666run("niri", "validate", "-c", "niri/config.kdl");
6767'''
69[tasks.nugu-termux]
70description = "Generate both Nugu Termux palettes without selecting a mode"
71run = '''
72cd("nugu");
73run("just", "--shell", "nu", "--clear-shell-args", "--shell-arg=--no-config-file", "--shell-arg=--commands", "termux");
74'''
76[tasks.termux-test]
77description = "Test Nugu Termux generation and manual theme switching"
78run = '''
79run("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)'} } }");
80run("node", "--test", "termux/theme.test.mjs");
81'''
6983[tasks.wezterm-test]
7084description = "Test WezTerm modernization and instance helpers"
7185run = '''

nugu/README.md

Modified

+9 / −0 text lines

1212Layout, effects, and keybindings remain untouched.
1313Validate 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`.
18Run `mise run nugu-termux` from the dotfiles root to generate both variants into `../termux/themes/`.
19Generation does not select a theme.
20The Nushell command `termux-theme dark` or `termux-theme light` selects one manually and reloads Termux.
21See [Termux setup and recovery](../termux/README.md).
22Validate generation and switching with `mise run termux-test` from the dotfiles root.
1524## TODO
1726* [x] helix

nugu/justfile

Modified

+11 / −1 text lines

1616 open aerc.nu | nu-check --as-module --debug aerc.nu
1717 open neovim.nu | nu-check --as-module --debug neovim.nu
1818 open niri.nu | nu-check --as-module --debug niri.nu
19 open termux.nu | nu-check --as-module --debug termux.nu
2021fmt: lint
2122 taplo fmt
7980 mkdir ../zed/themes
8081 cp {{output}}/zed/nugu.json ../zed/themes/nugu.json
83# Generate both modes without changing Termux's active colors.properties.
84termux:
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
8292# Generate and install both modes; Niri's config selects one via include.
8393niri:
8494 mkdir {{output}}/niri
128138 cp {{output}}/aerc/nugu-dark ../aerc/stylesets/nugu-dark
129139 cp {{output}}/aerc/nugu-light ../aerc/stylesets/nugu-light
131install: helix wezterm ghostty zed linux-console hypr aerc yazi nvim niri
141install: helix wezterm ghostty zed linux-console hypr aerc yazi nvim niri termux

nugu/termux.nu

Added
new file mode 100644

+46 / −0 text lines

1use nugu.nu
2
3# Match WezTerm's Nugu mapping, using only Termux-supported properties.
4export 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)
46}

nushell/login.nu

Modified

+1 / −0 text lines

1212use yazi.nu *
1313use akku.nu
1414use colors.nu
15use termux-theme.nu
1516use commands.nu *
1617use syncthing.nu
1718use minimax.nu

nushell/modules/termux-theme.nu

Added
new file mode 100644

+42 / −0 text lines

1# Resolve the module itself first because ~/.config/nushell is a symlink.
2const themes = (path self | path expand | path dirname | path join ".." ".." "termux" "themes" | path expand)
3
4def modes [] { [dark light] }
5
6# Select a generated Nugu palette and reload Termux without restarting sessions.
7export 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)"
42}

termux/README.md

Added
new file mode 100644

+50 / −0 text lines

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

termux/theme.test.mjs

Added
new file mode 100644

+187 / −0 text lines

1import assert from "node:assert/strict";
2import { spawnSync } from "node:child_process";
3import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
4import { tmpdir } from "node:os";
5import { dirname, join, resolve } from "node:path";
6import { fileURLToPath } from "node:url";
7import test from "node:test";
8
9const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
10const justArgs = ["--shell", "nu", "--clear-shell-args", "--shell-arg=--no-config-file", "--shell-arg=--commands", "termux"];
12function 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;
16}
18function success(result) {
19 assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`);
20 return result.stdout;
21}
23function failure(result, message) {
24 assert.notEqual(result.status, 0);
25 assert.match(result.stderr, message);
26}
28const nu = success(run("nu", ["--no-config-file", "-c", "$nu.current-exe"])).trim();
30function 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;
44}
46test("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 });
115 }
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);
129 }
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);
158 }
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

+27 / −0 text lines

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

termux/themes/nugu-light.properties

Added
new file mode 100644

+27 / −0 text lines

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