Method: Duffy::System.battery_percent

Defined in:
lib/duffy/system.rb

.battery_percentObject

The battery percentage 0.0 .. 100.0 nil if no battery or error reading value. Darwin: pmset -g batt Linux:



99
100
101
102
103
104
105
106
107
# File 'lib/duffy/system.rb', line 99

def battery_percent
  case RUBY_PLATFORM
    when /darwin/ then `pmset -g batt`.scan(/^.*\t(.*%);/)[0][0].to_f
    when /linux/  then File.read("/sys/class/power_supply/BAT0/charge_now").to_i * 100 / File.read("/sys/class/power_supply/BAT0/charge_full").to_i
    else nil
  end
rescue
  nil
end