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

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