Skip to content

Instantly share code, notes, and snippets.

@grrowl
Created January 11, 2019 22:51
Show Gist options
  • Save grrowl/b082d034e30e27a5c6967ebc66ad2bc8 to your computer and use it in GitHub Desktop.
Save grrowl/b082d034e30e27a5c6967ebc66ad2bc8 to your computer and use it in GitHub Desktop.
Show AC wattage and battery drain (for bitbar)
#!/bin/bash
# <bitbar.title>ac-wattage</bitbar.title>
# <bitbar.version>1.0</bitbar.version>
# <bitbar.author>Tom McKenzie</bitbar.author>
# <bitbar.author.github>grrowl</bitbar.author.github>
# <bitbar.desc>Shows the currently AC Charger wattage, if any.</bitbar.desc>
# Get SPPowerDataType
powerdata=$(system_profiler SPPowerDataType)
connected=$(echo "$powerdata" | grep 'Connected:' | awk 'END{print $2}')
charging=$(echo "$powerdata" | grep 'Charging:' | awk 'END{print $2}')
wattage=$(echo "$powerdata" | grep 'Wattage (W):' | awk 'END{print $3}')
# battery percent approx.: $remaining / $fullcharge
remaining=$(echo "$powerdata" | grep 'Charge Remaining (mAh):' | awk '{print $4}')
fullcharge=$(echo "$powerdata" | grep 'Full Charge Capacity (mAh):' | awk '{print $5}')
charge=$((($remaining * 100) / $fullcharge))
# https://apple.stackexchange.com/a/16519
# current draw = (mA * mV * 0.000001)
amperage=$(echo "$powerdata" | grep 'Amperage (mA):' | awk 'END{print $3}')
voltage=$(echo "$powerdata" | grep 'Voltage (mV):' | awk 'END{print $3}')
draw=$(((${amperage} * ${voltage}) / -1000000))
# todo:
# show +/- battery draw/charging in watts if > 0 or charge < 50%
# show battery charge % if < 80%
# aim: if charging, how much. if dying, how fast
# show all minutae in dropdown
if [ "$connected" = "Yes" ]; then
if [ $wattage -lt $draw ]; then
color="darkred"
elif [ $wattage -lt 50 ]; then
color="orange"
else
color="green"
fi
if [ $charge -lt 95 ]; then
echo "${wattage}W ${charge}% | color=$color size=13"
else
echo "${wattage}W | color=$color size=13"
fi
else
echo "${charge}% | size=12"
fi
echo "---"
echo "Battery charge: ${charge}% | color=white"
if [ ! -z $wattage ]; then
echo "Wattage (W): $wattage"
fi
echo "Current draw (W): $draw"
echo "---"
echo "AC Connected: $connected"
echo "Charging: $charging"
echo "Refresh | refresh=true"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment