This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskw.py
164 lines (127 loc) · 4.02 KB
/
taskw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from . import commands
import arrow
import subprocess
import json
class Tasks:
def __init__(self, logging=False):
self.logging = logging
def fprint(self, message):
if self.logging:
print(message)
def _formatOutput(self, data):
self.fprint(data)
try:
return json.loads(data)
except:
return str(data, 'latin-1')
def cmd(self, command):
self.fprint(f"Executing {command}")
if not command.startswith("task "):
command = f"task {command}"
command = f"{command} rc.confirmation:0"
self.fprint(f"Command finalized: {command}")
f = subprocess.run(command.split(), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if f.stdout:
return self._formatOutput(f.stdout)
else:
return self._formatOutput(f.stderr)
def add(self, description):
return self.cmd(f'task add {description}')
def done(self, _id):
return self.cmd(f'task done {_id}')
def delete(self, _id):
return self.cmd(f'task delete {_id}')
def export(self, _id = ""):
if _id:
f = f'task {str(_id)} export'
else:
f = 'task export'
return self.cmd(f)
def get(self, dom):
return self.cmd(f'task _get {dom}')
class TwRun(commands.BaseCommand):
def __init__(self, host):
super().__init__(host)
self.addListener("tw")
self.addListener("taskw")
self.addListener("taskwarrior")
self.inter = True
self.tw = Tasks(self.cfg._get('debug', False))
def onTrigger(self, value):
out = self.tw.cmd(value)
return self.output(f"\n{out}")
class TwDOM(commands.BaseCommand):
def __init__(self, host):
super().__init__(host)
self.addListener("tdom")
self.inter = True
self.tw = Tasks(self.cfg._get('debug', False))
def onTrigger(self, value):
out = self.tw.get(value)
return self.output(out)
class Twls(commands.BaseCommand):
def __init__(self, host):
super().__init__(host)
self.addListener("list tasks")
self.tw = Tasks(self.cfg._get('debug', False))
def onTrigger(self):
out = self.tw.export()
s = "\n"
for item in out:
if item['status'] == "pending":
pro = ""
if item.get('project', None):
pro += f"[{item['project']}] "
created = arrow.get(item['entry'], 'YYYYMMDD')
modified = arrow.get(item['modified'], 'YYYYMMDD')
dt = f"- Created on {created.day}/{created.month}/{created.year}"
if created != modified:
dt += f" (Modified on {modified.day}/{modified.month}/{modified.year})"
s += f" {item['id']}: {pro}{item['description']} {dt}\n"
return self.output(s)
class TwGetTask(commands.BaseCommand):
def __init__(self, host):
super().__init__(host)
self.addListener("show task")
self.inter = True
self.tw = Tasks(self.cfg._get('debug', False))
def onTrigger(self, value):
try:
item = self.tw.export(value)[0]
except:
return self.message("Problem finding that task.")
s = ""
if item['status'] == "pending":
pro = ""
if item.get('project', None):
pro += f"[{item['project']}] "
created = arrow.get(item['entry'], 'YYYYMMDD')
modified = arrow.get(item['modified'], 'YYYYMMDD')
dt = f"- Created on {created.day}/{created.month}/{created.year}"
if created != modified:
dt += f" (Modified on {modified.day}/{modified.month}/{modified.year})"
s += f" {item['id']}: {pro}{item['description']} {dt}"
return self.output(s)
class AddTaskwPrompt(commands.BaseCommand):
def __init__(self, host):
super().__init__(host)
self.addListener("add a task", 100)
self.addListener("add new task", 95)
self.addListener("create task", 95)
self.tw = Tasks(self.cfg._get('debug', False))
def onTrigger(self):
return self.hold("What should I write?")
def onHeld(self, value):
if value == "":
return self.message("Cancelled.")
self.tw.add(value)
return self.message("Task added.")
class AddTaskw(commands.BaseCommand):
def __init__(self, host):
super().__init__(host)
self.addListener("add task")
self.inter = True
self.tw = Tasks(self.cfg._get('debug', False))
def onTrigger(self, value):
self.tw.add(value)
return self.message("Task added.")