From 3ef552bbe53c3c4453f7d02d06643a585a948c51 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 29 Nov 2025 18:42:33 +0100 Subject: [PATCH] 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. --- test/test_cli.py | 8 ++++++++ topen.py | 10 +++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/test/test_cli.py b/test/test_cli.py index fe70abb..1d9601b 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -9,3 +9,11 @@ def test_open_editor_escapes_shell(): with patch("subprocess.run") as run_mock: open_editor(Path("my note$1.txt"), "vim") 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() diff --git a/topen.py b/topen.py index 03f559a..b5374e1 100755 --- a/topen.py +++ b/topen.py @@ -65,7 +65,7 @@ def main(cfg: "TConf | None" = None, io: "_IO | None" = None) -> int: _ensure_parent_dir(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 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}") -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.""" - _ = 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: