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/ztm.py

204 lines
5.1 KiB
Python
Raw Normal View History

2018-02-14 23:34:54 +00:00
import re
2018-02-14 15:16:03 +00:00
import sys
import subprocess
2018-02-14 23:34:54 +00:00
import tempfile
from pyfzf.pyfzf import FzfPrompt
2018-02-13 09:19:09 +00:00
2018-02-14 15:16:03 +00:00
from model import Model
from bcolors import bcolors
2018-02-13 09:19:09 +00:00
2018-02-14 15:16:03 +00:00
class Main:
def __init__(self):
self.model = Model()
2018-02-14 23:34:54 +00:00
self.fzf = FzfPrompt()
2018-02-14 15:16:03 +00:00
def run(self):
2018-02-14 23:34:54 +00:00
print(bcolors.HEADER + '''
GEEEEEEEL .. :
,##############Wf.,;;L#K;;. ,W, .Et
........jW##Wt t#E t##, ,W#t
tW##Kt t#E L###, j###t
tW##E; t#E .E#j##, G#fE#t
tW##E; t#E ;WW; ##,:K#i E#t
.fW##D, t#E j#E. ##f#W, E#t
.f###D, t#E .D#L ###K: E#t
.f####Gfffffffffff; t#E :K#t ##D. E#t
.fLLLLLLLLLLLLLLLLLi fE ... #G ..
: j
2018-02-14 15:16:03 +00:00
Task manager from Zordsdavini (2018)
2018-02-14 23:34:54 +00:00
''' + bcolors.ENDC)
2018-02-14 15:16:03 +00:00
self.menu()
def menu(self):
2018-02-14 23:34:54 +00:00
menu = input(bcolors.OKGREEN + 'What you want to do? (?+/tcq) ' + bcolors.ENDC)
2018-02-14 15:16:03 +00:00
if menu == 'q':
2018-02-14 23:34:54 +00:00
self.bye()
2018-02-14 15:16:03 +00:00
elif menu == '?':
self.about()
elif menu == '/':
self.search()
elif menu == '+':
self.add()
else:
print(bcolors.FAIL + 'This is not implemented...\n' + bcolors.ENDC)
self.menu()
2018-02-14 15:16:03 +00:00
def about(self):
2018-02-14 23:34:54 +00:00
print(bcolors.WARNING + '''
2018-02-14 15:16:03 +00:00
Short instruction
-----------------
? - help (this dialog)
+ - add
/ - search
t - tag manager
c - configuration
q - exit
2018-02-14 23:34:54 +00:00
''' + bcolors.ENDC)
2018-02-14 15:16:03 +00:00
self.menu()
def search(self):
tasks = []
tasksData = self.model.get_all_tasks()
if not tasksData:
self.add()
tasksData = self.model.get_all_tasks()
for t in self.model.get_all_tasks():
2018-02-14 23:34:54 +00:00
tasks.append('%s: %s' % (t['aid'], t['description']))
2018-02-14 15:16:03 +00:00
2018-02-14 23:34:54 +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)
2018-02-14 15:16:03 +00:00
def add(self):
print('\n' + bcolors.HEADER + 'Adding task\n' + bcolors.ENDC)
description = input('Description: ')
aid = self.model.create_task_draft(description)
self.manage_task(aid)
def manage_task(self, aid):
task = self.model.get_task(aid)
2018-02-14 23:34:54 +00:00
print('\n' + bcolors.HEADER + 'Managing: [' + task['aid'] + '] ' + task['description'] + bcolors.ENDC)
2018-02-15 11:08:12 +00:00
long_term = ' '
if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x'
2018-02-14 23:34:54 +00:00
print('''
#%s
Description: %s
Tags: [%s]
Long Term: [%s]
Created: %s
2018-02-15 11:08:12 +00:00
''' % (task['aid'], task['description'], '', long_term, task['created_at']))
2018-02-14 15:16:03 +00:00
self.manage_task_menu(aid)
def manage_task_menu(self, aid):
menu = input(bcolors.OKGREEN + 'What you want to do? (?e*+-v&><q) ' + bcolors.ENDC)
2018-02-14 15:16:03 +00:00
if menu == 'q':
2018-02-14 23:34:54 +00:00
self.bye()
2018-02-14 15:16:03 +00:00
elif menu == '?':
self.manage_task_about(aid)
2018-02-14 23:34:54 +00:00
elif menu == 'e':
self.edit_task(aid)
2018-02-15 11:08:12 +00:00
elif menu == '*':
self.toggle_long_term(aid)
elif menu == '<':
self.menu()
else:
print(bcolors.FAIL + 'This is not implemented...\n' + bcolors.ENDC)
self.manage_task_menu(aid)
2018-02-14 15:16:03 +00:00
def manage_task_about(self, aid):
2018-02-14 23:34:54 +00:00
print(bcolors.WARNING + '''
2018-02-14 15:16:03 +00:00
Short instruction
-----------------
? - help (this dialog)
e - edit content
* - toggle long term
+ - add tag
- - remove tag
v - mark done
& - add child task
> - go to child
< - back
2018-02-14 15:16:03 +00:00
q - exit
2018-02-14 23:34:54 +00:00
''' + bcolors.ENDC)
2018-02-14 15:16:03 +00:00
self.manage_task_menu(aid)
2018-02-14 23:34:54 +00:00
def edit_task(self, aid):
task = self.model.get_task(aid)
2018-02-15 11:08:12 +00:00
long_term = ' '
if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x'
2018-02-14 23:34:54 +00:00
content = '''%s
Tags: [%s]
Long Term: [%s]
Created: %s
2018-02-15 11:08:12 +00:00
''' % (task['aid'], '', long_term, task['created_at'])
2018-02-14 23:34:54 +00:00
content += '\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 + '\n[content has been saved]' + bcolors.ENDC)
2018-02-14 23:34:54 +00:00
self.manage_task(aid)
2018-02-15 11:08:12 +00:00
def toggle_long_term(self, aid):
self.model.toggle_long_term(aid)
self.manage_task(aid)
2018-02-14 23:34:54 +00:00
def bye(self):
print(bcolors.FAIL + 'bye o/' + bcolors.ENDC)
sys.exit(0)
2018-02-14 15:16:03 +00:00
if __name__ == '__main__':
a = Main()
a.run()