From d4d772e9391a299b50b811cc3a26264eaa68cd47 Mon Sep 17 00:00:00 2001 From: Felix Wittwer Date: Fri, 11 Dec 2015 16:43:40 +0100 Subject: [PATCH] Add edit support, close #23 --- tdo/main.py | 10 ++++++++++ tdo/todolist/help.py | 1 + tdo/todolist/listmanagement.py | 27 +++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/tdo/main.py b/tdo/main.py index 23bdf69..a93e9aa 100755 --- a/tdo/main.py +++ b/tdo/main.py @@ -59,6 +59,16 @@ def main(argv=sys.argv): if ret_val[1]: # if there were changes, save them todolist.save(ret_val[0]) + elif argv[1] == 'edit': + # tdo edit x - edit task no. x + if len(argv) < 3: + print('Please enter a task ID!') + else: + ret_val = todolist.edit(todos, argv[2]) + + if ret_val[1]: + # save changes, if there are any + todolist.save(ret_val[0]) elif argv[1] == 'newlist': # tdo newlist "name" -- create list "name" if len(argv) < 3: diff --git a/tdo/todolist/help.py b/tdo/todolist/help.py index d298a13..dd1acd6 100644 --- a/tdo/todolist/help.py +++ b/tdo/todolist/help.py @@ -4,6 +4,7 @@ tdo Lists all undone tasks, sorted by category. tdo all Lists all tasks. tdo add "task" [list] Add a task to a certain list or the default list. +tdo edit id Edit a task description. tdo done id Mark tha task with the ID 'id' as done. tdo newlist "name" Create a new list named 'name' tdo remove "list" Delete the list 'list' diff --git a/tdo/todolist/listmanagement.py b/tdo/todolist/listmanagement.py index ab80d2c..35328b0 100644 --- a/tdo/todolist/listmanagement.py +++ b/tdo/todolist/listmanagement.py @@ -25,6 +25,28 @@ def add(todolist, todoid, todoname, category='default'): return todolist, True, todoid +def edit(todolist, task_id): + found = False + changed = False + for category in todolist: + for item in todolist[category]: + if item == str(task_id): + found = True + print('Task #{num}: {content}'.format( + num=task_id, content=todolist[category][task_id][0])) + inp = input("Enter your new task description \ +(leave blank for abort): ") + if inp != '': + todolist[category][task_id][0] = inp + changed = True + + if not found: + print('There is no task with the ID {num}.'.format(num=task_id)) + return todolist, False + else: + return todolist, changed + + def done(todolist, task_id): ''' Marks an item as done. @@ -51,10 +73,7 @@ def done(todolist, task_id): print('There is no task with the ID {num}.'.format(num=task_id)) return todolist, False else: - if changed: - return todolist, True - else: - return todolist, False + return todolist, changed def addlist(todolist, new_category):