don't get stuck running the event loop once there are no more clients
[wmiirc-lua.git] / plugins / clock.lua
blobb0f75200981a95f4d982eeee4d478537ee3451ab
1 --
2 -- Copyright (c) 2007, Bart Trojanowski <bart@jukie.net>
3 --
4 -- Simple clock applet for wmii bar.
5 --
6 local wmii = require("wmii")
7 local os = require("os")
9 module("clock") -- module name
10 api_version=0.1 -- api version, see doc/plugin-api
12 -- ------------------------------------------------------------
13 -- CLOCK CONFIGURATION VARIABLES
15 -- these can be overridden by wmiirc
17 wmii.set_conf ("clock.update", 1)
18 wmii.set_conf ("clock.format", "%Y/%m/%d %H:%M:%S")
20 -- ------------------------------------------------------------
21 -- MODULE VARIABLES
23 local widget = nil -- the display on the bar
24 local timer = nil -- the 1/second tick timer
26 -- ------------------------------------------------------------
27 -- THE TIMER WIDGET
29 -- Note that widgets are sorted in ascending order from left to
30 -- right on wmii's bar. By convention this is a 3 digit number
31 -- and is prefixed to the widget name. There is currently no
32 -- way to reorder a widget, but it is planed for a future release.
34 local widget = wmii.widget:new ("999_clock")
36 local xmessagebox = "xmessage -center -buttons quit:0 -default quit -file -"
37 local function button_handler (ev, button)
38 -- 3 is right button
39 if button == 3 then
40 os.execute("ncal -y | wmiisetsid " .. xmessagebox .. "&")
41 end
42 end
44 widget:add_event_handler("RightBarClick", button_handler)
48 -- ------------------------------------------------------------
49 -- THE TIMER FUNCTION
51 -- The timer function will be called every X seconds. If the
52 -- timer function returns a number
54 local function clock_timer (time_since_update)
55 local fmt = wmii.get_conf("clock.format") or "%c"
56 widget:show (os.date(fmt))
58 -- returning a positive number of seconds before next wakeup, or
59 -- nil (or no return at all) repeats the last schedule, or
60 -- -1 to stop the timer
61 return 1
62 end
64 local timer = wmii.timer:new (clock_timer, 1)