Skip to content

Instantly share code, notes, and snippets.

@bjesus
Last active May 21, 2023 11:27
Show Gist options
  • Save bjesus/178a9bd3453470d74803945dbbf9ed40 to your computer and use it in GitHub Desktop.
Save bjesus/178a9bd3453470d74803945dbbf9ed40 to your computer and use it in GitHub Desktop.
Khal indicator for Waybar
"custom/events": {
"format": "{}",
"tooltip": true,
"interval": 300,
"format-icons": {
"default": ""
},
"exec": "waybar-khal.py",
"return-type": "json"
},
#!/usr/bin/env python
import subprocess
import datetime
import json
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 = []
for line in lines:
clean_line = escape(line).split(" ::")[0]
if len(clean_line) and not clean_line[0] 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:
data['text'] = " " + output.split('\n')[1]
else:
data['text'] = ""
data['tooltip'] = output
print(json.dumps(data))
@bjesus
Copy link
Author

bjesus commented Sep 13, 2020

screenshot_2020-09-13_21-46-51_000571993

@Sekki21956
Copy link

Hi, I really liked your code but for me it had some problems with notes on appointments. The time format I'm using is also different which stopped the code from working at first. I looked through the khal docs and found the --format option. This allows you from the beginning to only print what you want in the end. datetime stuff was fixed by just using now and 7days.
This is my new code:

#!/usr/bin/env python
import json
import subprocess
data = {}
output = subprocess.check_output("khal list now 7days --format \"{start-end-time-style} {title}\"", shell=True).decode("utf-8")
lines = output.split("\n")
new_lines = []
for line in lines:
    if len(line) and line[0].isalpha():
        line = "\n<b>"+line+"</b>"
    new_lines.append(line)
output = "\n".join(new_lines).strip()
if "Today" in output:
    data['text'] = " " + output.split('\n')[1]
else:
    data['text'] = ""
data['tooltip'] = output
print(json.dumps(data))

I hope this can be useful to someone. Let me know what you think, I'm kinda new to python ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment