Debian Codegraph @

obvious/clock/init.lua

raw symbols SCIP
1 --------------------------------
2 -- Author: Gregor Best --
3 -- Copyright 2009 Gregor Best --
4 --------------------------------
5
6 local os_time = os.time
7 local os_date = os.date
8 local tostring = tostring
9
10 local pairs = pairs
11 local print = print
12 local setmetatable = setmetatable
13 local tonumber = tonumber
14 local type = type
15 local os = {
16 date = os.date,
17 getenv = os.getenv
18 }
19 local io = {
20 lines = io.lines,
21 popen = io.popen,
22 }
23 local string = {
24 match = string.match
25 }
26 local table = {
27 insert = table.insert
28 }
29 local capi = {
30 mouse = mouse,
31 screen = screen
32 }
33 local awful = require("awful")
34 local wibox = require("wibox")
35 local beautiful = require("beautiful")
36 local naughty = require("naughty")
37 local lib = {
38 hooks = require("obvious.lib.hooks"),
39 markup = require("obvious.lib.markup")
40 }
41
42 local clock = {}
43
44 local initialized = false
45 local defaults = {
46 shorttimeformat = "%T",
47 longtimeformat = "%T %D",
48 editor = nil,
49 shorttimer = 60,
50 longtimer = 120,
51 scrolling = false,
52 scrolltimeout = 10,
53 }
54 local settings = setmetatable({}, { __index = defaults })
55
56 local menu
57
58 local function edit(file)
59 if not settings.editor then
60 naughty.notify({ text="Obvious Clock: You need to configure your" ..
61 " editor. See readme.",
62 timeout=0 })
63 else
64 awful.util.spawn(settings.editor .. " " .. file)
65 end
66 end
67
68 local function pread(cmd)
69 local pipe = io.popen(cmd)
70 if not pipe then
71 return ''
72 end
73 local results = pipe:read '*a'
74 pipe:close()
75 return results
76 end
77
78 -- non BSD-like `cal` for -h return help
79 local function is_bsd()
80 return not pread('cal -h'):find('help')
81 end
82
83 local show_calendar
84 do
85 local cal_notification
86
87 function show_calendar(year, month)
88 local cmd = 'cal'
89 if is_bsd() then
90 cmd = 'cal -h'
91 end
92 cmd = cmd .. ' ' .. tostring(month) .. ' ' .. tostring(year)
93
94 local notify_args = {
95 text = lib.markup.font("monospace",
96 pread(cmd):
97 gsub("([^0-9])(" .. tonumber(os.date("%d")) .. ")([^0-9])",
98 "%1<span foreground=\"#FF0000\">%2</span>%3"):gsub("\n+$", "")),
99 screen = capi.mouse.screen
100 }
101
102 if cal_notification and cal_notification.box.visible then
103 notify_args.replaces_id = cal_notification.id
104 end
105
106 cal_notification = naughty.notify(notify_args)
107 end
108 end
109
110 local alarmfile = awful.util.getdir("config").."/alarms"
111
112 local fulldate = false
113 local alarms = { }
114
115 local widget = wibox.widget.textbox()
116
117 local last_scroll_time = 0
118 local displayyear = 0
119 local displaymonth = 0
120
121 widget:buttons(awful.util.table.join(
122 awful.button({ }, 3, function ()
123 menu:toggle()
124 end),
125 awful.button({}, 4, function()
126 displaymonth = displaymonth + 1
127
128 if displaymonth == 13 then
129 displaymonth = 1
130 displayyear = displayyear + 1
131 end
132
133 show_calendar(displayyear, displaymonth)
134 last_scroll_time = os_time()
135 end),
136 awful.button({}, 5, function()
137 displaymonth = displaymonth - 1
138
139 if displaymonth == 0 then
140 displaymonth = 12
141 displayyear = displayyear - 1
142 end
143
144 show_calendar(displayyear, displaymonth)
145 last_scroll_time = os_time()
146 end),
147 awful.button({ }, 1, function ()
148 if #alarms > 0 then
149 for _, v in pairs(alarms) do
150 naughty.notify({
151 text = v[2],
152 title = v[1],
153 screen = capi.mouse.screen
154 })
155 end
156 alarms = { }
157 widget.bg = beautiful.bg_normal
158 else
159 if not settings.scrolling or os_time() - settings.scrolltimeout > last_scroll_time then
160 local date_info = os_date '*t'
161 displayyear = date_info.year
162 displaymonth = date_info.month
163 end
164
165 show_calendar(displayyear, displaymonth)
166 last_scroll_time = os_time()
167 end
168 end)
169 ))
170
171 local function read_alarms(file)
172 local rv = { }
173 local date = nil
174
175 if not awful.util.file_readable(file) then
176 return { }
177 end
178
179 for line in io.lines(file) do
180 line = line:gsub("\\n", "\n")
181 if not date then
182 date = line
183 else
184 rv[date] = line
185 date = nil
186 end
187 end
188 return rv
189 end
190
191 local function update (trigger_alarms)
192 if trigger_alarms == nil then
193 trigger_alarms = true
194 end
195 local date
196 if fulldate then
197 if type(settings.longtimeformat) == "string" then
198 date = os.date(settings.longtimeformat)
199 elseif type(settings.longtimeformat) == "function" then
200 date = os.date(settings.longtimeformat())
201 end
202 if not date then
203 date = os.date(defaults.longtimeformat)
204 end
205 else
206 if type(settings.shorttimeformat) == "string" then
207 date = os.date(settings.shorttimeformat)
208 elseif type(settings.shorttimeformat) == "function" then
209 date = os.date(settings.shorttimeformat())
210 end
211 if not date then
212 date = os.date(defaults.shorttimeformat)
213 end
214 end
215
216 if #alarms > 0 then
217 date = lib.markup.fg.color(beautiful.fg_focus, date)
218 widget.bg = beautiful.bg_focus
219 else
220 widget.bg = beautiful.bg_normal
221 end
222
223 widget:set_markup(date)
224
225 if trigger_alarms then
226 local data = read_alarms(alarmfile)
227 local currentdate = os.date("%a-%d-%m-%Y:%H:%M")
228 for date, message in pairs(data) do
229 if currentdate:match(date) then
230 naughty.notify({
231 text = message,
232 title = currentdate,
233 screen = capi.screen.count()
234 })
235 local add = true
236 for _, v in pairs(alarms) do
237 if v[1] == date and v[2] == message then
238 add = false
239 break
240 end
241 end
242 if add then table.insert(alarms, { currentdate, message }) end
243 end
244 end
245 update(false)
246 end
247 end
248
249 widget:connect_signal("mouse::enter", function ()
250 fulldate = true
251 update(false)
252 end)
253
254 widget:connect_signal("mouse::leave", function ()
255 fulldate = false
256 update(false)
257 end)
258
259 function clock.set_editor(e)
260 settings.editor = e or defaults.editor
261 end
262
263 function clock.set_longformat(strOrFn)
264 settings.longtimeformat = strOrFn or defaults.longtimeformat
265 update(false)
266 end
267
268 function clock.set_shortformat(strOrFn)
269 settings.shorttimeformat = strOrFn or defaults.shorttimeformat
270 update(false)
271 end
272
273 function clock.set_shorttimer(delay)
274 settings.shorttimer = delay or defaults.shorttimer
275 end
276
277 function clock.set_longtimer(delay)
278 settings.longtimer = delay or defaults.longtimer
279 end
280
281 function clock.set_scrolling(active)
282 settings.scrolling = active
283 end
284
285 function clock.set_scrolltimeout(timeout)
286 settings.scrolltimeout = timeout
287 end
288
289 return setmetatable(clock, { __call = function ()
290 update()
291 if not initialized then
292 lib.hooks.timer.register(settings.shorttimer, settings.longtimer, update)
293 lib.hooks.timer.start(update)
294
295 menu = awful.menu.new({
296 id = "clock",
297 items = {
298 { "Edit Todo", function () edit(os.getenv("HOME") .. "/todo") end },
299 { "Edit Alarms", function () edit(alarmfile) end }
300 }
301 })
302
303 initialized = true
304 end
305
306 return widget
307 end })
308
309 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et