以前,我在以下链接中收到了帮助:
Lua Line Wrapping excluding certain characters
上面的简短说明是我正在寻找一种方法来运行换行功能,同时忽略某些字符的字符数。
现在我遇到了另一个问题。我希望能够将最后一个颜色代码传送到新行。例如:
if (isset($_POST['schoolId'])) {
echo "yes";
}else {
echo "nope";
}//keeps echoing nope because the schoolId is not received
运行我想到的功能会导致:
If this line @Rwere over 79 characters, I would want to @Breturn the last known colour code @Yon the line break.
而不是
If this line @Rwere over 79 characters, I would want to @Breturn the last known
@Bcolour code @Yon the line break.
我希望它能够这样做,因为在很多情况下,MUD会默认回到@w颜色代码,所以它会使文本变得粗糙。
我认为最简单的方法就是反向匹配,所以我写了一个reverse_text函数:
If this line @Rwere over 79 characters, I would want to @Breturn the last known
colour code @Yon the line break.
然后转过来:
function reverse_text(str)
local text = {}
for word in str:gmatch("[^%s]+") do
table.insert(text, 1, word)
end
return table.concat(text, " ")
end
到
@GThis @Yis @Ba @Mtest.
我在创建string.match时遇到的问题是颜色代码可以采用以下两种格式之一:
@%a 或 @ x%d%d%d
此外,我不希望它返回不着色的颜色代码,表示为:
@@%a 或 @@ x%d%d%d
在不影响我的要求的情况下,实现我的最终目标的最佳途径是什么?
答案 0 :(得分:1)
function wrap(str, limit, indent, indent1)
indent = indent or ""
indent1 = indent1 or indent
limit = limit or 79
local here = 1-#indent1
local last_color = ''
return indent1..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
local delta = 0
local color_before_current_word = last_color
word:gsub('()@([@%a])',
function(pos, c)
if c == '@' then
delta = delta + 1
elseif c == 'x' then
delta = delta + 5
last_color = word:sub(pos, pos+4)
else
delta = delta + 2
last_color = word:sub(pos, pos+1)
end
end)
here = here + delta
if fi-here > limit then
here = st - #indent + delta
return "\n"..indent..color_before_current_word..word
end
end)
end