Skip to content

Instantly share code, notes, and snippets.

@jeremyvisser
Created February 25, 2017 01:55
Show Gist options
  • Save jeremyvisser/7218528cb798d2ad31dbb3493bd36b3f to your computer and use it in GitHub Desktop.
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.
#!/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