Skip to content

Instantly share code, notes, and snippets.

@notanimposter
Created June 15, 2020 07:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save notanimposter/0904d5fec451b1a7824faf273e370eaa to your computer and use it in GitHub Desktop.
A function for displaying tables... prettily
function utf8_length(str)
return select(2, str:gsub('[^\128-\193]', ''))
end
function box_reverse(str)
local map = {
[""] = "",
[""] = "",
[""] = "",
[""] = "",
[""] = "",
[""] = "",
[""] = "",
[""] = ""
}
return str:gsub('[%z\1-\127\194-\244][\128-\191]*', function(c)
return #c > 1 and map[c]:reverse()
end):reverse()
end
function prettyPrint(o, soFar)
local soFar = soFar or {}
if type(o) == 'table' and not soFar[tostring(o)] and o ~= circuit then
soFar[tostring(o)] = true
local s = "\n"
for k,v in pairs(o) do
--if type(k) ~= 'number' then k = '"'..k..'"' end
s = s.."\n"..tostring(k).." : "..prettyPrint(v, soFar):gsub('\n','\n')
end
s = s.."\n"..""
local longest = 0
for sub in s:gmatch('\n[^\n]+') do
local l = utf8_length(sub)
if l > longest then
longest = l
end
end
s = s:gsub('\n(%w-)(\226?\148?\140?\148?\130?)([^\n]*)', function(aa,a,b)
local r = " "
if a:find("\226\148[\140\148]") and aa == "" then
r = ""
end
return "\n"..a..b..string.rep(r, longest-utf8_length(a..a..b))..box_reverse(a)
end)
return s
else
return tostring(o)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment