Miniwindow Calendar refuses to show month

Posted by AdInfinitum on Wed 09 May 2018 09:46 AM — 16 posts, 65,469 views.

#0
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).
Amended on Wed 09 May 2018 09:02 PM by AdInfinitum
#1
Ha! I wound up fixing it myself. It turns out that I didn't properly insert the month. I had to do:
styles[1] = {}
table.insert(styles[1], "{color = "springgreen", text = savemonth})

in order for it to work. Problem solved, yay!
Australia Forum Administrator #2
Glad you solved it. You might want to tell people how to use it. I found something like this worked (in the Immediate window). You need to execute the posted code first.


showCalendar ('2018-05-10')


Plus, you have an errant quote there. Your fix should read:


table.insert(styles[1], {color = "springgreen", text = savemonth})
#3
Good catch on the errant quote. In the script itself, I had it fixed, but I'm going to update the code so anyone can copy/paste it.

Also, in my edit, I did show how to use it with os.date, but could have shown how to use it with other dates, too.

Now I just need to learn how to get windows to stay in place over restarts. Still quite new to the miniwindows scene.
Australia Forum Administrator #4
AdInfinitum said:

Now I just need to learn how to get windows to stay in place over restarts. Still quite new to the miniwindows scene.


http://www.gammon.com.au/forum/?id=9594
#5
Ah, perfect. However, it doesn't seem to save its position upon reinstall. Here's the plugin I now have, maybe someone can look it over and improve upon it?

Too many characters, have to link it:

https://pastebin.com/XuYm7gPg

It will stay in the same spot whenever I call the function, so there's that. Just need it to remain in position when I close MUSH or reinstall. Thank you for everything!
Australia Forum Administrator #6
You have a typo:


function OnPlugionSaveState()
    movewindow.save_state(win)
end


That should be:


function OnPluginSaveState()
    movewindow.save_state(win)
end
#7
D'oh! Good catch on that. I didn't even see it when you posted what the error was until I took it apart letter by letter.

Still, it's not saving the position on plugin reinstall or shutting MUSH down and reopening.
Australia Forum Administrator #8
It did when I tested it.
#9
Nick Gammon said:

It did when I tested it.


Hmm. You're right. I tested it in a Vanilla MUSH, with nothing else loaded, and it worked. Note that this was tested in 4.94, did not test in the 5.06 update.

Did you try running it on Fiendish's Aardwolf Package? It makes me wonder if Fiendish's layout save plugin is the reason why the window isn't saving. I'll contact him about it as well, though.

Glad to see that I did something right, at least.
Australia Forum Administrator #10
No I didn't test on Aardwolf. Possibly you are right about Fiendish's plugins doing it, although how they could manage that is a mystery.
#11
Tested it on a Vanilla copy of the Aardwolf MUSHclient package, and I have confirmed it as the culprit for the window not saving its position.

I was not able to disable every plugin, so with the few remaining, listed below, I may have narrowed it enough for Fiendish to figure out. Now I just need to get a hold of him.

Plugins unable to be disabled:
aard_chat_echo
aard_package_update_checker
aard_soundpack
aard_Copy_Colour_Codes
GMCP_handler
Hyperlink_URL2
Miniwindow_Z_Order_Monitor (my guess)
Repaint_Buffer


The only one that makes logical sense would be the Miniwindow_Z_Order_Monitor plugin. I don't expect you to do anything with this info, Nick; I'm just relaying the information in case anyone else has the same issue.
USA Global Moderator #12
Nick Gammon said:

Possibly you are right about Fiendish's plugins doing it, although how they could manage that is a mystery.


What fun would it be if I didn't manage to do something surprising now and then? :)
USA Global Moderator #13
Anyway, the location problem in one but not the other would be because my movewindow.lua is slightly different than the stock one to accomodate my miniwindow layout save/restore code. I'll see if I can reconcile them so that this doesn't happen.

To fix it in the plugin, replace

movewindow.install (win, 7)

with

movewindow.install (win, 7, miniwin.create_absolute_location)
Amended on Sat 12 May 2018 07:00 PM by Fiendish
#14
Fiendish said:

Anyway, the location problem in one but not the other would be because my movewindow.lua is slightly different than the stock one to accomodate my miniwindow layout save/restore code. I'll see if I can reconcile them so that this doesn't happen.

To fix it in the plugin, replace

movewindow.install (win, 7)

with

movewindow.install (win, 7, miniwin.create_absolute_location)



It should be noted that the fix only works for reinstalling the plugin. For some reason or another, it doesn't work once you restart MUSH.
Australia Forum Administrator #15
That is just a default position. There must be some other change required.