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

4a3d207ab14a6680d251ddc91740a09f56da4b8a
[etc/awesome.git] / widget / 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.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.offset = calendar.offset + offs
34
35     local current_month = (offs == 0 or calendar.offset == 0)
36
37     if current_month then -- today highlighted
38         calendar.offset = 0
39         calendar.icon = string.format("%s%s.png", calendar.icons, tonumber(os.date("%d")))
40         f = calendar.cal
41     else -- no current month showing, no day to highlight
42        local month = tonumber(os.date("%m"))
43        local year  = tonumber(os.date("%Y"))
44
45        month = month + calendar.offset
46
47        while month > 12 do
48            month = month - 12
49            year = year + 1
50        end
51
52        while month < 1 do
53            month = month + 12
54            year = year - 1
55        end
56
57        calendar.icon = nil
58        f = string.format("%s %s %s", calendar.cal, month, year)
59     end
60
61     if calendar.followtag then
62         calendar.notification_preset.screen = awful.screen.focused()
63     else
64         calendar.notification_preset.screen = src or 1
65     end
66
67     if f == calendar then
68         calendar.update(f, false)
69         calendar.hide()
70         calendar.notification = naughty.notify({
71             preset      = calendar.notification_preset,
72             icon        = calendar.icon,
73             timeout     = t_out or calendar.notification_preset.timeout or 5
74         })
75     else
76         calendar.update(f, true, t_out)
77     end
78 end
79
80 function calendar.update(f, show, t_out)
81     local fg, bg = calendar.notification_preset.fg, calendar.notification_preset.bg
82
83     helpers.async(f, function(ws)
84         ws = ws:gsub("%c%[%d+[m]?%s?%d+%c%[%d+[m]?",
85              markup.bold(markup.color(bg, fg, os.date("%e")))):gsub("\n*$", "")
86
87         if f == calendar.cal then
88             calendar.notification_preset.text = ws
89         end
90
91         if show then
92             calendar.hide()
93             calendar.notification = naughty.notify({
94                 preset      = calendar.notification_preset,
95                 text        = ws,
96                 icon        = calendar.icon,
97                 timeout     = t_out or calendar.notification_preset.timeout or 5
98             })
99         end
100     end)
101 end
102
103 function calendar.attach(widget)
104     widget:connect_signal("mouse::enter", function () calendar.show(0) end)
105     widget:connect_signal("mouse::leave", function () calendar.hide() end)
106     widget:buttons(awful.util.table.join(awful.button({ }, 1, function ()
107                                              calendar.show(0, -1, calendar.scr_pos) end),
108                                          awful.button({ }, 3, function ()
109                                              calendar.show(0, 1, calendar.scr_pos) end),
110                                          awful.button({ }, 4, function ()
111                                              calendar.show(0, -1, calendar.scr_pos) end),
112                                          awful.button({ }, 5, function ()
113                                              calendar.show(0, 1, calendar.scr_pos) end)))
114 end
115
116 local function worker(args)
117     local args                   = args or {}
118     calendar.cal                 = args.cal or "/usr/bin/cal"
119     calendar.attach_to           = args.attach_to or {}
120     calendar.followtag           = args.followtag or false
121     calendar.icons               = args.icons or helpers.icons_dir .. "cal/white/"
122     calendar.notification_preset = args.notification_preset
123
124     if not calendar.notification_preset then
125         calendar.notification_preset = {
126             font = "Monospace 10",
127             fg   = "#FFFFFF",
128             bg   = "#000000"
129         }
130     end
131
132     for i, widget in ipairs(calendar.attach_to) do calendar.attach(widget) end
133
134     calendar.update(calendar.cal, false)
135 end
136
137 return setmetatable(calendar, { __call = function(_, ...) return worker(...) end })