[eww] Add packages updates

This commit is contained in:
Viyurz 2024-10-21 16:49:00 +02:00
parent d65c8aad42
commit 866c052c26
Signed by: Viyurz
SSH key fingerprint: SHA256:IskOHTmhHSJIvAt04N6aaxd5SZCVWW1Guf9tEcxIMj8
3 changed files with 95 additions and 6 deletions

View file

@ -8,10 +8,16 @@
; DateTime
(defpoll datetime :interval "1s" "date +'%a %e %b %X'")
(defpoll month :interval "1m" :initial "0" "date +'%m'")
(defpoll day :interval "1m" :initial "0" "date +'%d'")
(defpoll month :interval "1m" :initial "1" "date +'%m'")
(defpoll day :interval "1m" :initial "1" "date +'%d'")
(defpoll year :interval "1m" :initial "0" "date +'%Y'")
; Notifications status
(deflisten nf-enabled :initial "true" "while :; do makoctl mode | grep -q dnd && echo true || echo false; sleep 0.1; done")
; Packages updates
(deflisten packages-updates :initial '{"count": "-", "class": " "}' "~/.config/eww/scripts/pkg-updates.sh")
; Window Title
(deflisten window-title "~/.config/eww/scripts/get-window-title.sh")
@ -19,9 +25,6 @@
(deflisten workspaces :initial "[]" "~/.config/eww/scripts/get-workspaces.sh")
(deflisten active-workspace "~/.config/eww/scripts/get-active-workspace.sh")
; Notifications status
(deflisten nf-enabled :initial "true" "while :; do makoctl mode | grep -q dnd && echo true || echo false; sleep 0.1; done")
; Widgets
@ -34,6 +37,7 @@
:class "systray"
)
(nf-state)
(packages-updates)
)
)
@ -58,6 +62,14 @@
)
)
(defwidget packages-updates []
(label :text "󰏖 ${packages-updates.count}"
:class "${packages-updates.class}"
:tooltip "${packages-updates.tooltip}"
)
)
(defwidget bar-middle []
(label :limit-width 2
:text "${window-title}"

View file

@ -0,0 +1,10 @@
# Copy to /etc/pacman.d/hooks/
[Trigger]
Operation = Upgrade
Operation = Remove
Type = Package
Target = *
[Action]
When = PostTransaction
Exec = /bin/pkill -SIGUSR1 -u 1000 pkg-updates.sh

View file

@ -0,0 +1,67 @@
#!/bin/bash
# Time in seconds between each check
interval=3600
# Number of updates displayed in tooltip
max_shown=25
function check {
# Initiate variables
declare -i retry_count=0
declare -i max_retries=5
declare -i retry_delay=30
declare -i success=0
# Retry until success or retry count reaches max_retries
while [[ $retry_count -le $max_retries && $success -ne 1 ]]; do
cmd_res="$(checkupdates 2>&1)"
case $? in
# Update(s) available
0)
success=1
count=$(echo "$cmd_res" | wc -l)
tooltip="$(echo "$cmd_res" | head -n "$max_shown" | sed -z 's/\n/\\n/g' | sed 's/\\n$//')"
if [[ $count -gt $max_shown ]]; then
tooltip="$tooltip\n[..] +$((count - max_shown))"
fi
class=" "
;;
# No update available
2)
success=1
count=0
tooltip="No update available."
class="disabled"
;;
*)
retry_count+=1
count=""
tooltip="$(echo "$cmd_res" | sed -z 's/\n/\\n/g' | sed 's/\\n$//')"
class="error"
;;
esac
# Show time of check
time="$(date '+%H:%M:%S')"
tooltip="$tooltip\n\nLatest check: $time"
cat <<- EOF
{"count": "$count", "tooltip": "$tooltip", "class": "$class"}
EOF
[[ $success -ne 1 ]] && sleep $retry_delay
done
}
declare -i sleep_pid
while :; do
check
sleep $interval &
sleep_pid=$!
trap "kill $sleep_pid" USR1
wait $sleep_pid
done