Skip to content

Instantly share code, notes, and snippets.

@sdebruyn
Created January 10, 2024 11:44
Show Gist options
  • Save sdebruyn/82c20febe6b57b9f9f4a31b421446afe to your computer and use it in GitHub Desktop.
Save sdebruyn/82c20febe6b57b9f9f4a31b421446afe to your computer and use it in GitHub Desktop.
battery function
battery() {
if !ioreg > /dev/null 2>&1; then
echo "ioreg not found. Exiting."
return 1
fi
_ioreg=`ioreg -l`
_profile=`system_profiler SPPowerDataType`
MOUSE=`echo $_ioreg -l | grep -A 10 "Mouse" | grep '"BatteryPercent" =' | sed 's/[^0-9]*//g'`
TRACKPAD=`echo $_ioreg -l | grep -A 10 "Track" | grep '"BatteryPercent" =' | sed 's/[^0-9]*//g'`
KEYBOARD=`echo $_ioreg -l | grep -A 10 "Keyboard" | grep '"BatteryPercent" =' | sed 's/[^0-9]*//g'`
CYCLE=`echo $_profile | grep "Cycle Count" | awk '{print $3}'`
if [ -n "$MOUSE" ]; then
echo "Mouse: "$MOUSE"%"
fi
if [ -n "$TRACKPAD" ]; then
echo "Trackpad: "$TRACKPAD"%"
fi
if [ -n "$KEYBOARD" ]; then
echo "Keyboard: "$KEYBOARD"%"
fi
if [ -n "$CYCLE" ] && [ "$CYCLE" -ne 0 ]; then
echo "Mac battery "`echo $_profile | grep "State of Charge" | awk '{print $5}'`"%"
echo "Charging: "`echo $_profile | grep "Charging" | head -n 1 | awk '{print $2}'`
echo "Cycles: "$CYCLE
echo "Condition: "`echo $_profile | grep "Condition" | awk '{print $2}'`
echo "Health: "`echo $_profile | grep "Maximum Capacity" | awk '{print $3}'`
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment