88 lines
2.6 KiB
Python
Executable File
88 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from datetime import datetime
|
|
import parsedatetime
|
|
|
|
TAGS = ['t', 'due']
|
|
|
|
class todo_item:
|
|
tag = []
|
|
full_tag = ""
|
|
date = None
|
|
text = ""
|
|
fancy_text = ""
|
|
|
|
def __init__(self, text):
|
|
self.text = text
|
|
try:
|
|
self.full_tag = re.findall("[^ ]+:\\d{4}-\\d{2}-\\d{2}", text)[0].strip()
|
|
self.tag = self.full_tag.split(":")
|
|
self.date = datetime.strptime(self.tag[1], "%Y-%m-%d")
|
|
except IndexError:
|
|
pass
|
|
|
|
def __repr__(self):
|
|
if self.text:
|
|
return self.text
|
|
else:
|
|
return ''
|
|
|
|
def fancy_print(self, relative=False):
|
|
if self.date < datetime.today():
|
|
#if the date has already passed, wrap with bold/unbold escapes
|
|
fancy_text = self.text.replace(
|
|
self.full_tag, '\033[1m' + self.full_tag + '\033[22m')
|
|
else:
|
|
fancy_text = self.text
|
|
|
|
if relative:
|
|
difference = (self.date - datetime.now()).days + 1
|
|
fancy_text = fancy_text.replace(
|
|
self.full_tag, "{}:{} days".format(self.tag[0], difference))
|
|
|
|
return fancy_text
|
|
|
|
def add(tag, date_input, desc):
|
|
print(tag, date_input, desc)
|
|
cal = parsedatetime.Calendar()
|
|
time_struct, parse_status = cal.parse(date_input)
|
|
date = datetime(*time_struct[:6]).strftime("%Y-%m-%d")
|
|
subprocess.call(["todo.sh", "add", desc, tag + ":" + date])
|
|
|
|
def due(search_term="", relative=False):
|
|
tagList = "\|".join(TAGS)
|
|
command = ["todo.sh", "list", tagList, search_term]
|
|
lines = subprocess.check_output(command).decode(encoding='UTF-8').split('\n')
|
|
items = [item for item in [todo_item(line) for line in lines] if item.date is not None]
|
|
items.sort(key= lambda x: x.date)
|
|
|
|
for i in items:
|
|
print(i.fancy_print(relative))
|
|
|
|
if __name__ == '__main__':
|
|
if sys.argv[1] == "usage":
|
|
print(" due [-r] [TERM...]\n"
|
|
" Shows all items with a t: or due: tag that match TERM. '-r' makes dates relative\n"
|
|
" due add [due date] [tag] [description]\n"
|
|
" Adds a task with a dated tag")
|
|
|
|
exit(0)
|
|
|
|
if len(sys.argv) >= 3:
|
|
if sys.argv[2] == "-r":
|
|
relative = True
|
|
del sys.argv[2]
|
|
elif sys.argv[2] == "add":
|
|
add(sys.argv[3], sys.argv[4], " ".join(sys.argv[5:]))
|
|
exit(0)
|
|
else:
|
|
relative = False
|
|
search_term = " ".join(sys.argv[2:])
|
|
due(search_term, relative)
|
|
else:
|
|
due()
|