lain/widget/watch.lua

41 lines
1.1 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
--]]
2017-02-08 20:45:11 +01:00
local helpers = require("lain.helpers")
local textbox = require("wibox.widget.textbox")
2014-08-08 14:10:25 +02:00
-- Template for asynchronous watcher widgets
2017-02-08 14:15:48 +01:00
-- lain.widget.watch
2014-08-08 14:10:25 +02:00
2017-02-08 20:45:11 +01:00
local function factory(args)
local watch = { widget = args.widget or textbox() }
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
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
2017-02-08 20:45:11 +01:00
return factory