[Mudlet]Chatgpt帮忙写的基于宽度换行的函数
本帖最后由 sulryn 于 2023-8-20 10:46 PM 编辑因为Mudlet的自动换行存在bug,经炮大佬点拨,找gpt3.5写了这样功能的函数,主要适用于消息框miniconsole的显示。
(若行里有emoji会使颜色错位一格的问题,不会修,影响不大,selectString然后replcae删掉或替换掉就好)
注意:输入建议使用copy2decho来获取颜色代码,这样才能保留颜色
function wrapTextByWidth(text, maxWidth)
local lines = {}
local line = ""
local i = 1
local textLength = utf8.len(text)
local lineWidth = 0
local function handleWidth(char)
local charWidth = utf8.width(char, true, 1)
lineWidth = lineWidth + charWidth
if lineWidth <= maxWidth then
line = line .. char
else
lineWidth = charWidth
table.insert(lines, line)
line = char
end
i = i + 1
end
while i <= textLength do
local char = utf8.sub(text, i, i)
if char == "<" then
local endBracketIndex = utf8.find(text,">", i + 1)
if endBracketIndex then
local colorCode = utf8.sub(text, i, endBracketIndex)
if utf8.find(colorCode, "%d+%p%d+") or colorCode == "<r>" then
line = line .. colorCode
i = endBracketIndex + 1
else
handleWidth(char)
end
else
i = i + 1
end
else
handleWidth(char)
end
end
table.insert(lines, line)
local result = table.concat(lines, "\n")
local empty_space = utf8.match(result, "<%d+%p%d+%p%d+%p%d+%p%d+%p%d+>%s<r>$")
if empty_space then
result = utf8.sub(result, 1, -#empty_space - 1)
end
return result
end
我今天更新了一版
页:
[1]