Version 0.4.0
- restructured files - extract functions and color codes - filter channels
This commit is contained in:
103
GricheldeUtils.lua
Normal file
103
GricheldeUtils.lua
Normal file
@@ -0,0 +1,103 @@
|
||||
-- import addon read namespace from global env
|
||||
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
|
||||
|
||||
-- show strings differently to distinguish them from numbers
|
||||
function Grichelde: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
|
||||
return toString(val)
|
||||
else
|
||||
return "{}"
|
||||
end
|
||||
else
|
||||
return toString(val)
|
||||
end
|
||||
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
|
||||
|
||||
indent = indent or 0
|
||||
known = known or {}
|
||||
|
||||
if val == nil then
|
||||
print(rep(" ", indent) .. "<nil>")
|
||||
elseif type(val) == "string" then
|
||||
print(rep(" ", indent) .. "\"" .. val .. "\"")
|
||||
elseif type(val) == "table" then
|
||||
if tLen(val) > 0 then
|
||||
for key, value in pairs(val) do
|
||||
if value == nil then
|
||||
print(rep(" ", indent) .. self:PlainValue(key) .. "= <nil>")
|
||||
elseif type(value) == "table" then
|
||||
print(rep(" ", indent) .. self:PlainValue(key) .. "= {")
|
||||
if tLen(value) > 0 then
|
||||
if not known[value] then
|
||||
self:tPrint(value, indent + 4, known)
|
||||
known[value] = true
|
||||
else
|
||||
print("<known table> " .. self:PlainValue(value))
|
||||
end
|
||||
end
|
||||
print(rep(" ", indent) .. "}")
|
||||
else
|
||||
print(rep(" ", indent) .. self:PlainValue(key) .. " = " .. self:PlainValue(value))
|
||||
end
|
||||
end
|
||||
else
|
||||
print(rep(" ", indent) .. "{}")
|
||||
end
|
||||
else
|
||||
print(rep(" ", indent) .. toString(val))
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user