#!/usr/bin/env bash set -euo pipefail bat="$(upower -e | grep -m1 BAT || true)" if [ -z "$bat" ]; then echo '{"text":" --","class":"no-battery","tooltip":"No battery found"}' exit 0 fi info="$(upower -i "$bat")" full=$(echo "$info" | awk -F: '/energy-full:/{gsub(/[^0-9.]/,"",$2); print $2; exit}') design=$(echo "$info" | awk -F: '/energy-full-design:/{gsub(/[^0-9.]/,"",$2); print $2; exit}') if [ -z "$full" ] || [ -z "$design" ] || [ "$design" = "0" ]; then # Try charge-full fallback full=$(echo "$info" | awk -F: '/charge-full:/{gsub(/[^0-9.]/,"",$2); print $2; exit}') design=$(echo "$info" | awk -F: '/charge-full-design:/{gsub(/[^0-9.]/,"",$2); print $2; exit}') fi wear="--" if [ -n "$full" ] && [ -n "$design" ] && [ "$design" != "0" ]; then health=$(awk -v f="$full" -v d="$design" 'BEGIN{printf "%.0f", (f/d)*100}') wear=$(( 100 - health )) text="❤ ${health}%" else text="❤ --" fi cycles=$(echo "$info" | awk -F: '/cycle count:/{gsub(/[^0-9]/,"",$2); print $2; exit}') vendor=$(echo "$info" | awk -F: '/vendor:/{gsub(/^ +/,"",$2); print $2; exit}') model=$(echo "$info" | awk -F: '/model:/{gsub(/^ +/,"",$2); print $2; exit}') tt="Battery health info\nWear: ${wear}%\nCycles: ${cycles:---}\nVendor: ${vendor:---}\nModel: ${model:---}" printf '{"text":"%s","class":"battery-health","tooltip":"%s"}\n' "$text" "$(echo "$tt" | sed ':a;N;$!ba;s/\n/\\n/g')"