lain/widgets/watch.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
-- Template for asynchronous watcher widgets
-- lain.widgets.watch
2014-08-08 14:10:25 +02:00
local function worker(args)
local watch = {}
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
watch.widget = args.widget or textbox()
2017-01-26 20:53:55 +01:00
function watch.update()
helpers.async(cmd, function(f)
2015-08-05 12:28:54 +02:00
output = f
if output ~= watch.prev then
widget = watch.widget
2016-03-03 12:40:49 +01:00
settings()
watch.prev = output
2016-03-03 12:40:49 +01:00
end
2014-08-08 14:10:25 +02:00
end)
end
watch.timer = helpers.newtimer(cmd, timeout, watch.update, nostart, stoppable)
2014-08-08 14:10:25 +02:00
return watch
2014-08-08 14:10:25 +02:00
end
return setmetatable({}, { __call = function(_, ...) return worker(...) end })