aboutsummaryrefslogtreecommitdiffstats
path: root/build
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-29 10:40:57 +0200
committerDanilo M. <danix@danix.xyz>2026-09-29 10:40:57 +0200
commita955a93101aae45bca030c4ea9074ddda86a2fc9 (patch)
tree6ed641634a51eca252d5912de7593f9f8f2347e8 /build
parenta08ffb69ac7553350e1e5259bb0101ea997a70ae (diff)
downloadgrub-theme-udt-a955a93101aae45bca030c4ea9074ddda86a2fc9.tar.gz
grub-theme-udt-a955a93101aae45bca030c4ea9074ddda86a2fc9.zip
Title the panel with the machine's pretty name
Read PRETTY_HOSTNAME from /etc/machine-info, falling back to NAME from /etc/os-release. The name stays on the machine rather than in the repo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'build')
-rwxr-xr-xbuild32
1 files changed, 26 insertions, 6 deletions
diff --git a/build b/build
index 5e14ce0..5397388 100755
--- a/build
+++ b/build
@@ -13,6 +13,7 @@ dist/udt/ untouched. Never touches /boot: root copies dist/udt/ there.
"""
import os
import re
+import shlex
import shutil
import struct
import subprocess
@@ -132,15 +133,27 @@ def font(fname, pattern, size, name):
return cached
-def os_name():
- for line in Path("/etc/os-release").read_text().splitlines():
- if line.startswith("NAME="):
- return line[5:].strip('"')
- return "GNU/Linux"
+def shell_var(path, key):
+ """KEY's value from a KEY="value" file (machine-info, os-release), or None."""
+ try:
+ lines = Path(path).read_text().splitlines()
+ except FileNotFoundError:
+ return None
+ for line in lines:
+ if line.startswith(f"{key}="):
+ return " ".join(shlex.split(line[len(key) + 1:])) or None
+ return None
+
+
+def title():
+ """The machine's pretty name if root has set one, else the OS name."""
+ name = (shell_var("/etc/machine-info", "PRETTY_HOSTNAME")
+ or shell_var("/etc/os-release", "NAME") or "GNU/Linux")
+ return name.replace('"', "'") # it lands inside a quoted theme.txt string
def assemble(stage, text, c, wallpaper, fonts):
- (stage / "theme.txt").write_text(text.replace("%TITLE%", os_name()))
+ (stage / "theme.txt").write_text(text.replace("%TITLE%", title()))
slices = {
"panel.png": (8, 8, (*c["bg_alt"], PANEL_ALPHA)),
# The selected item: a 4px accent bar on the left, surface behind.
@@ -183,6 +196,13 @@ def selftest():
assert set(re.findall(r"@[A-Z_]+@", tpl)) <= PLACEHOLDERS, "template token udt does not fill"
for _, _, name in FONTS.values():
assert f'"{name}"' in tpl, f"template does not use font {name}"
+ with tempfile.NamedTemporaryFile("w") as f:
+ f.write('PRETTY_HOSTNAME="A B\'s C"\nEMPTY=\n')
+ f.flush()
+ assert shell_var(f.name, "PRETTY_HOSTNAME") == "A B's C"
+ assert shell_var(f.name, "EMPTY") is None
+ assert shell_var(f.name, "MISSING") is None
+ assert shell_var("/nonexistent", "X") is None
print("selftest ok")