54 lines
1.7 KiB
Bash
Executable File
54 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
mode="${1:-status}"
|
|
|
|
read -r mem_total mem_used swap_total swap_used <<EOF
|
|
$(free -b | awk '
|
|
/^Mem:/ { mem_total=$2; mem_used=$3 }
|
|
/^Swap:/ { swap_total=$2; swap_used=$3 }
|
|
END { printf "%s %s %s %s\n", mem_total, mem_used, swap_total, swap_used }
|
|
')
|
|
EOF
|
|
|
|
format_bytes() {
|
|
numfmt --to=iec-i --suffix=B --format='%.1f' "$1"
|
|
}
|
|
|
|
mem_used_h="$(format_bytes "$mem_used")"
|
|
mem_total_h="$(format_bytes "$mem_total")"
|
|
mem_pct="$(awk -v used="$mem_used" -v total="$mem_total" 'BEGIN { if (total > 0) printf "%.1f", (used/total)*100; else print "0.0" }')"
|
|
|
|
if [[ "$swap_total" -gt 0 ]]; then
|
|
swap_used_h="$(format_bytes "$swap_used")"
|
|
swap_total_h="$(format_bytes "$swap_total")"
|
|
swap_pct="$(awk -v used="$swap_used" -v total="$swap_total" 'BEGIN { if (total > 0) printf "%.1f", (used/total)*100; else print "0.0" }')"
|
|
else
|
|
swap_used_h="0.0B"
|
|
swap_total_h="0.0B"
|
|
swap_pct="0.0"
|
|
fi
|
|
|
|
case "$mode" in
|
|
ram)
|
|
notify-send "RAM usage" "${mem_used_h} / ${mem_total_h} (${mem_pct}%)"
|
|
;;
|
|
swap)
|
|
if [[ "$swap_total" -gt 0 ]]; then
|
|
notify-send "Swap usage" "${swap_used_h} / ${swap_total_h} (${swap_pct}%)"
|
|
else
|
|
notify-send "Swap usage" "Swap is disabled"
|
|
fi
|
|
;;
|
|
status)
|
|
if [[ "$swap_total" -gt 0 ]]; then
|
|
tooltip="Left click: RAM (${mem_used_h}/${mem_total_h}, ${mem_pct}%)\nRight click: Swap (${swap_used_h}/${swap_total_h}, ${swap_pct}%)"
|
|
else
|
|
tooltip="Left click: RAM (${mem_used_h}/${mem_total_h}, ${mem_pct}%)\nRight click: Swap (disabled)"
|
|
fi
|
|
|
|
printf '{"text":"","tooltip":"%s"}\n' "$tooltip"
|
|
;;
|
|
esac
|