I'm still learning about miniwindows, and tackling projects one at a time. My most recent project was constructing a calendar in a miniwindow so I can glance up and see what day it is.
In short, it works almost perfectly. However, no matter what I try, it simply refuses to show the month. In the code below, you'll see where I've tried to insert into table styles the month as the first element in the table as well as the last element in the table. But it seems to simply disappear. Instead of showing the month, it acts like it's there because the height of the window increases, but it never displays it. What am I doing incorrectly?
local win = "RealCalendar"
local font = "f"
local daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
local monthsInYear = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
function isLeapYear(year)
return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
end
local week = {}
styles = {}
function center(str)
local l = math.floor((27 - #str)/2)
return string.rep(" ", l) .. str
end
function getDaysInMonth(month, year)
if month == 2 and isLeapYear(year) then
return 29
else
return daysInMonth[month]
end
end
function getDayOfWeek(dd, mm, yy)
dw = os.date('*t', os.time{year = yy, month = mm, day = dd})['wday']
return dw, ({"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"})[dw]
end
function getDateParts(date_str)
_, _, y, m, d = string.find(date_str, "(%d+)-(%d+)-(%d+)")
return tonumber(y), tonumber(m), tonumber(d)
end
function showCalendar(cdate)
local yy, mm, dd = getDateParts(cdate)
local monthDays = getDaysInMonth(mm, yy)
local dayWeek = getDayOfWeek(1, mm, yy)
local dayStart = 1
local d = 1
local daysOfWeek = {{"Sun", 1}, {"Mon", 2}, {"Tue", 3}, {"Wed", 4}, {"Thu", 5}, {"Fri", 6}, {"Sat", 7}}
local daysOfWeekOrdered = {}
for k = 1, 7 do
p = k + dayStart - 1
if (p > 7) then
p = p - 7
end
v = {}
v.dayname = daysOfWeek[p][1]
v.daynum = daysOfWeek[p][2]
table.insert(daysOfWeekOrdered, v)
end
if (dd < 10) then
actday = "0" .. dd
else
actday = dd
end
savemonth = center(monthsInYear[mm])
styles[1] = {}
table.insert(styles[1], {color = "springgreen", text = savemonth})
for i,v in ipairs(daysOfWeekOrdered) do
table.insert(week, {color = "white", text = v.dayname .. " "})
if (dayWeek == v.daynum) then
d = - i + 2
end
end
d = tonumber(d)
while (d < monthDays) do
if week[1] then
local s = ""
for _,v in ipairs(week) do
s = s .. v.text
end
max_width = WindowTextWidth(win, font, s)
table.insert(styles, week)
week = {}
end
for i, v in ipairs(daysOfWeek) do
if (d >= 1 and d <= monthDays) then
if (d < 10) then
if (d == dd) then
table.insert(week, {color = "yellow", text = " 0" .. d .. " "})
else
table.insert(week, {color = "white", text = " 0" .. d .. " "})
end
elseif (d == dd) then
table.insert(week, {color = "yellow", text = " " .. d .. " "})
else
table.insert(week, {color = "white", text = " " .. d .. " "})
end
else
table.insert(week, {color = "white", text = string.rep(" ", 4)})
end
d = d + 1
end
end
if week[1] then
table.insert(styles, week)
end
windowDraw()
end
if not WindowInfo(win, 1) then
WindowCreate(win, 0, 0, 0, 0, 6, 0, 0)
WindowFont(win, font, "Monoid", 9)
end
function windowDraw()
LEFT_MARGIN = 10
TOP_MARGIN = 5
local font_height = WindowFontInfo(win, font, 1)
local window_width = max_width + LEFT_MARGIN
local window_height = font_height * (#styles + 3) + TOP_MARGIN
require 'movewindow'
--movewindow.install(win, 6, 2, true, nil, {mouseup=MouseUp, mousedown=MouseDown, dragmove=LeftClickOnly, dragrelease=LeftClickOnly})
wininfo = movewindow.install (win, miniwin.pos_center_right, 0)
WindowCreate(win, wininfo.window_left, wininfo.window_top, window_width, window_height, wininfo.window_mode, wininfo.window_flags, ColourNameToRGB("black"))
local y = font_height * 2 + TOP_MARGIN
for _, weeks in ipairs(styles) do
local x = 5
WindowText(win, font, savemonth, x, y, 0, 0, ColourNameToRGB("black"))
for _, style in ipairs(weeks) do
x = x + WindowText(win, font, style.text, x, y, 0, 0, ColourNameToRGB(style.color))
end
y = y + font_height
end
movewindow.add_drag_handler (win, 0, 0, 0, window_height)
WindowSetZOrder(win, 50)
movewindow.save_state(win)
WindowShow(win, true)
end
This is very annoying, to say the least. Thank you.
[EDIT] The syntax to show the calendar is showCalendar(os.date("%Y-%m-%d")
[EDIT] Fixed code to escape backslashes, square brackets. - Nick
[EDIT] Changed code so people can copy/paste for their own use. Personally, I have it in a .lua file, and I just dofile ("calendar.lua") and run my syntax.
You can use either the syntax listed in the first edit, or you can do: showCalendar("yyyy-mm-dd") (e.g. showCalendar("2014-02-05") to show Feb 5, 2014). |