Version 0.5.0
- add replacements via options UI - restructure debug functions
This commit is contained in:
@@ -2,58 +2,17 @@
|
||||
local _G = _G
|
||||
local Grichelde = _G.Grichelde
|
||||
|
||||
local type, print, pairs, select, unpack, format, rep, toString
|
||||
= Grichelde.functions.type, Grichelde.functions.print, Grichelde.functions.pairs, Grichelde.functions.select, Grichelde.functions.unpack, Grichelde.functions.format, Grichelde.functions.rep, Grichelde.functions.toString
|
||||
|
||||
function Grichelde:Format(message, ...)
|
||||
local msg = message
|
||||
local l = select("#", ...)
|
||||
if l > 0 then
|
||||
-- sanitize nil values in vararg
|
||||
local packed = { ... }
|
||||
for i = 1, l do
|
||||
packed[i] = toString(packed[i]) or "nil"
|
||||
end
|
||||
-- print("packed = ", packed)
|
||||
-- self:tPrint(packed)
|
||||
-- cannot assign unpacked to a vararg variable and print it for debug
|
||||
msg = format(message, unpack(packed))
|
||||
end
|
||||
return msg or "nil"
|
||||
end
|
||||
|
||||
function Grichelde:Print(...)
|
||||
print(self:Format(...))
|
||||
end
|
||||
|
||||
local function prefixedPrint(colorCode, prefix, endClose, ...)
|
||||
print(colorCode .. prefix .. endClose .. ": " .. ...)
|
||||
end
|
||||
|
||||
function Grichelde:PrefixedPrint(...)
|
||||
prefixedPrint(self.COLOR_CODES.PREFIX, self.L.AddonName, self.COLOR_CODES.CLOSE, self:Format(...))
|
||||
end
|
||||
|
||||
function Grichelde:DebugPrint(...)
|
||||
if (self.debug) then
|
||||
prefixedPrint(self.COLOR_CODES.GRAY, self.L.AddonName, self.COLOR_CODES.CLOSE, self:Format(...))
|
||||
end
|
||||
end
|
||||
|
||||
local function tLen(t)
|
||||
local count = 0
|
||||
for _ in pairs(t) do count = count + 1 end
|
||||
return count
|
||||
end
|
||||
local type, print, pairs, tSize, select, unpack, find, format, rep, toString
|
||||
= Grichelde.functions.type, Grichelde.functions.print, Grichelde.functions.pairs, Grichelde.functions.tSize, Grichelde.functions.select, Grichelde.functions.unpack, Grichelde.functions.find, Grichelde.functions.format, Grichelde.functions.rep, Grichelde.functions.toString
|
||||
|
||||
-- show strings differently to distinguish them from numbers
|
||||
function Grichelde:PlainValue(val)
|
||||
local function plainValue(val)
|
||||
if val == nil then
|
||||
return "<nil>"
|
||||
elseif type(val) == "string" then
|
||||
return '"' .. val .. '"'
|
||||
elseif type(val) == "table" then
|
||||
if tLen(val) > 0 then
|
||||
if tSize(val) > 0 then
|
||||
return toString(val)
|
||||
else
|
||||
return "{}"
|
||||
@@ -64,40 +23,134 @@ function Grichelde:PlainValue(val)
|
||||
end
|
||||
|
||||
--- Prints any value to default channel, do NOT return a string.
|
||||
function Grichelde:tPrint(val, indent, known)
|
||||
if (not self.debug) then return end
|
||||
|
||||
local function tPrint(val, indent, known, printFunc)
|
||||
local printF = printFunc or print
|
||||
indent = indent or 0
|
||||
known = known or {}
|
||||
|
||||
if val == nil then
|
||||
print(rep(" ", indent) .. "<nil>")
|
||||
printF(rep(" ", indent) .. "<nil>")
|
||||
elseif type(val) == "string" then
|
||||
print(rep(" ", indent) .. "\"" .. val .. "\"")
|
||||
printF(rep(" ", indent) .. "\"" .. val .. "\"")
|
||||
elseif type(val) == "table" then
|
||||
if tLen(val) > 0 then
|
||||
if tSize(val) > 0 then
|
||||
for key, value in pairs(val) do
|
||||
if value == nil then
|
||||
print(rep(" ", indent) .. self:PlainValue(key) .. "= <nil>")
|
||||
printF(rep(" ", indent) .. plainValue(key) .. "= <nil>")
|
||||
elseif type(value) == "table" then
|
||||
print(rep(" ", indent) .. self:PlainValue(key) .. "= {")
|
||||
if tLen(value) > 0 then
|
||||
printF(rep(" ", indent) .. plainValue(key) .. "= {")
|
||||
if tSize(value) > 0 then
|
||||
if not known[value] then
|
||||
self:tPrint(value, indent + 4, known)
|
||||
tPrint(value, indent + 4, known, printF)
|
||||
known[value] = true
|
||||
else
|
||||
print("<known table> " .. self:PlainValue(value))
|
||||
printF("<known table> " .. plainValue(value))
|
||||
end
|
||||
end
|
||||
print(rep(" ", indent) .. "}")
|
||||
printF(rep(" ", indent) .. "}")
|
||||
else
|
||||
print(rep(" ", indent) .. self:PlainValue(key) .. " = " .. self:PlainValue(value))
|
||||
printF(rep(" ", indent) .. plainValue(key) .. " = " .. plainValue(value))
|
||||
end
|
||||
end
|
||||
else
|
||||
print(rep(" ", indent) .. "{}")
|
||||
printF(rep(" ", indent) .. "{}")
|
||||
end
|
||||
else
|
||||
print(rep(" ", indent) .. toString(val))
|
||||
printF(rep(" ", indent) .. toString(val))
|
||||
end
|
||||
end
|
||||
|
||||
-- split at first word of a text line
|
||||
function Grichelde:SplitOnFirstMatch(text, delimPattern, start)
|
||||
if text == nil then return nil end
|
||||
local pattern = "^(.-)" .. (delimPattern or " " ) .."(.*)"
|
||||
local pos = start or 1
|
||||
self:DebugPrint("SplitOnFirstMatch : text: %s, pattern: %s, start: %d", text, pattern, start)
|
||||
local _, _, left, right = find(text, pattern, pos)
|
||||
self:DebugPrint("SplitOnFirstMatch : left: %s, right: %s", left, right)
|
||||
return left or text, right
|
||||
end
|
||||
|
||||
-- split at last word of a text line
|
||||
function Grichelde:SplitOnLastMatch(text, delimPattern, start)
|
||||
local pattern = "(.*)" .. (delimPattern or " ") .. "(.-)$"
|
||||
local pos = start or 1
|
||||
self:DebugPrint("SplitOnLastMatch : text: %s, pattern: %s, start: %d", text, pattern, start)
|
||||
local _, _, left, right = find(text, pattern, pos)
|
||||
self:DebugPrint("SplitOnLastMatch : left: %s, right: %s", left, right)
|
||||
return left, right or text
|
||||
end
|
||||
|
||||
-- split at last word of a text line
|
||||
function Grichelde:TestMatch(text, pattern)
|
||||
local _, _, left, right = find(text, pattern, 1)
|
||||
self:DebugPrint("TestMatch : left: %s, right: %s", left, right)
|
||||
end
|
||||
|
||||
function Grichelde:Format(message, ...)
|
||||
if ( not message ) then
|
||||
return "<nil>"
|
||||
elseif type(message) == "string" then
|
||||
if ( not find(message, "%%")) then
|
||||
return message, ...
|
||||
else
|
||||
local l = select("#", ...)
|
||||
if l > 0 then
|
||||
-- sanitize nil values in vararg
|
||||
local packed = { ... }
|
||||
for i = 1, l do
|
||||
packed[i] = toString(packed[i]) or "nil"
|
||||
end
|
||||
-- print("packed = ", packed)
|
||||
-- self:tPrint(packed)
|
||||
-- cannot assign unpacked to a vararg variable and print it for debug
|
||||
-- Manually set count as unpack() stops on nil (bug with #table)
|
||||
return format(message, unpack(packed, 1, l))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- deprecated
|
||||
function Grichelde:Print(...)
|
||||
print(self:Format(...))
|
||||
end
|
||||
|
||||
function Grichelde:PrefixedPrint(...)
|
||||
print(self.COLOR_CODES.PREFIX .. self.L.AddonName .. self.COLOR_CODES.CLOSE .. ":", self:Format(...))
|
||||
end
|
||||
|
||||
function Grichelde:DebugPrint(obj, ...)
|
||||
local function prefixedDebugPrint(...)
|
||||
print(self.COLOR_CODES.GRAY .. self.L.AddonName .. self.COLOR_CODES.CLOSE .. ":", self:Format(...))
|
||||
end
|
||||
|
||||
if (self.debug) then
|
||||
if obj == nil then
|
||||
prefixedDebugPrint("<nil>")
|
||||
else
|
||||
if type(obj) == "string" then
|
||||
local l = select("#", ...)
|
||||
if ( l == 0 or not find(obj, "%%")) then
|
||||
prefixedDebugPrint(obj, ...)
|
||||
else
|
||||
-- sanitize nil values in vararg
|
||||
local packed = { ... }
|
||||
for i = 1, l do
|
||||
packed[i] = toString(packed[i]) or "nil"
|
||||
end
|
||||
|
||||
-- print("packed = ", packed)
|
||||
-- self:tPrint(packed)
|
||||
-- cannot assign unpacked to a vararg variable and print it for debug
|
||||
local fmtMsg = format(obj, unpack(packed, 1, l)) -- manually set count as unpack() stops on nil (bug with #table)
|
||||
prefixedDebugPrint(fmtMsg)
|
||||
end
|
||||
elseif type(obj) == "table" then
|
||||
tPrint(obj, 0, {}, prefixedDebugPrint)
|
||||
else
|
||||
prefixedDebugPrint(plainValue(obj))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user