machine_setup/ansible/roles/sway/files/waybar_battery_freebsd.bash
2022-11-12 12:52:26 -05:00

60 lines
1.5 KiB
Bash

#!/usr/bin/env bash
#
# Read battery status in FreeBSD
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# TODO: Which is better, apm or reading sysctls from hw.acpi.battery ?
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
while true; do
battery_percentage=$(apm -l)
battery_status=$(apm -b)
seconds_remaining=$(apm -t)
class=""
if [ $battery_status -eq 3 ]; 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 [ $battery_percentage -le 15 ]; then
class="critical"
elif [ $battery_percentage -le 30 ]; then
class="warning"
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
jq --unbuffered --compact-output <<EOF
{
"text":"${battery_percentage}% ${battery_icon}",
"tooltip":"${time_remaining}",
"percentage":${battery_percentage},
"class":"${class}"
}
EOF
sleep $SLEEP_INTERVAL
done