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

8c707c8e1745e8e35a042c08232fc34ed9c1a10f
[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           = io
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)
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 = '/usr/bin/cal | sed -r -e "s/(^| )( '
41     -- let's take font only, font size is set in calendar table
42     local font = beautiful.font:sub(beautiful.font:find(""),
43                  beautiful.font:find(" "))
44
45     if offs == 0
46     then -- current month showing, today highlighted
47         if today >= 10
48         then
49            init_t = '/usr/bin/cal | sed -r -e "s/(^| )('
50         end
51
52         calendar.offset = 0
53         calendar.notify_icon = calendar.icons .. today .. ".png"
54
55         -- bg and fg inverted to highlight today
56         f = io.popen( init_t .. today ..
57                       ')($| )/\\1<b><span foreground=\\"'
58                       .. calendar.bg ..
59                       '\\" background=\\"'
60                       .. calendar.fg ..
61                       '\\">\\2<\\/span><\\/b>\\3/"' )
62
63     else -- no current month showing, no day to highlight
64        local month = tonumber(os.date('%m'))
65        local year = tonumber(os.date('%Y'))
66
67        calendar.offset = calendar.offset + offs
68        month = month + calendar.offset
69
70        if month > 12 then
71            month = month % 12
72            year = year + 1
73            if month <= 0 then
74                month = 12
75            end
76        elseif month < 1 then
77            month = month + 12
78            year = year - 1
79            if month <= 0 then
80                month = 1
81            end
82        end
83
84        calendar.notify_icon = nil
85
86        f = io.popen('/usr/bin/cal ' .. month .. ' ' .. year)
87     end
88
89     c_text = "<tt><span font='" .. font .. " "
90              .. calendar.font_size .. "'><b>"
91              .. f:read() .. "</b>\n\n"
92              .. f:read() .. "\n"
93              .. f:read("*all"):gsub("\n*$", "")
94              .. "</span></tt>"
95     f:close()
96
97     cal_notification = naughty.notify({ text = c_text,
98                                         icon = calendar.notify_icon,
99                                         position = calendar.position,
100                                         fg = calendar.fg,
101                                         bg = calendar.bg,
102                                         timeout = tims })
103 end
104
105 function calendar:attach(widget, args)
106     local args = args or {}
107     calendar.icons = args.icons or icons_dir .. "cal/white/"
108     calendar.font_size = tonumber(args.font_size) or 12
109     calendar.fg = args.fg or beautiful.fg_normal or "#FFFFFF"
110     calendar.bg = args.bg or beautiful.bg_normal or "#FFFFFF"
111     calendar.position = args.position or "top_right"
112
113     calendar.offset = 0
114     calendar.notify_icon = nil
115
116     widget:connect_signal("mouse::enter", function () calendar:show() end)
117     widget:connect_signal("mouse::leave", function () calendar:hide() end)
118     widget:buttons(awful.util.table.join( awful.button({ }, 1, function ()
119                                               calendar:show(0, -1) end),
120                                           awful.button({ }, 3, function ()
121                                               calendar:show(0, 1) end) ))
122 end
123
124 return setmetatable(calendar, { __call = function(_, ...) return create(...) end })