skin_cmd.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """``hermes skin`` — list, switch, and tweak skins from the CLI.
  2. ``set`` is the load-bearing verb: it changes ONE color of the ACTIVE skin **in place**, so tweaking
  3. (say) the tool marker never disturbs the rest of the look — background included. Editing the file
  4. bumps its mtime; the gateway's skin watcher repaints every live surface within ~a second.
  5. """
  6. from __future__ import annotations
  7. import argparse
  8. import re
  9. import sys
  10. from pathlib import Path
  11. from hermes_constants import display_hermes_home, get_hermes_home
  12. _HEX_RE = re.compile(r"^#[0-9a-fA-F]{6}$")
  13. def _skins_dir() -> Path:
  14. return get_hermes_home() / "skins"
  15. def _active_skin() -> str:
  16. from hermes_cli.config import load_config
  17. display = (load_config() or {}).get("display") or {}
  18. return str(display.get("skin") or "default")
  19. def _use(name: str) -> None:
  20. """Activate a skin (persists display.skin via the shared config writer)."""
  21. from hermes_cli.config import config_command
  22. config_command(argparse.Namespace(config_command="set", key="display.skin", value=name, force=True))
  23. def _skin_set(key: str, value: str, skin: str | None) -> int:
  24. import hermes_yaml as yaml
  25. if not _HEX_RE.match(value):
  26. print(f"✗ {value!r} is not a #rrggbb hex color", file=sys.stderr)
  27. return 1
  28. name = skin or _active_skin()
  29. path = _skins_dir() / f"{name}.yaml"
  30. if path.exists():
  31. data = yaml.safe_load(path.read_text(encoding="utf-8-sig")) or {}
  32. target = name
  33. else:
  34. # Built-in (or missing): fork into an editable copy that keeps its full palette, under a
  35. # fresh name so the built-in stays intact for revert.
  36. from hermes_cli.skin_engine import load_skin
  37. resolved = load_skin(name)
  38. target = f"{name}-custom"
  39. path = _skins_dir() / f"{target}.yaml"
  40. data = {"name": target, "description": f"{name} + custom {key}",
  41. "colors": dict(resolved.colors), "branding": dict(resolved.branding),
  42. "tool_prefix": resolved.tool_prefix}
  43. if not isinstance(data.get("colors"), dict):
  44. data["colors"] = {}
  45. data["colors"][key] = value
  46. data.setdefault("name", target)
  47. # Atomic write: write_text truncates with no fsync; safe_load("") → None → {} would
  48. # permanently lose the palette on the next set.
  49. # See #51356.
  50. from utils import atomic_yaml_write
  51. atomic_yaml_write(path, data, sort_keys=False)
  52. if target != name:
  53. _use(target)
  54. print(f"✓ {key} = {value} in {display_hermes_home()}/skins/{target}.yaml (live within ~1s)")
  55. return 0
  56. def _skin_list() -> int:
  57. from hermes_cli.skin_engine import list_skins
  58. active = _active_skin()
  59. for s in list_skins():
  60. mark = "*" if s["name"] == active else " "
  61. print(f"{mark} {s['name']:<16} {s.get('source', ''):<8} {s.get('description', '')}")
  62. return 0
  63. def skin_command(args) -> None:
  64. """Dispatch ``hermes skin <verb>``."""
  65. verb = getattr(args, "skin_command", None)
  66. if verb == "set":
  67. sys.exit(_skin_set(args.key, args.value, getattr(args, "skin", None)))
  68. elif verb == "use":
  69. _use(args.name)
  70. print(f"✓ active skin → {args.name} (live within ~1s)")
  71. else: # list / default
  72. sys.exit(_skin_list())