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

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