config_sway/bin/waybar_khal.py

51 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python
import subprocess
import datetime
import json
from html import escape
data = {}
today = datetime.date.today().strftime("%Y-%m-%d")
days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Today",
"Tomorrow" ]
next_week = (datetime.date.today() + datetime.timedelta(days=10)).strftime("%Y-%m-%d")
title = subprocess.check_output("khal list --format \"{title}\" now "+next_week,
shell=True).decode("UTF-8").split("\n")
start = subprocess.check_output("khal list --format \"{start}\" now "+next_week,
shell=True).decode("UTF-8").split("\n")
end = subprocess.check_output("khal list --format \"{end}\" now "+next_week,
shell=True).decode("UTF-8").split("\n")
lines = ""
for i in range(len(title)):
t = escape(title[i])
s = escape(start[i])
e = escape(end[i])
line = ""
if len(t) and t.split(",")[0] in days:
line = f"\n<b>{t}</b>"
else:
ss = s.split(" ")
es = e.split(" ")
if len(es) == 1 or len(ss) == 1:
line = f"<i>{t}</i>"
else:
line = f"{ss[1]} - {es[1]}: {t}"
lines += f"{line}\n"
if today in lines:
event = lines.split('\n')[2]
if len(event) > 30:
event = f"{event[0:30]}"
data["text"] = f"{event}"
data["alt"] = "event"
else:
data["text"] = "-"
data["alt"] = "idle"
data["tooltip"] = lines
print(json.dumps(data))