String 通过换行代码传递值的Lua
在此之前,我在以下链接中收到了帮助: 上面的简短描述是,我正在寻找一种方法,可以在忽略某些字符的字符数的情况下运行换行函数 现在我遇到了另一个问题。我想把最后一个色码带到新的生产线上。例如:String 通过换行代码传递值的Lua,string,lua,match,String,Lua,Match,在此之前,我在以下链接中收到了帮助: 上面的简短描述是,我正在寻找一种方法,可以在忽略某些字符的字符数的情况下运行换行函数 现在我遇到了另一个问题。我想把最后一个色码带到新的生产线上。例如: 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
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颜色代码,因此这将使文本着色变得相当困难
我认为最简单的方法是反向匹配,因此我编写了一个反向文本函数:
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
此外,我不希望它返回不带颜色的颜色代码,这表示为:
@@%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
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