Get OSX Leopard Battery Stats For Your Laptop


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby -wKU
  2. res = Array.new
  3. res = %x(/usr/sbin/ioreg -p IODeviceTree -n "battery" -w 0).grep(/Battery/)[0].match(/\{(.*)\}/)[1].split(',')
  4. stats = Hash.new
  5.  
  6. res.each do |d|
  7. key, val = d.split("=")
  8. key.downcase!.delete!(" ")
  9. if key =~ /\"(.*)\"/
  10. key = $1
  11. end
  12. stats[:"#{key}"] = val
  13. end
  14.  
  15. puts
  16. puts "-------------------------------"
  17. puts " Battery Stats"
  18. puts "-------------------------------"
  19. puts
  20. stats.each { |k, v| puts "#{k} => #{v}" }
  21. percentOfOriginal = (stats[:capacity].to_f / stats[:absolutemaxcapacity].to_f) * 100
  22. puts "Battery capacity is currently #{sprintf("%.2f", percentOfOriginal)}% of original."

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.