Skip to content

Instantly share code, notes, and snippets.

@osipxd
Last active November 12, 2021 11:42
Embed
What would you like to do?
Print current Mac wattage (with accuracy up to minute)
#!/usr/bin/env bash
set -euo pipefail
function extract_float_field() {
field=$1
ioreg -rw0 -c AppleSmartBattery |
grep BatteryData |
sed -E "s/.*\"$field\"=([0-9]+).*/\1/" |
xargs -I % lldb --batch -o "print/f %" | # Convert IEEE-754 float
grep -o '$0 = [0-9.]*' |
sed 's/.*= //'
}
printf -- 'Charging (watt): '
extract_float_field AdapterPower
printf -- 'Consumption (watt): '
extract_float_field SystemPower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment