diff --git a/todoist_taskwarrior/cli.py b/todoist_taskwarrior/cli.py index f76c233..459bc80 100644 --- a/todoist_taskwarrior/cli.py +++ b/todoist_taskwarrior/cli.py @@ -29,10 +29,8 @@ def synchronize(): ~/.todoist-sync """ - - io.important('Syncing tasks with todoist... ', nl=False) - todoist.sync() - io.success('OK') + with io.with_feedback('Syncing tasks with todoist'): + todoist.sync() @cli.command() @@ -100,9 +98,8 @@ def add_task(tid, name, project, tags, priority, entry, due, recur): Returns the taskwarrior task. """ - io.info(f"Importing '{name}' ({project}) - ", nl=False) - try: - tw_task = taskwarrior.task_add( + with io.with_feedback(f"Importing '{name}' ({project})"): + return taskwarrior.task_add( name, project=project, tags=tags, @@ -112,11 +109,6 @@ def add_task(tid, name, project, tags, priority, entry, due, recur): recur=recur, todoist_id=tid, ) - except: - io.error('FAILED') - else: - io.success('OK') - return tw_task def task_prompt(**task_data): diff --git a/todoist_taskwarrior/io.py b/todoist_taskwarrior/io.py index 2ae0d61..3daed50 100644 --- a/todoist_taskwarrior/io.py +++ b/todoist_taskwarrior/io.py @@ -1,5 +1,6 @@ """Utilities for pretty output """ +import contextlib from click import echo, prompt as cprompt, style @@ -47,3 +48,16 @@ def task(task): output += f'{key}: {value}\n' echo(output) + + +@contextlib.contextmanager +def with_feedback(description, success_status='OK', error_status='FAILED'): + info(f'{description}... ', nl=False) + try: + yield + except Exception as e: + error(f'{error_status} ({e})') + raise + else: + success(success_status) +