tablite v1.0.5
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
π License
MIT Β© swordfatih