Handle show all vs show completed

This commit is contained in:
Gürkan Gür 2022-08-09 10:46:38 +02:00
parent eeaa19b724
commit 01a246c15e
2 changed files with 49 additions and 38 deletions

View file

@ -16,7 +16,6 @@
- [ ] Add zsh completion
- [ ] Support projects with spaces in their names
- [ ] Separate "show all" vs "show completed"
- [ ] Add a flake for easy installing
### Usage

View file

@ -118,6 +118,13 @@ def get_args():
action='store_true',
help='Print all tasks (overrides default project, if there is one)'
)
listparser.add_argument(
'-c',
'--completed',
dest='show_completed',
action='store_true',
help='Print completed tasks too'
)
subparsers.add_parser(
'del',
formatter_class=argparse.RawTextHelpFormatter,
@ -220,42 +227,29 @@ def get_tasks(
def fill_table(
table,
tasks: Dict,
show_completed: bool
show_completed: bool,
):
counter = 0
for _, task in tasks.items():
counter += 1
if show_completed:
table.add_row(
# '✅' if task['completed'] else "❌",
task['project'],
f'[strike]{task["content"]}' if task['completed'] else task['content'],
','.join(task['labels']) if task['labels'] else '--',
task['alias'],
style='italic' if counter % 2 == 0 else None
# style=f"magenta{' on white' if counter % 2 == 0 else ''}"
)
else:
table.add_row(
# str(task_id),
task['project'],
task['content'],
','.join(task['labels']) if task['labels'] else '--',
task['alias'],
style='italic' if counter % 2 == 0 else None
# style=f"magenta{' on white' if counter % 2 == 0 else ''}"
)
if task['completed'] and not show_completed:
continue
table.add_row(
task['project'],
f'[strike]{task["content"]}' if task['completed'] else task['content'],
','.join(task['labels']) if task['labels'] else '--',
task['alias'],
style='italic' if counter % 2 == 0 else None
)
def _get_task_id_from_alias(tasks: Dict, alias: str):
def _get_task_details_from_alias(tasks: Dict, alias: str) -> Tuple[
Dict, str
]:
for task_id, values in tasks.items():
if alias == values['alias']:
return task_id
from pprint import pprint
pprint(tasks)
pprint(alias)
exit(0)
# XXX: Handle None
return task_id, values['content']
raise Exception(f'Can\'t find any task for alias {alias}. Sync needed?')
@timeit
@ -273,13 +267,20 @@ def delete_tasks(
successfully_removed = []
for task_alias in command:
todoist_id = _get_task_id_from_alias(tasks=tasks, alias=task_alias)
todoist_id, content = _get_task_details_from_alias(
tasks=tasks,
alias=task_alias
)
assert type(todoist_id) is int
try:
api.delete_task(task_id=todoist_id)
print(f'{task_alias} is gone 👋')
print(f'{content} is gone 👋')
successfully_removed.append(todoist_id)
except Exception as e:
print(f'{task_alias} is borked ☠️ : {e}')
print(
f'Todoist borked while deleting {content}'
f'({task_alias}) ☠️ : {e}'
)
return successfully_removed
@ -300,13 +301,20 @@ def close_tasks(
successfully_closed = []
for task_alias in command:
todoist_id = _get_task_id_from_alias(tasks=tasks, alias=task_alias)
todoist_id, content = _get_task_details_from_alias(
tasks=tasks,
alias=task_alias
)
assert type(todoist_id) is int
try:
api.close_task(task_id=todoist_id)
print(f'{task_alias} completed 💪')
print(f'{content} completed 💪')
successfully_closed.append(todoist_id)
except Exception as e:
print(f'{task_alias} is borked while closing ☠️ : {e}')
print(
f'Todoist borked while closing {content}'
f'({task_alias}) ☠️ : {e}'
)
return successfully_closed
@ -340,7 +348,7 @@ def list_tasks(
parsed_task: Dict,
cache: Dict,
json: bool = False,
show_all: bool = False
show_completed: bool = False
):
if not json:
table = get_table()
@ -359,7 +367,11 @@ def list_tasks(
print('No tasks found 🤷')
sys.exit(0)
fill_table(table, tasks, show_completed=show_all)
fill_table(
table=table,
tasks=tasks,
show_completed=show_completed
)
console = Console()
console.print(table)
@ -533,7 +545,7 @@ def main():
parsed_task=parsed_task,
json=args.json,
cache=cache,
show_all=args.show_all
show_completed=args.show_completed
)
elif operation == 'add':
new_cache = add_tasks(