diff --git a/bin/notifications b/bin/notifications new file mode 100755 index 0000000..2a74693 --- /dev/null +++ b/bin/notifications @@ -0,0 +1,72 @@ +#!/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)) diff --git a/home.nix b/home.nix index f81a386..cea9507 100644 --- a/home.nix +++ b/home.nix @@ -44,6 +44,8 @@ ${config.programs.rofi.package}/bin/rofi -matching normal -sort -show run ''; + ".bin/notifications".source = bin/notifications; + ".bin/window_dir".source = pkgs.writers.writeBash "window_dir" '' PID=$(${pkgs.hyprland}/bin/hyprctl activewindow | grep pid | cut -d' ' -f 2) PID=$(echo $(ps --ppid $PID -o pid | tail -n1))