Method: LinuxStat::Battery.charge

Defined in:
lib/linux_stat/battery.rb

.chargeObject

Returns the charge of the battery.

If the battery is not present or the information is not available, it will return nil.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/linux_stat/battery.rb', line 107

def charge
	@@charge_now_file ||= if File.readable?(File.join(PATH, 'charge_now'))
		File.join(PATH, 'charge_now').freeze
	elsif File.readable?(File.join(PATH, 'energy_now'))
		File.join(PATH, 'energy_now').freeze
	end

	@@charge_full_file ||= if File.readable?(File.join(PATH, 'charge_full'))
		File.join(PATH, 'charge_full').freeze
	elsif File.readable?(File.join(PATH, 'energy_full'))
		File.join(PATH, 'energy_full').freeze
	end

	return nil unless @@charge_now_file && @@charge_full_file

	charge_now = IO.read(@@charge_now_file).to_i
	charge_full = IO.read(@@charge_full_file).to_i

	percentage = charge_now.*(100).fdiv(charge_full)
	percentage > 100 ? 100.0 : percentage < 0 ? 0.0 : percentage
end