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

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