Skip to content

Instantly share code, notes, and snippets.

@Willamin
Created July 21, 2017 15:43
Show Gist options
  • Save Willamin/5cb5bf1cd77103020dd65e2e6a8b3235 to your computer and use it in GitHub Desktop.
Save Willamin/5cb5bf1cd77103020dd65e2e6a8b3235 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def output_nicely(title, value)
puts format('%-25s %6.2f', "#{title}:", value)
end
data = `/usr/sbin/system_profiler SPPowerDataType`
remaining = data[/Charge Remaining.*: (\d+)/, 1].to_f
capacity = data[/Full Charge Capacity.*: (\d+)/, 1].to_f
amperage = data[/Amperage.*: (\d+)/, 1].to_f
voltage = data[/Voltage.*: (\d+)/, 1].to_f
calc_power = amperage * voltage / 1000000
calc_percent = remaining / capacity
calc_remaining_hours = remaining / amperage
output_nicely 'Remaining (%)', (calc_percent * 100)
output_nicely 'Current Power Draw (W)', (calc_power)
output_nicely 'Remaining Time (H)', (calc_remaining_hours)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment