Skip to content

Instantly share code, notes, and snippets.

@Capncavedan
Created April 9, 2012 15:45
Show Gist options
  • Save Capncavedan/2344352 to your computer and use it in GitHub Desktop.
Save Capncavedan/2344352 to your computer and use it in GitHub Desktop.
Ruby script to show OS X battery info at command line
With a nod to http://blog.tommorris.org/post/1230771656/ruby-snippet-for-macintosh-battery-status
Changed percentage to base off current battery capacity rather than design capacity,
and to extract and show estimated time remaining as well as whether wall-plugged
and whether charging.
The command-line ioreg utility returns a huge amount of data.
We're most interested in a set of lines having to do with the battery,
which is cleaned parsed within the Ruby script.
See the last file in this gist for an example of the battery section.
When operating from the battery, the output of the Ruby script is like so:
64% - 2:20 remaining
When operating from the wall charger, the output is like so:
Running off wall
Charging - 97% full
#!/usr/bin/ruby
ioreg_data = IO.popen("ioreg -w0 -l").readlines().join
ioreg_data.sub!(/.*(\+\-o AppleSmartBattery.*?\+\-o).*/m, '\\1')
ioreg_data.gsub!(/\s+\|/, "")
ioreg_data.gsub!(/\s\s+/, "\n")
battery = Hash.new
ioreg_data.split(/\n+/).each do |line|
key, value = line.split(' = ')[0..1]
next if key.nil? || value.nil?
battery[ key.gsub(/"/, '').strip ] = value.gsub(/"/, '').strip
end
# battery.each do |k, v|
# puts "#{k} :: #{v}"
# end
exit 1 unless (battery['CurrentCapacity'] && battery['MaxCapacity'] && battery['MaxCapacity'].to_i > 0)
percent_full = 100 * battery['CurrentCapacity'].to_f / battery['MaxCapacity'].to_f
hours_remaining = battery['TimeRemaining'].to_i/60
minutes_remaining = battery['TimeRemaining'].to_i - hours_remaining*60
if battery['ExternalConnected'] == 'Yes'
puts "Running off wall"
puts sprintf("Charging - %d%% full", percent_full) if battery['IsCharging'] == 'Yes'
puts "Fully charged" if battery['FullyCharged'] == 'Yes'
else
puts sprintf("Battery %d%% full, %d:%02d remaining", percent_full, hours_remaining, minutes_remaining)
end
exit 0
<snip>
| | +-o AppleSmartBattery <class AppleSmartBattery, id 0x1000001fd, registered, matched, active, busy 0 (0 ms), retain 5>
| | {
| | "ExternalConnected" = Yes
| | "TimeRemaining" = 19
| | "InstantTimeToEmpty" = 65535
| | "ExternalChargeCapable" = Yes
| | "CellVoltage" = (4183,4183,0,0)
| | "PermanentFailureStatus" = 0
| | "BatteryInvalidWakeSeconds" = 30
| | "AdapterInfo" = 0
| | "MaxCapacity" = 6216
| | "Voltage" = 8366
| | "DesignCycleCount70" = 65535
| | "Manufacturer" = "SMP"
| | "Location" = 0
| | "CurrentCapacity" = 6043
| | "LegacyBatteryInfo" = {"Amperage"=811,"Flags"=7,"Capacity"=6216,"Current"=6043,"Voltage"=8366,"Cycle Count"=162}
| | "FirmwareSerialNumber" = 1
| | "BatteryInstalled" = Yes
| | "CycleCount" = 162
| | "DesignCapacity" = 6700
| | "AvgTimeToFull" = 19
| | "ManufactureDate" = 15946
| | "BatterySerialNumber" = "D86106500QWDCQ8A8"
| | "PostDischargeWaitSeconds" = 120
| | "Temperature" = 3055
| | "MaxErr" = 1
| | "ManufacturerData" = <000000000301000a01620000034b3136033030410353414e002d00>
| | "FullyCharged" = No
| | "InstantAmperage" = 803
| | "DeviceName" = "bq20z451"
| | "IOGeneralInterest" = "IOCommand is not serializable"
| | "Amperage" = 811
| | "IsCharging" = Yes
| | "DesignCycleCount9C" = 1000
| | "PostChargeWaitSeconds" = 120
| | "AvgTimeToEmpty" = 65535
| | }
| |
<snip>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment