Lua 行绕行,不包括某些字符。
2015-8-16 7:58:49
收藏:0
阅读:95
评论:1
我找到了一个代码,当我写关于我玩的 MUD 的笔记时想要使用。每个注释的行只能有 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 等)。
有没有什么干净的方法可以解决这个问题?
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
```lua 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将
sp、st、word和fi传入该函数,函数内定义了变量delta,初始化为0。word:gsub('@([@%a])', function(c) ... end)用于将word字符串内所有出现@开头的字符换成相应的数字,每换一个增加delta的值,最后加上delta,并与limit比较大小。若fi - here大于limit,则将here设为st减去indent的长度再加上delta,返回一个以\n开头,紧跟indent和word的字符串。