Added generic battery widget that uses acpi

This commit is contained in:
Thomas FORGIONE 2016-09-10 14:03:11 +02:00
parent 40d22733d3
commit 4699f4d592
1 changed files with 10 additions and 24 deletions

View File

@ -8,35 +8,21 @@ local beautiful = require("beautiful")
local previous_percent = 0
function batteryInfo(adapter)
local fcur = io.open("/sys/class/power_supply/"..adapter.."/energy_now")
local fcap = io.open("/sys/class/power_supply/"..adapter.."/energy_full")
local cur = fcur:read()
local cap = fcap:read()
fcur:close()
fcap:close()
local battery = math.floor(cur * 100 / cap)
if battery > 100 then
battery = 100
end
return battery
function batteryInfo()
local fd = io.popen('acpi | cut -d ":" -f 2 | cut -d "," -f 2 | tr -d "%"')
local percentage = fd:read('*all')
fd:close()
return tonumber(percentage)
end
function isCharging(adapter)
local fsta = io.open("/sys/class/power_supply/"..adapter.."/status")
local sta = fsta:read()
fsta:close()
return not sta:match("Discharging")
function isCharging()
local _,_,error = os.execute('acpi | grep Discharging')
return error ~= 0
end
function update_battery()
local percent = batteryInfo('BAT0')
local percent = batteryInfo()
local color
local symbol
@ -50,7 +36,7 @@ function update_battery()
color="white"
end
if isCharging('BAT0') then
if isCharging() then
color = 'green'
symbol = ''
else