0.1.1 • Published 3 years ago

table-writer v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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
    • rows0 is treated as table header
  • options
options
RequiredDefaultDescription
styleNosimplePredefined table style. See following Table Styles
formatNoN/ACustomize table style.
widthNoprocess.stdout.columnsMaximum 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       |                   |                    |
+-----------+-------------------+--------------------+