mirror of
https://github.com/Oreolek/ink-instead.git
synced 2024-07-18 06:24:37 +03:00
60 lines
1.5 KiB
Lua
60 lines
1.5 KiB
Lua
function table_print (tt, done)
|
|
done = done or {}
|
|
if type(tt) == "table" then
|
|
local sb = {}
|
|
for key, value in pairs (tt) do
|
|
table.insert(sb, " ") -- indent it
|
|
if type (value) == "table" and not done [value] then
|
|
done [value] = true
|
|
table.insert(sb, "{");
|
|
table.insert(sb, table_print (value, done))
|
|
table.insert(sb, " ") -- indent it
|
|
table.insert(sb, "} ");
|
|
elseif "number" == type(key) then
|
|
table.insert(sb, string.format("%s,", tostring(value)))
|
|
else
|
|
table.insert(sb, string.format(
|
|
"%s = %s,", tostring (key), tostring(value)))
|
|
end
|
|
end
|
|
return table.concat(sb)
|
|
else
|
|
return tt .. "\n"
|
|
end
|
|
end
|
|
|
|
function to_string( tbl )
|
|
if "nil" == type( tbl ) then
|
|
return tostring(nil)
|
|
elseif "table" == type( tbl ) then
|
|
return table_print(tbl)
|
|
elseif "string" == type( tbl ) then
|
|
return tbl
|
|
else
|
|
return tostring(tbl)
|
|
end
|
|
end
|
|
|
|
function tprint (tbl, indent)
|
|
if not indent then indent = 0 end
|
|
if type(tbl)=='string' then print(tbl); return end
|
|
for k, v in pairs(tbl) do
|
|
formatting = string.rep(" ", indent) .. k .. ": "
|
|
if type(v) == "table" then
|
|
print(formatting .. 'table:')
|
|
tprint(v, indent+1)
|
|
elseif type(v) == 'boolean' then
|
|
print(formatting .. tostring(v))
|
|
else
|
|
print(formatting .. v)
|
|
end
|
|
end
|
|
end
|
|
|
|
function read(file)
|
|
local f = io.open(file, "rb")
|
|
local content = f:read("*all")
|
|
f:close()
|
|
return content
|
|
end
|