-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.lua
174 lines (149 loc) · 5.88 KB
/
calendar.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
-- original code made by Bzed and published on http://awesome.naquadah.org/wiki/Calendar_widget
-- modified by Marc Dequènes (Duck) <Duck@DuckCorp.org> (2009-12-29), under the same licence,
-- and with the following changes:
-- + transformed to module
-- + the current day formating is customizable
-- captures
local os = os
local capi = {
mouse = mouse,
screen = screen,
}
local awful = require("awful")
local naughty = require("naughty")
local version_major, version_minor = awesome.version:match("(%d+)%.(%d+)")
version_major = tonumber(version_major)
version_minor = tonumber(version_minor)
local can_update_size = version_major and version_minor and version_major >= 4 and version_minor >= 2
------------------------------------------
-- utility functions
------------------------------------------
local function format_date(format, date)
return os.date(format, os.time(date))
end
------------------------------------------
-- calendar popup widget
------------------------------------------
local calendar = {}
function calendar:new(args)
return setmetatable({}, {__index = self}):init(args)
end
function calendar:init(args)
self.num_lines = 0
self.today_color = args.today_color or "#00ff00"
-- first day of week: monday=1, …, sunday=7
self.fdow = args.fdow or 1
-- notification area:
self.html = args.html or '<span font_desc="monospace">\n%s</span>'
-- highlight current date:
self.today = args.today or '<b><span color="' .. self.today_color .. '">%2i</span></b>'
self.anyday = args.anyday or '%2i'
self.page_title = args.page_title or '%B %Y' -- month year
self.col_title = args.col_title or '%a ' -- weekday
-- Date equality check is based on day_id. We deliberately ignore the year
-- to highlight the same day in different years:
self.day_id = args.day_id or '%m-%d'
self.empty_sep = args.empty_sep or " -"
self.week_col = args.week_col or " %V"
self.days_style = args.days_style or {}
self.position = args.position or naughty.config.defaults.position
return self
end
function calendar:day_style(day_of_week)
return self.days_style[day_of_week] or '%s'
end
function calendar:page(month, year)
local today = format_date(self.day_id)
-- 2001 started with a monday:
local d0 = format_date("*t", {year=2001, month=1, day=self.fdow })
local dA = format_date("*t", {year=year, month=month, day=1 })
local dB = format_date("*t", {year=year, month=month+1, day=0 })
local tA = {year=year, month=month, day=1 }
local colA = (dA.wday - d0.wday) % 7
local page_title = format_date(self.page_title, tA)
-- print column titles (weekday)
local page = " "
for d = 0, 6 do
page = page .. self:day_style(d+1):format(format_date(self.col_title, {
year = d0.year,
month = d0.month,
day = d0.day + d,
}))
end
-- print empty space before first day
page = page .. "\n" .. format_date(self.week_col, tA)
for column = 1, colA do
page = page .. self.empty_sep
end
-- iterate all days of the month
local nLines = 1
local column = colA
for day = 1, dB.day do
if column == 7 then
column = 0
nLines = nLines + 1
page = page .. "\n" .. format_date(self.week_col, {year=year, month=month, day=day})
end
if today == format_date(self.day_id, {day=day, month=month, year=year}) then
page = page .. " " .. self.today:format(day)
else
page = page .. " " .. self:day_style(column+1):format(self.anyday:format(day))
end
column = column + 1
end
for column = column, 6 do
page = page .. self.empty_sep
end
return page_title, self.html:format(page)
end
function calendar:switch(months)
self:show(self.year, self.month+months)
end
function calendar:show(year, month)
local today = os.time()
self.month = month or os.date('%m', today)
self.year = year or os.date('%Y', today)
local title, text = self:page(self.month, self.year)
-- NOTE: `naughty.replace_text` does not update bounds and can therefore
-- not be used when the size increases (before #1756 was merged):
local num_lines = select(2, text:gsub('\n', ''))
local will_fit = can_update_size or num_lines <= self.num_lines
if naughty.replace_text and self.notification and will_fit then
naughty.replace_text(self.notification, title, text)
else
self:hide()
self.notification = naughty.notify({
title = title,
text = text,
timeout = 0,
hover_timeout = 0.5,
screen = capi.mouse.screen,
position = self.position,
})
self.num_lines = num_lines
end
end
function calendar:hide()
if self.notification then
naughty.destroy(self.notification)
self.notification = nil
self.num_lines = 0
end
end
function calendar:attach(widget)
widget:connect_signal('mouse::enter', function() self:show() end)
widget:connect_signal('mouse::leave', function() self:hide() end)
widget:buttons(awful.util.table.join(
awful.button({ }, 1, function() self:switch( -1) end),
awful.button({ }, 3, function() self:switch( 1) end),
awful.button({ }, 4, function() self:switch( -1) end),
awful.button({ }, 5, function() self:switch( 1) end),
awful.button({ 'Shift' }, 1, function() self:switch(-12) end),
awful.button({ 'Shift' }, 3, function() self:switch( 12) end),
awful.button({ 'Shift' }, 4, function() self:switch(-12) end),
awful.button({ 'Shift' }, 5, function() self:switch( 12) end)
))
end
return setmetatable(calendar, {
__call = calendar.new,
})