ID:1725711
 
(See the best response by Ss4toby.)
Problem description:
I'm trying to split text up properly based on the length of the dialog/text.

I'm looking for a means to "skip" to the next available space, period or comma then start the next line, OR simply go back a few spaces and push the entire word onto the next line.

In other words I'm not really looking for a delimiter or wanting to place a delimiter system n at this point, what I'm looking for rather is a way to cleanly break the text into lines by checking for " " or "," or "." once the length has reached or neared a certain point.

Example:

"The shelter from the wind seemed inade
quate as the temperature dropped into ar
tic levels." <--what i have.

"The shelter from the wind seemed
inadequate as the temperature dropped into
artic levels." <--what i want


What I use thus far:
     CutLines(msg,cl=rgb(200,100,100))
var/x=1
var/begin=1
var/space=28
var/end=space
var/gm=max(1,round(length(msg)/space))
if(length(msg)>space)
var/a=round(length(msg)/space)
var/re=length(msg)%space
gm=a
if(re>0)
gm++
for(x,x<=gm,x++)
var/mm=copytext(msg,begin,end+1)
AddLines(WriteLine(mm,cl))
begin+=space
end+=space
Best response
Well... If your desire is to crop your text into certain lengths, while not chopping up words, there's actually an easy solution. The solution being, use findtext() to seek out spaces and try to only line break where spaces are found (naturally, if you have a word that is multiple lines big, you will need to just cut that word).

As for the best method of doing all this, I'm not entirely sure. It'd be idea if you could simply check the end of the chunk for a space, then descend every 2 characters until you find a space, and force-crop the text once you get to about the middle (because more then likely you've got yourself a massive word at this point).

Truth be told, I've always been terrible at explaining things, and that's not likely to change. With that said, here's a bit of functioning script (at least I think it is, just wrote it up):

mob/verb/CropText(tx as text)
var/MAX_LENGTH=30//Max lenght we'll let our lines be
var/runTimes=round(MAX_LENGTH/4)//We only want to run our for() loop about a quarter of our max length to chunk.

var/startingPoint=1
var/textLength=length(tx)

var/list/stringChunks = new//A list to put each cropped chunk into

while(startingPoint<textLength)
var/endPoint=startingPoint+MAX_LENGTH
var/forcedCrop=1//If the system was unable to find a space

if(endPoint<textLength+1)//Is our end point smaller then our text's length?

if(findtext(tx," ",startingPoint,endPoint) >= runTimes+startingPoint)//If a space even exists and it's passed our stopping point
for(var/n=MAX_LENGTH;n>=runTimes;n-=2)
var/spot=findtext(tx," ",startingPoint+n,endPoint)//Searching for a space within a given area.
if(spot)
forcedCrop=null
endPoint=spot
break

else
forcedCrop=null
endPoint=textLength+1//To include the last punctuation.

var/txt=copytext(tx,startingPoint,endPoint)
if(forcedCrop)
txt+="-"//Give them a dash to show that word was cut.
startingPoint=endPoint+1
stringChunks+=txt

//Output our list
for(var/t in stringChunks)
src<<t


Above code output:
The shelter from the wind
seemed inadequate as the
temperature dropped into
artic levels.


I know it's poor tutoring to just hand someone the answers, so I tried to explain what I felt needed explaining. So, hopefully this helps, and is what you were asking for.

*EDIT* I changed the script I provided because I realized it didn't check to make sure a space even existed in the chunk (meaning it would run the for() loop no matter what). Honestly, I'm torn as to whether it would be more idea to do the method I've provided, or more idea to parse through your spaces until you hit one that is too far out, then fall back onto the last one found.

Hopefully, someone else will come along and give some more insight into this dilemma.
In response to Ss4toby
I suffer from the same thing sometimes as well (being unable to adequately explain my ideas) so I pretty much understood what you were saying. The script is just an added bonus so thanks for taking the time to write it out.