Lua aligned plain-text table renderer
Lua
local function render_table(headers, rows)
local w = {}
for i, h in ipairs(headers) do w[i] = #tostring(h) end
for _, row in ipairs(rows) do
for i, cell in ipairs(row) do w[i] = math.max(w[i] or 0, #tostring(cell)) end
end
local function fmt(cells)
local parts = {}
for i, cell in ipairs(cells) do
parts[i] = tostring(cell) .. string.rep(' ', w[i] - #tostring(cell) + 2)
end
return (table.concat(parts):gsub('%s+$', ''))
end
local dashes = {}
for i = 1, #headers do dashes[i] = string.rep('-', w[i]) end
local out = { fmt(headers), fmt(dashes) }
for _, row in ipairs(rows) do out[#out + 1] = fmt(row) end
return table.concat(out, '\n')
end
return render_table