Skip to content

Instantly share code, notes, and snippets.

@coffeeaddict
Created April 16, 2014 07:33
Show Gist options
  • Save coffeeaddict/10824698 to your computer and use it in GitHub Desktop.
Save coffeeaddict/10824698 to your computer and use it in GitHub Desktop.
Battery stats
#!/usr/bin/env ruby
#
# Read battery event file and return some numbers
require 'json'
stats = {}
File.open("/sys/class/power_supply/BAT1/uevent", File::RDONLY) do |f|
while(line = f.gets)
(key, value) = line.chomp.split('=')
value = value.to_i unless value =~ /\D+/
stats[key.downcase.gsub("power_supply_", "").to_sym] = value
end
end
case ARGV[0]
when "status"
puts stats[:status]
when "charge"
puts stats[:charge_now] / (stats[:charge_full_design] / 100)
else
puts stats.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment