Jump to content




Breaking string into lines


  • You cannot reply to this topic
1 reply to this topic

#1 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 01 September 2014 - 03: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 by KingofGamesYami, 01 September 2014 - 03:40 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 September 2014 - 04:47 AM

Change:

                       lines[ #lines + 1 ] = word

... to:

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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users