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

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