Add ahead-of-time compilation for cli and optimize thumbnail caching
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 46s
Python / lint-format (pull_request) Successful in 49s
Python / test (pull_request) Successful in 47s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m42s
C++ / build (pull_request) Successful in 3m7s

This commit is contained in:
2026-07-08 12:23:45 +02:00
parent ffefd3e202
commit 6d5609fe5b
7 changed files with 97 additions and 31 deletions
+32 -14
View File
@@ -73,7 +73,8 @@ def _complete_accent(ctx, incomplete):
@app.command()
def list_presets(
json_format: bool = typer.Option(False, "--json", help="Output in JSON format"),
json_format: bool = typer.Option(
False, "--json", help="Output in JSON format"),
):
schemes = list_schemes()
if json_format:
@@ -139,7 +140,7 @@ def generate(
HOME = str(os.getenv("HOME"))
OUTPUT = Path(HOME + "/.local/state/zshell/scheme.json")
SEQ_STATE = Path(HOME + "/.local/state/zshell/sequences.txt")
THUMB_PATH = Path(HOME + "/.cache/zshell/imagecache/thumbnail.jpg")
THUMB_DIR = Path(HOME + "/.cache/zshell/imagecache/thumbnails")
WALL_DIR_PATH = Path(HOME + "/.local/state/zshell/wallpaper_path.json")
TEMPLATE_DIR = Path(HOME + "/.config/zshell/templates")
@@ -147,7 +148,8 @@ def generate(
CONFIG = Path(HOME + "/.config/zshell/config.json")
if preset is not None and image_path is not None:
raise typer.BadParameter("Use either --image-path or --preset, not both.")
raise typer.BadParameter(
"Use either --image-path or --preset, not both.")
def get_scheme_class(scheme_name: str):
match scheme_name:
@@ -273,7 +275,8 @@ def generate(
diff = difference_degrees(from_hct.hue, to_hct.hue)
rotation = min(diff * 0.8, 100)
output_hue = sanitize_degrees_double(
from_hct.hue + rotation * rotation_direction(from_hct.hue, to_hct.hue)
from_hct.hue + rotation *
rotation_direction(from_hct.hue, to_hct.hue)
)
tone = max(0.0, min(100.0, from_hct.tone * (1 + tone_boost)))
return Hct.from_hct(output_hue, from_hct.chroma, tone)
@@ -307,15 +310,26 @@ def generate(
return out
def generate_thumbnail(image_path, thumbnail_path, size=(128, 128)):
thumbnail_file = Path(thumbnail_path)
def thumbnail_cache_path(image_path: Path, thumb_dir: Path) -> Path:
stat = image_path.stat()
key = f"{image_path.stem}_{stat.st_size}_{int(stat.st_mtime)}"
safe_key = re.sub(r"[^A-Za-z0-9._-]", "_", key)
return thumb_dir / f"{safe_key}_thumbnail.jpg"
def generate_thumbnail(image_path: Path, thumb_dir: Path, size=(128, 128)) -> Path:
thumb_dir.mkdir(parents=True, exist_ok=True)
cache_path = thumbnail_cache_path(image_path, thumb_dir)
if cache_path.exists():
return cache_path
image = Image.open(image_path)
image.draft("RGB", size)
image = image.convert("RGB")
image.thumbnail(size, Image.Resampling.NEAREST)
image.save(cache_path, "JPEG")
thumbnail_file.parent.mkdir(parents=True, exist_ok=True)
image.save(thumbnail_path, "JPEG")
return cache_path
def apply_terms(sequences: str, sequences_tmux: str, state_path: Path) -> None:
state_path.parent.mkdir(parents=True, exist_ok=True)
@@ -523,7 +537,8 @@ def generate(
template = env.from_string(body)
text = template.render(**context)
except Exception as e:
raise RuntimeError(f"Template render failed for '{rel}': {e}") from e
raise RuntimeError(
f"Template render failed for '{rel}': {e}") from e
out_path.write_text(text, encoding="utf-8")
@@ -586,7 +601,8 @@ def generate(
(v.accents for v in meta.variants if v.id == p_variant), ()
)
if accent not in var_accents:
available = ", ".join(var_accents) if var_accents else "none"
available = ", ".join(
var_accents) if var_accents else "none"
raise typer.BadParameter(
f"Accent '{accent}' not available for '{p_scheme}:{p_variant}'. Available accents: {available}"
)
@@ -623,13 +639,13 @@ def generate(
seed = hex_to_hct(colors.get("primary", "#000000").lstrip("#"))
else:
image_path = image_path or Path(WALL_PATH)
generate_thumbnail(image_path, str(THUMB_PATH))
seed = seed_from_image(THUMB_PATH)
thumb_path = generate_thumbnail(image_path, THUMB_DIR)
seed = seed_from_image(thumb_path)
name = "dynamic"
flavor = "default"
if smart:
effective_mode = smart_mode(THUMB_PATH)
effective_mode = smart_mode(thumb_path)
elif mode is not None:
effective_mode = mode
else:
@@ -675,7 +691,9 @@ def generate(
print(f"rendered: {p}")
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
with open(OUTPUT, "w") as f:
tmp_output = OUTPUT.with_suffix(".json.tmp")
with open(tmp_output, "w") as f:
json.dump(output_dict, f, indent=4)
os.replace(tmp_output, OUTPUT)
except Exception as e:
print(f"Error: {e}")