84 lines
2.3 KiB
Python
Executable File
84 lines
2.3 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\\d\\d\\d-\\d\\d-\\d\\d", 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(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, "t:" + date])
|
|
|
|
def due(search_term="", relative=False):
|
|
command = ["todo.sh", "list"]
|
|
command.append(search_term)
|
|
lines = subprocess.check_output(command).decode(encoding='UTF-8').split('\n')
|
|
items = []
|
|
|
|
for line in lines:
|
|
if " t:" in line:
|
|
item = todo_item(line)
|
|
if item.date is not None:
|
|
items.append(item)
|
|
|
|
items.sort(key= lambda x: x.date)
|
|
|
|
for i in items:
|
|
print(i.fancy_print(relative))
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) >= 3:
|
|
if sys.argv[2] == "-r":
|
|
relative = True
|
|
del sys.argv[2]
|
|
else:
|
|
relative = False
|
|
search_term = " ".join(sys.argv[2:])
|
|
due(search_term, relative)
|
|
else:
|
|
due()
|