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

e4216e4056953d7620f4a73944478bb2d2f18a74
[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 async        = require("lain.helpers").async
10 local icons_dir    = require("lain.helpers").icons_dir
11 local markup       = require("lain.util.markup")
12 local awful        = require("awful")
13 local naughty      = require("naughty")
14 local os           = { date   = os.date }
15 local string       = { format = string.format,
16                        gsub   = string.gsub }
17 local tonumber     = tonumber
18
19 -- Calendar notification
20 -- lain.widgets.calendar
21 local calendar = { offset = 0 }
22
23 function calendar.hide()
24     if not calendar.notification then return end
25     naughty.destroy(calendar.notification)
26     calendar.notification = nil
27 end
28
29 function calendar.show(t_out, inc_offset, scr)
30     calendar.hide()
31
32     local today = os.date("%d")
33     local offs = inc_offset or 0
34     local f
35
36     calendar.offset = calendar.offset + offs
37
38     local current_month = (offs == 0 or calendar.offset == 0)
39
40     if current_month then -- today highlighted
41         calendar.offset = 0
42         calendar.notify_icon = string.format("%s%s.png", calendar.icons, today)
43         f = calendar.cal
44     else -- no current month showing, no day to highlight
45        local month = tonumber(os.date("%m"))
46        local year  = tonumber(os.date("%Y"))
47
48        month = month + calendar.offset
49
50        while month > 12 do
51            month = month - 12
52            year = year + 1
53        end
54
55        while month < 1 do
56            month = month + 12
57            year = year - 1
58        end
59
60        calendar.notify_icon = nil
61        f = string.format("%s %s %s", calendar.cal, month, year)
62     end
63
64     if calendar.followtag then
65         calendar.notification_preset.screen = awful.screen.focused()
66     else
67         calendar.notification_preset.screen = src or 1
68     end
69
70     async(string.format("%s -c '%s'", awful.util.shell, f), function(ws)
71         fg, bg = calendar.notification_preset.fg, calendar.notification_preset.bg
72         ws = ws:gsub("%c%[7m%d+%c%[27m", markup.bold(markup.color(bg, fg, today)))
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, args)
83     local args                   = args or {}
84     calendar.cal                 = args.cal or "/usr/bin/cal --color=always"
85     calendar.followtag           = args.followtag or false
86     calendar.icons               = args.icons or icons_dir .. "cal/white/"
87     calendar.notification_preset = args.notification_preset
88
89     if not calendar.notification_preset then
90         calendar.notification_preset      = naughty.config.defaults
91         calendar.notification_preset.font = "Monospace 10"
92         calendar.notification_preset.fg   = "#FFFFFF"
93         calendar.notification_preset.bg   = "#000000"
94     end
95
96     if widget then
97         widget:connect_signal("mouse::enter", function () calendar.show() end)
98         widget:connect_signal("mouse::leave", function () calendar.hide() end)
99         widget:buttons(awful.util.table.join(awful.button({ }, 1, function ()
100                                                  calendar.show(0, -1, calendar.scr_pos) end),
101                                              awful.button({ }, 3, function ()
102                                                  calendar.show(0, 1, calendar.scr_pos) end),
103                                              awful.button({ }, 4, function ()
104                                                  calendar.show(0, -1, calendar.scr_pos) end),
105                                              awful.button({ }, 5, function ()
106                                                  calendar.show(0, 1, calendar.scr_pos) end)))
107     end
108 end
109
110 return calendar