Skip to content

Instantly share code, notes, and snippets.

@palladius
Last active November 10, 2022 03:53
Show Gist options
  • Save palladius/7ab7b0c2fd3e600920df87999b5242eb to your computer and use it in GitHub Desktop.
Save palladius/7ab7b0c2fd3e600920df87999b5242eb to your computer and use it in GitHub Desktop.
An excerpt with the logic of my Macbook Battery.
#!/usr/bin/env ruby
# Note1. buridone is currently not used. It's for a more efficient use to extrapolate all info from a single file read.
# Note2. This does NOT work on M1. I've just fixed this in the github repo sakura. Find the updated 0.2 code there.
def processMacCapacity(buridone)
ret = {}
ret[:debug] = 'Will fill numerator and denominator until I nail it.'
ret[:capacity_pct] = 142 # clearlly wrong
# `ioreg -l -w0 | grep AppleRawMaxCapacity`.split
# => ["|", "|", "\"AppleRawMaxCapacity\"", "=", "4320"]
ret[:AppleRawMaxCapacity] = `ioreg -l -w0 | grep AppleRawMaxCapacity`.split[4].to_i
ret[:AppleRawCurrentCapacity] = `ioreg -l -w0 | grep AppleRawCurrentCapacity`.split[4].to_i
# => ["\"StateOfCharge\"=41"]
ret[:StateOfCharge] = `ioreg -l -w0 | grep BatteryData`.split(',').select{|e| e.match /StateOfCharge/ }[0].split('=')[1].to_i
ret[:AppleDesignCapacity] = `ioreg -l -w0 | grep BatteryData`.split(',').select{|e| e.match /DesignCapacity/ }[0].split('=')[1].to_i
# derived values..
# this should be your battery life i guess?x
ret[:BatteryCapacityPercent] = ret[:AppleRawCurrentCapacity]*100.0/ret[:AppleRawMaxCapacity]
ret[:battery_health] = ret[:AppleRawMaxCapacity]*100.0/ret[:AppleDesignCapacity]
return ret
end
def real_program
capacity_hash = processMacCapacity(`ioreg -l -w0 | grep Capacity`)
deb "capacity_hash: '''#{white capacity_hash}'''"
if $DEBUG
capacity_hash.each{|k,v|
puts "[DEB] #{k}:\t#{v}"
}
end
puts "1. 🔋 BatteryLife % 🔌🪫: #{capacity_hash[:BatteryCapacityPercent].round(2)}"
puts "2. 🔋 BatteryHealth % 🛟: #{capacity_hash[:battery_health].round(2)}"
end
def main(filename)
deb "I'm called by #{white filename}"
init # Enable this to have command line parsing capabilities!
real_program
end
main(__FILE__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment