lain/widgets/abase.lua

44 lines
1.2 KiB
Lua
Raw Normal View History

2014-08-08 14:10:25 +02:00
--[[
Licensed under GNU General Public License v2
* (c) 2014, Luke Bonham
--]]
local helpers = require("lain.helpers")
2017-01-29 14:45:15 +01:00
local textbox = require("wibox.widget.textbox")
2014-08-08 14:10:25 +02:00
local setmetatable = setmetatable
2017-01-29 14:45:15 +01:00
-- Template for custom asynchronous widgets
2014-08-08 14:10:25 +02:00
-- lain.widgets.abase
local function worker(args)
2017-01-29 14:45:15 +01:00
local abase = {}
local args = args or {}
local timeout = args.timeout or 5
local nostart = args.nostart or false
local stoppable = args.stoppable or false
2017-01-26 20:53:55 +01:00
local cmd = args.cmd
2017-01-29 14:45:15 +01:00
local settings = args.settings or function() widget:set_text(output) end
2014-08-08 14:10:25 +02:00
2017-01-29 14:45:15 +01:00
abase.widget = args.widget or textbox()
2017-01-26 20:53:55 +01:00
2014-08-08 14:10:25 +02:00
function abase.update()
helpers.async(cmd, function(f)
2015-08-05 12:28:54 +02:00
output = f
2016-03-03 12:40:49 +01:00
if output ~= abase.prev then
widget = abase.widget
settings()
abase.prev = output
end
2014-08-08 14:10:25 +02:00
end)
end
abase.timer = helpers.newtimer(cmd, timeout, abase.update, nostart, stoppable)
2014-08-08 14:10:25 +02:00
2017-01-25 17:13:14 +01:00
return abase
2014-08-08 14:10:25 +02:00
end
return setmetatable({}, { __call = function(_, ...) return worker(...) end })