2014-08-08 14:10:25 +02:00
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
|
|
|
* (c) 2014, Luke Bonham
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
2017-01-20 20:58:22 +01:00
|
|
|
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-02-08 14:09:52 +01:00
|
|
|
-- Template for asynchronous watcher widgets
|
|
|
|
-- lain.widgets.watch
|
2014-08-08 14:10:25 +02:00
|
|
|
|
|
|
|
local function worker(args)
|
2017-02-08 14:09:52 +01:00
|
|
|
local watch = {}
|
2017-01-24 16:15:40 +01:00
|
|
|
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-02-08 14:09:52 +01:00
|
|
|
watch.widget = args.widget or textbox()
|
2017-01-26 20:53:55 +01:00
|
|
|
|
2017-02-08 14:09:52 +01:00
|
|
|
function watch.update()
|
2017-01-20 20:58:22 +01:00
|
|
|
helpers.async(cmd, function(f)
|
2015-08-05 12:28:54 +02:00
|
|
|
output = f
|
2017-02-08 14:09:52 +01:00
|
|
|
if output ~= watch.prev then
|
|
|
|
widget = watch.widget
|
2016-03-03 12:40:49 +01:00
|
|
|
settings()
|
2017-02-08 14:09:52 +01:00
|
|
|
watch.prev = output
|
2016-03-03 12:40:49 +01:00
|
|
|
end
|
2014-08-08 14:10:25 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2017-02-08 14:09:52 +01:00
|
|
|
watch.timer = helpers.newtimer(cmd, timeout, watch.update, nostart, stoppable)
|
2014-08-08 14:10:25 +02:00
|
|
|
|
2017-02-08 14:09:52 +01:00
|
|
|
return watch
|
2014-08-08 14:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable({}, { __call = function(_, ...) return worker(...) end })
|