Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- print_utils.lua
- local print_utils = {}
- local level_colors = {
- info = colors.white,
- warn = colors.yellow,
- error = colors.red,
- debug = colors.gray
- }
- local function timestamp()
- local t = textutils.formatTime(os.time(), true)
- return "[" .. t .. "]"
- end
- local function raw_tostring(val)
- if type(val) == "string" then
- return '"' .. val .. '"'
- elseif type(val) == "boolean" then
- return val and "true" or "false"
- else
- return tostring(val)
- end
- end
- -- Affiche une structure complète (table) avec indentation
- local function dump_table(tbl, indent, visited)
- indent = indent or 0
- visited = visited or {}
- if visited[tbl] then
- return string.rep(" ", indent) .. "<circular reference>"
- end
- visited[tbl] = true
- local lines = {}
- table.insert(lines, string.rep(" ", indent) .. "{")
- for k, v in pairs(tbl) do
- local keyStr = "[" .. raw_tostring(k) .. "]"
- if type(v) == "table" then
- table.insert(lines, string.rep(" ", indent + 2) .. keyStr .. " = " .. dump_table(v, indent + 2, visited))
- else
- local valStr = raw_tostring(v)
- table.insert(lines, string.rep(" ", indent + 2) .. keyStr .. " = " .. valStr)
- end
- end
- table.insert(lines, string.rep(" ", indent) .. "}")
- return table.concat(lines, "\n")
- end
- function print_utils.dump(obj)
- if type(obj) == "table" then
- return dump_table(obj)
- else
- return raw_tostring(obj)
- end
- end
- function print_utils.print_msg(level, msg)
- if term.isColor then
- term.setTextColor(level_colors[level] or colors.white)
- end
- print(timestamp() .. " [" .. level:upper() .. "] " .. msg)
- if term.isColor then
- term.setTextColor(colors.white)
- end
- end
- function print_utils.info(msg)
- print_utils.print_msg("info", print_utils.dump(msg))
- end
- function print_utils.warn(msg)
- print_utils.print_msg("warn", print_utils.dump(msg))
- end
- function print_utils.error(msg)
- print_utils.print_msg("error", print_utils.dump(msg))
- end
- function print_utils.debug(msg)
- print_utils.print_msg("debug", print_utils.dump(msg))
- end
- return print_utils
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement