Created
June 15, 2020 07:36
Star
You must be signed in to star a gist
A function for displaying tables... prettily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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