nix-system/bin/notifications
2025-02-02 19:19:45 +01:00

72 lines
2.1 KiB
Python
Executable file

#!/bin/env python
import os, sys
# take at most n elements
def take(it, n):
for i, x in enumerate(it):
if i == n:
break
yield x
# above an example of the format
def parse(s):
def split():
i, j = 0, 0
level = 0
while i < len(s):
if s[i] == '{':
if level == 0:
j = i
level += 1
elif s[i] == '}':
level -= 1
if level == 0:
yield s[j:i+1]
i += 1
def section_to_dict(section):
d = {}
for line in section.split('\n'):
line_stripped = line.strip()
if ':' in line_stripped:
k, v = line_stripped.split(':', 1)
if k in ['appname', 'summary', 'body', 'urgency']:
if k in ['appname', 'summary', 'body']:
d[k] = v.strip().strip("'")
if k == 'urgency':
d[k] = v.strip()
return d
return map(section_to_dict, reversed(list(split())))
def pretty_print(notification):
colors_from_urgency = { # posix escape codes
"NORMAL": (2, None),
"URGENT": (1, 7),
}
def enclose_color(s, fg, bg):
def color_code(c):
if c is not None:
return f"\033[38;5;{c}m"
return ""
return f"{color_code(fg)}{color_code(bg)}{s}\033[0m"
#]] nvim's python parser if fucked without this
fg, bg = colors_from_urgency[notification['urgency']]
return f"{enclose_color(notification['summary'], fg, bg)} :: {notification['body']}"
with open(os.path.expanduser("~/.local/state/notifications-history"), "r") as f:
s = f.read()
notifications = parse(s)
if len(sys.argv) == 2:
try:
notifications = take(notifications, int(sys.argv[1]))
except ValueError as e:
print("Argument must be the number of notifications to show", file=sys.stderr)
raise e
for notification in notifications:
print(pretty_print(notification))