Fabrimat

TestWrap

Mar 6th, 2021 (edited)
1,123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.84 KB | None | 0 0
  1. local function wrap(str, limit)
  2.   local Lines, here, limit = {}, 1, limit or 72
  3.   Lines[1] = string.sub(str,1,str:find("(%s+)()(%S+)()")-1)  -- Put the first word of the string in the first index of the table.
  4.  
  5.   str:gsub("(%s+)()(%S+)()",
  6.         function(sp, st, word, fi)  -- Function gets called once for every space found.
  7.           if fi-here > limit then
  8.                 here = st
  9.                 Lines[#Lines+1] = word                                             -- If at the end of a line, start a new table index...
  10.           else Lines[#Lines] = Lines[#Lines].." "..word end  -- ... otherwise add to the current table index.
  11.         end)
  12.  
  13.   return Lines
  14. end
  15.  
  16. local myText = "THIS IS A REALLY LONG TEXT THAT I WOULD LIKE TO WRAP AT A CERTAIN MARGIN."
  17.  
  18. local myTable = wrap(myText,20)
  19.  
  20. for k,v in pairs(myTable) do print(k.." = "..v) end
Advertisement
Add Comment
Please, Sign In to add comment