due: Switch to object-oriented, make file more usable as a module
This commit is contained in:
parent
b3a0a55526
commit
9b1367e2f8
100
due
100
due
@ -8,45 +8,69 @@ from datetime import datetime
|
||||
|
||||
TAGS = ['t', 'due']
|
||||
|
||||
def getTag(item):
|
||||
if ":" not in item:
|
||||
return None
|
||||
try:
|
||||
sub = re.findall("[^ ]+:\\d\\d\\d\\d-\\d\\d-\\d\\d", item)[0].strip()
|
||||
except IndexError:
|
||||
return None
|
||||
return sub.split(":")
|
||||
class todo_item:
|
||||
tag = []
|
||||
full_tag = ""
|
||||
date = None
|
||||
text = ""
|
||||
fancy_text = ""
|
||||
|
||||
def getDate(date):
|
||||
return datetime.strptime(date, "%Y-%m-%d")
|
||||
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 highlightDate(item):
|
||||
full_tag = item[1] + ":" + item[0]
|
||||
if getDate(item[0]) < datetime.today():
|
||||
#if the date has already passed, wrap with bold/unbold escapes
|
||||
item[2] = item[2].replace(full_tag, '\033[1m' + full_tag + '\033[22m')
|
||||
return item
|
||||
|
||||
def relativeDate(item):
|
||||
full_tag = item[1] + ":" + item[0]
|
||||
difference = getDate(item[0]) - datetime.today()
|
||||
item[2] = item[2].replace(full_tag, item[1] + ":" + str(difference.days) + "days")
|
||||
return item
|
||||
|
||||
def main(argv):
|
||||
lines = subprocess.check_output(["todo.sh", "list"]).decode(encoding='UTF-8').split('\n')
|
||||
items = []
|
||||
for line in lines:
|
||||
tag = getTag(line)
|
||||
if tag is not None:
|
||||
items.append([tag[1], tag[0], line])
|
||||
|
||||
items.sort()
|
||||
for i in items:
|
||||
if len(argv) > 2 and argv[2] == "rel":
|
||||
print(relativeDate(highlightDate(i))[2])
|
||||
def __repr__(self):
|
||||
if self.text:
|
||||
return self.text
|
||||
else:
|
||||
print(highlightDate(i)[2])
|
||||
return ''
|
||||
|
||||
#if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
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 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()
|
||||
|
Loading…
Reference in New Issue
Block a user