Files
configs/.config/waybar/scripts/check-mount.sh
2026-04-17 19:38:35 +02:00

44 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/bash
# Usage: check-mount.sh <mountpoint> <label>
# Outputs Waybar custom module JSON
MOUNT="$1"
LABEL="$2"
ICON_MOUNT="󰋊"
ICON_DOWN=""
json_escape() {
local s="$1"
s=${s//\\/\\\\}
s=${s//\"/\\\"}
s=${s//$'\n'/\\n}
printf '%s' "$s"
}
markup_escape() {
local s="$1"
s=${s//&/&amp;}
s=${s//</&lt;}
s=${s//>/&gt;}
printf '%s' "$s"
}
icon_markup() {
local icon="$1"
local color="$2"
printf '<span font_desc="SauceCodePro Nerd Font 15" foreground="%s">%s</span>' "$color" "$icon"
}
if mountpoint -q "$MOUNT"; then
read -r used avail pcent <<< "$(df -h --output=used,avail,pcent "$MOUNT" | tail -1)"
text="<span foreground=\"#D8DEE9\">$(markup_escape "$LABEL $used/$avail ($pcent)")</span> $(icon_markup "$ICON_MOUNT" "#A3BE8C")"
tooltip="$(markup_escape "$MOUNT mounted - used: $used, free: $avail ($pcent)")"
printf '{"text":"%s","tooltip":"%s","class":"mounted"}\n' \
"$(json_escape "$text")" "$(json_escape "$tooltip")"
else
text="<span foreground=\"#EBCB8B\">$(markup_escape "$LABEL N/A")</span> $(icon_markup "$ICON_DOWN" "#BF616A")"
tooltip="$(markup_escape "$MOUNT is NOT mounted")"
printf '{"text":"%s","tooltip":"%s","class":"unmounted"}\n' \
"$(json_escape "$text")" "$(json_escape "$tooltip")"
fi