-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlunaco.lua
More file actions
339 lines (294 loc) · 10.1 KB
/
Copy pathlunaco.lua
File metadata and controls
339 lines (294 loc) · 10.1 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
-- Help
local HELP_TEXT = [[
usage: lunaco.lua [-h]
Lua script for a small conky calendar
options:
-h, --help show this help message and exit
--align-body=<value> set alignment of whole calendar
(default: c; options: c, l, r)
--align-header=<value> set alignment of calendar header
(default: c; options: c, l, r)
note: this is the only option
still active in combination with
the --no-format flag
--color=<color> set main text color (hex, omit #)
--font-family=<font> set font family (default:
Monospace)
--header-color=<color> color for header text (hex, omit
#)
--hide-dow hide days of week in the header
--hide-month hide the month in the header
--hide-year hide the year in the header
--no-format do not include any formatting
options
--size=<num> set font size (default: 11)
--today-bold set today's date in bold text
--today-color=<color> color for today's date (hex, omit
#)
--weekend-color=<color> color for weekend days (hex, omit
#)
]]
-- Options
local OPTIONS = {
["--align-body"] = "c",
["--align-header"] = "c",
["--color"] = "",
["--font-family"] = "Monospace",
["--header-color"] = "",
["--hide-dow"] = false,
["--hide-month"] = false,
["--hide-year"] = false,
["--no-format"] = false,
["--size"] = 11,
["--today-bold"] = false,
["--today-color"] = "",
["--weekend-color"] = ""
}
local function addOrderedTableEntry(tbl, key, value)
-- Add entry to a table while preserving
-- the order. The table will then be printed
-- in the expected sequence.
table.insert(tbl, key)
local index = tbl[#tbl]
tbl[index] = value
end
local function generateFontTag()
-- generate font tag
if not OPTIONS['--no-format'] then
return "${font " .. OPTIONS['--font-family']
.. ":size=" .. OPTIONS['--size'] .. "}"
end
return ""
end
local function generateColorTag(key)
-- generate color tag
local key = key or "--color"
local targetColor = OPTIONS[key]
if targetColor:len() > 0 and not OPTIONS['--no-format'] then
return "${color #" .. targetColor .. "}"
end
return ""
end
local function generateAlignTag()
if not OPTIONS['--no-format'] then
if OPTIONS['--align-body'] == "l" then
return ""
else
return "$align" .. OPTIONS['--align-body']
end
end
return ""
end
local function getYear()
-- Return full year as a number
return tonumber(os.date("%Y"))
end
local function getToday()
-- Return day as a number
return tonumber(os.date("%d"))
end
local function dayToString(dayNum)
local day = tostring(dayNum)
if day:len() == 1 then return "0" .. day end
return day
end
local function allMonths()
-- Return table of all months with
-- full names
local monthAlphaTbl = {}
addOrderedTableEntry(monthAlphaTbl, 1, "January")
addOrderedTableEntry(monthAlphaTbl, 2, "February")
addOrderedTableEntry(monthAlphaTbl, 3, "March")
addOrderedTableEntry(monthAlphaTbl, 4, "April")
addOrderedTableEntry(monthAlphaTbl, 5, "May")
addOrderedTableEntry(monthAlphaTbl, 6, "June")
addOrderedTableEntry(monthAlphaTbl, 7, "July")
addOrderedTableEntry(monthAlphaTbl, 8, "August")
addOrderedTableEntry(monthAlphaTbl, 9, "September")
addOrderedTableEntry(monthAlphaTbl,10, "October")
addOrderedTableEntry(monthAlphaTbl,11, "November")
addOrderedTableEntry(monthAlphaTbl,12, "December")
return monthAlphaTbl
end
local function getMonth(alpha)
-- false is default for alpha param
local alpha = alpha or false
if (not alpha) then
-- return number version if no alpha
return tonumber(os.date("%m"))
end
-- return full name of month
local months = allMonths()
return months[tonumber(os.date("%m"))]
end
local function getWeekday(target)
-- target defaults to false
local target = target or false
local weekDayTbl = {}
addOrderedTableEntry(weekDayTbl, 1, "Sunday")
addOrderedTableEntry(weekDayTbl, 2, "Monday")
addOrderedTableEntry(weekDayTbl, 3, "Tuesday")
addOrderedTableEntry(weekDayTbl, 4, "Wednesday")
addOrderedTableEntry(weekDayTbl, 5, "Thursday")
addOrderedTableEntry(weekDayTbl, 6, "Friday")
addOrderedTableEntry(weekDayTbl, 7, "Saturday")
-- if a target is specified, return it;
-- otherwise return the full table of days
if (not target) then return weekDayTbl end
return weekDayTbl[target]
end
local function weekdaysHeader()
-- format header of weekdays
local weekdays = getWeekday()
local weekdaysHeaderText = " "
for _,v in pairs(getWeekday()) do
weekdaysHeaderText = weekdaysHeaderText .. string.sub(v,1,2) .. " "
end
return weekdaysHeaderText
end
local function calendarHeader(year, month)
-- format full header for calendar
local weekdaysHeader = weekdaysHeader()
local maxHeaderLength = weekdaysHeader:len()
local len = tostring(month):len() + tostring(year):len() +1
local spacing = " "
if (not (len % 2 == 0)) then spacing = " " end
if OPTIONS['--hide-month'] or OPTIONS['--hide-year'] then spacing = "" end
local padding = ""
local paddingLength = (maxHeaderLength - len)/2
for v=1,paddingLength do
padding = padding .. " "
end
local colorTag = generateColorTag('--header-color')
if OPTIONS['--no-format'] then colorTag = "" end
local header = spacing
if not OPTIONS['--hide-year'] then header = year .. header end
if not OPTIONS['--hide-month'] then header = header .. month end
local align = generateAlignTag()
if (header:len() > maxHeaderLength) then
print(align .. colorTag .. header)
else
if OPTIONS['--align-header'] == "r" then
padding = ""
for v=1,(maxHeaderLength - len - spacing:len()) do
padding = padding .. " "
end
print(align .. colorTag .. padding .. header .. " ")
elseif OPTIONS['--align-header'] == "l" then
padding = ""
for v=1,(maxHeaderLength - len - spacing:len()) do
padding = padding .. " "
end
print(align .. colorTag .. " " .. header .. padding)
else
print(align .. colorTag .. padding .. header .. padding)
end
end
if not OPTIONS["--hide-dow"] then
print(align .. colorTag .. weekdaysHeader .. generateColorTag())
end
end
local function calcDaysInMonth(year, month)
-- given a month and year, calculate how
-- many days are in the month
local days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
-- calculate for leapyear
if (month == 2) then
if (year % 4 == 0) then
if (year % 100 == 0 and year % 400 == 0) then return 29 end
return 29
end
end
return days[month]
end
local function printMonthCalendar(year, monthAlpha, monthNumber)
-- given a month and year, print calendar
-- for the month
calendarHeader(year, monthAlpha)
local numDays = calcDaysInMonth(year, monthNumber)
local lineLength = weekdaysHeader():len()
local align = generateAlignTag()
local today = getToday()
-- day of week to start on
local startDoW = os.date('*t', os.time{year=year, month=monthNumber, day=1})['wday']
local currentLine = ""
local currentLineColorTagCount = 0
for d=1,numDays do
local currentWday = os.date('*t', os.time{year=year, month=monthNumber, day=d})['wday']
if d == 1 then
local initPadding = ""
for i=1,((startDoW-1)*3) do
initPadding = initPadding .. " "
end
currentLine = initPadding
end
local customTags = ""
local resetTags = ""
if (currentWday == 1 or currentWday == 7) and not OPTIONS['--no-format']
and OPTIONS['--weekend-color']:len() > 0 then
customTags = "${color #" .. OPTIONS['--weekend-color'] .. "}"
currentLineColorTagCount = currentLineColorTagCount + 1
end
if d == today and not OPTIONS['--no-format'] then
local boldStatus = ""
if OPTIONS['--today-bold'] then boldStatus = ":bold" end
customTags = "${font " .. OPTIONS['--font-family'] .. boldStatus .. ":size=" .. OPTIONS['--size'] .. "}"
if OPTIONS['--today-color']:len() > 0 then
customTags = customTags .. "${color #" .. OPTIONS['--today-color'] .. "}"
currentLineColorTagCount = currentLineColorTagCount + 1
end
end
if customTags:len() > 0 then
resetTags = "" .. generateFontTag()
if string.match(customTags, "${color") then
resetTags = resetTags .. generateColorTag()
currentLineColorTagCount = currentLineColorTagCount + 1
end
end
currentLine = currentLine .. " " .. customTags .. dayToString(d)
if currentWday < 7 then currentLine = currentLine .. resetTags end
if currentWday == 7 then
local line = align .. currentLine .. generateFontTag() .. " "
print(line)
currentLine = ""
currentLineColorTagCount = 0
end
if d == numDays then
local padding = " "
for v=1,(7-currentWday)*3 do
padding = padding .. " "
end
print(align .. currentLine .. padding .. "${color}")
end
end
end
local function isArgUsed(argPassed, shortArg, fullArg)
-- check if an arg has been used
if (argPassed == shortArg or argPassed == fullArg) then
return true
end
return false
end
local function getArgValue(argPassed, argLabel)
if type(OPTIONS[argLabel]) == "boolean" then return true end
return string.sub(argPassed, argLabel:len()+2, argPassed:len())
end
local YEAR = getYear()
local MONTH_ALPHA = getMonth(true)
local MONTH_NUM = getMonth()
if isArgUsed(arg[1], "-h", "--help") then
print(HELP_TEXT)
elseif arg[1] then
for _,currentArg in pairs(arg) do
for i,_ in pairs(OPTIONS) do
if string.sub(currentArg, 1, tostring(i):len()) == tostring(i) then
OPTIONS[i] = getArgValue(currentArg, i)
end
end
end
--for i,v in pairs(OPTIONS) do print(i, v) end
if not OPTIONS['--no-format'] then print(generateFontTag(), generateColorTag()) end
printMonthCalendar(YEAR, MONTH_ALPHA, MONTH_NUM)
else
print("No args passed. Use -h flag for help.")
end