北大侠客行MUD论坛

 找回密码
 注册
搜索
热搜: 新手 wiki 升级
查看: 841|回复: 1

[Mudlet]Chatgpt帮忙写的基于宽度换行的函数

[复制链接]
发表于 2023-8-20 12:08:27 | 显示全部楼层 |阅读模式
本帖最后由 sulryn 于 2023-8-20 10:46 PM 编辑

因为Mudlet的自动换行存在bug,经炮大佬点拨,找gpt3.5写了这样功能的函数,主要适用于消息框miniconsole的显示。
(若行里有emoji会使颜色错位一格的问题,不会修,影响不大,selectString然后replcae删掉或替换掉就好)
注意:输入建议使用copy2decho来获取颜色代码,这样才能保留颜色




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
北大侠客行Mud(pkuxkx.net),最好的中文Mud游戏!
 楼主| 发表于 2023-11-25 12:47:01 | 显示全部楼层
function wrapTextByWidth(text, maxWidth)
    local lines = {}
    local line = ""
   
    local i = 1
    local textLength = utf8.len(text)
    local lineWidth = 0
   
    local function handleWidth(char)
      local charWidth = utf8.width(char, true, 1)
      lineWidth = lineWidth + charWidth
      if lineWidth <= maxWidth then
        line = line .. char
      else
        lineWidth = charWidth
        table.insert(lines, line)
        line = char
      end
      i = i + 1
    end
   
    while i <= textLength do
        local char = utf8.sub(text, i, i)
        
        if char == "<" then
            local endBracketIndex = utf8.find(text,">", i + 1)
            if endBracketIndex then
                local colorCode = utf8.sub(text, i, endBracketIndex)
                if utf8.find(colorCode, "%d+%p%d+") or colorCode == "" then
                  line = line .. colorCode
                  i = endBracketIndex + 1
                else
                  handleWidth(char)
                end
            else
              i = i + 1
            end
        else
           handleWidth(char)
        end
    end
   
    table.insert(lines, line)
   
    local result = table.concat(lines, "\n")
    local empty_space = utf8.match(result, "<%d+%p%d+%p%d+%p%d+%p%d+%p%d+>%s$")
    if empty_space then
      result = utf8.sub(result, 1, -#empty_space - 1)
    end
    return result
end

我今天更新了一版
北大侠客行Mud(pkuxkx.net),最好的中文Mud游戏!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|北大侠客行MUD ( 京ICP备16065414号-1 )

GMT+8, 2024-4-27 08:12 PM , Processed in 0.009222 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表