From ceb1548a6ad80412b223d92c6614d5d23d9606f3 Mon Sep 17 00:00:00 2001 From: thomas Date: Sat, 2 May 2026 02:08:02 +0100 Subject: [PATCH] add updates logic --- dotfiles/.config/waybar/config | 19 ++-- .../waybar/scripts/check-system-updates | 103 ++++++++++++++++++ .../.config/waybar/scripts/install-updates.sh | 1 + dotfiles/.config/waybar/style.css | 19 ++++ 4 files changed, 132 insertions(+), 10 deletions(-) create mode 100644 dotfiles/.config/waybar/scripts/check-system-updates create mode 100644 dotfiles/.config/waybar/scripts/install-updates.sh diff --git a/dotfiles/.config/waybar/config b/dotfiles/.config/waybar/config index ba2b53b..a2c511b 100644 --- a/dotfiles/.config/waybar/config +++ b/dotfiles/.config/waybar/config @@ -1,5 +1,5 @@ { - "modules-left": ["battery", "clock#date", "clock", "custom/launcher"], + "modules-left": ["battery", "clock#date", "clock","custom/updates", "custom/launcher"], "modules-center": ["hyprland/workspaces"], "modules-right": ["network", "bluetooth", "pulseaudio", "custom/power"], @@ -136,15 +136,14 @@ }, "sort-by-number": true }, - "custom/updates": { - "format": " {}", - "escape": true, + "custom/updatespacman": { + "format": "{}{icon}", "return-type": "json", - "exec": "~/.config/ml4w/scripts/ml4w-check-system-updates", - "interval": 1800, - "signal": 1, - "hide-empty-text": true, - "on-click": "~/.config/ml4w/settings/installupdates.sh", - "on-click-right": "~/.config/ml4w/settings/software.sh" + "format-icons": { + "has-updates": " 󰏖", + "updated": "" + }, + "exec-if": "which waybar-module-pacman-updates", + "exec": "waybar-module-pacman-updates --no-zero-output" } } diff --git a/dotfiles/.config/waybar/scripts/check-system-updates b/dotfiles/.config/waybar/scripts/check-system-updates new file mode 100644 index 0000000..afb896d --- /dev/null +++ b/dotfiles/.config/waybar/scripts/check-system-updates @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +# _ _ _ _ +# | | | |_ __ __| | __ _| |_ ___ ___ +# | | | | '_ \ / _` |/ _` | __/ _ \/ __| +# | |_| | |_) | (_| | (_| | || __/\__ \ +# \___/| .__/ \__,_|\__,_|\__\___||___/ +# |_| +# + +# Check if command exists +_checkCommandExists() { + cmd="$1" + if ! command -v "$cmd" >/dev/null; then + echo 1 + return + fi + echo 0 + return +} + +script_name=$(basename "$0") + +# Count the instances +instance_count=$(ps aux | grep -F "$script_name" | grep -v grep | grep -v $$ | wc -l) + +if [ $instance_count -gt 1 ]; then + sleep $instance_count +fi + + +# ----------------------------------------------------- +# Define threshholds for color indicators +# ----------------------------------------------------- + +threshhold_green=0 +threshhold_yellow=25 +threshhold_red=100 + +# ----------------------------------------------------- +# Check for updates +# ----------------------------------------------------- + +# Arch +if [[ $(_checkCommandExists "pacman") == 0 ]]; then + + check_lock_files() { + local pacman_lock="/var/lib/pacman/db.lck" + local checkup_lock="${TMPDIR:-/tmp}/checkup-db-${UID}/db.lck" + + while [ -f "$pacman_lock" ] || [ -f "$checkup_lock" ]; do + sleep 1 + done + } + + check_lock_files + + yay_installed="false" + paru_installed="false" + if [[ $(_checkCommandExists "yay") == 0 ]]; then + yay_installed="true" + fi + if [[ $(_checkCommandExists "paru") == 0 ]]; then + paru_installed="true" + fi + if [[ $yay_installed == "true" ]] && [[ $paru_installed == "false" ]]; then + aur_helper="yay" + elif [[ $yay_installed == "false" ]] && [[ $paru_installed == "true" ]]; then + aur_helper="paru" + else + aur_helper="yay" + fi + updates_aur=$($aur_helper -Qum | wc -l) + updates_pacman=$(checkupdates | wc -l) + updates=$((updates_aur+updates_pacman)) + +# Fedora +elif [[ $(_checkCommandExists "dnf") == 0 ]]; then + updates=$(dnf check-update -q | grep -c ^[a-z0-9]) +# Others +else + updates=0 +fi + +# ----------------------------------------------------- +# Output in JSON format for Waybar Module custom-updates +# ----------------------------------------------------- + +css_class="green" + +if [ "$updates" -gt $threshhold_yellow ]; then + css_class="yellow" +fi + +if [ "$updates" -gt $threshhold_red ]; then + css_class="red" +fi +if [ "$updates" != 0 ]; then + if [ "$updates" -gt $threshhold_green ]; then + printf '{"text": "%s", "alt": "%s", "tooltip": "Click to update your system", "class": "%s"}' "$updates" "$updates" "$css_class" + else + printf '{"text": "0", "alt": "0", "tooltip": "No updates available", "class": "green"}' + fi +fi \ No newline at end of file diff --git a/dotfiles/.config/waybar/scripts/install-updates.sh b/dotfiles/.config/waybar/scripts/install-updates.sh new file mode 100644 index 0000000..bbff6b0 --- /dev/null +++ b/dotfiles/.config/waybar/scripts/install-updates.sh @@ -0,0 +1 @@ +$(cat ~/.config/ml4w/settings/terminal.sh) --class dotfiles-floating -e ~/.config/waybar/scripts/check-system-updates \ No newline at end of file diff --git a/dotfiles/.config/waybar/style.css b/dotfiles/.config/waybar/style.css index fea0596..8dad178 100644 --- a/dotfiles/.config/waybar/style.css +++ b/dotfiles/.config/waybar/style.css @@ -16,6 +16,25 @@ window#waybar { .modules-center { } + +/* Updated Update Module Styling */ +#custom-updates { + padding: 5px 15px; + background-color: rgba(0, 43, 51, 0.85); /* Matches your other modules */ + border-radius: 20px; + margin: 5px 3px; +} + +#custom-updates.yellow { + background-color: #ffcc66; /* A nice yellow/orange */ + color: #000000; +} + +#custom-updates.red { + background-color: #cc3436; /* Your existing red color */ + color: #ffffff; +} + .modules-left { margin: 2px 0 0 5px; }