Skip to content

Commit 1291b1c

Browse files
methodical-marmotlcpz
methodical-marmot
authored andcommitted
widget.cal: added option for displaying week numbers
1 parent fd2e616 commit 1291b1c

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ If you want to create a pull request, make sure that:
2424

2525
- Your code fits with the general style of the module. In particular, you should use the same indentation pattern that the code uses, and also avoid adding space at the ends of lines.
2626

27-
- Your code its easy to understand, maintainable, and modularized. You should also avoid code duplication wherever possible by adding functions to or using lain.helpers_. If something is unclear, or you can't write it in such a way that it will be clear, explain it with a comment.
27+
- Your code its easy to understand, maintainable, and modularized. You should also avoid code duplication wherever possible by adding functions to or using lain.helpers_. If something is unclear, or you can not write it in such a way that it will be clear, explain it with a comment.
2828

2929
- You test your changes before submitting to make sure that you code works and does not break other parts of the module.
3030

31-
- You eventually update ``wiki`` submodule with a thorough section.
31+
- You update ``wiki`` submodule with a thorough section, if necessary.
3232

3333
Contributed widgets have to be put in ``widget/contrib``.
3434

widget/cal.lua

+45-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,22 @@ local function factory(args)
2828
week_start = args.week_start or 2,
2929
three = args.three or false,
3030
followtag = args.followtag or false,
31+
week_number = args.week_number or "none",
32+
week_number_format = args.week_number_format or args.week_number == "left" and "%3d | " or "| %-3d",
3133
icons = args.icons or helpers.icons_dir .. "cal/white/",
3234
notification_preset = args.notification_preset or {
3335
font = "Monospace 10", fg = "#FFFFFF", bg = "#000000"
3436
}
3537
}
3638

39+
function cal.get_week_number(m, st_day, x)
40+
return string.format(cal.week_number_format, os.date("%V", m) + (x ~= 0 and floor((x + st_day) / 7) - 1 or 0))
41+
end
42+
43+
function cal.sum_week_days(x, y)
44+
return (x + y) % 7
45+
end
46+
3747
function cal.build(month, year)
3848
local current_month, current_year = tonumber(os.date("%m")), tonumber(os.date("%Y"))
3949
local is_current_month = (not month or not year) or (month == current_month and year == current_year)
@@ -44,13 +54,47 @@ local function factory(args)
4454
local notifytable = { [1] = string.format("%s%s\n", string.rep(" ", floor((28 - this_month:len())/2)), markup.bold(this_month)) }
4555
for x = 0,6 do notifytable[#notifytable+1] = os.date("%a ", os.time { year=2006, month=1, day=x+cal.week_start }) end
4656
notifytable[#notifytable] = string.format("%s\n%s", notifytable[#notifytable]:sub(1, -2), string.rep(" ", st_day*4))
57+
local strx
4758
for x = 1,mth_days do
48-
local strx = x ~= today and x or markup.bold(markup.color(cal.notification_preset.bg, cal.notification_preset.fg, x) .. " ")
59+
strx = x
60+
if x == today then
61+
if x < 10 then x = " " .. x end
62+
strx = markup.bold(markup.color(cal.notification_preset.bg, cal.notification_preset.fg, x) .. " ")
63+
end
4964
strx = string.format("%s%s", string.rep(" ", 3 - tostring(x):len()), strx)
5065
notifytable[#notifytable+1] = string.format("%-4s%s", strx, (x+st_day)%7==0 and x ~= mth_days and "\n" or "")
5166
end
5267
if string.len(cal.icons or "") > 0 and today then cal.icon = cal.icons .. today .. ".png" end
5368
cal.month, cal.year = d.month, d.year
69+
70+
if cal.week_number ~= "none" then
71+
local m = os.time { year = year or current_year, month = month and month or current_month, day = 0 }
72+
local head_prepend = string.rep(" ", tostring(string.format(cal.week_number_format, 0)):len())
73+
74+
if cal.week_number == "left" then
75+
notifytable[1] = head_prepend .. notifytable[1] -- month-year row
76+
notifytable[2] = head_prepend .. notifytable[2] -- weekdays row
77+
notifytable[8] = notifytable[8]:gsub("\n", "\n" .. cal.get_week_number(m, st_day, 0)) -- first week of the month
78+
79+
for x = 10,#notifytable do
80+
if cal.sum_week_days(st_day, x) == 2 then
81+
notifytable[x] = cal.get_week_number(m, st_day, x) .. notifytable[x]
82+
end
83+
end
84+
elseif cal.week_number == "right" then
85+
notifytable[8] = notifytable[8]:gsub("\n", head_prepend .. "\n") -- weekdays row
86+
for x = 9,#notifytable do
87+
if cal.sum_week_days(st_day, x) == 1 then
88+
notifytable[x] = notifytable[x]:gsub("\n", cal.get_week_number(m, st_day, x - 7) .. "\n")
89+
end
90+
end
91+
-- last week of the month
92+
local end_days = cal.sum_week_days(st_day, mth_days)
93+
if end_days ~= 0 then end_days = 7 - end_days end
94+
notifytable[#notifytable] = notifytable[#notifytable] .. string.rep(" ", 4 * end_days) .. cal.get_week_number(m, st_day, mth_days + end_days)
95+
end
96+
end
97+
5498
return notifytable
5599
end
56100

wiki

Submodule wiki updated from 50fc0e4 to d03338f

0 commit comments

Comments
 (0)