dotfiles/.scripts/waybar-pkg-update-count.sh

50 lines
905 B
Bash
Raw Normal View History

#!/bin/bash
# Initiate variables
retry_count=0
max_retries=5
retry_delay=30
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
output=$(echo "$cmd_res" | wc -l)
tooltip="$(echo "$cmd_res" | sed -z 's/\n/\\n/g' | sed 's/\\n$//')"
class="update-available"
;;
# No update available
2)
success=1
output=0
tooltip="No update available."
class="no-update"
;;
*)
sleep $retry_delay
retry_count+=1
output=""
tooltip="$(echo "$cmd_res" | sed -z 's/\n/\\n/g' | sed 's/\\n$//')"
class="error"
;;
esac
done
# Show time of check
time="$(date '+%H:%M:%S')"
tooltip="$tooltip\nLatest check: $time"
cat << EOF
{"text": "$output", "alt": "$class", "tooltip": "$tooltip", "class": "$class"}
EOF