fix: Do not print annotation added if it already exists
Some checks are pending
website / build (push) Waiting to run
website / deploy (push) Blocked by required conditions

Turned annotation adding into a separate (pure) checking function to see
if we have to add an annotation, and a (side-effect) function which
actually adds it.

This way we can decouple checking and send an info to io only if it is
necessary while keeping the two tasks of checking and adding separate.
This commit is contained in:
Marty Oehme 2025-11-28 23:00:09 +01:00
parent 46135f9325
commit 9b2dc37279
Signed by: Marty
GPG key ID: 4E535BC19C61886E

View file

@ -70,8 +70,9 @@ def main(cfg: "TConf | None" = None, io: "_IO | None" = None) -> int:
open_editor(fpath, editor=cfg.notes_editor) open_editor(fpath, editor=cfg.notes_editor)
if fpath.exists(): if fpath.exists():
add_annotation_if_missing(task, annotation_content=cfg.notes_annot) if is_annotation_missing(task, annotation_content=cfg.notes_annot):
io.out(f"Added annotation: {cfg.notes_annot}") add_annotation(task, annotation_content=cfg.notes_annot)
io.out(f"Added annotation: {cfg.notes_annot}")
return 0 return 0
io.out("No note file, doing nothing.") io.out("No note file, doing nothing.")
return 0 return 0
@ -101,16 +102,22 @@ def open_editor(file: Path, editor: str) -> None:
_ = subprocess.run(f"{editor} {file}", shell=True) _ = subprocess.run(f"{editor} {file}", shell=True)
def add_annotation_if_missing(task: Task, annotation_content: str) -> None: def is_annotation_missing(task: Task, annotation_content: str) -> bool:
"""Conditionally adds an annotation to a task. """Checks if the task is missing the annotation.
Only adds the annotation if the task does not yet have an Only succeeds if the _complete_ annatotation is found,
annotation with exactly that content (i.e. avoids and not just as a substring.
duplication).
Returns True if annotation was added, otherwise False.
""" """
for annot in task["annotations"] or []: for annot in task["annotations"] or []:
if annot["description"] == annotation_content: if annot["description"] == annotation_content:
return return False
return True
def add_annotation(task: Task, annotation_content: str) -> None:
"""Adds an annotation to a task."""
task.add_annotation(annotation_content) task.add_annotation(annotation_content)