Created
February 25, 2017 01:55
-
-
Save jeremyvisser/7218528cb798d2ad31dbb3493bd36b3f to your computer and use it in GitHub Desktop.
Read battery percentage and charge/discharge state. Useful as a tmux status plugin. Uses shell builtins heavily to be as fast as possible.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -o nounset | |
set -o errexit | |
BAT=/sys/class/power_supply/BAT0 | |
if [[ ! -d $BAT ]]; then | |
echo 'No battery' | |
exit 0 | |
fi | |
cd $BAT | |
read BAT_FULL < charge_full | |
read BAT_NOW < charge_now | |
BAT_PCT=$(( 100 * $BAT_NOW / $BAT_FULL )) | |
BAT_STAT="" | |
read STATUS < status | |
case $STATUS in | |
Full) | |
BAT_STAT="=" | |
;; | |
Discharging) | |
BAT_STAT="<" | |
;; | |
Charging) | |
BAT_STAT=">" | |
;; | |
esac | |
echo "${BAT_STAT}${BAT_PCT}%" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment