Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua换行,不包括某些字符_Lua_Word Wrap - Fatal编程技术网

Lua换行,不包括某些字符

Lua换行,不包括某些字符,lua,word-wrap,Lua,Word Wrap,我找到了一个代码,当我在我玩的泥上写笔记时,我想使用它。每条便笺的行长只能为79个字符,因此,有时写便笺会很麻烦,除非你正在计算字符数。代码如下: function wrap(str, limit, indent, indent1) indent = indent or "" indent1 = indent1 or indent limit = limit or 79 local here = 1-#indent1 return indent1..str:gsub("(%s

我找到了一个代码,当我在我玩的泥上写笔记时,我想使用它。每条便笺的行长只能为79个字符,因此,有时写便笺会很麻烦,除非你正在计算字符数。代码如下:

function wrap(str, limit, indent, indent1)
  indent = indent or ""
  indent1 = indent1 or indent
  limit = limit or 79
  local here = 1-#indent1
  return indent1..str:gsub("(%s+)()(%S+)()",
                          function(sp, st, word, fi)
                            if fi-here > limit then
                              here = st - #indent
                              return "\n"..indent..word
                            end
                          end)
end
这将非常有效;我可以键入一行300个字符,它将格式化为79个字符,尊重完整的字

我遇到的问题是,有时,我想在行中添加颜色代码,而颜色代码不按字数计算。例如:

@GThis is a colour-coded @Yline that should @Bbreak off at 79 @Mcharacters, but ignore @Rthe colour codes (@G, @Y, @B, @M, @R, etc) when doing so.
从本质上讲,它可以去除色码,并适当地打断线条,但不会丢失色码

进行编辑,以包括应检查的内容以及最终输出内容。

该函数将仅检查以下字符串是否有换行:

This is a colour-coded line that should break off at 79 characters, but ignore the colour codes (, , , , , etc) when doing so.
但实际上会返回:

@GThis is a colour-coded @Yline that should @Bbreak off at 79 @Ncharacters, but ignore 
the colour codes (@G, @Y, @B, @M, @R, etc) when doing so.
更复杂的是,我们还有xterm颜色代码,它们很相似,但看起来像这样:

@x123
它总是@x后跟一个3位数字。最后,为了使事情更加复杂,我不希望它去掉有用途的颜色代码(可能是@R、@@x123等等)


有没有一种干净的方法让我错过了?

我对你所说的“忽略”有点困惑。你能为你的例子提供你期望的输出吗?在同一个单词中可以使用多种颜色吗(例如
@RL@Gu@Ba
)?@EgorSkriptunoff,是的。如果我愿意的话,我可以为每个字母设置不同的颜色代码<代码>@x123L@Ru@例如x032a,先生,你是个天才。我之所以编辑这篇文章,是因为当需要
if c='@'
时,你有
if c='@'
。感谢您简洁快速地提出解决方案!似乎我的编辑未被批准。无论如何,有关所需的编辑更改,请参见前面的注释。再次感谢!
function(sp, st, word, fi)
  local delta = 0
  word:gsub('@([@%a])', 
    function(c)
      if c == '@'     then delta = delta + 1 
      elseif c == 'x' then delta = delta + 5
      else                 delta = delta + 2 
      end
    end)
  here = here + delta
  if fi-here > limit then
    here = st - #indent + delta
    return "\n"..indent..word
  end
end