Jump to content

Module:Debug utilities

From HopperWiki

Documentation for this module may be created at Module:Debug utilities/doc

--[[
Module:Debug utilities
Part of the Module:Utilities family.

Developer tools for inspecting and debugging Lua tables and module output.
No Cargo queries, no wiki output dependencies.
All functions are re-exported through Module:Utilities for backward compatibility.

Functions:
  print_table   Recursively serialize a Lua table to a human-readable string
  warning_box   Render a styled warning/error box for display on wiki pages
--]]


local dbg = {}


-- ============================================================
-- print_table
-- ============================================================

--[[
Recursively serializes a Lua table into a readable indented string.
Non-table values are returned as plain tostring() output.
Useful for mw.log() debugging during module development.

Example output for {a = 1, b = {c = 2}}:
  a = 1
  b = {
    c = 2
  }
--]]
function dbg.print_table(t, indent)
    indent = indent or ""
    if type(t) ~= "table" then
        return tostring(t)
    end

    local lines = {}
    for k, v in pairs(t) do
        if type(v) == "table" then
            table.insert(lines, indent .. tostring(k) .. " = {")
            table.insert(lines, dbg.print_table(v, indent .. "  "))
            table.insert(lines, indent .. "}")
        else
            table.insert(lines, indent .. tostring(k) .. " = " .. tostring(v))
        end
    end
    return table.concat(lines, "\n")
end


-- ============================================================
-- warning_box
-- ============================================================

-- Renders a styled mw-message-box warning div with a bold title and body text.
-- Canonical copy lives here; Module:Wiki output utilities re-exports this
-- function from the facade rather than maintaining its own copy.
function dbg.warning_box(title, msg)
    local box = mw.html.create("div")
        :addClass("mw-message-box mw-message-box-warning")
        :cssText("margin:0.8em 0;")
    box:tag("strong"):wikitext(title):done()
    box:tag("div")
        :cssText("margin-top:0.4em; white-space:pre-wrap;")
        :wikitext(msg)
    return tostring(box)
end


return dbg
Cookies help us deliver our services. By using our services, you agree to our use of cookies.