0.9.3 ā€¢ Published 2 years ago

dmt-table v0.9.3

Weekly downloads
3
License
ISC
Repository
github
Last release
2 years ago

dmtsys table

(nodejs, cli)

A simple and modern node.js library for nice tables in all modern computer terminals šŸ’»

How to create a basic table?

import Table from 'dmt-table';

const table = new Table();

const headers = ['country', 'code'];
table.push(headers);

table.push(Table.divider);

table.push(['United States of America', 'USA']);
table.push(['China', 'CH']);
table.push(['Russia', 'RU']);

table.push(Table.divider);

table.push(['Germany', 'DE']);
table.push(['France', 'FR']);
table.push(['United Kingdom', 'UK']);

table.push(Table.divider);

table.push(['Argentina', 'AR']);
table.push(['Mexico', 'MX']);

console.log(table.toString());

Output:

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”
ā”‚ country                  ā”‚ code ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ United States of America ā”‚ USA  ā”‚
ā”‚ China                    ā”‚ CH   ā”‚
ā”‚ Russia                   ā”‚ RU   ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ Germany                  ā”‚ DE   ā”‚
ā”‚ France                   ā”‚ FR   ā”‚
ā”‚ United Kingdom           ā”‚ UK   ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ Argentina                ā”‚ AR   ā”‚
ā”‚ Mexico                   ā”‚ MX   ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

See ./examples folder for more runnable examples.

For fancier tables please use another library

šŸ’” Recommended: table JS library.

See an example output at hodlings cli project which uses table.

With this great library you can specify separate column styles and many more. No sure if it can do simple dividers.

dmt-table is for really basic tables and it also supports dividers.

Based on

šŸ™ cli-table2 which is based on šŸ™ cli-table

This library dmt-table is a rewrite of cli-table2 to modern ES6 JavaScript with added new features like easy to use table dividers (horizontal line between table sections).

This library has no tests because they are the same as in cli-table2 and nothing critical was changed. Maybe tests will be added here in the future on further feature developments.

šŸ†• Dividers:

table.push(Table.divider);

How it's implemented

Very elegantly! Thank you for asking. A precision hack was used.

These three steps are the gist of the hack:

Divider class:

class Divider {}

export default Divider;

Expose it directly through Table class:

Object.defineProperty(Table, 'divider', { value: new Divider() });

Split Divider row into the correct number of Divider cells:

function generateCells(rows) {
  //ā€¦

  if (row instanceof Divider) {
    const numCells = rows[0].length;

    // Create an array with same element repeated multiple times:
    // https://stackoverflow.com/a/34104348/458177
    return Array(numCells)
      .fill(new Divider())
      .map(cell => {
        return new Cell(cell);
      });
  }

  //ā€¦
}
0.9.3

2 years ago

0.9.2

2 years ago

0.9.1

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago