summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorElio Torquet <92248577+elioTrqt@users.noreply.github.com>2025-08-04 01:14:10 -0400
committerGitHub <noreply@github.com>2025-08-04 15:14:10 +1000
commit06a710249011550b128b07d6245685659067512a (patch)
treeac95217b9b0d6662b31a714e2e50355d119490be /src
parentshell: fix log when no log rules (diff)
downloadcaelestia-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')
-rw-r--r--src/caelestia/utils/colour.py28
-rw-r--r--src/caelestia/utils/paths.py2
-rw-r--r--src/caelestia/utils/theme.py34
3 files changed, 63 insertions, 1 deletions
diff --git a/src/caelestia/utils/colour.py b/src/caelestia/utils/colour.py
new file mode 100644
index 0000000..a86c205
--- /dev/null
+++ b/src/caelestia/utils/colour.py
@@ -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()}
diff --git a/src/caelestia/utils/paths.py b/src/caelestia/utils/paths.py
index a4ef36f..923c99c 100644
--- a/src/caelestia/utils/paths.py
+++ b/src/caelestia/utils/paths.py
@@ -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"
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)