Skip to content

Instantly share code, notes, and snippets.

@AaronM04
Last active March 24, 2021 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronM04/d90bb9173a4cc1a5879adb4a037768f1 to your computer and use it in GitHub Desktop.
Save AaronM04/d90bb9173a4cc1a5879adb4a037768f1 to your computer and use it in GitHub Desktop.
Flicker-free battery monitor for OpenBSD
#!/bin/sh
# Flicker-free battery monitor for OpenBSD. Requires Python 3
# One could somewhat easily rewrite the Python part in Perl if they want to use base exclusively.
BATT=acpibat0
while true; do
RATE="$(sysctl -n hw.sensors.$BATT.power0)"
RATE="${RATE%" ("*}"
REMAINING="$(sysctl -n hw.sensors.$BATT.watthour3)"
REMAINING="${REMAINING%" ("*}"
LASTFULL="$(sysctl -n hw.sensors.$BATT.watthour0)"
LASTFULL="${LASTFULL%" ("*}"
DATE=$(date)
# Don't like this python3 call? Replace the 5 lines after this comment with:
# TIMELEFT=$(echo "scale=2;${REMAINING%" "*}/${RATE%" "*}" | bc)
TIMELEFT=$(python3 -c "rate='$RATE';remaining='$REMAINING';rate=float(rate.split()[0]);remaining=float(remaining.split()[0]);hr=(remaining/rate); print('{}:{:02}'.format(int(hr),int(hr*60)%60))" 2>/dev/null)
if [[ "x$TIMELEFT" == "x" ]]; then
# ZeroDivisionError
TIMELEFT="N/A"
else
H=$(echo $TIMELEFT | cut -d: -f1) # hours
M=$(echo $TIMELEFT | cut -d: -f2 | cut -c1) # highest digit of minutes
# should match 0:00 through 0:19
if [[ $H == 0 && ( $M == 1 || $M == 0 ) ]]; then
echo -en '\a'
fi
fi
print "\033[H\033[2J$DATE\nTIME LEFT: $TIMELEFT\n---\nRate: $RATE\nRemaining: $REMAINING\nLast full: $LASTFULL"
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment