起因:做了个工具在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 then
return true,2
else
return false,1
end
end
local function Utf8StringLen(str)
local len = 0
local strTab = StringToUtf8Table(str)
for i, v in ipairs(strTab) do
if IsChineseChar(v) then
len = len + 12
else
len = len + _ConsoleCharWidth[v] or 3 -- 3 is width of space
end
end
return len
end
local longestStr = ""
for k, v in pairs(_QuickJumpTab) do
local fileStr = GetFileLine(v)
if Utf8StringLen(fileStr) > Utf8StringLen(longestStr) then
longestStr = fileStr
end
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 then
return str
end
local spaceLen = len - strLen
local needFillCount = max(spaceLen,1)
if not fillStr then
fillStr = " "
needFillCount = needFillCount/_ConsoleCharWidth[fillStr]
end
local spaceStr = string.rep(fillStr,ceil(needFillCount))
if align == "right" then
return concat({str,spaceStr})
else
return concat({spaceStr,str})
end
end
local exSymbolLen = _ConsoleCharWidth[":"] + _ConsoleCharWidth["["] + _ConsoleCharWidth["]"]
local _WrapDebugStr = function(file,line,des,fileColor,lineColor)
fileColor = fileColor or "#CA550C"
--"cyan"
lineColor = lineColor or "#00FFFF"
local maxFileLen = longestStrLen+8
local 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 + exSymbolLen
if des then
local colorLen = Utf8StringLen("<color=white></color>")
des = _wrapColor("white",des)
des = FillLen(des,(maxFileLen+colorLen)/fillCharWidth,"right",desFillStr)
else
des = FillLen("",maxFileLen/fillCharWidth,"left",desFillStr)
end
str = 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,
}