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


Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Miniwindows
. . -> [Subject]  MiniWindow text wrapping?

MiniWindow text wrapping?

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


Posted by Thorndraco   (5 posts)  [Biography] bio
Date Mon 05 Jan 2009 02:34 AM (UTC)
Message
How would I do this? I can't seem to get text to wrap to the next line in the miniwindows.
[Go to top] top

Posted by WillFa   USA  (525 posts)  [Biography] bio
Date Reply #1 on Mon 05 Jan 2009 03:02 AM (UTC)

Amended on Mon 05 Jan 2009 03:03 AM (UTC) by WillFa

Message

function string.wrap(str, limit, indent, indent1)
  indent = indent or ""
  indent1 = indent1 or indent
  limit = limit or 72
  local here = 1-#indent1
  return indent1..str:gsub("(%s+)()(%S+)()",
                          function(sp, st, word, fi)
                            if fi-here > limit then
                              here = st - #indent
                              return "\n"..indent..word
                            end
                          end)
end

local i = 0
for _,v in ipairs(utils.split(TEXTTOPRINT:wrap(30, "  ", ""), "\n")) do
WindowText(win,font, v, 0, i * fontheight, width, (i+1) * fontheight, color, false)
i=i+1
end


TEXTTOPRINT, fontheight, width, and color need to be specified in your other code, so this isn't just cut'n'paste.

But it doesn't happen automatically.
[Go to top] top

Posted by Thorndraco   (5 posts)  [Biography] bio
Date Reply #2 on Mon 05 Jan 2009 03:24 AM (UTC)

Amended on Mon 05 Jan 2009 04:04 AM (UTC) by Thorndraco

Message
[string "Trigger: "]:19: unfinished string near '"'

In mine line 19 is this one:
return "\n"..indent..word

[Go to top] top

Posted by WillFa   USA  (525 posts)  [Biography] bio
Date Reply #3 on Mon 05 Jan 2009 04:55 AM (UTC)
Message
all the quotes are balanced. Everything copy correctly?

try changing the "\n" to string.char(13)

Is this in a trigger's Send field or in your main script?
[Go to top] top

Posted by Nick Gammon   Australia  (23,042 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Mon 05 Jan 2009 05:21 AM (UTC)
Message
If you are doing send-to-script I think the \n becomes a linefeed, which is why you are getting the error. Willfa probably showed something from a script file, which doesn't have this problem.

As for the wrapping, text is not automatically wrapped, as Willfa said, this is a feature, not a bug. ;)

The general technique to do this would be:


  • Break the text into words delimited by spaces (eg. using string.gmatch)
  • For each word:

    • See if the x-position (horizontal) plus the width of the next word (see WindowTextWidth) will fit into the number of pixels you want for the width
    • If so, write the word to the current line, and update the x-position
    • If not, start a new line (update the y-position) and set the x-position back to some low figure (eg. 0, 1, 5 or whatever margin you want)



You would need to allow for words that are too wide to fit anyway, so if the x-position was already the left margin, output the word anyway.

- Nick Gammon

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

Posted by Thorndraco   (5 posts)  [Biography] bio
Date Reply #5 on Mon 05 Jan 2009 03:10 PM (UTC)
Message
string.char(13) worked fine.

Yes this is a send-to-script trigger.

Here I'll explain what I'm hoping to do:

I want pages and whispers to print to a miniwindow. I want to append new messages like you can do with the notepad and possibly allow for scrolling to read previous messages (though having previous lines scroll up would be fine as well). I tried the notepad route and it's just way too annoying and isn't flexible enough.

I'm new to Lua, but willing to learn.
[Go to top] top

Posted by Thorndraco   (5 posts)  [Biography] bio
Date Reply #6 on Mon 05 Jan 2009 03:26 PM (UTC)

Amended on Mon 05 Jan 2009 03:34 PM (UTC) by Thorndraco

Message
This is what I have so far, but it won't print anything to the window:


pages = "%0"

win = GetPluginID ()
WindowCreate (win, 0, 0, 700, 200, 12, 0, ColourNameToRGB("black"))
WindowShow (win, true)

WindowFont (win, "f2", "Tahoma", 12, false, true, false, false)

function string.wrap(str, limit, indent, indent1)
indent = indent or ""
indent1 = indent1 or indent
limit = limit or 72
local here = 1-#indent1
return indent1..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
if fi-here > limit then
here = st - #indent
return string.char(13)..indent..word
end
end)
end

local i = 0
for _,v in ipairs(utils.split(pages:wrap(30, " ", ""), string.char(13))) do
WindowText(win,font, v, 0, i * 12, 700, (i+1) * 12, ColourNameToRGB("white"), false)
i=i+1
end

