起因:做了个工具在console窗口罗列一些信息,基本结构是 [ 文件名 :行号 ],因为文件,行号长度不一,想要做到如下效果。
初步尝试,用以下方法:
string format = "{0,-10} {1,5}"; // -10 表示左对齐,10个字符宽;5 表示右对齐,5个字符宽
Debug.Log(string.Format(format, "File", "Line"));
Debug.Log(string.Format(format, "UI/Titan/UI_Titan_Main", "1"));
Debug.Log(string.Format(format, "Utils/GlobalFunction", "8"));
实际效果如下:
竟然没对齐?拷贝到IntelliJ 里查看
Utils/GlobalFunction | :8
Lua/HookReloadLuaFuc | :35
Lua/HookReloadLuaFuc | :1290
IntelliJ用了等宽字符,所以看起来是对齐的。
后续就简单了,拿到console里字体的宽度就行了。
这里贴一个大概的实现,宽度表放在最后
一个简单的lua实现
local _QuickJumpTab = {"Utils/GlobalFunction:8","Lua/HookReloadLuaFuc:35:print_table","Lua/HookReloadLuaFuc:1290:print_table","UI/Titan/TitanUtil:10:泰坦相关Gid",
}local function GetFileLine(str)local strTab = split(str,":")return unpack(strTab)
end-- 判断是否是中文字符 并且返回字符长度
local function IsChineseChar(char)local byte = string.byte(char)if byte >= 0x80 thenreturn true,2elsereturn false,1end
endlocal function Utf8StringLen(str)local len = 0local strTab = StringToUtf8Table(str)for i, v in ipairs(strTab) doif IsChineseChar(v) thenlen = len + 12elselen = len + _ConsoleCharWidth[v] or 3 -- 3 is width of spaceendendreturn len
endlocal longestStr = ""
for k, v in pairs(_QuickJumpTab) dolocal fileStr = GetFileLine(v)if Utf8StringLen(fileStr) > Utf8StringLen(longestStr) thenlongestStr = fileStrend
end
local longestStrLen = Utf8StringLen(longestStr)
--logGreen("longestStrLen\t" .. longestStrLen)local _wrapColor = function(color,str)return concat({"<color=",color,">",str,"</color>"})
end-- 自己实现一个往左边或者右边加空格补齐长度的函数
-- 用于打印的时候对齐
local FillLen = function(str,len,align,fillStr)align = align or "left"local strLen = Utf8StringLen(str)if strLen >= len thenreturn strendlocal spaceLen = len - strLenlocal needFillCount = max(spaceLen,1)if not fillStr thenfillStr = " "needFillCount = needFillCount/_ConsoleCharWidth[fillStr]endlocal spaceStr = string.rep(fillStr,ceil(needFillCount))if align == "right" thenreturn concat({str,spaceStr})elsereturn concat({spaceStr,str})end
endlocal exSymbolLen = _ConsoleCharWidth[":"] + _ConsoleCharWidth["["] + _ConsoleCharWidth["]"]local _WrapDebugStr = function(file,line,des,fileColor,lineColor)fileColor = fileColor or "#CA550C"--"cyan"lineColor = lineColor or "#00FFFF"local maxFileLen = longestStrLen+8local fileStr = FillLen(file,maxFileLen,"right")local lineStr = FillLen(":" .. line,36,"left")fileStr = _wrapColor(fileColor,fileStr)lineStr = _wrapColor(lineColor,lineStr)local desFillStr = "-"local fillCharWidth = _ConsoleCharWidth[desFillStr]maxFileLen = maxFileLen + 36 + exSymbolLenif des thenlocal colorLen = Utf8StringLen("<color=white></color>")des = _wrapColor("white",des)des = FillLen(des,(maxFileLen+colorLen)/fillCharWidth,"right",desFillStr)elsedes = FillLen("",maxFileLen/fillCharWidth,"left",desFillStr)endstr = format("[%s%s]\n%s",fileStr,lineStr,des)return str
end
_ConsoleCharWidth = {[" "] = 3,["!"] = 3,["\""] = 5,["#"] = 8,["$"] = 7,["%"] = 11,["&"] = 9,["'"] = 3,["("] = 4,[")"] = 4,["*"] = 6,["+"] = 8,[","] = 3,["-"] = 6, -- 4 original["."] = 3,["/"] = 4,[":"] = 3,[";"] = 3,["<"] = 8,["="] = 8,[">"] = 8,["?"] = 7,["@"] = 12,["["] = 4,["\\"] = 4,["]"] = 4,["^"] = 6,["_"] = 5,["`"] = 3,["{"] = 5,["|"] = 3,["}"] = 5,["~"] = 8,A = 8,B = 8,C = 9,D = 9,E = 7,F = 7,G = 9,H = 9,I = 3,J = 7,K = 8,L = 7,M = 11,N = 9,O = 9,P = 8,Q = 9,R = 8,S = 8,T = 8,U = 9,V = 8,W = 11,X = 8,Y = 8,Z = 8,a = 7,b = 7,c = 7,d = 7,e = 7,f = 4,g = 7,h = 7,i = 3,j = 3,k = 7,l = 3,m = 10,n = 7,o = 7,p = 7,q = 7,r = 4,s = 6,t = 4,u = 7,v = 7,w = 10,x = 6,y = 6,z = 6,["0"] = 8,["1"] = 6,["2"] = 7,["3"] = 8,["4"] = 8,["5"] = 7,["6"] = 7,["7"] = 7,["8"] = 7,["9"] = 7,
}