Lua 通过换行代码携带一个值
Lua Carrying over a value through line wrap code
之前,我在以下方面得到了帮助link:
上面的简短描述是我正在寻找一种方法来实现 运行 换行功能,同时忽略某些字符的字符数。
现在我遇到了另一个问题。我希望能够将最后一个颜色代码带到新行。例如:
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.
而不是
If this line @Rwere over 79 characters, I would want to @Breturn the last known
colour code @Yon the line break.
我希望它这样做,因为在许多情况下,MUD 将默认返回@w 颜色代码,因此这会使着色文本变得相当困难。
我认为最简单的方法是反向匹配,所以我编写了一个 reverse_text 函数:
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.
到
@Mtest. @Ba @Yis @GThis
我在创建 string.match 时 运行 遇到的问题是颜色代码可以采用以下两种格式之一:
@%a 或 @x%d%d%d
此外,我不希望它成为 return 不着色的颜色代码,表示为:
@@%a 或 @@x%d%d%d
在不影响我的要求的情况下实现最终目标的最佳方法是什么?
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
之前,我在以下方面得到了帮助link:
上面的简短描述是我正在寻找一种方法来实现 运行 换行功能,同时忽略某些字符的字符数。
现在我遇到了另一个问题。我希望能够将最后一个颜色代码带到新行。例如:
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.
而不是
If this line @Rwere over 79 characters, I would want to @Breturn the last known
colour code @Yon the line break.
我希望它这样做,因为在许多情况下,MUD 将默认返回@w 颜色代码,因此这会使着色文本变得相当困难。
我认为最简单的方法是反向匹配,所以我编写了一个 reverse_text 函数:
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.
到
@Mtest. @Ba @Yis @GThis
我在创建 string.match 时 运行 遇到的问题是颜色代码可以采用以下两种格式之一:
@%a 或 @x%d%d%d
此外,我不希望它成为 return 不着色的颜色代码,表示为:
@@%a 或 @@x%d%d%d
在不影响我的要求的情况下实现最终目标的最佳方法是什么?
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