From: Samir Benmendil Date: Wed, 10 Dec 2014 00:09:28 +0000 (+0000) Subject: awesome: calendar widget X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/d396af0a5a6c17c81e4c4d248b90eefc0cb55f2a awesome: calendar widget that silly lain widget just didn't work way I wanted it to. Importing and modifying it to my needs --- diff --git a/awesome/rc.lua b/awesome/rc.lua index b91d41c..1bf335e 100644 --- a/awesome/rc.lua +++ b/awesome/rc.lua @@ -1,4 +1,4 @@ --- Standard awesome library {{{1 +-- Awesome libraries {{{1 gears = require("gears") awful = require("awful") awful.rules = require("awful.rules") diff --git a/awesome/topbar.lua b/awesome/topbar.lua index b945d80..638e967 100644 --- a/awesome/topbar.lua +++ b/awesome/topbar.lua @@ -5,7 +5,6 @@ local utils = require("utils") local wibox = require("wibox") -- Create a textclock widget -mytextclock = awful.widget.textclock(" %a %b %d, %k:%M ", 10) separator = wibox.widget.textbox() separator:set_markup('│') @@ -97,7 +96,7 @@ for s = 1, screen.count() do right_layout:add(require("widgets/battery_widget")) end right_layout:add(separator) - right_layout:add(mytextclock) + right_layout:add(require("widgets/calendar")) right_layout:add(mylayoutbox[s]) -- Now bring it all together (with the tasklist in the middle) diff --git a/awesome/widgets/calendar.lua b/awesome/widgets/calendar.lua new file mode 100644 index 0000000..cdf58ae --- /dev/null +++ b/awesome/widgets/calendar.lua @@ -0,0 +1,97 @@ +--[[ + + 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