machine_setup/ansible/roles/sway/files/waybar_battery_freebsd
2022-10-28 00:27:32 -04:00

51 lines
1.4 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)
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 [ $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}
}
EOF
sleep $SLEEP_INTERVAL
done