This repository has been archived on 2022-08-23. You can view files and clone it, but cannot push or open issues or pull requests.
ztm/task.py

211 lines
6 KiB
Python
Raw Normal View History

2018-02-16 05:31:30 +00:00
import re
2018-02-16 15:24:23 +00:00
import sqlite3
2018-02-16 05:31:30 +00:00
import subprocess
2018-02-16 15:24:23 +00:00
import sys
2018-02-16 05:31:30 +00:00
import tempfile
from pyfzf.pyfzf import FzfPrompt
from model import Model
from bcolors import bcolors
class Task:
def __init__(self):
self.model = Model()
self.fzf = FzfPrompt()
def search(self):
tasks = []
tasksData = self.model.get_all_tasks()
if not tasksData:
self.add()
tasksData = self.model.get_all_tasks()
2018-02-16 09:16:29 +00:00
for t in tasksData:
2018-02-16 15:24:23 +00:00
tasks.append('%s: %s [ %s ]' % (t['aid'], t['description'], t['tag_names']))
2018-02-16 05:31:30 +00:00
selected = self.fzf.prompt(tasks)
if selected:
m = re.search(r'^(.+):', selected[0])
aid = m.group(1)
if aid:
self.edit_task(aid)
else:
print(bcolors.FAIL + 'Task was not selected...' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
def add(self):
print(bcolors.HEADER + 'Adding task' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
description = input('Description: ')
aid = self.model.create_task_draft(description)
print(bcolors.OKBLUE + '[task has been created]' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
self.manage_task(aid)
def manage_task(self, aid):
task = self.model.get_task(aid)
print(bcolors.HEADER + 'Managing task: [' + task['aid'] + '] ' + task['description'] + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
long_term = ' '
if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x'
tags = ''
if task['tags']:
tags = ' '.join([t['name'] for t in task['tags']])
print('''%s
2018-02-16 05:31:30 +00:00
Description: %s
Tags: [%s]
Long Term: [%s]
Created: %s ''' % (task['aid'], task['description'], tags, long_term, task['created_at']))
2018-02-16 05:31:30 +00:00
if task['done'] and task['done'] != 'FALSE':
print(bcolors.OKGREEN + 'Finished: ' + task['finished_at'] + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
self.manage_task_menu(aid)
def manage_task_menu(self, aid):
2018-02-16 09:16:29 +00:00
menu = input(bcolors.OKBLUE + '~task: ' + bcolors.OKGREEN + 'What you want to do? (?e*+-v&><q) ' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
if menu == 'q':
self.bye()
elif menu == '?':
self.manage_task_about(aid)
elif menu == 'e':
self.edit_task(aid)
elif menu == '*':
self.toggle_long_term(aid)
elif menu == '+':
self.add_tags(aid)
elif menu == '-':
self.remove_tags(aid)
2018-02-16 05:31:30 +00:00
elif menu == 'v':
self.toggle_done(aid)
elif menu == '<':
return
else:
print(bcolors.FAIL + 'This is not implemented...' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
self.manage_task_menu(aid)
def manage_task_about(self, aid):
print(bcolors.WARNING + '''
Short instruction
-----------------
? - help (this dialog)
e - edit content
* - toggle long term
+ - add tag
- - remove tag
v - mark done/undone
& - add child task
> - go to child
< - back
q - exit
''' + bcolors.ENDC)
self.manage_task_menu(aid)
def edit_task(self, aid):
task = self.model.get_task(aid)
long_term = ' '
if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x'
tags = ''
if task['tags']:
tags = ' '.join([t['name'] for t in task['tags']])
2018-02-16 05:31:30 +00:00
content = '''%s
Tags: [%s]
Long Term: [%s]
Created: %s ''' % (task['aid'], tags, long_term, task['created_at'])
2018-02-16 05:31:30 +00:00
if task['done'] and task['done'] != 'FALSE':
content += '\nFinished: ' + task['finished_at']
content += '\n\n# ' + task['description']
if task['content']:
content += task['content']
with tempfile.NamedTemporaryFile(suffix='.md', mode='r+') as temp:
f = open(temp.name, 'r+')
f.write(content)
f.close()
subprocess.call(['vim', temp.name])
f = open(temp.name, 'r')
new_content = f.read()
f.close()
temp.close()
found = False
content = ''
for row in new_content.splitlines():
if found:
content += '\n' + row
continue
if row == '# ' + task['description']:
found = True
self.model.save_content(aid, content)
print(bcolors.OKBLUE + '[content has been saved]' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
self.manage_task(aid)
def toggle_long_term(self, aid):
self.model.toggle_long_term(aid)
print(bcolors.OKBLUE + '[task has been updated]' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
self.manage_task(aid)
def toggle_done(self, aid):
self.model.toggle_done(aid)
print(bcolors.OKBLUE + '[task has been updated]' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
self.manage_task(aid)
def add_tags(self, aid):
task = self.model.get_task(aid)
unlinked_tags = self.model.get_tags_not_in_task(task['id'])
2018-02-16 15:24:23 +00:00
if type(unlinked_tags) is sqlite3.Cursor and unlinked_tags.rowcount == 0:
print(bcolors.FAIL + 'Where is no more unlinked tags left...' + bcolors.ENDC)
self.manage_task(aid)
tags = [t['name'] for t in unlinked_tags]
selected = self.fzf.prompt(tags, '--multi --cycle')
if selected:
self.model.link_tags_to_task(task['id'], selected)
print(bcolors.OKBLUE + '[tags have been linked]' + bcolors.ENDC)
self.manage_task(aid)
else:
print(bcolors.FAIL + 'Tag was not selected...' + bcolors.ENDC)
def remove_tags(self, aid):
task = self.model.get_task(aid)
if not task['tags']:
print(bcolors.FAIL + 'Where is no tags linked...' + bcolors.ENDC)
self.manage_task(aid)
tags = [t['name'] for t in task['tags']]
selected = self.fzf.prompt(tags, '--multi --cycle')
if selected:
self.model.unlink_tags_from_task(task['id'], selected)
print(bcolors.OKBLUE + '[tags have been unlinked]' + bcolors.ENDC)
self.manage_task(aid)
else:
print(bcolors.FAIL + 'Tag was not selected...' + bcolors.ENDC)
2018-02-16 05:31:30 +00:00
def bye(self):
print(bcolors.FAIL + 'bye o/' + bcolors.ENDC)
sys.exit(0)