config_sway/bin/waybar_khal.py

71 lines
2.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import subprocess
from datetime import date, datetime, timedelta
import json
from html import escape
TEXT_MAX_LEN = 40
today = date.today().strftime("%Y-%m-%d")
days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Today",
"Tomorrow" ]
next_week = (date.today() + 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")
text = "-"
tooltip = ""
alt = "idle"
for i in range(len(title)):
t = escape(title[i])
s = escape(start[i])
e = escape(end[i])
line = ""
## header
if len(t) and t.split(",")[0] in days:
line = f"\n<b>{t}</b>"
## event
else:
t_start = s.split(" ")
t_end = e.split(" ")
## all day
if len(t_start) == 1 or len(t_end) == 1:
line = f"<i>{t}</i>"
## start - end
else:
ts_start = datetime.timestamp(datetime.strptime(s, '%Y-%m-%d %H:%M'))
ts_end = datetime.timestamp(datetime.strptime(e, '%Y-%m-%d %H:%M'))
ts_now = datetime.timestamp(datetime.now())
## today
if t_start[0] == today:
## upcoming or ongoing
if ts_now < ts_end:
## ongoing
if ts_start < ts_now:
if ts_start < ts_now and ts_now < ts_end:
text = f"{t_start[1]} - {t_end[1]} {t}"
alt = "now"
## upcoming
elif text == "-":
text = f"{t_start[1]} - {t_end[1]} {t}"
alt = "today"
line = f"{t_start[1]} - {t_end[1]}: {t}"
tooltip += f"\n{line}"
data = {}
if len(text) > TEXT_MAX_LEN:
text = f"{text[0:TEXT_MAX_LEN]}"
data["text"] = text
data["alt"] = alt
data["tooltip"] = tooltip
print(json.dumps(data))