Advertisement
helenaryuu

calendar_awesome

Dec 12th, 2012
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. -- original code made by Bzed and published on http://awesome.naquadah.org/wiki/Calendar_widget
  2. -- modified by Marc Dequènes (Duck) <Duck@DuckCorp.org> (2009-12-29), under the same licence,
  3. -- and with the following changes:
  4. --   + transformed to module
  5. --   + the current day formating is customizable
  6.  
  7. local string = string
  8. --local print = print
  9. local tostring = tostring
  10. local os = os
  11. local capi = {
  12.     mouse = mouse,
  13.     screen = screen
  14. }
  15. local awful = require("awful")
  16. local naughty = require("naughty")
  17. module("calendar2")
  18.  
  19. local calendar = {}
  20. local current_day_format = "<u>%s</u>"
  21.  
  22. function displayMonth(month,year,weekStart)
  23.         local t,wkSt=os.time{year=year, month=month+1, day=0},weekStart or 1
  24.         local d=os.date("*t",t)
  25.         local mthDays,stDay=d.day,(d.wday-d.day-wkSt+1)%7
  26.  
  27.         --print(mthDays .."\n" .. stDay)
  28.         local lines = "    "
  29.  
  30.         for x=0,6 do
  31.                 lines = lines .. os.date("%a ",os.time{year=2006,month=1,day=x+wkSt})
  32.         end
  33.  
  34.         lines = lines .. "\n" .. os.date(" %V",os.time{year=year,month=month,day=1})
  35.  
  36.         local writeLine = 1
  37.         while writeLine < (stDay + 1) do
  38.                 lines = lines .. "    "
  39.                 writeLine = writeLine + 1
  40.         end
  41.  
  42.         for d=1,mthDays do
  43.                 local x = d
  44.                 local t = os.time{year=year,month=month,day=d}
  45.                 if writeLine == 8 then
  46.                         writeLine = 1
  47.                         lines = lines .. "\n" .. os.date(" %V",t)
  48.                 end
  49.                 if os.date("%Y-%m-%d") == os.date("%Y-%m-%d", t) then
  50.                         x = string.format(current_day_format, d)
  51.                 end
  52.                 if (#(tostring(d)) == 1) then
  53.                         x = " " .. x
  54.                 end
  55.                 lines = lines .. "  " .. x
  56.                 writeLine = writeLine + 1
  57.         end
  58.         local header = os.date("%B %Y\n",os.time{year=year,month=month,day=1})
  59.  
  60.         return header .. "\n" .. lines
  61. end
  62.  
  63. function switchNaughtyMonth(switchMonths)
  64.         if (#calendar < 3) then return end
  65.         local swMonths = switchMonths or 1
  66.         calendar[1] = calendar[1] + swMonths
  67.         calendar[3].box.widgets[2].text = string.format('<span font_desc="%s">%s</span>', "monaco", displayMonth(calendar[1], calendar[2], 2))
  68. end
  69.  
  70. function addCalendarToWidget(mywidget, custom_current_day_format)
  71.   if custom_current_day_format then current_day_format = custom_current_day_format end
  72.  
  73.   mywidget:add_signal('mouse::enter', function ()
  74.         local month, year = os.date('%m'), os.date('%Y')
  75.         calendar = { month, year,
  76.         naughty.notify({
  77.                 text = string.format('<span font_desc="%s">%s</span>', "monospace", displayMonth(month, year, 2)),
  78.                 timeout = 0,
  79.                 hover_timeout = 0.5,
  80.                 screen = capi.mouse.screen
  81.         })
  82.   }
  83.   end)
  84.   mywidget:add_signal('mouse::leave', function () naughty.destroy(calendar[3]) end)
  85.  
  86.   mywidget:buttons(awful.util.table.join(
  87.     awful.button({ }, 1, function()
  88.         switchNaughtyMonth(-1)
  89.     end),
  90.     awful.button({ }, 3, function()
  91.         switchNaughtyMonth(1)
  92.     end),
  93.     awful.button({ }, 4, function()
  94.         switchNaughtyMonth(-1)
  95.     end),
  96.     awful.button({ }, 5, function()
  97.         switchNaughtyMonth(1)
  98.     end),
  99.     awful.button({ 'Shift' }, 1, function()
  100.         switchNaughtyMonth(-12)
  101.     end),
  102.     awful.button({ 'Shift' }, 3, function()
  103.         switchNaughtyMonth(12)
  104.     end),
  105.     awful.button({ 'Shift' }, 4, function()
  106.         switchNaughtyMonth(-12)
  107.     end),
  108.     awful.button({ 'Shift' }, 5, function()
  109.         switchNaughtyMonth(12)
  110.     end)
  111.   ))
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement