aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/dot_config/waybar/executable_mako-status.sh
blob: 12e4d82f98ccd4a240e8bbe527c68e717f6be175 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh
# Waybar status: count of *pending* notifications, where pending = ids in
# mako's history/list that have NOT been explicitly dismissed by the user
# via Mod+n / Mod+Shift+n / the history picker.
#
# State file: $XDG_RUNTIME_DIR/mako-dismissed (per-session, plain id list).

set -eu

if ! command -v makoctl >/dev/null 2>&1; then
  printf '{"text":"","tooltip":"mako not installed","class":"off"}
'
  exit 0
fi

state=${XDG_RUNTIME_DIR:-/tmp}/mako-dismissed
: >>"$state"

# This makoctl has no -f flag; parse the text dump. Each notification
# starts with "Notification N: <summary>". Visible bubbles live in
# `list`, closed ones in `history`; their id-spaces are disjoint.
extract_ids() {
  makoctl "$1" 2>/dev/null |
    sed -n 's/^Notification \([0-9][0-9]*\):.*/\1/p'
}

all_ids=$({
  extract_ids list
  extract_ids history
} | sort -un)

# Prune stale ids (no longer present in mako) from the dismissed file.
if [ -s "$state" ] && [ -n "$all_ids" ]; then
  tmp=$(mktemp)
  printf '%s
' "$all_ids" >"$tmp.all"
  grep -Fxf "$tmp.all" "$state" >"$tmp" 2>/dev/null || :
  mv "$tmp" "$state"
  rm -f "$tmp.all"
fi

if [ -z "$all_ids" ]; then
  pending=0
else
  pending=$(printf '%s
' "$all_ids" | grep -Fxvf "$state" | grep -c . || true)
fi

if [ "$pending" -gt 0 ]; then
  printf '{"text":"󰂞 %s","tooltip":"%s pending","class":"pending"}
' \
    "$pending" "$pending"
else
  printf '{"text":"󰂜","tooltip":"no pending notifications","class":"empty"}
'
fi