]> 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:

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