Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- description.txt test contents:
- Loremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlaboreetdoloremagnaaliquyamerat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
- I can
- add
- more
- lines
- like
- this
- ]]
- function core.wrap_text(text, limit)
- text = text:gsub("\r", ""):trim()
- if text:len() <= limit then
- return {text}
- end
- local retval = {}
- if limit == 1 then
- text = text:gsub("%s+", "")
- text:gsub(".", function(c)
- table.insert(retval, c)
- end)
- return retval
- end
- while text:len() > 0 do
- local maxlen = math.min(text:len(), limit)
- local s = text:sub(1, maxlen)
- local space = s:reverse():find(" ") or 0
- local line = s:find("\n")
- local splitpos = (line and line - 1)
- or (maxlen - space)
- if not line and text:len() <= limit then
- retval[#retval + 1] = text
- break
- end
- if not line and splitpos == limit then
- retval[#retval + 1] =
- s:sub(1, limit - 1) .. "-"
- text = text:sub(limit + 1,
- text:len())
- else
- retval[#retval + 1] = s:sub(1, splitpos)
- text = text:sub(splitpos + 2,
- text:len())
- end
- end
- return retval
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement