Skip to content

Instantly share code, notes, and snippets.

@magicoal-nerb
Created March 3, 2025 09:02
Show Gist options
  • Save magicoal-nerb/4f4d9f79acc2a03a2b00659eb9204737 to your computer and use it in GitHub Desktop.
Save magicoal-nerb/4f4d9f79acc2a03a2b00659eb9204737 to your computer and use it in GitHub Desktop.
local function pprintHelper(buff, value, depth, mem)
-- >_>
local t = type(value);
if(t == "string")then
-- String
table.insert(buff, '"');
table.insert(buff, value);
return table.insert(buff, '"');
elseif(t == "number")then
-- Number
return table.insert(buff, string.format("%.3f", value));
elseif(t ~= "table")then
-- Not a table
return table.insert(buff, tostring(t));
elseif(not select(2, next(value)))then
-- Empty
return table.insert(buff, '{ }');
elseif(#value ~= 0)then
-- List
table.insert(buff, '{ ');
local length = #value;
for i = 1, length - 1 do
pprintHelper(buff, value[i], depth, mem);
table.insert(buff, ', ');
end
pprintHelper(buff, value[length], depth, mem);
return table.insert(buff, ' }');
end
table.insert(buff, '{\n');
mem[value] = true;
-- Dictionary
local tab = string.rep('\t', depth);
for name, v in pairs(value)do
table.insert(buff, tab);
pprintHelper(buff, name, depth+1, mem);
table.insert(buff, ': ');
if(type(v) == "table" and mem[v])then
table.insert(buff, '**cyclic reference ');
table.insert(buff, tostring(v));
table.insert(buff, ' **');
else
pprintHelper(buff, v, depth + 1, mem);
end
table.insert(buff, '\n');
end
table.insert(buff, string.rep('\t', depth-1));
table.insert(buff, '}');
end
return function(x)
local buff = {};
pprintHelper(buff, x, 1, {});
print(table.concat(buff));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment