From b3769ea57de2865057e58b78471c4f5ef1e09eb1 Mon Sep 17 00:00:00 2001 From: Deven Blake Date: Thu, 19 Aug 2021 08:33:08 -0400 Subject: [PATCH] battery monitor tool --- dotfiles-old/bin/batterymonitor | 54 +++++++++++++++++++++++++++++++++ dotfiles-old/manifest_file | 1 + 2 files changed, 55 insertions(+) create mode 100755 dotfiles-old/bin/batterymonitor diff --git a/dotfiles-old/bin/batterymonitor b/dotfiles-old/bin/batterymonitor new file mode 100755 index 0000000..2949920 --- /dev/null +++ b/dotfiles-old/bin/batterymonitor @@ -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 diff --git a/dotfiles-old/manifest_file b/dotfiles-old/manifest_file index a421643..ffd9b5e 100644 --- a/dotfiles-old/manifest_file +++ b/dotfiles-old/manifest_file @@ -16,6 +16,7 @@ .config/displaym/i3wm/sxhkdrc .config/i3/config .config/i3status/config +bin/batterymonitor bin/data bin/displaym bin/mnt