-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprint.lua
85 lines (76 loc) · 2.94 KB
/
print.lua
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
--
-- 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,
["table"] = function(thing)
if luatype(thing.under) == "table" or luatype(thing.above) == "table" then
return "{"..
(thing.type ~= nil and "\n\ttype = "..tostring(thing.type) or "")..
(thing.above ~= nil and "\n\tabove = "..(thing.above
and "{x="..thing.above.x..",y="..thing.above.y..",z="..thing.above.z.."}"
or tostring(thing.above)
) or "")..
(thing.under ~= nil and "\n\tunder = "..(thing.under
and "{x="..thing.under.x..",y="..thing.under.y..",z="..thing.under.z.."}"
or tostring(thing.under)
) or "")..
(thing.ref ~= nil and "\n\tref = "..tostring(thing.ref) or "")
.."\n}"
elseif mineunit.utils.is_coordinate(thing) then
return "{x="..thing.x..",y="..thing.y..",z="..thing.z.."}"
end
return tostring(thing)
end,
["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