table-writer
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

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       |                   |                    |
+-----------+-------------------+--------------------+

Package Sidebar

Install

npm i table-writer

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

11.5 kB

Total Files

13

Last publish

Collaborators

  • homoluctus