Merge remote-tracking branch 'lethosor/lua-printall-ipairs'

develop
expwnent 2014-09-16 17:08:45 -04:00
commit d425c2edf9
2 changed files with 17 additions and 1 deletions

@ -167,6 +167,15 @@ function printall(table)
end end
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
end
end
function copyall(table) function copyall(table)
local rv = {} local rv = {}
for k,v in pairs(table) do rv[k] = v end for k,v in pairs(table) do rv[k] = v end
@ -286,6 +295,7 @@ function dfhack.interpreter(prompt,hfile,env)
" '= foo' => '_1,_2,... = foo'\n".. " '= foo' => '_1,_2,... = foo'\n"..
" '! foo' => 'print(foo)'\n".. " '! foo' => 'print(foo)'\n"..
" '~ foo' => 'printall(foo)'\n".. " '~ foo' => 'printall(foo)'\n"..
" '@ foo' => 'printall_ipairs(foo)'\n"..
"All of these save the first result as '_'.") "All of these save the first result as '_'.")
print_banner = false print_banner = false
end end
@ -305,6 +315,10 @@ function dfhack.interpreter(prompt,hfile,env)
print(table.unpack(data,2,data.n)) print(table.unpack(data,2,data.n))
printall(data[2]) printall(data[2])
end, end,
['@'] = function(data)
print(table.unpack(data,2,data.n))
printall_ipairs(data[2])
end,
['='] = function(data) ['='] = function(data)
for i=2,data.n do for i=2,data.n do
local varname = '_'..vcnt local varname = '_'..vcnt

@ -23,7 +23,7 @@ elseif cmd=="--save" or cmd=="-s" then
elseif cmd~=nil then elseif cmd~=nil then
-- Support some of the prefixes allowed by dfhack.interpreter -- Support some of the prefixes allowed by dfhack.interpreter
local prefix local prefix
if string.match(cmd, "^[~!]") then if string.match(cmd, "^[~@!]") then
prefix = string.sub(cmd, 1, 1) prefix = string.sub(cmd, 1, 1)
cmd = 'return '..string.sub(cmd, 2) cmd = 'return '..string.sub(cmd, 2)
end end
@ -39,6 +39,8 @@ elseif cmd~=nil then
print(table.unpack(rv,2,rv.n)) print(table.unpack(rv,2,rv.n))
if prefix == '~' then if prefix == '~' then
printall(rv[2]) printall(rv[2])
elseif prefix == '@' then
printall_ipairs(rv[2])
end end
end end
else else