--[[
Licensed under GNU General Public License v2
* (c) 2014-, Samir Benmendil
* (c) -2013, Luke Bonham (lain)
--]]
local awful = require("awful")
local beautiful = require("beautiful")
local naughty = require("naughty")
local io = { popen = io.popen }
local os = { date = os.date }
local tonumber = tonumber
local calendar = {}
calendar.cal = "/usr/bin/cal --color=always"
calendar.font = "monospace 10"
calendar.fg = beautiful.fg_normal or "#FFFFFF"
calendar.bg = beautiful.bg_normal or "#FFFFFF"
calendar.position = "top_right"
calendar.offset = 0
local calwidget = awful.widget.textclock(" %a %b %d, %k:%M ", 10)
calwidget:connect_signal("mouse::enter", function () calendar:show(0) end)
calwidget:connect_signal("mouse::leave", function () calendar:hide() end)
calwidget:buttons(awful.util.table.join(
awful.button({ }, 1, function () calendar:show(-1) end),
awful.button({ }, 3, function () calendar:show( 1) end),
awful.button({ }, 4, function () calendar:show(-1) end),
awful.button({ }, 5, function () calendar:show( 1) end)))
-- Calendar notification
local cal_notification = nil
function calendar:hide()
if cal_notification ~= nil then
naughty.destroy(cal_notification)
cal_notification = nil
end
end
function calendar:show(inc_offset)
calendar:hide()
local offs = inc_offset or 0
calendar.offset = calendar.offset + offs
local today = tonumber(os.date('%d'))
local month = tonumber(os.date('%m'))
local year = tonumber(os.date('%Y'))
if offs ~= 0 or calendar.offset ~= 0
then -- no current month showing, no day to highlight
month = month + calendar.offset
if month > 12 then
month = month % 12
year = year + 1
if month <= 0 then
month = 12
end
elseif month < 1 then
month = month + 12
year = year - 1
if month <= 0 then
month = 1
end
end
end
local f = io.popen(calendar.cal .. ' ' .. month .. ' ' .. year)
local text = string.format("%s\n%s",
calendar.font,
f:read(),
f:read("*a"):gsub("%s\n","\n"):gsub("%s+$", ""))
f:close()
text = text:gsub("%[7m","")
text = text:gsub("%[27m", "")
cal_notification = naughty.notify({
text = text,
position = calendar.position,
fg = calendar.fg,
bg = calendar.bg,
screen = mouse.screen
})
end
return calwidget