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

112 lines
2.8 KiB
Python
Raw Normal View History

from pyfzf.pyfzf import FzfPrompt
from model import Model
2019-01-07 06:58:44 +00:00
from screen import Screen
class Tag:
def __init__(self):
self.model = Model()
self.fzf = FzfPrompt()
2019-01-07 06:58:44 +00:00
self.screen = Screen()
2019-01-07 07:28:05 +00:00
2019-01-07 06:58:44 +00:00
self.header = ''
self.info = None
def manage_tag(self, tid=None):
if tid:
tag = self.model.get_tag(tid)
2019-01-07 06:58:44 +00:00
self.header = 'Managing tag: ' + tag['name']
else:
2019-01-07 06:58:44 +00:00
self.header = 'Managing tags'
tagsData = self.model.get_all_tags()
if not tagsData:
self.add_tag()
tagsData = self.model.get_all_tags()
2019-01-07 06:58:44 +00:00
self.info = ' '.join([t['name'] for t in tagsData]) + '\n'
self.manage_tag_menu(tid)
def manage_tag_menu(self, tid=None):
2019-01-07 06:58:44 +00:00
about = '''
Short instruction
-----------------
? - help (this dialog)
/ - search tag
+ - add tag
- - remove tag
& - search linked tasks
< - back
q - exit
'''
self.screen.change_path('~tag', '?/+-&<q', about, self.header, self.info)
menu = self.screen.print()
if menu == 'q':
2019-01-07 06:58:44 +00:00
self.screen.bye()
elif menu == '/':
self.search_tag(tid)
self.manage_tag_menu(tid)
elif menu == '?':
2019-01-07 06:58:44 +00:00
self.screen.activate_about()
self.manage_tag(tid)
elif menu == '+':
self.add_tag()
elif menu == '-':
self.remove_tag(tid)
elif menu == '<':
return
else:
2019-01-07 06:58:44 +00:00
self.screen.add_fail('This is not implemented...')
self.manage_tag_menu(tid)
def add_tag(self):
2019-01-07 06:58:44 +00:00
self.screen.print_init('Adding tag')
name = input('Name: ')
tid = self.model.create_tag(name)
2019-01-07 06:58:44 +00:00
self.screen.add_message('tag has been created')
self.manage_tag(tid)
def search_tag(self, tid=None):
tagsData = self.model.get_all_tags()
if not tagsData:
self.add_tag()
tagsData = self.model.get_all_tags()
tags = []
for t in tagsData:
tags.append(t['name'])
selected = self.fzf.prompt(tags)
if selected:
tag = self.model.get_tag_by_name(selected[0])
self.manage_tag(tag['id'])
else:
2019-01-07 06:58:44 +00:00
self.screen.add_fail('Tag was not selected...')
def remove_tag(self, tid=None):
tagsData = self.model.get_all_tags()
if not tagsData:
2019-01-07 06:58:44 +00:00
self.screen.add_fail('Where is no tags...')
self.manage_tag_menu()
tags = []
for t in tagsData:
tags.append(t['name'])
selected = self.fzf.prompt(tags)
if selected:
self.model.remove_tag_by_name(selected[0])
2019-01-07 06:58:44 +00:00
self.screen.add_message('tag has been deleted')
self.manage_tag()
else:
2019-01-07 06:58:44 +00:00
self.screen.add_fail('Tag was not selected...')