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

wiki updated
[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 icons_dir    = require("lain.helpers").icons_dir
10
11 local awful        = require("awful")
12 local beautiful    = require("beautiful")
13 local naughty      = require("naughty")
14
15 local io           = { popen = io.popen }
16 local os           = { date = os.date }
17 local string       = { format = string.format,
18                        sub    = string.sub,
19                        gsub   = string.gsub }
20 local tonumber     = tonumber
21
22 local setmetatable = setmetatable
23
24 -- Calendar notification
25 -- lain.widgets.calendar
26 local calendar = {}
27 local cal_notification = nil
28
29 function calendar.hide()
30     if cal_notification ~= nil then
31         naughty.destroy(cal_notification)
32         cal_notification = nil
33     end
34 end
35
36 function calendar.show(t_out, inc_offset, scr)
37     calendar.hide()
38
39     local f, c_text
40     local offs  = inc_offset or 0
41     local tims  = t_out or 0
42     local today = tonumber(os.date('%d'))
43
44     calendar.offset = calendar.offset + offs
45
46     if offs == 0 or calendar.offset == 0
47     then -- current month showing, today highlighted
48         calendar.offset = 0
49         calendar.notify_icon = string.format("%s%s.png", calendar.icons, today)
50
51         -- bg and fg inverted to highlight today
52         f = io.popen(calendar.cal_format(today))
53     else -- no current month showing, no day to highlight
54        local month = tonumber(os.date('%m'))
55        local year  = tonumber(os.date('%Y'))
56
57        month = month + calendar.offset
58
59        while month > 12 do
60            month = month - 12
61            year = year + 1
62        end
63
64        while month < 1 do
65            month = month + 12
66            year = year - 1
67        end
68
69        calendar.notify_icon = nil
70        f = io.popen(string.format('%s %s %s', calendar.cal, month, year))
71     end
72
73     c_text = "<tt><span font='" .. calendar.font .. " "
74              .. calendar.font_size .. "'><b>"
75              .. f:read() .. "</b>\n\n"
76              .. f:read() .. "\n"
77              .. f:read("*all"):gsub("\n*$", "")
78              .. "</span></tt>"
79     f:close()
80
81     if calendar.followtag then
82         scrp = awful.screen.focused()
83     else
84         scrp = scr or calendar.scr_pos
85     end
86
87     cal_notification = naughty.notify({
88         text = c_text,
89         icon = calendar.notify_icon,
90         position = calendar.position,
91         fg = calendar.fg,
92         bg = calendar.bg,
93         timeout = tims,
94         screen = scrp
95     })
96 end
97
98 function calendar.attach(widget, args)
99     local args = args or {}
100
101     calendar.cal         = args.cal or "/usr/bin/cal"
102     calendar.cal_format  = args.cal_format or function(today)
103         return string.format("%s | sed -r -e 's/_\\x08//g' -e '0,/(^| )%d($| )/ s/(^| )%d($| )/\\1<b><span foreground=\"%s\" background=\"%s\">%d<\\/span><\\/b>\\2/'",
104                              calendar.cal, today, today, calendar.bg, calendar.fg, today)
105     end
106     calendar.icons       = args.icons or icons_dir .. "cal/white/"
107     calendar.font        = args.font or beautiful.font:gsub(" %d.*", "")
108     calendar.font_size   = tonumber(args.font_size) or 11
109     calendar.fg          = args.fg or beautiful.fg_normal or "#FFFFFF"
110     calendar.bg          = args.bg or beautiful.bg_normal or "#000000"
111     calendar.position    = args.position or "top_right"
112     calendar.scr_pos     = args.scr_pos or 1
113     calendar.followtag   = args.followtag or false
114
115     calendar.offset      = 0
116     calendar.notify_icon = nil
117
118     widget:connect_signal("mouse::enter", function () calendar.show(0, 0, calendar.scr_pos) end)
119     widget:connect_signal("mouse::leave", function () calendar.hide() end)
120     widget:buttons(awful.util.table.join(awful.button({ }, 1, function ()
121                                              calendar.show(0, -1, calendar.scr_pos) end),
122                                          awful.button({ }, 3, function ()
123                                              calendar.show(0, 1, calendar.scr_pos) end),
124                                          awful.button({ }, 4, function ()
125                                              calendar.show(0, -1, calendar.scr_pos) end),
126                                          awful.button({ }, 5, function ()
127                                              calendar.show(0, 1, calendar.scr_pos) end)))
128 end
129
130 return setmetatable(calendar, { __call = function(_, ...) return create(...) end })