Files
einvoice/jr_einvoice/editor.py

21 lines
685 B
Python

from __future__ import annotations
import os
import shutil
import subprocess
from pathlib import Path
def open_in_editor(path: Path, logger) -> None:
# Prefer $EDITOR, then common editors
editor = os.environ.get("EDITOR")
if not editor:
for cand in ["code", "codium", "nano", "vim", "vi"]:
if shutil.which(cand):
editor = cand
break
if not editor:
raise RuntimeError("No editor found. Set $EDITOR or install nano/vim/code.")
logger.info("Opening editor: %s %s", editor, path)
# Split editor if it contains args (simple)
parts = editor.split()
subprocess.run(parts + [str(path)], check=False)