Thierry FOURNIER | 4d7bfa1 | 2018-02-25 21:28:05 +0100 | [diff] [blame] | 1 | function color(index, str) |
| 2 | return "\x1b[" .. index .. "m" .. str .. "\x1b[00m" |
| 3 | end |
| 4 | |
| 5 | function nocolor(index, str) |
| 6 | return str |
| 7 | end |
| 8 | |
| 9 | function sp(count) |
| 10 | local spaces = "" |
| 11 | while count > 0 do |
| 12 | spaces = spaces .. " " |
| 13 | count = count - 1 |
| 14 | end |
| 15 | return spaces |
| 16 | end |
| 17 | |
| 18 | function print_rr(p, indent, c, wr) |
| 19 | local i = 0 |
| 20 | local nl = "" |
| 21 | |
| 22 | if type(p) == "table" then |
| 23 | wr(c("33", "(table)") .. " " .. c("34", tostring(p)) .. " [") |
| 24 | |
| 25 | mt = getmetatable(p) |
| 26 | if mt ~= nil then |
| 27 | wr("\n" .. sp(indent+1) .. c("31", "METATABLE") .. ": ") |
| 28 | print_rr(mt, indent+1, c, wr) |
| 29 | end |
| 30 | |
| 31 | for k,v in pairs(p) do |
| 32 | if i > 0 then |
| 33 | nl = "\n" |
| 34 | else |
| 35 | wr("\n") |
| 36 | end |
| 37 | wr(nl .. sp(indent+1)) |
| 38 | if type(k) == "number" then |
| 39 | wr(c("32", tostring(k))) |
| 40 | else |
| 41 | wr("\"" .. c("32", tostring(k)) .. "\"") |
| 42 | end |
| 43 | wr(": ") |
| 44 | print_rr(v, indent+1, c, wr) |
| 45 | i = i + 1 |
| 46 | end |
| 47 | if i == 0 then |
| 48 | wr(" " .. c("35", "/* empty */") .. " ]") |
| 49 | else |
| 50 | wr("\n" .. sp(indent) .. "]") |
| 51 | end |
| 52 | elseif type(p) == "string" then |
| 53 | wr(c("33", "(string)") .. " \"" .. c("34", p) .. "\"") |
| 54 | else |
| 55 | wr(c("33", "(" .. type(p) .. ")") .. " " .. c("34", tostring(p))) |
| 56 | end |
| 57 | end |
| 58 | |
| 59 | function print_r(p, col, wr) |
| 60 | if col == nil then col = true end |
| 61 | if wr == nil then wr = function(msg) io.stdout:write(msg) end end |
| 62 | if col == true then |
| 63 | print_rr(p, 0, color, wr) |
| 64 | else |
| 65 | print_rr(p, 0, nocolor, wr) |
| 66 | end |
| 67 | wr("\n") |
| 68 | end |