Hello, community! I'd need to separate given text by a given maximal amount of characters per line and fill up the line with spaces if the next word can't fit in. I googled for a simple function to do this as I thought that it can already have some answer, but there are so many Lua modifications that I simply couldn't find anything that would work for me. (Hope that my goal is understandable, sorry for my bad English.) So, I'm asking you as a noob in pattern matching, does anyone know how to solve this problem? Thanks for any feedback!
5 replies to this topic
#1
Posted 24 April 2014 - 05:03 PM
#3
Posted 24 April 2014 - 05:39 PM
I fail to see how pagedPrint is related to his quesiton...
Here's a an excerpt from one of my own codes. This will split strings into lines.
Here's a an excerpt from one of my own codes. This will split strings into lines.
local tLine = {}
local lineLength = 25 --idk how long you want them to be
while #text > 0 do --splits text into a table containing each line
local line = text:sub(1,lineLength)
local newLine = string.find(line.."","\n") --check for new line character
if newLine then
line = line:sub(1,newLine-1)
text = text:sub(#line+2,#text)
elseif #line == lineLength then
local endSpace = line:find"%s$" or line:find"%s%S-$" or lineLength
line = line:sub(1,endSpace)
text = text:sub(#line+1,#text)
else
text = ""
end
tLine[#tLine+1] = line
end
I suppose what you're lookig for would be something like thislocal line = text:sub(1,lineLength) --extract the max length of each line from the string
local endSpace = line:find"%s$" or line:find"%s%S-$" or lineLength --look for a space at the end, or the space closest to the end, or just return the max line length
line = line:sub(1,endSpace) --extract the new string
line = line..string.rep(" ",endSpace-lineLength) --fill in the blank spaces with well... spaces
Edited by CometWolf, 24 April 2014 - 05:40 PM.
#4
Posted 25 April 2014 - 02:10 PM
CometWolf, on 24 April 2014 - 05:39 PM, said:
-snip-
line = line..string.rep(" ",endSpace-lineLength) --fill in the blank spaces with well... spaces Shouldn't it be lineLenght-endSpace ? I added your code, fixed that line with if endSpace-lineLenght>0 then ... end but it's weirdly adding the spaces (in fact, it's not adding them till the eol, only about 1 space is added). On the other hand, the output from (I added this to a wrap(str,limit) function) wrap("this is some text asdasdasdasd",5) is something likethis me xt sdasd ...(simply cutting off first 2 symbols if line!=1)... Weird isn't it? I'm using CC 1.63, it might be because of that, I debugged your code at first with my Android device and SigmaScript while at school, and it worked (well, dunno if the spaces were added properly, SigmaScript doesn't offer better output than a console-no colors, etc.). by the way here is your code when put into a function that should work
Spoiler
Hope I'm not too stupid and it's not obvious.... (but as usual, it is isn't it? Im just sooo tired... )And hope that you will be able to help me, thanks for any feedback!
Edited by viluon, 25 April 2014 - 02:12 PM.
#5
Posted 25 April 2014 - 03:21 PM
You are indeed correct about the endSpace thing. The original code is part of what i use to render message windows in Turtle Architect so i know it works, it dosen't require the appended strings though. string.rep is annoying like that, you'll have to resort to using math.max(value,0) to avoid any negatives.
#6
Posted 26 April 2014 - 07:08 PM
Sorry for the long delay, I've edited it a bit and guess what, IT WORKS!!! Huge thanks to you, CometWolf!
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











