This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

Text Wrapping?

Started by H4X0RZ, 23 July 2013 - 04:57 AM
H4X0RZ #1
Posted 23 July 2013 - 06: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?
Kingdaro #2
Posted 23 July 2013 - 07: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'))
H4X0RZ #3
Posted 23 July 2013 - 07:19 AM
Thank you very much !