Lua换行,不包括某些字符 [英] Lua Line Wrapping excluding certain characters

查看:6
本文介绍了Lua换行,不包括某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个代码,当我在我弹奏的泥浆上写音符时,我想使用它。每个音符的行长只能是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 "
"..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等)。

有没有我遗漏的干净利落的方法?

推荐答案

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 "
"..indent..word
  end
end

这篇关于Lua换行,不包括某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