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

#288 trying metatables
[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 tonumber     = tonumber
17
18 -- Calendar notification
19 -- lain.widgets.calendar
20 local calendar = { offset = 0 }
21
22 function calendar.hide()
23     naughty.destroy(naughty.getById(calendar.id))
24 end
25
26 function calendar.show(t_out, inc_offset, scr)
27     local today = os.date("%d")
28     local offs = inc_offset or 0
29     local f
30
31     calendar.offset = calendar.offset + offs
32
33     local current_month = (offs == 0 or calendar.offset == 0)
34
35     if current_month then -- today highlighted
36         calendar.offset = 0
37         calendar.notify_icon = string.format("%s%s.png", calendar.icons, today)
38         f = calendar.cal
39     else -- no current month showing, no day to highlight
40        local month = tonumber(os.date("%m"))
41        local year  = tonumber(os.date("%Y"))
42
43        month = month + 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.notify_icon = nil
56        f = string.format("%s %s %s", calendar.cal, month, year)
57     end
58
59     if calendar.followtag then
60         calendar.notification_preset.screen = awful.screen.focused()
61     else
62         calendar.notification_preset.screen = src or 1
63     end
64
65     helpers.async(f, function(ws)
66         fg, bg = calendar.notification_preset.fg, calendar.notification_preset.bg
67         ws = ws:gsub("%c%[%d+[m]?%d+%c%[%d+[m]?", markup.bold(markup.color(bg, fg, today)))
68         calendar.id = naughty.notify({
69             replaces_id = calendar.id,
70             preset      = calendar.notification_preset,
71             text        = ws:gsub("\n*$", ""),
72             icon        = calendar.notify_icon,
73             timeout     = t_out or calendar.notification.preset.timeout or 5
74         }).id
75     end)
76 end
77
78 function calendar.attach(widget, args)
79     local args                   = args or {}
80     calendar.cal                 = args.cal or "/usr/bin/cal --color=always"
81     calendar.followtag           = args.followtag or false
82     calendar.icons               = args.icons or helpers.icons_dir .. "cal/white/"
83     calendar.notification_preset = args.notification_preset
84
85     if not calendar.notification_preset then
86         calendar.notification_preset = {
87             font = "Monospace 10",
88             fg   = "#FFFFFF",
89             bg   = "#000000"
90         }
91     end
92
93     if widget then
94         widget:connect_signal("mouse::enter", function () calendar.show(0) end)
95         widget:connect_signal("mouse::leave", function () calendar.hide() end)
96         widget:buttons(awful.util.table.join(awful.button({ }, 1, function ()
97                                                  calendar.show(0, -1, calendar.scr_pos) end),
98                                              awful.button({ }, 3, function ()
99                                                  calendar.show(0, 1, calendar.scr_pos) end),
100                                              awful.button({ }, 4, function ()
101                                                  calendar.show(0, -1, calendar.scr_pos) end),
102                                              awful.button({ }, 5, function ()
103                                                  calendar.show(0, 1, calendar.scr_pos) end)))
104     end
105 end
106
107 return calendar