-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprint.lua
More file actions
91 lines (82 loc) · 3.05 KB
/
Copy pathprint.lua
File metadata and controls
91 lines (82 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--
-- Wrappers and helpers for print
--
local luaprint = _G.print
local luatype = mineunit.utils and mineunit.utils.luatype or _G.type
function mineunit:prepend_print(s)
self._prepend_output = s
end
function mineunit:prepend_flush()
if self._prepend_output then
io.stdout:write(self._prepend_output)
self._prepend_output = nil
end
end
local function printwrapper(...)
mineunit:prepend_flush()
luaprint(...)
end
local formatters = {
["nil"] = tostring,
["xnil"] = tostring,
--luacheck: push ignore 561 cyclomatic complexity
["table"] = function(thing)
local above = rawget(thing, "above")
local under = rawget(thing, "under")
if luatype(under) == "table" or luatype(above) == "table" then
local thingtype = rawget(thing, "type")
local ref = rawget(thing, "ref")
return "{"..
(thingtype ~= nil and "\n\ttype = "..tostring(thingtype) or "")..
(above ~= nil and "\n\tabove = "..(above
and "{x="..above.x..",y="..above.y..",z="..above.z.."}"
or tostring(above)
) or "")..
(under ~= nil and "\n\tunder = "..(under
and "{x="..under.x..",y="..under.y..",z="..under.z.."}"
or tostring(under)
) or "")..
(ref ~= nil and "\n\tref = "..tostring(ref) or "")
.."\n}"
elseif mineunit.utils.is_coordinate(thing) then
return mineunit.utils.format_coordinate(thing)
end
return tostring(thing)
end,
--luacheck: pop
["xtable"] = tostring,
["number"] = tostring,
["xnumber"] = tostring,
["boolean"] = tostring,
["xboolean"] = tostring,
}
local function fmtprint(fmtstr, ...)
local args = {...}
local matcher = fmtstr:gmatch("%%(.)")
local index = 0
for argtype in matcher do
index = index + 1
local t = luatype(args[index])
if formatters[t] then
if argtype == "s" then
args[index] = formatters[t](args[index])
elseif argtype == "x" then
args[index] = formatters["x"..t](args[index])
elseif argtype == "t" then
args[index] = dump(args[index])
end
end
end
return printwrapper(fmtstr:gsub("%%t", "%%s"):format(unpack(args)))
end
function mineunit:debug(...) if self:config("verbose") > 3 then printwrapper("D:",...) end end
function mineunit:info(...) if self:config("verbose") > 2 then printwrapper("I:",...) end end
function mineunit:warning(...) if self:config("verbose") > 1 then printwrapper("W:",...) end end
function mineunit:error(...) if self:config("verbose") > 0 then printwrapper("E:",...) end end
function mineunit:print(...) if self:config("print") then printwrapper(...) end end
function mineunit:debugf(fmtstr, ...) if self:config("verbose") > 3 then fmtprint("D: "..fmtstr,...) end end
function mineunit:infof(fmtstr, ...) if self:config("verbose") > 2 then fmtprint("I: "..fmtstr,...) end end
function mineunit:warningf(fmtstr, ...) if self:config("verbose") > 1 then fmtprint("W: "..fmtstr,...) end end
function mineunit:errorf(fmtstr, ...) if self:config("verbose") > 0 then fmtprint("E: "..fmtstr,...) end end
function mineunit:printf(fmtstr, ...) if self:config("print") then fmtprint(fmtstr,...) end end
_G.print = function(...) mineunit:print(...) end