1.0.5 β€’ Published 3 years ago

tablite v1.0.5

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

tablite

Turn your data into a modern text table.


Table of Contents


☁️ Installation

npm install tablite --save

πŸ“‹ Example

Check out the examples folder for more.

const Table = require('tablite');

let options = {size: 100, header: true, ratios: 10, 10, align: 'l,c'};

let data = [ 'Color', 'hex', 'Description', ['Green', '#008000', Seeing the colour \u001b[32mgreen\u001b[0m has been linked to more creative thinkingβ€”so greens are good options for home offices.], 'Violet', '#0000FF', People link a greyish violet with sophistication, so it can be a good selection for places where you’re trying to make the β€œright” impression., 'Blue', '#EE82EE', People are more likely to tell you that blue is their favourite colour than any other shade.\n That makes it a safe choice.];

console.log(new Table(data, options).toString());

```js
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Color   β”‚   hex   β”‚ Description                                                                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Green   β”‚ #008000 β”‚ Seeing the colour green has been linked to more creative thinkingβ€”so greens β”‚
β”‚         β”‚         β”‚ are good options for home offices.                                          β”‚
β”‚         β”‚         β”‚                                                                             β”‚
β”‚ Violet  β”‚ #0000FF β”‚ People link a greyish violet with sophistication, so it can be a good       β”‚
β”‚         β”‚         β”‚ selection for places where you’re trying to make the β€œright” impression.    β”‚
β”‚         β”‚         β”‚                                                                             β”‚
β”‚ Blue    β”‚ #EE82EE β”‚ People are more likely to tell you that blue is their favourite colour than β”‚
β”‚         β”‚         β”‚ any other shade.                                                            β”‚
β”‚         β”‚         β”‚ That makes it a safe choice.                                                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Documentation

Every table used in this documentation was generated with tablite itself.

Methods

Here are a list of the simple methods to help you.

╒═════════════╀═════════════════════════════════════════════════════════════════════╀════════════════════════════════╕
β”‚    Method   β”‚ Documentation                                                       β”‚ Usage example                  β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════β•ͺ════════════════════════════════║
β”‚ constructor β”‚ Constructor of the table. Generates an empty table by default.      β”‚ console.log(new Table(data,    β”‚
β”‚             β”‚ Takes two optional parameters: data and options.                    β”‚ options).toString());          β”‚
β”‚             β”‚ You can directly build your table with these.                       β”‚                                β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════β•ͺ════════════════════════════════║
β”‚     set     β”‚ Set new options. Takes your option structure as parameter.          β”‚ let table = new Table();       β”‚
β”‚             β”‚ Optional: you can disable regeneration with the second parameter.   β”‚ table.set(options);            β”‚
β”‚             β”‚ (see options section for more informations)                         β”‚                                β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════β•ͺ════════════════════════════════║
β”‚    input    β”‚ Set new data. Takes your data structure as parameter.               β”‚ let table = new Table();       β”‚
β”‚             β”‚ Optional: you can disable regeneration with the second parameter.   β”‚ table.input(data);             β”‚
β”‚             β”‚ (see options section for more informations)                         β”‚                                β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════β•ͺ════════════════════════════════║
β”‚   generate  β”‚ Regenerate your table. Don't forget to set options and input data   β”‚ table.generate();              β”‚
β”‚             β”‚ first.                                                              β”‚                                β”‚
β”‚             β”‚ Returns the generated table array. (see toString() for string       β”‚                                β”‚
β”‚             β”‚ version)                                                            β”‚                                β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════β•ͺ════════════════════════════════║
β”‚    split    β”‚ Split your table into different arrays of a specified maximum       β”‚ console.log(table.split(50)[0  β”‚
β”‚             β”‚ character size.                                                     β”‚ ].join('\n'));                 β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════β•ͺ════════════════════════════════║
β”‚   toString  β”‚ Convert the table into a printable string.                          β”‚ console.log(table.toString()); β”‚
β•˜β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•›

Options

You can give multiple options to customize your table.

Here are several ways to structure your options, they would all give the same output as the previous example code:

╒══════════╀═════════════════════════════════════════════════════════════════════════════════════════════════╕
β”‚   Type   β”‚ Usage example                                                                                   β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════════════════════║
β”‚  Object  β”‚ let options = new Object();                                                                     β”‚
β”‚          β”‚ options.size = 100;                                                                             β”‚
β”‚          β”‚ options.header = true;                                                                          β”‚
β”‚          β”‚ options.ratios = [10, 10];                                                                      β”‚
β”‚          β”‚ options.align = 'l,c';                                                                          β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════════════════════║
β”‚   JSON   β”‚ let options = JSON.parse('{"size": 100, "header": true, "ratios": [10, 10], "align": "l,c"}');  β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════════════════════║
β”‚    Map   β”‚ let options = new Map();                                                                        β”‚
β”‚          β”‚ options.set('size', 100);                                                                       β”‚
β”‚          β”‚ options.set('header', true);                                                                    β”‚
β”‚          β”‚ options.set('ratios', [10, 10]);                                                                β”‚
β”‚          β”‚ options.set('align', 'l,c');                                                                    β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════════════════════║
β”‚   Array  β”‚ let options = {size: 100, header: true, ratios: [10, 10], align: 'l,c'};                        β”‚
β•˜β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•›

Here is a list of the available options and their documentation:

╒════════════╀═════════════════════════════════════════════════════════════════════════════════╀═══════════════════════════════╕
β”‚   Option   β”‚ Documentation                                                                   β”‚ Data                          β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════β•ͺ═══════════════════════════════║
β”‚    size    β”‚ Specify the maximum character count of the rows.                                β”‚ Integer (might be given as a  β”‚
β”‚            β”‚ The real size might be 1 character less, depends on column count's multiples.   β”‚ string)                       β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════β•ͺ═══════════════════════════════║
β”‚   header   β”‚ Toggle the header line.                                                         β”‚ Boolean                       β”‚
β”‚            β”‚ Useless if gaps are filled (see gap option).                                    β”‚                               β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════β•ͺ═══════════════════════════════║
β”‚   border   β”‚ Specify the border you like, single-line is used by default.                    β”‚ String containing border name β”‚
β”‚            β”‚ (See borders section for available borders)                                     β”‚                               β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════β•ͺ═══════════════════════════════║
β”‚     gap    β”‚ Specify the separation between rows.                                            β”‚ String containg separation    β”‚
β”‚            β”‚ (See gaps section for available separations)                                    β”‚ name                          β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════β•ͺ═══════════════════════════════║
β”‚    align   β”‚ Specify columns' aligns. If only one align is given, it is applied to every     β”‚ Array of strings or one       β”‚
β”‚            β”‚ columns.                                                                        β”‚ string separated by commas    β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════β•ͺ═══════════════════════════════║
β”‚   ratios   β”‚ Specify columns' ratio. For example: [10,10] will apply 10% to the first        β”‚ Array of percentages or one   β”‚
β”‚            β”‚ column, 10% to the second and it will split the remaining 80% between remaining β”‚ string separated by commas or β”‚
β”‚            β”‚ columns if they exist.                                                          β”‚ an integer as percentage      β”‚
β”‚            β”‚ Otherwise they will all be added to the last column.                            β”‚                               β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•ͺ═════════════════════════════════════════════════════════════════════════════════β•ͺ═══════════════════════════════║
β”‚    ansi    β”‚ The table adapt a row's width when it contains an ANSI command.                 β”‚ Boolean                       β”‚
β”‚            β”‚ Disable this option if your terminal doesn't recognize ANSI commands.           β”‚                               β”‚
β•˜β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•›

Borders

Wonder how the following table-ception was generated using tablite? Check out the examples folder.

       Name            double-line           single-line               dot                 rounded              classical               simple                modern               inversed                none         

Demonstration ╔════════╦════════╗ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β” ................... .--------.--------. +--------+--------+ ======== ======== ╒════════╀════════╕ ╓────────β•₯────────╖
β•‘ I β•‘ love β•‘ β”‚ I β”‚ love β”‚ . I . love . | I | love | | I | love | I love β”‚ I β”‚ love β”‚ β•‘ I β•‘ love β•‘ I love
╠════════╬════════╣ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€ ................... :--------+--------: +--------+--------+ ======== ======== β•žβ•β•β•β•β•β•β•β•β•ͺ════════║ β•Ÿβ”€β”€β”€β”€β”€β”€β”€β”€β•«β”€β”€β”€β”€β”€β”€β”€β”€β•’
β•‘ Tab β•‘ lite β•‘ β”‚ Tab β”‚ lite β”‚ . Tab . lite . | Tab | lite | | Tab | lite | Tab lite β”‚ Tab β”‚ lite β”‚ β•‘ Tab β•‘ lite β•‘ Tab lite
β•šβ•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β• β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ................... '--------'--------' +--------+--------+ ======== ======== β•˜β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•› β•™β”€β”€β”€β”€β”€β”€β”€β”€β•¨β”€β”€β”€β”€β”€β”€β”€β”€β•œ

#### Gaps
Gaps are the separation lines between rows. Here is a list of the available ones:
```js
        Name                 space                    fill                     small                    none

   Demonstration      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚    I   β”‚  love  β”‚      β”‚    I   β”‚  love  β”‚      β”‚    I   β”‚  love  β”‚      β”‚    I   β”‚  love  β”‚
                      β”‚        β”‚        β”‚      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€      β”œ        β”‚        ─      β”‚   Tab  β”‚  lite  β”‚
                      β”‚   Tab  β”‚  lite  β”‚      β”‚   Tab  β”‚  lite  β”‚      β”‚   Tab  β”‚  lite  β”‚      β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜

New line character and template literals

You can use the \n character wherever you like in your data. It works. You can also use the template literals (`...`) for your complex cells.

const Table = require('tablite');

const data = [['I', 'love'], 
['Tablite', `Here is a very
complex cell that will print 
on several lines..`]];

console.log(new Table(data).toString());

ANSI

ANSI commands such as colors are also supported: the table's size won't be changed. Howewer, I can't predict if every ANSI commands work with tablite. Just keep in mind that if you use color commands in your table, make sure there are on a full row; otherwise colors will spread on the borders.

πŸ… Badges

npm version Build Status Known Vulnerabilities Dependencies

πŸ“œ License

MIT Β© swordfatih

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago