diff options
author | Santo Cariotti <sancn@live.com> | 2017-08-22 11:51:16 +0200 |
---|---|---|
committer | Santo Cariotti <sancn@live.com> | 2017-08-22 11:51:16 +0200 |
commit | 19701a38439cc45c52b3c95c05b9a74763d7f1a3 (patch) | |
tree | f31c4ee8867aeea844355b8b5b7df2c7e3080377 | |
parent | 53d71f4b358aafc53c4ed4bdc335599cf183a8c7 (diff) |
fixed 'del todo'
-rw-r--r-- | lib/commands.py | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/lib/commands.py b/lib/commands.py index f1decba..2e347d3 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -64,10 +64,8 @@ class Commands(object): for num, i in enumerate(todolist): #num = index, i = value print('| {} |\t {}'.format(num, i.decode('utf-8'))) elif cmd[1] == 'ctodo': - with open(co['path'], 'rb') as fout: - for i, val in enumerate(fout.readlines()): - count = i - + count = countID() + if count is 0: #if idTODO is None or 0 print('0') else: @@ -95,16 +93,24 @@ class Commands(object): try: if len(cmd) > ListCommands.info['del'][1]: raise PersonalError(GREY + 'del ' + RED + 'accetta 1 parametro') + + count = countID() - if r.get('idTODO') is None or r.get('idTODO') == 0: #idTODO is null or 0 + if count == 0: #todo list is empty print('nessun todo in lista: goditi una Stout') - elif cmd[1] >= r.get('idTODO').decode('utf-8'): + elif int(cmd[1]) >= count: print('nessun todo con questo id') else: try: - msg = msgFromId(cmd[1]) #return value of sorted set's rank - r.zrem('todo', msg) - r.decr('idTODO') + with open(co['path'], 'rb') as fin: + lines = [x.strip() for x in fin.readlines()] + + del lines[int(cmd[1])+1] + + with open(co['path'], 'wb') as fout: + for line in lines: + fout.write(line + ('\n'.encode('utf-8'))) + print('Ok') except Exception as e: print(e) @@ -112,7 +118,9 @@ class Commands(object): ListCommands.err(type(e).__name__) -def msgFromId(value): - for i, val in enumerate(r.zrange('todo', 0, -1)): - if i == int(value): - return val.decode('utf-8') +def countID(): + with open(co['path'], 'rb') as fout: + for i, val in enumerate(fout.readlines()): + count = i + + return count |