fix: Do not print annotation added if it already exists
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:
parent
46135f9325
commit
9b2dc37279
1 changed files with 15 additions and 8 deletions
23
topen.py
23
topen.py
|
|
@ -70,8 +70,9 @@ def main(cfg: "TConf | None" = None, io: "_IO | None" = None) -> int:
|
|||
open_editor(fpath, editor=cfg.notes_editor)
|
||||
|
||||
if fpath.exists():
|
||||
add_annotation_if_missing(task, annotation_content=cfg.notes_annot)
|
||||
io.out(f"Added annotation: {cfg.notes_annot}")
|
||||
if is_annotation_missing(task, annotation_content=cfg.notes_annot):
|
||||
add_annotation(task, annotation_content=cfg.notes_annot)
|
||||
io.out(f"Added annotation: {cfg.notes_annot}")
|
||||
return 0
|
||||
io.out("No note file, doing nothing.")
|
||||
return 0
|
||||
|
|
@ -101,16 +102,22 @@ def open_editor(file: Path, editor: str) -> None:
|
|||
_ = subprocess.run(f"{editor} {file}", shell=True)
|
||||
|
||||
|
||||
def add_annotation_if_missing(task: Task, annotation_content: str) -> None:
|
||||
"""Conditionally adds an annotation to a task.
|
||||
def is_annotation_missing(task: Task, annotation_content: str) -> bool:
|
||||
"""Checks if the task is missing the annotation.
|
||||
|
||||
Only adds the annotation if the task does not yet have an
|
||||
annotation with exactly that content (i.e. avoids
|
||||
duplication).
|
||||
Only succeeds if the _complete_ annatotation is found,
|
||||
and not just as a substring.
|
||||
|
||||
Returns True if annotation was added, otherwise False.
|
||||
"""
|
||||
for annot in task["annotations"] or []:
|
||||
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)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue