Improve ilua's table output for tables that have both an array part and additional keys

Previously for such tables it would just show the array part and ignore the rest.
This commit is contained in:
Celtic Minstrel 2019-11-17 13:57:36 -05:00
parent 61d0da9c84
commit ece5490fd9

View File

@ -9,7 +9,6 @@
local pretty_print_limit = 20
local max_depth = 7
local table_clever = true
-- imported global functions
local sub = string.sub
@ -39,15 +38,7 @@ function ilua.join(tbl,delim,limit,depth)
end
end
push(jstack,tbl)
-- this is a hack to work out if a table is 'list-like' or 'map-like'
-- you can switch it off with ilua.table_options {clever = false}
local is_list
if table_clever then
local index1 = n > 0 and tbl[1]
local index2 = n > 1 and tbl[2]
is_list = index1 and index2
end
if is_list then
if n > 0 then
for i,v in ipairs(tbl) do
res = res..delim..ilua.val2str(v)
k = k + 1
@ -56,20 +47,21 @@ function ilua.join(tbl,delim,limit,depth)
break
end
end
else
for key,v in pairs(tbl) do
if type(key) == 'number' then
key = '['..tostring(key)..']'
else
key = tostring(key)
end
res = res..delim..key..'='..ilua.val2str(v)
k = k + 1
if k > limit then
res = res.." ... "
break
end
end
for key,v in pairs(tbl) do
if type(key) == 'number' then
if key <= n then goto continue end
key = '['..tostring(key)..']'
else
key = tostring(key)
end
res = res..delim..key..'='..ilua.val2str(v)
k = k + 1
if k > limit then
res = res.." ... "
break
end
::continue::
end
pop(jstack)
return sub(res,2)
@ -92,6 +84,8 @@ function ilua.val2str(val)
else
return '{'..ilua.join(val,',')..'}'
end
--elseif tp == 'userdata' then
elseif tp == 'string' then
return "'"..val.."'"
elseif tp == 'number' then