EDIT: Scratch that, forgot to change font to "f2"
What this does do though is the width doesn't seem to work correctly as it stops at about 200pixels instead of 700 and the fontheight has to be set to nearly double to keep lines from overlapping.
[Go to top] top

Posted by Nick Gammon   Australia  (23,042 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Mon 05 Jan 2009 08:14 PM (UTC)
Message
You can't do it like that as you are not calculating the width of the word in a variable-pitch font. You need to do along the lines of what I suggested. This works for me:


-- test text

pages = [[
I want pages and whispers to print to a miniwindow. I want to append new messages like you can do with the
 notepad and possibly allow for scrolling to read previous messages (though having previous lines scroll up would
 be fine as well). I tried the notepad route and it's just way too annoying and isn't flexible enough.
]]

-- window and font info

win = GetPluginID ()
local width = 700
local height = 200
local font = "f2"

-- make window

WindowCreate (win, 0, 0, width, height, 12, 0, ColourNameToRGB("darkslategray"))

-- show border so we can see what we are doing

WindowRectOp (win, 1, 1, 1, -1, -1, ColourNameToRGB("blue")) 

-- set up our font

WindowFont (win, font, "Tahoma", 9, false, true, false, false)

-- how far in to draw

local left_margin = 5
local right_margin = 5

-- derive stuff from font

local space_width = WindowTextWidth (win, font, " ")
local line_height = WindowFontInfo (win, font, 1)

-- starting point

local x = left_margin 
local y = line_height

-- do each word

for word in string.gmatch (pages, "%S+") do

  local word_width = WindowTextWidth (win, font, word)

  -- time to start a new line?

  if (word_width + x) > (width - right_margin) and (x > left_margin) then
    y = y + line_height
    x = left_margin
  end -- if

  -- draw the text

  WindowText (win, font, word, x, y, 0, 0, ColourNameToRGB("white"))
  
  -- advance past the word and a single space
  
  x = x + word_width + space_width 

end -- for loop

WindowShow (win, true)



You can test that in an Immediate window. The code above breaks the text into words, and tries to fit each word on the line, starting a new line if necessary. It does not honour newlines (or multiple consecutive spaces).

For a newline (eg. a new chat line) simply start a new line as it does above, ie.


y = y + line_height
x = left_margin




- Nick Gammon

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

Posted by Thorndraco   (5 posts)  [Biography] bio
Date Reply #8 on Tue 06 Jan 2009 01:17 AM (UTC)
Message
Thank you this has been a tremendous help!

This is also helping me to understand Lua better as I'm pulling this apart and seeing why it works. So, thank you for that as well.
[Go to top] top

Posted by BBAlpha   (10 posts)  [Biography] bio
Date Reply #9 on Sun 31 Jan 2010 02:35 PM (UTC)
Message
This is something I'd like to get working for myself, as well. I've taken the code here (more or less), put it into appropriate triggers, and gotten it all to work.

I'd like to retain style due to chats/tells/says being different colors (and configurable through the mud, something I do change on occasion), though I could arbitrarily set those details within the script in each trigger. Also, I use a fixed width font religiously and so I can code with that assumption in mind.

The biggie is keeping prior chats stored away so that every time the chat window gets updated, I'm not simply starting the window from scratch with just the one trigger's text. That is, I'd like to tack on each incoming trigger text to the bottom and then find a way to 'scroll' the text as it comes.

What I came up with was padding each incoming text so that it filled an appropriate multiple of my miniwindow's width in letters. Then I concatenate in certain control words so my script would know what color to print them, as well as hint at where newlines should be added. All of this would be stored in a MUSH variable, which I'd reference each script call. Take the prior block of text, append the newly triggered text and padding, chop off the beginning text that won't fit, paint the miniwindow.

Sound about right, or is there a better way I'm overlooking?
[Go to top] top

Posted by Nick Gammon   Australia  (23,042 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Sun 31 Jan 2010 07:04 PM (UTC)
Message

Template:post=9996 Please see the forum thread: http://gammon.com.au/forum/?id=9996.



In there I show how you can direct all output from the main window to a miniwindow, wrapping to the width of the window, including if you use a variable-pitch font.

By simply changing the trigger from matching on "*" to whatever your chat line looks like, you could redirect chats only.

The plugin retains text colour, wraps at whatever width you specify for it (at the nearest space), and keeps x lines of output, where x is a number you specify in the plugin.

Also, near the bottom of that post I suggest an amendment that will keep the background colour of each style as well.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[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.


31,263 views.

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]