Skip to content

Instantly share code, notes, and snippets.

@magicoal-nerb
Last active July 20, 2025 10:01
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.
-- pprint.lua
-- suprisingly fast for being a non-tail call optimized
-- pretty printer. huh neat
-- poopbarrel/magicoal_nerb :^)
local function pprintHelper(buff, value, depth)
-- >_>
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)
table.insert(buff, ', ')
end
pprintHelper(buff, value[length], depth)
return table.insert(buff, ' }')
end
table.insert(buff, '{\n')
-- dictionary
local tab = string.rep('\t', depth)
for name, v in pairs(value)do
table.insert(buff, tab)
pprintHelper(buff, name, depth+1)
table.insert(buff, ': ')
pprintHelper(buff, v, depth + 1)
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