33 lines
934 B
Python
33 lines
934 B
Python
from pathlib import Path
|
|
|
|
YELLOW = "\033[33m"
|
|
RESET = "\033[0m"
|
|
|
|
BASHRC = "~/.bashrc"
|
|
SNIPPETS_DIR = Path(__file__).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(c, cmd: str) -> None:
|
|
"""Print command in yellow then execute it with a pty."""
|
|
print(f"{YELLOW}$ {cmd}{RESET}")
|
|
c.run(cmd, pty=True)
|
|
|
|
|
|
def append_bashrc_section(c, marker: str, content: str) -> None:
|
|
"""Idempotently add a named section to ~/.bashrc, replacing it if it already exists."""
|
|
import re
|
|
|
|
begin = f"# BEGIN {marker}"
|
|
end = f"# END {marker}"
|
|
section = f"\n{begin}\n{content.strip()}\n{end}\n"
|
|
|
|
bashrc = Path(BASHRC).expanduser()
|
|
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)
|