-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
676 lines (601 loc) · 21.7 KB
/
Copy pathtask.py
File metadata and controls
676 lines (601 loc) · 21.7 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#!/usr/bin/env python
# import modules
import os
import re
import json
import time
import argparse
import readline
from os import listdir
from os.path import isfile, join, dirname, basename
from datetime import date, timedelta
from sys import stdout
from sys import exit as byebye
#set up terminal help text and argparse
parser = argparse.ArgumentParser(description="""
Task is a todo list application that allows you
to quickly create task lists by typing them
without any additional frizz. Write anything
into the input field and see it added as a new
item on your list.\n
Task understands you. By using a natural suffix
like 'in 3 days' it will automatically create a
timestamp and sort the added task according to
it's due date.\n
Once a task is finished, you can use it's id
and delete it by typing ':d id' where 'id' is
the number displayed with the task.\n
""")
parser.add_argument("-f", "--file",
help="specify an alternative filename to use as default",
metavar="(NAME)")
args = parser.parse_args()
# global variables for reuse
if args.file: # Enables user to select a
fileName = args.file + ".txt" # different file to store
else: # the tasks in
fileName = "tasks.txt"
appName = "Task" # Name of the application
dirName = "hello-task" # Directory name
homeDir = os.path.expanduser("~") # Use user's home as base
targetDir = homeDir + "/.local/share/" + dirName # Determine target directory
targetFile = targetDir + "/" + fileName # Construct full path
tasks = [] # Parsed todo.txt task list
lvl = 4 # Foresight (view) level
message = "" # Feedback messages
openTasks = 0 # Used to count open tasks
# set up classes for easier color coding
class color:
black = "\033[30m" # used when also supplying a background
red = "\033[31m" # used for urgent or deletion
green = "\033[32m" # used for additions or confirmations
yellow = "\033[33m" # used for program questions and semi-urgent
orange = "\033[214m" # used to highlight projects
purple = "\033[128m" # used to highlight contexts
white = "\033[37m" # used when also supplying a background
reset = "\033[0m" # resets all color, reverts to default printing
class bgcolor:
black = "\033[40m"
red = "\033[41m"
class style:
bold = "\033[1m"
underline = "\033[4m"
reverse = "\033[7m"
# returns a list of available task files (todo.txt files only)
def listFiles():
return [f for f in listdir(targetDir)
if isfile(join(targetDir, f)) and f.endswith(".txt")]
# creates a strikethrough effect on fonts that support it
def strike(text):
return "̶".join(text) + "̶"
# today's date as an ISO string (todo.txt date format)
def today():
return date.today().isoformat()
# true if a token looks like a todo.txt date (YYYY-MM-DD)
def isDate(token):
return re.match(r'^\d{4}-\d{2}-\d{2}$', token) is not None
# clear screen buffer
def clearScreen():
os.system("cls" if os.name == "nt" else "clear")
# fancy lines
def titleLine(message, seperator):
return print(message.center(int(size[1]), seperator))
# update feedback messages
def updateMsg(msgBody, msgType):
global message
hint = [color.yellow + " [!] ",
color.yellow + " [?] ",
color.red + " [×] ",
color.green + " [+] ",
color.green + " [✓] "]
message = hint[msgType] + msgBody + " "
# set different mode
def mode(m):
modes = [color.white + " NORMAL ",
color.yellow + " HELP ",
color.red + " DELETION ",
color.yellow + " FORESIGHT ",
color.yellow + " OPEN FILE ",
color.yellow + " NEW FILE "]
return modes[m]
# render the modeline
def modeline(v):
# TODO make reflow work on terminals that support reflow by resize
size = os.popen('stty size', 'r').read().split()
escLength = 15
actions = [":h Help | :o Open | :d Done | :p Purge | :r Remove",
"enter Go Back",
"enter Go Back | id Remove File",
"enter Go Back | value Set Foresight",
"enter Go Back | id Open File",
"enter Go Back | name Create New File"]
left = mode(v) + color.white + " " + actions[v] + " "
right = " #" + str(openTasks) + " ~" + str(lvl) + " " + message
# calculate padding and account for escape sequence color codes
padding = int(size[1]) - len(left) - len(right) + escLength
output = left + " " * padding + right
if (len(output) - escLength) > int(size[1]):
overflow = len(output) - escLength - int(size[1]) + 4
return print(style.reverse + output[:-overflow] + "... " + color.reset)
else:
return print(style.reverse + output + color.reset)
# render filename above task list
def fileline():
if len(listFiles()) > 1:
indicator = " [+]"
else:
indicator = ""
size = os.popen('stty size', 'r').read().split()
padding = int(size[1]) - len(fileName) - len(indicator)
print(color.white + style.reverse + " " + fileName + indicator + " " * (padding - 1) + color.reset + "\n")
# path of the sidecar that stores per-file view settings (foresight level).
# kept separate so the todo.txt file itself stays a pure list of tasks.
def confPath(path):
return join(dirname(path), "." + basename(path) + ".conf")
# read the foresight level for the current file, defaulting to 4
def loadLvl():
try:
with open(confPath(targetFile)) as conf:
value = int(conf.read().strip())
return value if value in range(1, 5) else 4
except BaseException:
return 4
# persist the foresight level for the current file
def saveLvl(value):
with open(confPath(targetFile), "w") as conf:
conf.write(str(value))
# parse a single todo.txt line into a task dict
def lineToTask(line):
t = {"done": False, "completed": None, "priority": None,
"created": None, "due": None, "task": ""}
tokens = line.split(" ")
i = 0
# completion marker and optional completion date
if i < len(tokens) and tokens[i] == "x":
t["done"] = True
i += 1
if i < len(tokens) and isDate(tokens[i]):
t["completed"] = tokens[i]
i += 1
# priority, e.g. (A)
if i < len(tokens) and re.match(r'^\([A-Z]\)$', tokens[i]):
t["priority"] = tokens[i][1]
i += 1
# creation date
if i < len(tokens) and isDate(tokens[i]):
t["created"] = tokens[i]
i += 1
# the rest is the description, with the due: tag pulled out as metadata
description = []
for token in tokens[i:]:
due = re.match(r'^due:(\d{4}-\d{2}-\d{2})$', token)
if due:
t["due"] = due.group(1)
else:
description.append(token)
t["task"] = " ".join(description)
return t
# serialize a task dict back into a todo.txt line
def taskToLine(t):
parts = []
if t["done"]:
parts.append("x")
if t["completed"]:
parts.append(t["completed"])
elif t["priority"]:
parts.append("(" + t["priority"] + ")")
if t["created"]:
parts.append(t["created"])
if t["task"]:
parts.append(t["task"])
if t["due"]:
parts.append("due:" + t["due"])
return " ".join(parts)
# read a todo.txt file into a list of task dicts, ids follow line order
def readTasks(path):
parsed = []
with open(path) as todofile:
for line in todofile:
line = line.rstrip("\n")
if line.strip() == "":
continue
parsed.append(lineToTask(line))
for index, t in enumerate(parsed):
t["id"] = index + 1
return parsed
# write the in-memory task list back out as todo.txt
def writeTasks(path):
with open(path, "w") as todofile:
for t in tasks:
todofile.write(taskToLine(t) + "\n")
# one-shot migration of a legacy json file into todo.txt format
def jsonMigrate(jsonPath, txtPath):
global tasks
with open(jsonPath) as legacy:
old = json.load(legacy)
migrated = []
for o in old.get("tasks", []):
t = {"done": str(o.get("done")) == "true", "completed": None,
"priority": None, "created": None, "due": None,
"task": str(o.get("task", ""))}
# the old format stored the due date as a unix timestamp
if "due" in o:
try:
t["due"] = date.fromtimestamp(int(o["due"])).isoformat()
except BaseException:
t["due"] = None
migrated.append(t)
tasks = migrated
writeTasks(txtPath)
# carry the foresight level over into the new sidecar
try:
legacyLvl = int(old["settings"][0]["lvl"])
except BaseException:
legacyLvl = 4
saveLvl(legacyLvl if legacyLvl in range(1, 5) else 4)
# check if the task file exists, execute creation if not
def fileCheck(path):
if isfile(path):
updateMsg("File loaded", 4)
taskList(path)
else:
# if no todo.txt exists yet but a legacy json file does, migrate it once.
# the original json is left in place untouched as a backup.
legacy = path[:-4] + ".json" if path.endswith(".txt") else path + ".json"
if isfile(legacy):
jsonMigrate(legacy, path)
updateMsg("Migrated tasks from json", 4)
taskList(path)
else:
fileCreate(path)
# create an empty todo.txt file and directory
def fileCreate(path):
if not os.path.exists(targetDir):
os.mkdir(targetDir, 0o755)
open(path, "w").close()
saveLvl(4)
updateMsg("New file storage created", 4)
taskList(path)
# add a new task to the todo.txt file
def addTask(n):
global tasks
t = {"done": False, "completed": None, "priority": None,
"created": today(), "due": None, "task": n}
# if a natural 'in X days' suffix is present, convert it to a due: tag
# and strip it from the description
dueTime = re.search(r'(in\s+(\d+?)\s+day(s\b|\b))$', n, re.M | re.I)
if dueTime:
dueDays = int(re.search(r'(\d+)', dueTime.group(), re.M | re.I).group())
t["task"] = n[:-(len(dueTime.group()) + 1)]
t["due"] = (date.today() + timedelta(days=dueDays)).isoformat()
tasks.append(t)
updateMsg("New task added", 3)
writeTasks(targetFile)
taskList(targetFile)
# remove items from the todo.txt file
def removeTask(n):
global tasks
# if removeTask was called without any parameters, purge all done tasks
if len(n.split()) == 0:
targets = [t["id"] for t in tasks if t["done"]]
# otherwise move through the passed parameters
else:
targets = n.split()
for token in targets:
try:
check = int(token)
except ValueError:
updateMsg("Please use the id of the task", 0)
continue
match = next((t for t in tasks if t["id"] == check), None)
if match is None:
updateMsg("Unable to find task id " + str(check), 0)
elif not match["done"]:
updateMsg("Unable to remove unfinished tasks", 0)
else:
tasks.remove(match)
writeTasks(targetFile)
updateMsg("Removed task", 2)
taskList(targetFile)
# toggle item's done state instead of directly removing it
def doneToggle(n):
global tasks
global openTasks
for token in n.split():
try:
check = int(token)
except ValueError:
updateMsg("Please use the id of the task", 0)
continue
match = next((t for t in tasks if t["id"] == check), None)
if match is None:
updateMsg("Unable to find task id " + str(check), 0)
elif not match["done"]:
match["done"] = True
match["completed"] = today()
if openTasks > 0:
openTasks = openTasks - 1
updateMsg("Marked task as done", 4)
else:
match["done"] = False
match["completed"] = None
openTasks = openTasks + 1
updateMsg("Marked task as not done", 4)
writeTasks(targetFile)
taskList(targetFile)
# read todo.txt file into memory and print to stdout as sorted groups
def renderTasks(content):
global tasks
global lvl
global openTasks
group = {}
gkey = ""
gval = ""
glvl = 0
task = 0
tasks = readTasks(content)
lvl = loadLvl()
# let's get things sorted
for o in tasks:
# if a due date exists, assign o to a group based on how far away it is
if o["due"]:
days = (date.fromisoformat(o["due"]) - date.today()).days
if days < 0:
gkey = 2
gval = style.bold + color.red + "Overdue"
glvl = 3
elif days == 0:
gkey = 3
gval = color.red + "Today"
glvl = 1
elif days == 1:
gkey = 4
gval = color.yellow + "Tomorrow"
glvl = 1
else:
gkey = int(days + 4)
gval = "In " + str(int(days)) + " days"
glvl = 4
# if there's no due date, put o into the "whenever" group
else:
gkey = 1
gval = color.white + "Unscheduled"
glvl = 2
# create groups dynamically based on the existence of keys
if gkey not in group:
group[gkey] = [{
"due": gval,
"lvl": glvl,
"item": []
}]
# add tasks to their group keys
if not o["done"]:
taskDescription = str(o["task"])
doneState = ' '
task = task + 1
else:
taskDescription = strike(str(o["task"]))
doneState = color.green + ' ✓ ' + color.reset
openTasks = task
idSpacing = (5 - len(str(o["id"]))) * " "
group[gkey][0]["item"].append(doneState + str(o["id"]) + idSpacing + taskDescription)
#print something cute if no tasks exist
if not group:
moji("empty")
else:
printCounter = 0
# and finally print everything to the terminal
# since the sortKey is useless to us, we're only interested in the dueGroups
# for the output, we still need to query sortKey to get proper sorting
for (sortKey, dueGroups) in sorted(group.items()):
for dueGroup in dueGroups:
# print only the dueGroup that matches current view level settings
if dueGroup["lvl"] <= lvl:
printCounter = printCounter + 1
print(" " + dueGroup["due"] + color.reset + "\n")
for task in dueGroup["item"]:
print(task)
print("")
# remind user if hidden tasks
if printCounter == 0:
moji("hidden")
# display todo.txt content as task list
def taskList(tasks):
clearScreen()
fileline()
renderTasks(tasks)
stdout.write("\x1b]2;" + appName + "\x07")
modeline(0)
userInput()
# await user input and add or remove tasks
def userInput():
# print("Type ':help' or ':?' for more info")
choice = input(" > ").strip()
if choice in (":help", ":?", ":h"):
userHelp()
elif choice in (":exit", ":quit", ":q", ":e"):
byebye
elif choice.startswith(":d"):
doneToggle(choice[2:].strip())
elif choice.startswith(":f"):
foresight(choice[2:].strip())
elif choice.startswith(":p"):
removeTask(choice[2:].strip())
elif choice.startswith(":o"):
fileswitcher()
elif choice.startswith(":n"):
newfile(choice[2:].strip())
elif choice.startswith(":r"):
fileRemover()
# catch user input error to prevent creation of unneccesary tasks
elif choice.lower() in ("quit", "exit"):
updateMsg("Did you want to quit?", 1)
taskList(targetFile)
elif choice == "":
updateMsg("Not sure what to do", 1)
taskList(targetFile)
else:
addTask(choice)
# update foresight
def foresight(n):
global lvl
try:
value = int(n)
if value in range(1, 5):
lvl = value
saveLvl(value)
updateMsg("Foresight set to " + str(value), 4)
else:
raise ValueError
except BaseException:
clearScreen()
fileline()
print(""" Change amount of tasks to display
1 tasks due today and tomorrow
2 same as 1 plus unscheduled
3 same as 2 plus overdue
4 same as 3 plus tasks due days after
""")
modeline(3)
foresightSelect = input(" > ").strip()
try:
select = int(foresightSelect)
foresight(select)
except BaseException:
updateMsg("Please select a value between 1-4", 0)
taskList(targetFile)
# switch to other files
def fileswitcher():
global targetFile
global fileName
clearScreen()
fileline()
print(" Open available file\n")
i = 0
fileList = listFiles()
for singleFile in fileList:
i = i + 1
idSpacing = (5 - len(str(i))) * " "
print(" " + str(i) + idSpacing + singleFile)
print("\n")
modeline(4)
fileSelect = input(" > ").strip()
if fileSelect.startswith("a"):
taskList(targetFile)
elif int(fileSelect):
try:
selection = int(fileSelect) - 1
if selection <= len(fileList):
fileName = fileList[selection]
targetFile = targetDir + "/" + fileName
updateMsg("Opened file", 4)
taskList(targetFile)
else:
raise
except:
updateMsg("Please select a valid option", 0)
fileswitcher()
else:
updateMsg("Please select a valid option", 0)
fileswitcher()
# routine to delete unused/empty files manually
def fileRemover():
global fileName
global targetDir
clearScreen()
fileline()
print(" Select a file for removal\n")
i = 0
deletionList = listFiles()
if fileName in deletionList:
deletionList.remove(fileName)
for singleFile in deletionList:
i = i + 1
idSpacing = (5 - len(str(i))) * " "
print(" " + str(i) + idSpacing + singleFile)
print("\n")
modeline(2)
try:
deleteFile = input(" > ").strip().split()[0]
try:
deletionPath = targetDir + "/" + deletionList[int(deleteFile) -1]
try:
os.remove(deletionPath)
# clean up the per-file settings sidecar as well
if isfile(confPath(deletionPath)):
os.remove(confPath(deletionPath))
updateMsg("File deleted", 2)
taskList(targetFile)
except:
updateMsg("Could not delete file", 0)
taskList(targetFile)
except:
updateMsg("Please select a valid option", 0)
fileRemover()
except:
updateMsg("No file selected", 0)
taskList(targetFile)
# create new file
def newfile(file):
global targetFile
global fileName
global tasks
global lvl
clearScreen()
fileline()
if len(file) < 1:
print(" Please specify a new filename\n")
modeline(5)
newFile = input(" > ").strip().split()[0]
if len(newFile) > 0:
tasks = []
lvl = 4
fileName = newFile + ".txt"
targetFile = targetDir + "/" + fileName
fileCheck(targetFile)
else:
updateMsg("Please specify a filename", 0)
taskList(targetFile)
elif len(file) >= 1:
tasks = []
lvl = 4
fileName = file.strip().split(" ")[0] + ".txt"
targetFile = targetDir + "/" + fileName
fileCheck(targetFile)
# short help print
def userHelp():
clearScreen()
fileline()
print(""" Available commands are
:d (id ...) Mark a task id as done, seperate multiple tasks by space
:p (id ...) Permanently remove a task, seperate multiple tasks by space
:f (1-4) Viewing level of tasks, type :f to see further explanation
:o Open another existing file
:n (name) Creates a new file or opens existing one if filename exists
:r Remove a file from disk
:help, :? View this screen
:quit, :exit exit the application
""")
modeline(1)
input(" > Press return to go back...")
taskList(targetFile)
# cute
def moji(mode):
size = os.popen('stty size', 'r').read().split()
kao = ""
msg = ""
if mode == "empty":
kao = "(.❛ ᴗ ❛.) "
msg = "An empty file is nice, but how about adding some tasks?"
elif mode == "hidden":
kao = "( ̄ω ̄;) "
msg = "I know you think you\'re done but trust me there\'s more..."
else:
kao = "(╥﹏╥) "
msg = "You\'ll never get to see me..."
padding = int(size[1]) - len(kao) - len(msg)
halfpad = int(padding / 2)
print("\n\n\n\n" + " " * halfpad + kao + msg + " " * halfpad + "\n\n\n\n")
# execute program only if not imported as module
if __name__ == "__main__":
fileCheck(targetFile)