battery monitor tool
This commit is contained in:
parent
99bfbaafc9
commit
b3769ea57d
54
dotfiles-old/bin/batterymonitor
Executable file
54
dotfiles-old/bin/batterymonitor
Executable file
@ -0,0 +1,54 @@
|
||||
#!/bin/sh
|
||||
|
||||
POLLING_FREQUENCY=1
|
||||
|
||||
# ISO 8601
|
||||
alias date="date '+%Y-%m-%dT%T'"
|
||||
|
||||
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/,$//') ($(acpi -a))"
|
||||
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 \
|
||||
&& exit 1 \
|
||||
|| true
|
||||
|
||||
current_level=$(get_current_level)
|
||||
previous_level=$current_level
|
||||
|
||||
emit "$(printf "Current level: %d%%" "$current_level")"
|
||||
|
||||
while true; do
|
||||
current_level=$(get_current_level)
|
||||
if ! [ $current_level -eq $previous_level ]; then
|
||||
[ $current_level -lt $previous_level ] \
|
||||
&& emit "$(printf "Discharged: %d%% -> %d%%\n" $previous_level $current_level )" \
|
||||
|| emit "$(printf " Charged: %d%% -> %d%%\n" $previous_level $current_level )"
|
||||
previous_level=$current_level
|
||||
fi
|
||||
|
||||
sleep $POLLING_FREQUENCY
|
||||
done
|
@ -16,6 +16,7 @@
|
||||
.config/displaym/i3wm/sxhkdrc
|
||||
.config/i3/config
|
||||
.config/i3status/config
|
||||
bin/batterymonitor
|
||||
bin/data
|
||||
bin/displaym
|
||||
bin/mnt
|
||||
|
Loading…
Reference in New Issue
Block a user