Created
January 10, 2024 11:44
-
-
Save sdebruyn/82c20febe6b57b9f9f4a31b421446afe to your computer and use it in GitHub Desktop.
battery function
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
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