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

38b4d8b3d46e2a0ae91b29d32131c1154432f4fa
[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 notification = nil
25
26 local function create(background, foreground)
27     calendar.offset = 0
28     calendar.icons_dir = icons_dir .. "cal/white/" -- default
29     calendar.notify_icon = nil
30     calendar.font_size = 12
31     calendar.bg = background or beautiful.bg_normal or "#FFFFFF"
32     calendar.fg = foreground or beautiful.fg_focus or "#FFFFFF"
33 end
34
35 function calendar:hide()
36     if notification ~= nil then
37         naughty.destroy(notification)
38         notification = nil
39     end
40 end
41
42 function calendar:show(t_out, inc_offset)
43     calendar:hide()
44
45     local offs = inc_offset or 0
46     local tims = t_out or 0
47     local f, c_text
48     local today = tonumber(os.date('%d'))
49     local init_t = '/usr/bin/cal | sed -r -e "s/(^| )( '
50     -- let's take font only, font size is set in calendar table
51     local font = beautiful.font:sub(beautiful.font:find(""),
52                  beautiful.font:find(" "))
53
54     if offs == 0
55     then -- current month showing, today highlighted
56         if today >= 10
57         then
58            init_t = '/usr/bin/cal | sed -r -e "s/(^| )('
59         end
60
61         calendar.offset = 0
62         calendar.notify_icon = calendar.icons_dir .. today .. ".png"
63
64         -- bg and fg inverted to highlight today
65         f = io.popen( init_t .. today ..
66                       ')($| )/\\1<b><span foreground=\\"'
67                       .. calendar.bg ..
68                       '\\" background=\\"'
69                       .. calendar.fg ..
70                       '\\">\\2<\\/span><\\/b>\\3/"' )
71
72     else -- no current month showing, no day to highlight
73        local month = tonumber(os.date('%m'))
74        local year = tonumber(os.date('%Y'))
75
76        calendar.offset = calendar.offset + offs
77        month = month + calendar.offset
78
79        if month > 12 then
80            month = month % 12
81            year = year + 1
82            if month <= 0 then
83                month = 12
84            end
85        elseif month < 1 then
86            month = month + 12
87            year = year - 1
88            if month <= 0 then
89                month = 1
90            end
91        end
92
93        calendar.notify_icon = nil
94
95        f = io.popen('/usr/bin/cal ' .. month .. ' ' .. year)
96     end
97
98
99     c_text = "<tt><span font='" .. font .. " "
100              .. calendar.font_size .. "'><b>"
101              .. f:read() .. "</b>\n\n"
102              .. f:read() .. "\n"
103              .. f:read("*all"):gsub("\n*$", "")
104              .. "</span></tt>"
105     f:close()
106
107     notification = naughty.notify({ text = c_text,
108                                     icon = calendar.notify_icon,
109                                     fg = calendar.fg,
110                                     bg = calendar.bg,
111                                     timeout = tims })
112 end
113
114 function calendar:attach(widget, background, foreground)
115     create(background, foreground)
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 })