awesome/lib/awful/widget/textclock.lua.in

49 lines
1.5 KiB
Lua
Raw Normal View History

---------------------------------------------------------------------------
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2009 Julien Danjou
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
local setmetatable = setmetatable
local os = os
local textbox = require("wibox.widget.textbox")
local timer = require("gears.timer")
--- Text clock widget.
-- awful.widget.textclock
local textclock = { mt = {} }
-- This lowers the timeout so that it occurs "correctly". For example, a timeout
-- of 60 is rounded so that it occurs the next time the clock reads ":00 seconds".
local function calc_timeout(real_timeout)
return real_timeout - os.time() % real_timeout
end
--- Create a textclock widget. It draws the time it is in a textbox.
-- @param format The time format. Default is " %a %b %d, %H:%M ".
-- @param timeout How often update the time. Default is 60.
-- @return A textbox widget.
function textclock.new(format, timeout)
local format = format or " %a %b %d, %H:%M "
local timeout = timeout or 60
local w = textbox()
local t = timer { timeout = timeout }
t:connect_signal("timeout", function()
w:set_markup(os.date(format))
t.timeout = calc_timeout(timeout)
t:again()
end)
t:start()
t:emit_signal("timeout")
return w
end
function textclock.mt:__call(...)
return textclock.new(...)
end
return setmetatable(textclock, textclock.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80