dotfiles/desktop/.config/waybar/modules/khal.py
Marty Oehme ea066eebb5
waybar: Fix event module spacing
Added a new state which should fix the icon spacing issues:

When we have no upcoming events or upcoming events but none today, we
only display the icon and so we do not add any additional spacing.
(This is alt state `event` or `no-event`)

Only if we have an upcoming event today (alt state `event-today`) are we
printing it directly on the status bar and only then should be have
additional spacing. So we have an icon (the same as for event) with the
correct spacing so that whether there is text on the statusbar or not,
we space correctly.
2025-03-10 20:22:01 +01:00

42 lines
1.2 KiB
Python
Executable file

#!/usr/bin/env python
# from https://gist.github.com/bjesus/178a9bd3453470d74803945dbbf9ed40
# List upcoming khal events in simple json container fit for waybar
#
# Hovering over the item displays all upcoming
# The icon changes if there are events today, and displays the
# closest upcoming one.
import datetime
import json
import subprocess
from html import escape
data = {}
today = datetime.date.today().strftime("%Y-%m-%d")
next_week = (datetime.date.today() + datetime.timedelta(days=10)).strftime("%Y-%m-%d")
output = subprocess.check_output("khal list now " + next_week, shell=True)
output = output.decode("utf-8")
lines = output.split("\n")
new_lines: list[str] = []
for line in lines:
clean_line = escape(line).split(" ::")[0]
if len(clean_line) and clean_line[0] not in ["0", "1", "2"]:
clean_line = "\n<b>" + clean_line + "</b>"
new_lines.append(clean_line)
output = "\n".join(new_lines).strip()
if today in output: # an event today!
data["alt"] = "event-today"
data["text"] = output.split("\n")[1]
elif output: # an event in the week!
data["alt"] = "event"
data["tooltip"] = output
else: # no events
data["alt"] = "no-event"
print(json.dumps(data))