From 4699f4d592ac8e81ca6059630b2ba51dbee01e11 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Sat, 10 Sep 2016 14:03:11 +0200 Subject: [PATCH] Added generic battery widget that uses acpi --- awesome/battery.lua | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/awesome/battery.lua b/awesome/battery.lua index 00b9999..d2fc4f4 100644 --- a/awesome/battery.lua +++ b/awesome/battery.lua @@ -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