[Custom Syntax] Automatic line breaks

Description: You made a script? Submit it here to show your creation to the public, share, take reviews, provision of the help and support is here!
Moderators: Lx Xzit, Yoshi021, 5438A38A, Semi-moderatos, Moderators

Erwill M
Topic author, Count
Count
Avatar
Erwill M
Topic author, Count
Count
Age: 24
Reputation: 189
Posts: 295
Joined: 29 Nov 2015

Post #1by Erwill » 27 Jun 2022, 22:45

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

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/crvisk0g9gf0gma/Automatic line breaks.zip/file

Return to “Script submission”

Who is online (over the past 5 minutes)

Users browsing this forum: 1 guest