"""Shared helpers for CLI commands.""" import re import subprocess from pathlib import Path YELLOW = "\033[33m" RESET = "\033[0m" BASHRC = Path("~/.bashrc").expanduser() SNIPPETS_DIR = Path(__file__).parent.parent.parent / "snippets" def load_snippet(name: str) -> str: """Read a shell snippet from the snippets directory.""" return (SNIPPETS_DIR / f"{name}.sh").read_text() def run(cmd: str, pty: bool = False) -> None: """Print command in yellow then execute it.""" print(f"{YELLOW}$ {cmd}{RESET}") subprocess.run(cmd, shell=True, check=True) def append_bashrc_section(marker: str, content: str) -> None: """Idempotently add a named section to ~/.bashrc.""" begin = f"# BEGIN {marker}" end = f"# END {marker}" section = f"\n{begin}\n{content.strip()}\n{end}\n" text = BASHRC.read_text() text = re.sub( rf"\n?{re.escape(begin)}.*?{re.escape(end)}\n?", "", text, flags=re.DOTALL ) BASHRC.write_text(text + section)