Page 1 of 1

[Custom Teascript Syntax] Automatic line breaks

Posted: Mon Jun 27, 2022 3:49 pm
by erwill
This syntax will return the string value with line breaks depending the maximum characters per line.

lreturn(txt, length, "", 0, 0, 0)

txt: the original string value
length: the maximum length of a line
Examples: show

Code: Select all

str(a) = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
str(b) = lreturn(str(a), 21, "", 0, 0, 0)
Return:
Lorem ipsum dolor sit
amet, consectetur
adipiscing elit.
Script

Code: Select all

export script lreturn(txt as string, length as double, render as string, cend as double, cstart as double, i as double, return string)
	length += 1 'Add 1 to length to be more consistent to the parameter (3 = 3 characters per line)
	if length < 2 'To prevent crash if the length parameter is less than 0
		call debug("LRETURN ERROR: length can't be less than 1 (length value: "&(length-1)&")")
		return left(txt, len(txt)) 'Returning directly txt will return a runtime error
	end
	render = "" 'Set render parameter to nothing if the user set something
	cend = 0 'Set cend parameter to nothing if the user set something
	'This small part is used to take into account the "Return line" characters already present in the txt parameter
	recountb:
	for i = cend to cend + length
		if mid(txt, i, 1) = chr(13)
			cend = i + 1
			goto recountb
		end
	next
	cend = length 'Set cend to the maximum length of a line
	cstart = 0 'Set cstart parameter to nothing if the user set something
	do until cend > len(txt) 
		if mid(txt, cend, 1) = " " or cend < cstart 'if the character where cend is is a space or if cend is before cstart
			if cend < cstart 'if cend is before cstart (meaning is there is no space between cend and cstart)
				cend += length
				render = render + mid(txt, cstart, cend-cstart) + chr(13)
				cstart = cend 'cstart will be cend to the next line
			else
				render = render + mid(txt, cstart, cend-cstart) + chr(13)
				cstart = cend + 1 'cstart will be cend plus 1 character to avoid the space as first character to the next line
			end
			'This small part is used to take into account the "Return line" characters already present in the txt parameter
			recount:
			for i = cend to cend + length
				if mid(txt, i, 1) = chr(13)
					cend = i + 1
					goto recount
					call debug("a")
				end
			next
			cend += length 'Set cend to the maximum length of a line
		end
		cend -= 1 'Shift cend to the left if it's not a space
	loop 
	render = render + mid(txt, cstart, cend-cstart) 'Add the last line when cend is above the text length
	return left(render, len(render)) 'Returning directly render will return a runtime error
end script 
Demo
https://www.mediafire.com/file/crvisk0g ... s.zip/file