Render CSV/TSV as aligned ASCII table
⟐ DataRender CSV as aligned ASCII table
$ echo "name,role,city
Alice,admin,Paris
Bob,editor,London
Charlie,v..." | csv-to-table (input) => {
const lines = input.trim().split("\n");
if (!lines.length) return "";
const delim = lines[0].includes("\t") ? "\t" : ",";
const rows = lines.map((l) => l.split(delim).map((c) => c.trim()));
const cols = Math.max(...rows.map((r) => r.length));
const widths = Array(cols).fill(0);
for (const row of rows)
for (let i = 0; i < cols; i++)
widths[i] = Math.max(widths[i], (row[i] || "").length);
const fmt = (row) =>
row.map((c, i) => (c || "").padEnd(widths[i])).join(" \u2502 ");
const sep = widths
.map((w) => "\u2500".repeat(w))
.join("\u2500\u253C\u2500");
const result = [fmt(rows[0]), sep];
for (let i = 1; i < rows.length; i++) result.push(fmt(rows[i]));
return result.join("\n");
}