tty-table: Terminal and Browser Table Formatter

5.0.0 · active · verified Sun Apr 19

tty-table is a versatile JavaScript library designed to render tabular data in various environments, including Node.js command-line interfaces, web browsers, and browser developer consoles. Currently at stable version 5.0.0, the library sees moderate release activity, with recent updates focusing on bug fixes and dependency management. Its key differentiators include extensive configuration options for headers, columns, and data rows, supporting custom formatters for cell values, and the ability to handle both static datasets and streaming data in terminal applications. Unlike some CLI table libraries, tty-table provides a unified API for both terminal and browser rendering, offering flexibility for developers building multi-platform tools or debugging in the browser console.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a basic table with custom headers, data (both array of arrays and array of objects), and rendering it to the console with styling and a custom formatter.

import { Table } from 'tty-table';

const header = [
  { value: "Name", align: "left", color: "green" },
  { value: "Age", align: "center", width: 5 },
  {
    value: "Occupation",
    formatter: (value: string) => value.toUpperCase(),
    color: "cyan",
    align: "right",
    width: 20
  }
];

const rows = [
  ["Alice", 30, "Software Engineer"],
  ["Bob", 24, "Data Scientist"],
  ["Charlie", 45, "Project Manager"],
  ["Diana", 28, "UX Designer"]
];

const options = {
  borderStyle: "solid",
  borderColor: "blue",
  headerAlign: "center",
  align: "left",
  width: "auto"
};

const t1 = new Table(header, rows, options);
console.log(t1.render());

// Example with a different data structure
const header2 = [
  { value: "Product", align: "left" },
  { value: "Price (USD)", align: "right",
    formatter: (price: number) => `$${price.toFixed(2)}`
  },
  { value: "In Stock", align: "center" }
];

const rows2 = [
  { product: "Laptop", price: 1200.50, inStock: true },
  { product: "Mouse", price: 25.99, inStock: false },
  { product: "Keyboard", price: 75.00, inStock: true }
];

const t2 = new Table(header2, rows2);
console.log(t2.render());

view raw JSON →