]> git.madduck.net Git - etc/awesome.git/blob - widget/cal.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:

7a81c68924fcc76f77e1d1924f03766b91cc38cb
[etc/awesome.git] / widget / cal.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2018, 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 floor    = math.floor
13 local os       = os
14 local string   = string
15 local ipairs   = ipairs
16 local tconcat  = table.concat
17 local tonumber = tonumber
18 local tostring = tostring
19
20 -- Calendar notification
21 -- lain.widget.cal
22 local function factory(args)
23     args = args or {}
24     local cal = {
25         weekStart           = args.weekStart or 2,
26         attach_to           = args.attach_to or {},
27         followtag           = args.followtag or false,
28         icons               = args.icons or helpers.icons_dir .. "cal/white/",
29         notification_preset = args.notification_preset or {
30             font = "Monospace 10", fg = "#FFFFFF", bg = "#000000"
31         }
32     }
33
34     function cal.hide()
35         if not cal.notification then return end
36         naughty.destroy(cal.notification)
37         cal.notification = nil
38     end
39
40     function cal.show(timeout, month, year)
41         local current_month, current_year = tonumber(os.date("%m")), tonumber(os.date("%Y"))
42         local is_current_month = (not month or not year) or (month == current_month and year == current_year)
43         local today = is_current_month and tonumber(os.date("%d")) -- otherwise nil and not highlighted
44         local t = os.time { year = year or current_year, month = month and month+1 or current_month+1, day = 0 }
45         local d = os.date("*t", t)
46         local mthDays, stDay, cmonth = d.day, (d.wday-d.day-cal.weekStart+1)%7, os.date("%B %Y", t)
47         local notifytable = { [1] = string.format("%s%s\n", string.rep(" ", floor((28 - cmonth:len())/2)), markup.bold(cmonth)) }
48         for x = 0,6 do notifytable[#notifytable+1] = os.date("%a ", os.time { year=2006, month=1, day=x+cal.weekStart }) end
49         notifytable[#notifytable] = string.format("%s\n%s", notifytable[#notifytable]:sub(1, -2), string.rep(" ", stDay*4))
50         for x = 1,mthDays do
51             local strx = x ~= today and x or markup.bold(markup.color(cal.notification_preset.bg, cal.notification_preset.fg, x) .. " ")
52             strx = string.format("%s%s", string.rep(" ", 3 - tostring(x):len()), strx)
53             notifytable[#notifytable+1] = string.format("%-4s%s", strx, (x+stDay)%7==0 and x ~= mthDays and "\n" or "")
54         end
55
56         cal.notification_preset.text = tconcat(notifytable)
57         cal.hide()
58         cal.notification = naughty.notify {
59             preset  = cal.notification_preset,
60             icon    = cal.icon,
61             timeout = timeout or cal.notification_preset.timeout or 5
62         }
63         cal.month, cal.year = d.month, d.year
64     end
65
66     function cal.hover_on() cal.show(0) end
67     function cal.hover_off() cal.hide() end
68     function cal.prev()
69         cal.month = cal.month - 1
70         if cal.month == 0 then
71             cal.month = 12
72             cal.year = cal.year - 1
73         end
74         cal.show(0, cal.month, cal.year)
75     end
76     function cal.next()
77         cal.month = cal.month + 1
78         if cal.month == 13 then
79             cal.month = 1
80             cal.year = cal.year + 1
81         end
82         cal.show(0, cal.month, cal.year)
83     end
84
85     function cal.attach(widget)
86         widget:connect_signal("mouse::enter", cal.hover_on)
87         widget:connect_signal("mouse::leave", cal.hover_off)
88         widget:buttons(awful.util.table.join(
89                     awful.button({}, 1, cal.prev),
90                     awful.button({}, 3, cal.next),
91                     awful.button({}, 2, cal.hover_on),
92                     awful.button({}, 5, cal.prev),
93                     awful.button({}, 4, cal.next)))
94     end
95
96     for _, widget in ipairs(cal.attach_to) do cal.attach(widget) end
97
98     return cal
99 end
100
101 return factory