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

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