]> git.madduck.net Git - etc/awesome.git/blob - widget/calendar.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

e7b330ad4282c9825277df0737d187ac0a03ee2e
[etc/awesome.git] / widget / calendar.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013, Luca CPZ
5
6 --]]
7
8 local helpers      = require("lain.helpers")
9 local markup       = require("lain.util.markup")
10 local awful        = require("awful")
11 local naughty      = require("naughty")
12 local mouse        = mouse
13 local os           = os
14 local string       = string
15 local ipairs       = ipairs
16 local tonumber     = tonumber
17 local setmetatable = setmetatable
18
19 -- Calendar notification
20 -- lain.widget.calendar
21 local calendar = { offset = 0 }
22
23 function calendar.hide()
24     if not calendar.notification then return end
25     naughty.destroy(calendar.notification)
26     calendar.notification = nil
27 end
28
29 function calendar.show(t_out, inc_offset, scr)
30     local f, offs = nil, inc_offset or 0
31
32     calendar.notification_preset.screen = scr or (calendar.followtag and awful.screen.focused()) or 1
33     calendar.offset = calendar.offset + offs
34
35     local current_month = (offs == 0 or calendar.offset == 0)
36
37     if current_month then -- today highlighted
38         calendar.offset = 0
39         calendar.icon   = calendar.icons:len() > 0 and string.format("%s%s.png", calendar.icons, tonumber(os.date("%d")))
40         f               = calendar.cal
41     else -- no current month showing, no day to highlight
42        local year  = tonumber(os.date("%Y"))
43        local month = tonumber(os.date("%m")) + calendar.offset
44
45        while month > 12 do
46            month = month - 12
47            year = year + 1
48        end
49
50        while month < 1 do
51            month = month + 12
52            year = year - 1
53        end
54
55        calendar.icon = nil
56        f = string.format("%s %s %s", calendar.cal, month, year)
57     end
58
59     helpers.async(f, function(ws)
60         local fg, bg = calendar.notification_preset.fg, calendar.notification_preset.bg
61         calendar.notification_preset.text = ws:gsub("%c%[%d+[m]?%s?%d+%c%[%d+[m]?",
62         markup.bold(markup.color(bg, fg, os.date("%e")))):gsub("[\n%s]*$", "")
63
64         local widget_focused = true
65
66         if t_out == 0 and mouse.current_widgets then
67             widget_focused = false
68             for i, widget in ipairs(calendar.attach_to) do
69                 for _,v in ipairs(mouse.current_widgets) do
70                     if widget == v then
71                         widget_focused = true
72                         break
73                     end
74                 end
75             end
76         end
77
78         if widget_focused then
79             calendar.hide()
80             calendar.notification = naughty.notify({
81                 preset  = calendar.notification_preset,
82                 icon    = calendar.icon,
83                 timeout = t_out or calendar.notification_preset.timeout or 5
84             })
85         end
86     end)
87 end
88
89 function calendar.hover_on() calendar.show(0) end
90 function calendar.hover_off() calendar.hide() end
91 function calendar.prev() calendar.show(0, -1) end
92 function calendar.next() calendar.show(0, 1) end
93
94 function calendar.attach(widget)
95     widget:connect_signal("mouse::enter", calendar.hover_on)
96     widget:connect_signal("mouse::leave", calendar.hover_off)
97     widget:buttons(awful.util.table.join(
98                 awful.button({}, 1, calendar.prev),
99                 awful.button({}, 3, calendar.next),
100                 awful.button({}, 2, calendar.hover_on),
101                 awful.button({}, 4, calendar.prev),
102                 awful.button({}, 5, calendar.next)))
103 end
104
105 local function factory(args)
106     local args                   = args or {}
107     calendar.cal                 = args.cal or "/usr/bin/cal"
108     calendar.attach_to           = args.attach_to or {}
109     calendar.followtag           = args.followtag or false
110     calendar.icons               = args.icons or helpers.icons_dir .. "cal/white/"
111     calendar.notification_preset = args.notification_preset
112
113     if not calendar.notification_preset then
114         calendar.notification_preset = {
115             font = "Monospace 10",
116             fg   = "#FFFFFF",
117             bg   = "#000000"
118         }
119     end
120
121     for i, widget in ipairs(calendar.attach_to) do calendar.attach(widget) end
122 end
123
124 return setmetatable(calendar, { __call = function(_, ...) return factory(...) end })