Update with some fixxes and improvements

This commit is contained in:
Robert Jelic
2025-02-20 11:32:33 +01:00
parent 2dfb2443cd
commit 6393198552
14 changed files with 446 additions and 154 deletions

View File

@@ -47,25 +47,61 @@ function utils.uuid()
math.random(0, 0xffff), math.random(0, 0xffff), math.random(0, 0xffff))
end
function utils.split(str, sep)
local parts = {}
local start = 1
local len = len(str)
local splitIndex = 1
function utils.split(str, delimiter)
local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
while true do
local index = str:find(sep, start, true)
if not index then
parts[splitIndex] = str:sub(start, len)
break
function utils.removeTags(input)
return input:gsub("{[^}]+}", "")
end
function utils.wrapText(str, width)
if str == nil then return {} end
str = utils.removeTags(str)
local lines = {}
local paragraphs = utils.split(str, "\n\n")
for i, paragraph in ipairs(paragraphs) do
if #paragraph == 0 then
table.insert(lines, "")
if i < #paragraphs then
table.insert(lines, "")
end
else
local textLines = utils.split(paragraph, "\n")
for _, line in ipairs(textLines) do
local words = utils.split(line, " ")
local currentLine = ""
for _, word in ipairs(words) do
if #currentLine == 0 then
currentLine = word
elseif #currentLine + #word + 1 <= width then
currentLine = currentLine .. " " .. word
else
table.insert(lines, currentLine)
currentLine = word
end
end
if #currentLine > 0 then
table.insert(lines, currentLine)
end
end
if i < #paragraphs then
table.insert(lines, "")
end
end
parts[splitIndex] = str:sub(start, index - 1)
start = index + 1
splitIndex = splitIndex + 1
end
return parts
return lines
end
return utils