Advertisement
constantin-net

awesome/calendar2.lua

Jun 25th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 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.             os.setlocale("ru_RU.utf8")
  30.             for x=0,6 do
  31.                     lines = lines .. os.date("%a   ",os.time{year=2006,month=1,day=x+wkSt})
  32.             end
  33.            lines = lines .. "\n"-- .. os.date(" %V",os.time{year=year,month=month,day=1}) --Убираем номер недели
  34.      
  35.             local writeLine = 1
  36.             while writeLine < (stDay + 1) do
  37.                     lines = lines .. "     " --интервал перед первым числом, если оно не в начале недели
  38.                     writeLine = writeLine + 1
  39.             end
  40.      
  41.             for d=1,mthDays do
  42.                     local x = d
  43.                     local t = os.time{year=year,month=month,day=d}
  44.                     if writeLine == 8 then
  45.                             writeLine = 1
  46.                             lines = lines .. "\n"-- .. os.date(" %V",t) --убираем номер недели
  47.                     end
  48.                     if os.date("%Y-%m-%d") == os.date("%Y-%m-%d", t) then
  49.                             x = string.format(current_day_format, d)
  50.                     end
  51.                     if (#(tostring(d)) == 1) then
  52.                             x = " " .. x --выравнивание между одно и двухзначными цифрами
  53.                     end
  54.                     lines = lines .. "   " .. x --интервал между числами
  55.                     writeLine = writeLine + 1
  56.             end
  57.             local header = os.date("%B %Y\n",os.time{year=year,month=month,day=1})
  58.             header = "          " .. header --выравниваем месяц и год по центру
  59.             return header .. "\n" .. lines
  60.     end
  61.      
  62.     function switchNaughtyMonth(switchMonths)
  63.             if (#calendar < 3) then return end
  64.             local swMonths = switchMonths or 1
  65.             calendar[1] = calendar[1] + swMonths
  66.             calendar[3].box.widgets[2].text = string.format('<span font_desc="%s">%s</span>', "monospace", displayMonth(calendar[1], calendar[2], 2))
  67.     end
  68.      
  69.     function switchNaughtyGoToToday()
  70.             if (#calendar < 3) then return end
  71.             local swMonths = switchMonths or 1
  72.             calendar[1] = os.date("*t").month
  73.             calendar[2] = os.date("*t").year
  74.            switchNaughtyMonth(0)
  75.     end
  76.      
  77.     function addCalendarToWidget(mywidget, custom_current_day_format)
  78.       if custom_current_day_format then current_day_format = custom_current_day_format end
  79.      
  80.       mywidget:add_signal('mouse::enter', function ()
  81.             local month, year = os.date('%m'), os.date('%Y')
  82.             calendar = { month, year,
  83.             naughty.notify({
  84.                     text = string.format('<span font_desc="%s">%s</span>', "monospace", displayMonth(month, year, 2)),
  85.                     timeout = 0,
  86.                     hover_timeout = 0.5,
  87.                     screen = capi.mouse.screen
  88.             })
  89.       }
  90.       end)
  91.       mywidget:add_signal('mouse::leave', function () naughty.destroy(calendar[3]) end)
  92.      
  93.       mywidget:buttons(awful.util.table.join(
  94.         awful.button({ }, 1, function()
  95.             switchNaughtyMonth(-1)
  96.         end),
  97.         awful.button({ }, 2, switchNaughtyGoToToday),
  98.         awful.button({ }, 3, function()
  99.             switchNaughtyMonth(1)
  100.         end),
  101.         awful.button({ }, 4, function()
  102.             switchNaughtyMonth(-1)
  103.         end),
  104.         awful.button({ }, 5, function()
  105.             switchNaughtyMonth(1)
  106.         end),
  107.         awful.button({ 'Shift' }, 1, function()
  108.             switchNaughtyMonth(-12)
  109.         end),
  110.         awful.button({ 'Shift' }, 3, function()
  111.             switchNaughtyMonth(12)
  112.         end),
  113.         awful.button({ 'Shift' }, 4, function()
  114.             switchNaughtyMonth(-12)
  115.         end),
  116.         awful.button({ 'Shift' }, 5, function()
  117.             switchNaughtyMonth(12)
  118.         end)
  119.       ))
  120.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement