Jump to content




Text Wrapping?


  • You cannot reply to this topic
2 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 23 July 2013 - 05:57 AM

I want to cut a a big line of text into some smaler lines which fits into this screen size:
local w,h = term.getSize()
sX,sY,eX,eY = 4,6,w - 4, h - 3
Is there a way to do this?

#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 23 July 2013 - 06:14 AM

Requires a bit of effort, but can be done.

function wrapText(text, limit)
	local lines = {}
	local curLine = ''
	for word in text:gmatch('%S+%s*') do
		curLine = curLine .. word
		if #curLine + #word >= limit then
			lines[#lines + 1] = curLine
			curLine = ''
		end
	end
	return lines
end

This basically searches through each grouping of nonspace characters and space characters (a word, basically), then adds to the current line in the loop. If the length of the line is greater than the limit given, throw the current line in our table of wrapped lines, and clear the current line.

After using this function, all you need to do is write the given lines manually to the screen.

Example:
local text = 'some really really really long text that needs to be wrapped severely'
local wrapped = wrapText(text, 15)

print(table.concat(wrapped, '\n'))


#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 23 July 2013 - 06:19 AM

Thank you very much !





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users