pipr.tools

csv-to-table

Render CSV/TSV as aligned ASCII table

⟐ Data

Try it

stdin0 chars
stdout0 chars

Example

Render CSV as aligned ASCII table

Usage
$ echo "name,role,city
Alice,admin,Paris
Bob,editor,London
Charlie,v..." | csv-to-table
View source
(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");
    }

Suggested Pipelines

Related Tools