2022-11-23 22:44:46 +01:00
|
|
|
-- Various global functions to cause less typing.
|
|
|
|
|
|
|
|
|
|
|
|
function assertf(bool, fmt, ...)
|
|
|
|
fmt = fmt or "Assetion Failed"
|
|
|
|
if not bool then
|
|
|
|
error(string.format(fmt,...),2)
|
|
|
|
end
|
|
|
|
end
|
2023-03-12 04:31:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
function errorf(str, ...)
|
|
|
|
--try calling string.format, if it errors, try calling it with exactly
|
|
|
|
--1 less argument (the error level)
|
|
|
|
local args = {...}
|
|
|
|
local succ, ret = pcall(string.format,str,...)
|
|
|
|
if not succ and type(args[#args]) ~= "number" then
|
|
|
|
errorf("Failed displaying error that looks like %q",str)
|
|
|
|
elseif type(args[#args]) == "number" then
|
|
|
|
local errlevel = table.remove(args,#args)
|
|
|
|
local succ2, ret = pcall(string.format,str,unpack(args))
|
|
|
|
if not succ2 then
|
|
|
|
errorf("Failed displaying error that looks like %q",str)
|
|
|
|
end
|
|
|
|
error(ret, errlevel+1)
|
|
|
|
end
|
|
|
|
error(ret,2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local oldtostring = tostring
|
|
|
|
function tostring(any)
|
|
|
|
--Pretty print tables by default
|
|
|
|
local printed_tables = {}
|
|
|
|
local function tostring_helper(a,tabs)
|
|
|
|
if type(a) ~= "table" then
|
|
|
|
return oldtostring(a)
|
|
|
|
end
|
|
|
|
if printed_tables[a] then
|
|
|
|
return oldtostring(a)
|
|
|
|
end
|
|
|
|
printed_tables[a] = true
|
|
|
|
local sbuilder = {"{\n"}
|
|
|
|
for k,v in pairs(a) do
|
|
|
|
table.insert(sbuilder,string.rep("\t",tabs))
|
|
|
|
table.insert(sbuilder,tostring_helper(k,tabs+1))
|
|
|
|
table.insert(sbuilder,":")
|
|
|
|
table.insert(sbuilder,tostring_helper(v,tabs+1))
|
|
|
|
table.insert(sbuilder,"\n")
|
|
|
|
end
|
|
|
|
table.insert(sbuilder,string.rep("\t",tabs-1))
|
|
|
|
table.insert(sbuilder,"}")
|
|
|
|
return table.concat(sbuilder)
|
|
|
|
end
|
|
|
|
return tostring_helper(any,1)
|
|
|
|
end
|