printall and printall_ipairs handle all iterables

develop
myk002 2021-08-06 21:05:40 -07:00
parent 4606d5742e
commit 9fc71ef6e1
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
1 changed files with 24 additions and 21 deletions

@ -162,22 +162,28 @@ NEWLINE = "\n"
COMMA = ","
PERIOD = "."
function printall(table)
if type(table) ~= 'table' then print(tostring(table)) return end
local ok,f,t,k = pcall(pairs,table)
if ok then
for k,v in f,t,k do
-- calls elem_cb(k, v) for each element of the table
-- returns true if we iterated successfully, false if not
local function safe_iterate(table, iterate_fn, elem_cb)
local function iterate()
for k,v in iterate_fn(table) do elem_cb(k, v) end
end
return pcall(iterate)
end
local function print_element(k, v)
print(string.format("%-23s\t = %s", tostring(k), tostring(v)))
end
function printall(table)
if not safe_iterate(table, pairs, print_element) then
print(tostring(table))
end
end
function printall_ipairs(table)
local ok,f,t,k = pcall(ipairs,table)
if ok then
for k,v in f,t,k do
print(string.format("%-23s\t = %s",tostring(k),tostring(v)))
end
if not safe_iterate(table, ipairs, print_element) then
print(tostring(table))
end
end
@ -200,15 +206,9 @@ local fill_chars = {
setmetatable(fill_chars, fill_chars)
local function print_fields(value, seen, indent, prefix)
local ok,f,t,k = pcall(pairs,value)
if not ok then
dfhack.print(prefix)
dfhack.println('<Type doesn\'t support iteration with pairs>')
return 0
end
local prev_value = "not a value"
local repeated = 0
for k, v in f,t,k do
local print_field = function(k, v)
-- Only show set values of bitfields
if value._kind ~= "bitfield" or v then
local continue = false
@ -234,7 +234,10 @@ local function print_fields(value, seen, indent, prefix)
end
end
end
if repeated > 0 then
if not safe_iterate(value, pairs, print_field) then
dfhack.print(prefix)
dfhack.println('<Type doesn\'t support iteration with pairs>')
elseif repeated > 0 then
dfhack.println(prefix .. "<Repeated " .. repeated .. " times>")
end
return 0
@ -535,11 +538,11 @@ function dfhack.interpreter(prompt,hfile,env)
end,
['~'] = function(data)
print(table.unpack(data,2,data.n))
if type(data[2]) == 'table' then printall(data[2]) end
printall(data[2])
end,
['@'] = function(data)
print(table.unpack(data,2,data.n))
if type(data[2]) == 'table' then printall_ipairs(data[2]) end
printall_ipairs(data[2])
end,
['^'] = function(data)
printall_recurse(data[2])