/table-writer

Generate ASCII Table

Primary LanguageTypeScriptMIT LicenseMIT

Table Writer

Generate ASCII table

GitHub Workflow Status GitHub npm node-current

Features

  • Automatic wrap
    • Line breaks if the number of words is greater than terminal width
  • Automatic padding
  • Choose predefined table styles
    • simple
    • grid

Installtion

npm i table-writer

or

yarn add table-writer

Usage

Basic

import { Table } from 'table-writer';

const table = new Table([
  ['header1', 'header2', 'header3'],
  ['row11', 'row12', 'row13'],
  ['row21', 'row22', 'row33']
]);
console.log(table.write());
 header1  header2  header3
---------------------------
 row11    row12    row13
 row21    row22    row33

Change table style

import { Table } from 'table-writer';

const table = new Table([
  ['header1', 'header2', 'header3'],
  ['row11', 'row12', 'row13'],
  ['row21', 'row22', 'row33']
], { style: 'grid' });
console.log(table.write());

API

class Table

constructor(rows [, options])

  • rows
    • string[][] (TableRows)
    • text context of table
    • rows[0][0] is treated as table header
  • options
    • object (TableOptions)
    • see options
options
Required Default Description
style No simple Predefined table style. See following Table Styles
format No N/A Customize table style.
width No process.stdout.columns Maximum terminal width.

add(row): void

Add a row to existing table rows. Treat table header if no talbe rows.

  • row
    • string[] (TableRow)
    • a row of table

write(): string

Render table.

Table Styles

Simple

 A          AAAAAAAAA          AAAAAAAAAA
--------------------------------------------------
 BBBBBBBBB  BBBBBBBBBBBBBBBBB  BBBBBBBBBBBBBBBBBB
 BB         BB                 BB
 CCCCCCCCC  CCCCCCCCCCCCCCCCC  CCCCCCCCCCCCCCCCCC
 CCCCCCCCC  CCCCCCCCCCCC       CCCCCCCCCCCC
 CCC

Grid

+-----------+-------------------+--------------------+
| A         | AAAAAAAAA         | AAAAAAAAAA         |
+===========+===================+====================+
| BBBBBBBBB | BBBBBBBBBBBBBBBBB | BBBBBBBBBBBBBBBBBB |
| BB        | BB                | BB                 |
+-----------+-------------------+--------------------+
| CCCCCCCCC | CCCCCCCCCCCCCCCCC | CCCCCCCCCCCCCCCCCC |
| CCCCCCCCC | CCCCCCCCCCCC      | CCCCCCCCCCCC       |
| CCC       |                   |                    |
+-----------+-------------------+--------------------+