wip
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
-- Private -----------------------------------------------------------------------------------------
|
||||
|
||||
local private = {}
|
||||
|
||||
--[[
|
||||
Creates a diagnostic string from a given object.
|
||||
|
||||
This function recursively explores tables trying to show as much detail as possible within the
|
||||
limits dictated by its arguments. For anything other than a table, it simply uses the results
|
||||
of calling `tostring` with the object in question.
|
||||
|
||||
The output is pretty printed for ease of reading using a new line for each key/value pair, and
|
||||
indenting successive layers by four spaces.
|
||||
|
||||
Args:
|
||||
value:any - the value to be dumped
|
||||
depth:int - the maximum layers of nested tables to explore (10 by default)
|
||||
indent:string - the initial indent to use at the first layer
|
||||
max_keys:int - the maximum number of keys to print for a single table (100 by default)
|
||||
seen:table[any,true] - a table containing all the values already seen during a single,
|
||||
recursive print attempt (to avoid cycles or duplication)
|
||||
|
||||
Returns:
|
||||
a string containing the pretty-printed version of the given object
|
||||
]]--
|
||||
function private.dump(value, depth, indent, max_keys, seen, current_indent)
|
||||
current_indent = current_indent or ""
|
||||
depth = depth or 10
|
||||
indent = indent or ""
|
||||
max_keys = max_keys or 100
|
||||
seen = seen or {}
|
||||
|
||||
local newline = ((indent == "") and "") or "\n"
|
||||
|
||||
if depth == 0 then return "<too deep: "..tostring(value)..">" end
|
||||
|
||||
if seen[value] == true then return "<already seen: "..tostring(value)..">" end
|
||||
seen[value] = true
|
||||
|
||||
local result = ""
|
||||
if type(value) == "table" then
|
||||
result = result.."{"..newline
|
||||
local inner_indent = current_indent..indent
|
||||
local key_count = 0
|
||||
for inner_key, inner_value in pairs(value) do
|
||||
if key_count >= max_keys then
|
||||
result = result..inner_indent.."<too many keys>"..newline
|
||||
break
|
||||
end
|
||||
|
||||
local inner_text = private.dump(
|
||||
inner_value, depth - 1, indent, max_keys, seen, inner_indent
|
||||
)
|
||||
result = result..inner_indent..inner_key.." = "..inner_text..", "..newline
|
||||
key_count = key_count + 1
|
||||
end
|
||||
result = result..current_indent.."}"
|
||||
else
|
||||
result = tostring(value)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
-- Export -----------------------------------------------------------------------------------------
|
||||
|
||||
_G.jvmi.dump = private.dump
|
||||
Reference in New Issue
Block a user