move predefined colorschemes from python to separate template

This commit is contained in:
Lysec
2026-03-04 15:38:50 +01:00
parent ced71e945f
commit f040c81530
10 changed files with 252 additions and 230 deletions
+2 -5
View File
@@ -18,8 +18,7 @@ from .palette import extract_palette
from .quantizer import extract_source_color, source_color_to_rgb
from .theme import generate_theme
from .renderer import TemplateRenderer
from .scheme import expand_predefined_scheme
from .terminal import TerminalColors, TerminalGenerator
from .scheme import expand_predefined_scheme, inject_terminal_colors
__all__ = [
# Color
@@ -55,7 +54,5 @@ __all__ = [
"TemplateRenderer",
# Scheme
"expand_predefined_scheme",
# Terminal
"TerminalColors",
"TerminalGenerator",
"inject_terminal_colors",
]
+41
View File
@@ -308,3 +308,44 @@ def expand_predefined_scheme(scheme_data: dict[str, str], mode: ThemeMode) -> di
"background": background.to_hex(),
"on_background": on_background.to_hex(),
}
def inject_terminal_colors(result: dict[str, str], scheme_mode_data: dict) -> dict[str, str]:
"""Flatten scheme's terminal section into template-ready color keys.
Adds keys like terminal_foreground, terminal_normal_black, terminal_bright_red, etc.
so predefined terminal templates can reference them as
{{colors.terminal_foreground.default.hex_stripped}}.
Args:
result: Expanded color palette dict to augment.
scheme_mode_data: Raw scheme JSON mode data (e.g., scheme_data["dark"]).
Returns:
The same result dict with terminal_ keys added.
"""
terminal = scheme_mode_data.get("terminal")
if not terminal:
return result
# Map of JSON keys to flattened key names
direct_keys = {
"foreground": "terminal_foreground",
"background": "terminal_background",
"cursor": "terminal_cursor",
"cursorText": "terminal_cursor_text",
"selectionFg": "terminal_selection_fg",
"selectionBg": "terminal_selection_bg",
}
for json_key, flat_key in direct_keys.items():
if json_key in terminal:
result[flat_key] = terminal[json_key]
# ANSI normal/bright color groups
for group in ("normal", "bright"):
if group in terminal:
for name, hex_val in terminal[group].items():
result[f"terminal_{group}_{name}"] = hex_val
return result
@@ -54,8 +54,8 @@ from lib import (
read_image, ImageReadError, extract_palette, generate_theme,
TemplateRenderer, expand_predefined_scheme,
extract_source_color, source_color_to_rgb, Color,
TerminalColors, TerminalGenerator
)
from lib.scheme import inject_terminal_colors
def parse_args() -> argparse.Namespace:
@@ -144,12 +144,6 @@ Examples:
help='Theme mode to use for "default" in templates (default: dark)'
)
parser.add_argument(
'--terminal-output',
type=str,
help='JSON mapping of terminal IDs to output paths: {"foot": "/path/to/output", ...}'
)
return parser.parse_args()
@@ -188,9 +182,11 @@ def main() -> int:
if mode in scheme_data:
# Multi-mode format
result[mode] = expand_predefined_scheme(scheme_data[mode], mode)
inject_terminal_colors(result[mode], scheme_data[mode])
elif "mPrimary" in scheme_data:
# Single-mode format - use same colors for requested mode
result[mode] = expand_predefined_scheme(scheme_data, mode)
inject_terminal_colors(result[mode], scheme_data)
else:
print(f"Error: Invalid scheme format - missing '{mode}' or 'mPrimary'", file=sys.stderr)
return 1
@@ -344,52 +340,6 @@ def main() -> int:
else:
renderer.process_config_file(args.config)
# Process terminal output if specified
if args.terminal_output and args.scheme:
try:
terminal_outputs = json.loads(args.terminal_output)
except json.JSONDecodeError as e:
print(f"Error parsing --terminal-output JSON: {e}", file=sys.stderr)
return 1
# Load scheme to check for terminal section
with open(args.scheme, 'r') as f:
scheme_data = json.load(f)
# Determine which mode to use for terminal colors
mode = args.default_mode
# Check if scheme has terminal colors
mode_data = scheme_data.get(mode, scheme_data)
if "terminal" not in mode_data:
print(f"Warning: Scheme has no 'terminal' section for mode '{mode}'", file=sys.stderr)
return 0
try:
# Extract scheme UI colors for derivation (mPrimary, mOnPrimary, mSecondary)
scheme_colors = {
"mPrimary": mode_data.get("mPrimary"),
"mOnPrimary": mode_data.get("mOnPrimary"),
"mSecondary": mode_data.get("mSecondary"),
}
terminal_colors = TerminalColors.from_dict(mode_data["terminal"], scheme_colors)
generator = TerminalGenerator(terminal_colors)
for terminal_id, output_path in terminal_outputs.items():
try:
content = generator.generate(terminal_id)
output_file = Path(output_path).expanduser()
output_file.parent.mkdir(parents=True, exist_ok=True)
output_file.write_text(content)
except ValueError as e:
print(f"Error generating {terminal_id}: {e}", file=sys.stderr)
except IOError as e:
print(f"Error writing {output_path}: {e}", file=sys.stderr)
except KeyError as e:
print(f"Error: Missing required terminal color: {e}", file=sys.stderr)
return 1
return 0