theme: add template system (#36)

* user template system

* fix when templates dir doesnt exist

Also color -> colour

---------

Co-authored-by: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>
This commit is contained in:
Elio Torquet
2025-08-04 01:14:10 -04:00
committed by GitHub
parent 2bde2ddfbf
commit 06a7102490
3 changed files with 63 additions and 1 deletions
+28
View File
@@ -0,0 +1,28 @@
class Colour:
_rgb_vals: tuple[int, ...]
_hex_vals: tuple[str, ...]
def __init__(self, hex: str):
hex = hex.ljust(8, "f")
self._hex_vals = tuple(hex[i : i + 2] for i in range(0, 7, 2))
self._rgb_vals = tuple(int(h, 16) for h in self._hex_vals)
@property
def hex(self) -> str:
return "".join(self._hex_vals[:-1])
@property
def hexalpha(self) -> str:
return "".join(self._hex_vals)
@property
def rgb(self) -> str:
return f"rgb({','.join(map(str, self._rgb_vals[:-1]))})"
@property
def rgbalpha(self) -> str:
return f"rgba({','.join(map(str, self._rgb_vals))})"
def get_dynamic_colours(colours: dict[str, str]) -> dict[str, Colour]:
return {name: Colour(code) for name, code in colours.items()}
+2
View File
@@ -17,6 +17,8 @@ c_cache_dir = cache_dir / "caelestia"
cli_data_dir = Path(__file__).parent.parent / "data"
templates_dir = cli_data_dir / "templates"
user_templates_dir = c_config_dir / "templates"
theme_dir = c_state_dir / "theme"
scheme_path = c_state_dir / "scheme.json"
scheme_data_dir = cli_data_dir / "schemes"
+33 -1
View File
@@ -1,7 +1,9 @@
import re
import subprocess
from pathlib import Path
from caelestia.utils.paths import c_state_dir, config_dir, templates_dir
from caelestia.utils.colour import get_dynamic_colours
from caelestia.utils.paths import c_state_dir, config_dir, templates_dir, theme_dir, user_templates_dir
def gen_conf(colours: dict[str, str]) -> str:
@@ -25,6 +27,25 @@ def gen_replace(colours: dict[str, str], template: Path, hash: bool = False) ->
return template
def gen_replace_dynamic(colours: dict[str, str], template: Path) -> str:
def fill_colour(match: re.Match) -> str:
data = match.group(1).strip().split(".")
if len(data) != 2:
return match.group()
col, form = data
if col not in colours_dyn or not hasattr(colours_dyn[col], form):
return match.group()
return getattr(colours_dyn[col], form)
# match atomic {{ . }} pairs
field = r"\{\{((?:(?!\{\{|\}\}).)*)\}\}"
colours_dyn = get_dynamic_colours(colours)
template_content = template.read_text()
template_filled = re.sub(field, fill_colour, template_content)
return template_filled
def c2s(c: str, *i: list[int]) -> str:
"""Hex to ANSI sequence (e.g. ffffff, 11 -> \x1b]11;rgb:ff/ff/ff\x1b\\)"""
return f"\x1b]{';'.join(map(str, i))};rgb:{c[0:2]}/{c[2:4]}/{c[4:6]}\x1b\\"
@@ -142,6 +163,16 @@ def apply_qt(colours: dict[str, str], mode: str) -> None:
write_file(config_dir / f"qt{ver}ct/qt{ver}ct.conf", conf)
def apply_user_templates(colours: dict[str, str]) -> None:
if not user_templates_dir.is_dir():
return
for file in user_templates_dir.iterdir():
if file.is_file():
content = gen_replace_dynamic(colours, file)
write_file(theme_dir / file.name, content)
def apply_colours(colours: dict[str, str], mode: str) -> None:
apply_terms(gen_sequences(colours))
apply_hypr(gen_conf(colours))
@@ -151,3 +182,4 @@ def apply_colours(colours: dict[str, str], mode: str) -> None:
apply_btop(colours)
apply_gtk(colours, mode)
apply_qt(colours, mode)
apply_user_templates(colours)