machine_setup/ansible/roles/sway/files/waybar_battery_linux

63 lines
1.8 KiB
Plaintext
Raw Normal View History

2022-10-28 04:27:32 +00:00
#!/usr/bin/env bash
#
# Read battery status in FreeBSD
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
batteries=$(find /sys/class/power_supply -name 'BAT*')
while true; do
text=""
tooltip=""
percentage=""
while read bat; do
battery_now=$(cat "$bat/charge_now")
battery_capacity=$(cat "$bat/charge_full")
battery_percentage=$(( battery_now * 100 / battery_capacity ))
battery_status=$(cat "$bat/status")
seconds_remaining="-1"
if [ $battery_status = "Charging" ]; then
if [ $battery_percentage -eq 100 ]; then
battery_icon=""
else
battery_icon=""
fi
elif [ $battery_percentage -le 20 ]; then
battery_icon=""
elif [ $battery_percentage -le 40 ]; then
battery_icon=""
elif [ $battery_percentage -le 60 ]; then
battery_icon=""
elif [ $battery_percentage -le 80 ]; then
battery_icon=""
elif [ $battery_percentage -lt 100 ]; then
battery_icon=""
else
battery_icon=""
fi
if [ $seconds_remaining -eq -1 ]; then
time_remaining="unknown time remaining."
else
time_remaining=$(printf '%dh:%dm:%ds\n' $((seconds_remaining/3600)) $((seconds_remaining%3600/60)) $((seconds_remaining%60)) )
fi
text="$text${battery_percentage}% ${battery_icon}"
tooltip="$tooltip${time_remaining}"
percentage="$percentage${battery_percentage}"
done<<<"$batteries"
jq --unbuffered --compact-output <<EOF
{
"text":"$text",
"tooltip":"$tooltip",
"percentage":$percentage
}
EOF
sleep $SLEEP_INTERVAL
done