1
0

split out battery from batterymonitor

This commit is contained in:
dtb 2022-06-26 16:46:44 -04:00
parent ad8cca39ae
commit e27110d9e9
2 changed files with 22 additions and 25 deletions

18
bin/battery Executable file
View File

@ -0,0 +1,18 @@
#!/bin/sh
# adapted from other script i wrote, also in the public domain
if [ "$(uname)" = "Linux" ]; then
# this is a dirty hack.
# acpi | awk '{print $4}' will print the battery percentage,
# and s/,$// strips the trailing comma.
printf "%s\n" "$(acpi | awk '{print $4}' | sed 's/,$//')"
elif [ "$(uname)" = "NetBSD" ]; then
printf "%s\n" "$(envstat -s acpibat0:charge \
| sed 1,2d \
| cut -d ':' -f 2 \
| awk '{print $1}' \
| cut -d '.' -f 1)"
else
printf "%s: unsupported system\n" "$(uname)" 1>&2
exit 1
fi

View File

@ -9,40 +9,19 @@ emit() {
printf "[%s] " "$(date)"
echo "$@"
}
get_current_level() {
# adapted from other script i wrote, also in the public domain
if [ "$(uname)" = "Linux" ]; then
# this is a dirty hack.
# acpi | awk '{print $4}' will print the battery percentage,
# and s/,$// strips the trailing comma.
# acpi -a will just print the power adapter status.
printf "%b" "$(acpi | awk '{print $4}' | sed 's/,$//')"
return 0
elif [ "$(uname)" = "NetBSD" ]; then
printf "%b" "$(envstat -s acpibat0:charge \
| sed 1,2d \
| cut -d ':' -f 2 \
| awk '{print $1}' \
| cut -d '.' -f 1)"
return 0
else
printf "get_battery_status: unsupported OS" >>/dev/stderr
return 1
fi
}
! get_current_level >/dev/null \
&& printf "Unable to get battery status on this system.\n" >>/dev/stderr \
! battery >/dev/null \
&& printf "Unable to get battery status on this system.\n" 1>&2 \
&& exit 1 \
|| true
current_level=$(get_current_level)
current_level=$(battery)
previous_level=$current_level
emit "$(printf "Current level: %d%%" "$current_level")"
while true; do
current_level=$(get_current_level)
current_level=$(battery)
if ! [ $current_level -eq $previous_level ]; then
[ $current_level -lt $previous_level ] \
&& emit "$(printf "Discharged: %d%% -> %d%%\n" $previous_level $current_level )" \