ref: Print optional error message on editor process error

Editor function takes an optional io object which is used to print an
error output if the subprocess errors.
This commit is contained in:
Marty Oehme 2025-11-29 18:42:33 +01:00
parent 762b4a288f
commit 28c551e157
Signed by: Marty
GPG key ID: 4E535BC19C61886E
2 changed files with 15 additions and 3 deletions

View file

@ -9,3 +9,11 @@ def test_open_editor_escapes_shell():
with patch("subprocess.run") as run_mock: with patch("subprocess.run") as run_mock:
open_editor(Path("my note$1.txt"), "vim") open_editor(Path("my note$1.txt"), "vim")
run_mock.assert_called_once_with(["vim", "my note$1.txt"], check=True) run_mock.assert_called_once_with(["vim", "my note$1.txt"], check=True)
#
# def test_add_annotation_saves_task():
# task = Mock()
# add_annotation(task, "hello")
# task.add_annotation.assert_called_once_with("hello")
# task.save.assert_called_once()

View file

@ -65,7 +65,7 @@ def main(cfg: "TConf | None" = None, io: "_IO | None" = None) -> int:
_ensure_parent_dir(fpath) _ensure_parent_dir(fpath)
io.out(f"Editing note: {fpath}") io.out(f"Editing note: {fpath}")
open_editor(fpath, editor=cfg.notes_editor) open_editor(fpath, editor=cfg.notes_editor, io=io)
if fpath.exists(): if fpath.exists():
if is_annotation_missing(task, annotation_content=cfg.notes_annot): if is_annotation_missing(task, annotation_content=cfg.notes_annot):
@ -95,9 +95,13 @@ def get_notes_file(uuid: str, notes_dir: Path, notes_ext: str) -> Path:
return Path(notes_dir).joinpath(f"{uuid}.{notes_ext}") return Path(notes_dir).joinpath(f"{uuid}.{notes_ext}")
def open_editor(file: Path, editor: str) -> None: def open_editor(file: Path, editor: str, io: "_IO | None" = None) -> None:
"""Opens a file with the chosen editor.""" """Opens a file with the chosen editor."""
_ = subprocess.run([editor, str(file)], check=True) try:
_ = subprocess.run([editor, str(file)], check=True)
except subprocess.CalledProcessError:
if io:
io.err("Editor exited with an error, aborting.\n")
def is_annotation_missing(task: Task, annotation_content: str) -> bool: def is_annotation_missing(task: Task, annotation_content: str) -> bool: