diff options
| author | Elio Torquet <92248577+elioTrqt@users.noreply.github.com> | 2025-08-04 01:14:10 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-04 15:14:10 +1000 |
| commit | 06a710249011550b128b07d6245685659067512a (patch) | |
| tree | ac95217b9b0d6662b31a714e2e50355d119490be /src/caelestia/utils/theme.py | |
| parent | shell: fix log when no log rules (diff) | |
| download | caelestia-cli-06a710249011550b128b07d6245685659067512a.tar.gz caelestia-cli-06a710249011550b128b07d6245685659067512a.tar.bz2 caelestia-cli-06a710249011550b128b07d6245685659067512a.zip | |
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>
Diffstat (limited to 'src/caelestia/utils/theme.py')
| -rw-r--r-- | src/caelestia/utils/theme.py | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/caelestia/utils/theme.py b/src/caelestia/utils/theme.py index efc2847..6136418 100644 --- a/src/caelestia/utils/theme.py +++ b/src/caelestia/utils/theme.py @@ -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) |