Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
---------------------------------------------------------------------------
-- @author jesus <ask@jesus.now>
-- @copyright 0 Jesus
-- @release v0.0.0
---------------------------------------------------------------------------
local setmetatable = setmetatable
local os = os
local textbox = require("wibox.widget.textbox")
local capi = { timer = timer }
--- Battery widget.
-- awful.widget.battery
local battery = { mt = {} }
function battery:percentage()
-- 'echo "scale=4; $(cat /sys/class/power_supply/BAT0/energy_now) / $(cat /sys/class/power_supply/BAT0/energy_full)" | bc'
local file_now = io.open('/sys/class/power_supply/BAT0/energy_now', 'r')
local energy_now = file_now:read '*all'
file_now:close()
local file_full = io.open('/sys/class/power_supply/BAT0/energy_full', 'r')
local energy_full = file_full:read '*all'
file_full:close()
return energy_now / energy_full
end
function battery:status()
local file_status = io.open('/sys/class/power_supply/BAT0/status', 'r')
local status = file_status:read '*all'
file_status:close()
return status
end
function battery:decimal()
return math.floor(battery:percentage() * 10000) / 10000
end
--- Create a battery widget. It draws the current battery percentage in a textbox.
-- @param timeout How often update the time. Default is 60.
-- @return A textbox widget.
function battery.new(timeout)
local timeout = timeout or 1
local w = textbox()
local timer = capi.timer { timeout = timeout }
timer:connect_signal("timeout", function()
local status = battery:status()
local decimal = battery:decimal()
--text = text .. status
local ts = ""
if status == "Charging\n" then
if decimal > 0.95 then
ts = " <span color=\"#00FFFF\">+</span> "
else
ts = " <span color=\"#00FF00\">+</span> "
end
else
if decimal < 0.15 then
ts = " <span color=\"#FF0000\">-</span> "
else
ts = " <span color=\"#FFFF00\">-</span> "
end
end
w:set_markup(decimal .. ts)
end)
timer:start()
timer:emit_signal("timeout")
return w
end
function battery.mt:__call(...)
return battery.new(...)
end
return setmetatable(battery, battery.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment