I need to modify the following function (found on http://lua-users.org...i/StringRecipes):
function wrap(str, limit)
limit = limit or 72
local here = 1
-- the "".. is there because :gsub returns multiple values
return ""..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
if fi-here > limit then
here = st
return "\n"..word
end
end)
end
Currently all the function does is it returns a string with words wrapped at a given margin. But what I'm trying to achieve is that every line is put into a table (ie lines with the words wrapped).
Spoiler
If you don't understand what I mean by 'word wrapping', consider the following piece of text:Derp was a man who lived in a forest. He lived peacefully, until he died. The end.
Let's say I can only print 20 characters on a line. To divide the piece of text equally, I could just do this:
Derp was a man who liv (20 character limit)
ed in a forest. He li
… (you get what I mean).
But what I want to happen is this:
Derp was a man who
lived in a forest. He
lived …
I have already tried the following but it doesn't work:
Lines = { }
function wrap(str, limit)
limit = limit or 72
local here = 1
return ""..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
if fi-here > limit then
here = st
Lines[#Lines+1] = "\n"..word -- this does not work
return "\n"..word
end
end)
end
Doing this gives the following result: http://repl.it/MDjAny help/tips would be greatly appreciated.
Thanks.