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

f351356f7527fee968d41669a7e7f41077514860
[etc/awesome.git] / widgets / 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 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.widgets.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 today = os.date("%e")
32     local offs = inc_offset or 0
33     local f
34
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.notify_icon = string.format("%s%s.png", calendar.icons, tonumber(today))
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.notify_icon = nil
60        f = string.format("%s %s %s", calendar.cal, month, year)
61     end
62
63     if calendar.followtag then
64         calendar.notification_preset.screen = awful.screen.focused()
65     else
66         calendar.notification_preset.screen = src or 1
67     end
68
69     helpers.async(f, function(ws)
70         fg, bg = calendar.notification_preset.fg, calendar.notification_preset.bg
71         ws = ws:gsub("%c%[%d+[m]?%s?%d+%c%[%d+[m]?", markup.bold(markup.color(bg, fg, today)))
72         calendar.hide()
73         calendar.notification = naughty.notify({
74             preset      = calendar.notification_preset,
75             text        = ws:gsub("\n*$", ""),
76             icon        = calendar.notify_icon,
77             timeout     = t_out or calendar.notification_preset.timeout or 5
78         })
79     end)
80 end
81
82 function calendar.attach(widget)
83     widget:connect_signal("mouse::enter", function () calendar.show(0) end)
84     widget:connect_signal("mouse::leave", function () calendar.hide() end)
85     widget:buttons(awful.util.table.join(awful.button({ }, 1, function ()
86                                              calendar.show(0, -1, calendar.scr_pos) end),
87                                          awful.button({ }, 3, function ()
88                                              calendar.show(0, 1, calendar.scr_pos) end),
89                                          awful.button({ }, 4, function ()
90                                              calendar.show(0, -1, calendar.scr_pos) end),
91                                          awful.button({ }, 5, function ()
92                                              calendar.show(0, 1, calendar.scr_pos) end)))
93 end
94
95 local function worker(args)
96     local args                   = args or {}
97     calendar.cal                 = args.cal or "/usr/bin/cal"
98     calendar.attach_to           = args.attach_to or {}
99     calendar.followtag           = args.followtag or false
100     calendar.icons               = args.icons or helpers.icons_dir .. "cal/white/"
101     calendar.notification_preset = args.notification_preset
102
103     if not calendar.notification_preset then
104         calendar.notification_preset = {
105             font = "Monospace 10",
106             fg   = "#FFFFFF",
107             bg   = "#000000"
108         }
109     end
110
111     for i, widget in ipairs(calendar.attach_to) do calendar.attach(widget) end
112 end
113
114 return setmetatable(calendar, { __call = function(_, ...) return worker(...) end })