[Mudlet]换行函数(基于宽度)
function wrapLineBasedOnWidth(maxWidth, windows, lineToWrap)--三个参数分别为:每行宽度,一个含窗口名的表,该行的文本
lineToWrap = lineToWrap or getCurrentLine()
local wrapAt = {}
local index = 1
local lineWidth = 0
local length = utf8.len(lineToWrap)
local totalWidth = utf8.width(lineToWrap, true, 0)
local wrapCount = totalWidth / maxWidth
if wrapCount <= 1 then
return
end
--开始计算换行位置
while wrapCount > 1 and index <= length do
local char = utf8.sub(lineToWrap, index, index)
local charWidth = utf8.width(char, true, 0)
lineWidth = charWidth + lineWidth
if lineWidth > maxWidth then
table.insert(wrapAt, 1, (index - 1))
wrapCount = wrapCount - 1
lineWidth = charWidth
end
index = index + 1
end
--开始换行
if wrapAt then
for i = 1, #wrapAt do
for j = 1, #windows do
local latestline = getLastLineNumber(windows)
moveCursor(windows, wrapAt, latestline - i)
insertText(windows, "\n")
echo(windows, "")
end
end
end
end
行首触发器例子:【闲聊】
local currentLine = line
geyser_GUI.chat_window.cut()
上面这个函数里有copy()和appendBuffer()
local miniconsoles = {"all_miniconsole", "chat_miniconsole"}
上面这个表是我需要换行的窗口
wrapLineBasedOnWidth(utility.chat_console_width, miniconsoles, currentLine)
utility.chat_console_width是我用getColumnCount()算好的变量
页:
[1]