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

Breaking string into lines

Started by KingofGamesYami, 01 September 2014 - 02:37 AM
KingofGamesYami #1
Posted 01 September 2014 - 04:37 AM
I've got a function that breaks a string down into 'lines', so I can fit it into a defined space. However, I'm having trouble with the spaces:


--#note: minx/maxx/miny/maxy are coords defining the space
	local lines = { [1]=""}
	for word in text:gmatch( "%S+" ) do
		local oldLine = lines[ #lines ]
		lines[ #lines ] = lines[ #lines ] .. " " .. word
		if #lines[ #lines ] > maxx - minx then
			lines[ #lines ] = oldLine
			lines[ #lines + 1 ] = word
		end
	end
The above works, however the first line is offset by one space…

Below does not have the offset, however none of the words are spaced. (edit: actually, the first line is spaced. The rest are not)

	local lines = { [1]=""}
	for word in text:gmatch( "%S+" ) do
		local oldLine = lines[ #lines ]
		lines[ #lines ] = lines[ #lines ] .. word .. " "
		if #lines[ #lines ] > maxx - minx then
			lines[ #lines ] = oldLine
			lines[ #lines + 1 ] = word
		end
	end

It could be the fact that I'm using mimic, however I have not run into any similar issues with it.

Additionally, the code I use to render it is here, if that makes any difference:

		local l = self.miny
		for _, line in ipairs( self.lines ) do
			term.setCursorPos( self.minx, l )
			term.write( line )
			l = l + 1
		end
Edited on 01 September 2014 - 02:40 AM
Bomb Bloke #2
Posted 01 September 2014 - 05:47 AM
Change:

                       lines[ #lines + 1 ] = word

… to:

                       lines[ #lines + 1 ] = word .. " "