aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/dot_config/waybar/executable_pacdiff-status.sh
blob: e1a2291aa95e68d2644684d223543baa29402dd6 (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
#!/bin/sh
# Waybar custom/pacdiff: shows count of unresolved .pacnew/.pacsave files.
# Source of truth is `pacdiff -o` (output mode — lists differing files,
# does nothing). Hidden when zero. Mako fires once when the count goes
# from "no problems" to "non-zero" (i.e. on the post-`pacman -Syu`
# settle), so you're nudged exactly once per upgrade wave.
#
# Click handler hands off to a root shell that sets DIFFPROG before
# invoking pacdiff (sudo-rs scrubs the env by default, so passing
# `DIFFPROG=… sudo pacdiff` or `sudo DIFFPROG=… pacdiff` doesn't
# survive — but `sudo sh -c 'DIFFPROG=… pacdiff'` does).

set -eu

STATE=${XDG_RUNTIME_DIR:-/tmp}/waybar-pacdiff-prev

emit_empty() {
  printf '{"text":"","class":"fresh","tooltip":""}\n'
  printf 0 >"$STATE" 2>/dev/null || :
  exit 0
}

command -v pacdiff >/dev/null 2>&1 || emit_empty

count=$(pacdiff -o 2>/dev/null | grep -c . || :)
case "$count" in
  '' | *[!0-9]*) count=0 ;;
esac

[ "$count" -eq 0 ] && emit_empty

text="pacdiff ${count}"
tooltip="${count} unresolved .pacnew/.pacsave file(s) — click to merge with \`nvim -d\`"
printf '{"text":"%s","class":"warn","tooltip":"%s"}\n' "$text" "$tooltip"

prev=0
if [ -f "$STATE" ]; then
  prev=$(cat "$STATE" 2>/dev/null || printf 0)
  case "$prev" in
    '' | *[!0-9]*) prev=0 ;;
  esac
fi

if [ "$prev" -eq 0 ] && [ "$count" -gt 0 ] &&
  command -v notify-send >/dev/null 2>&1; then
  notify-send \
    --app-name=pacdiff \
    --urgency=normal \
    --icon=text-x-generic \
    "Pacman config files need merging" \
    "${count} .pacnew/.pacsave file(s). Click the bar entry to run pacdiff."
fi

printf '%s' "$count" >"$STATE"