[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Miniwindows
. . -> [Subject]  Miniwindow Calendar refuses to show month

Miniwindow Calendar refuses to show month

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Wed 09 May 2018 09:46 AM (UTC)

Amended on Wed 09 May 2018 09:02 PM (UTC) by AdInfinitum

Message
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).
[Go to top] top

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Reply #1 on Wed 09 May 2018 05:24 PM (UTC)
Message
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!
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Wed 09 May 2018 08:53 PM (UTC)
Message
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})

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Reply #3 on Wed 09 May 2018 08:56 PM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Thu 10 May 2018 09:33 AM (UTC)
Message
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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Reply #5 on Thu 10 May 2018 06:54 PM (UTC)
Message
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!
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Thu 10 May 2018 09:41 PM (UTC)
Message
You have a typo:


function OnPlugionSaveState()
    movewindow.save_state(win)
end


That should be:


function OnPluginSaveState()
    movewindow.save_state(win)
end

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Reply #7 on Thu 10 May 2018 10:24 PM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #8 on Fri 11 May 2018 02:30 AM (UTC)
Message
It did when I tested it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Reply #9 on Fri 11 May 2018 01:52 PM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Fri 11 May 2018 09:30 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Reply #11 on Sat 12 May 2018 07:14 AM (UTC)
Message
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.
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #12 on Sat 12 May 2018 03:53 PM (UTC)
Message
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? :)

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #13 on Sat 12 May 2018 06:58 PM (UTC)

Amended on Sat 12 May 2018 07:00 PM (UTC) by Fiendish

Message
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)

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by AdInfinitum   (74 posts)  [Biography] bio
Date Reply #14 on Wed 16 May 2018 06:33 PM (UTC)
Message
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.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


37,493 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]