Files
dotfiles/.config/xmobar/updates.sh
T
Daniele Fucini b5c268ae7d xmobar: show reboot and .pacnew markers in the updates segment
Red R until the reboot, blue P until the files are merged, read from
update-notify's pending file; markers show alone when there are no updates.
2026-11-07 10:41:36 +01:00

63 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Pending updates for xmobar from update-notify's pending list, e.g. " 15/3 | " (repo/AUR), empty when none
# Entries already installed are dropped, so the count falls right after an upgrade. Orange from the threshold
# on, red "K" when a custom kernel needs a rebuild, red "R" when a reboot is needed, blue "P" for .pacnew files.
# With "list": show the pending packages in a notification (for a click on the bar)
file="${XDG_STATE_HOME:-$HOME/.local/state}/update-notify/pending"
[[ -r $file ]] || exit 0
mapfile -t entries < "$file"
names=()
for e in "${entries[@]}"; do
read -r src pkg _ <<< "$e"
case $src in repo|aur|kernel) names+=("$pkg") ;; esac
done
declare -A installed
if (( ${#names[@]} )); then
while read -r pkg ver; do
installed[$pkg]=$ver
done < <(pacman -Q "${names[@]}" 2>/dev/null)
fi
threshold=0 repo=() aur=() kernel=() pacnew=() reboot=""
running=$(uname -r)
for e in "${entries[@]}"; do
read -r src pkg new <<< "$e"
case $src in
threshold) threshold=$pkg; continue ;;
pacnew) [[ -e ${e#pacnew } ]] && pacnew+=("${e#pacnew }"); continue ;; # gone once merged
reboot) [[ $pkg == "$running" ]] && reboot=$pkg; continue ;; # gone after the reboot
esac
have=${installed[$pkg]:-}
[[ -z $have ]] && continue # removed since the last run
case $src in
repo) (( $(vercmp "$have" "$new") < 0 )) && repo+=("$pkg $new") ;;
aur) (( $(vercmp "$have" "$new") < 0 )) && aur+=("$pkg $new") ;;
kernel) (( $(vercmp "${have%-*}" "${new%-*}") < 0 )) && kernel+=("$pkg (stock $new)") ;;
esac
done
if [[ ${1:-} == list ]]; then
body=""
(( ${#repo[@]} )) && { printf -v l '%s\n' "${repo[@]}"; body+="Repo:"$'\n'"$l"; }
(( ${#aur[@]} )) && { printf -v l '%s\n' "${aur[@]}"; body+="AUR:"$'\n'"$l"; }
(( ${#kernel[@]} )) && { printf -v l 'Rebuild %s\n' "${kernel[@]}"; body+="$l"; }
(( ${#pacnew[@]} )) && { printf -v l '%s\n' "${pacnew[@]}"; body+="Merge with pacdiff:"$'\n'"$l"; }
[[ -n $reboot ]] && body+="Reboot (running $reboot)"$'\n'
body=${body%$'\n'}
notify-send "Pending updates" "${body:-None}"
exit
fi
total=$(( ${#repo[@]} + ${#aur[@]} ))
(( total + ${#kernel[@]} + ${#pacnew[@]} )) || [[ -n $reboot ]] || exit 0
text=$'\xef\x92\x87' # nf-oct-package, as UTF-8 bytes so the locale doesn't matter
(( total )) && text+=" ${#repo[@]}/${#aur[@]}" # markers alone: no "0/0"
(( total && total >= threshold )) && text="<fc=#ffb86c>$text</fc>"
(( ${#kernel[@]} )) && text+=" <fc=#f7768e>K</fc>"
[[ -n $reboot ]] && text+=" <fc=#f7768e>R</fc>"
(( ${#pacnew[@]} )) && text+=" <fc=#7aa2f7>P</fc>"
echo "$text | "