Use single quotes
[delightful.git] / calendar2.lua
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         if calendar[3].box.visible then
68             naughty.destroy(calendar[3])
69         end
70         calendar[3] = getWidget(calendar[1], calendar[2])
71 end
72
73 function getWidget(month, year)
74         return naughty.notify({
75                 text = string.format('<span font_desc="%s">%s</span>', "monospace", displayMonth(month, year, 2)),
76                 timeout = 0,
77                 hover_timeout = 0.5,
78                 screen = capi.mouse.screen
79         })
80 end
81
82 function addCalendarToWidget(mywidget, custom_current_day_format)
83   if custom_current_day_format then current_day_format = custom_current_day_format end
84
85   mywidget:connect_signal('mouse::enter', function ()
86         local month, year = os.date('%m'), os.date('%Y')
87         calendar = { month, year, getWidget(month, year) }
88   end)
89   mywidget:connect_signal('mouse::leave', function () naughty.destroy(calendar[3]) end)
90
91   mywidget:buttons(awful.util.table.join(
92     awful.button({ }, 1, function()
93         switchNaughtyMonth(-1)
94     end),
95     awful.button({ }, 3, function()
96         switchNaughtyMonth(1)
97     end),
98     awful.button({ }, 4, function()
99         switchNaughtyMonth(-1)
100     end),
101     awful.button({ }, 5, function()
102         switchNaughtyMonth(1)
103     end),
104     awful.button({ 'Shift' }, 1, function()
105         switchNaughtyMonth(-12)
106     end),
107     awful.button({ 'Shift' }, 3, function()
108         switchNaughtyMonth(12)
109     end),
110     awful.button({ 'Shift' }, 4, function()
111         switchNaughtyMonth(-12)
112     end),
113     awful.button({ 'Shift' }, 5, function()
114         switchNaughtyMonth(12)
115     end)
116   ))
117 end