cli-table3 =============================================================================== [![npm version](https://img.shields.io/npm/v/cli-table3.svg)](https://www.npmjs.com/package/cli-table3) [![Build Status](https://github.com/cli-table/cli-table3/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/cli-table/cli-table3/actions/workflows/test.yml) This utility allows you to render unicode-aided tables on the command line from your node.js scripts. `cli-table3` is based on (and api compatible with) the original [cli-table](https://github.com/Automattic/cli-table), and [cli-table2](https://github.com/jamestalmage/cli-table2), which are both unmaintained. `cli-table3` includes all the additional features from `cli-table2`. ![Screenshot](https://i.imgur.com/sYq4T.png) ## Features not in the original cli-table - Ability to make cells span columns and/or rows. - Ability to set custom styles per cell (border characters/colors, padding, etc). - Vertical alignment (top, bottom, center). - [Word wrapping options](./basic-usage.md#set-wordwrap-to-true-to-wrap-text-on-word-boundaries). - More robust truncation of cell text that contains ansi color characters. - Better handling of text color that spans multiple lines. - API compatible with the original cli-table. - Exhaustive test suite including the entire original cli-table test suite. - Lots of examples auto-generated from the tests ([basic](https://github.com/cli-table/cli-table3/blob/master/basic-usage.md), [advanced](https://github.com/cli-table/cli-table3/blob/master/advanced-usage.md)). ## Features - Customizable characters that constitute the table. - [Color/background](#colors) styling the table through [colors.js](https://github.com/marak/colors.js) [ansis](https://github.com/webdiscus/ansis). - Column width customization - Text truncation based on predefined widths - Text alignment (left, right, center) - Padding (left, right) - Easy-to-use API ## Installation ```bash npm install cli-table3 ``` ## How to use A portion of the unit test suite is used to generate examples: - [basic-usage](https://github.com/cli-table/cli-table3/blob/master/basic-usage.md) - covers basic uses. - [advanced](https://github.com/cli-table/cli-table3/blob/master/advanced-usage.md) - covers using the new column and row span features. This package is api compatible with the original [cli-table](https://github.com/Automattic/cli-table). So all the original documentation still applies (copied below). ### Horizontal Tables ```javascript var Table = require('cli-table3'); // instantiate var table = new Table({ head: ['TH 1 label', 'TH 2 label'] , colWidths: [100, 200] }); // table is an Array, so you can `push`, `unshift`, `splice` and friends table.push( ['First value', 'Second value'] , ['First value', 'Second value'] ); console.log(table.toString()); ``` ### Vertical Tables ```javascript var Table = require('cli-table3'); var table = new Table(); table.push( { 'Some key': 'Some value' } , { 'Another key': 'Another value' } ); console.log(table.toString()); ``` ### Cross Tables Cross tables are very similar to vertical tables, with two key differences: 1. They require a `head` setting when instantiated that has an empty string as the first header 2. The individual rows take the general form of { "Header": ["Row", "Values"] } ```javascript var Table = require('cli-table3'); var table = new Table({ head: ["", "Top Header 1", "Top Header 2"] }); table.push( { 'Left Header 1': ['Value Row 1 Col 1', 'Value Row 1 Col 2'] } , { 'Left Header 2': ['Value Row 2 Col 1', 'Value Row 2 Col 2'] } ); console.log(table.toString()); ``` ### Custom styles The ```chars``` property controls how the table is drawn: ```javascript var table = new Table({ chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗' , 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝' , 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼' , 'right': '║' , 'right-mid': '╢' , 'middle': '│' } }); table.push( ['foo', 'bar', 'baz'] , ['frob', 'bar', 'quuz'] ); console.log(table.toString()); // Outputs: // //╔══════╤═════╤══════╗ //║ foo │ bar │ baz ║ //╟──────┼─────┼──────╢ //║ frob │ bar │ quuz ║ //╚══════╧═════╧══════╝ ``` Empty decoration lines will be skipped, to avoid vertical separator rows just set the 'mid', 'left-mid', 'mid-mid', 'right-mid' to the empty string: ```javascript var table = new Table({ chars: {'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''} }); table.push( ['foo', 'bar', 'baz'] , ['frobnicate', 'bar', 'quuz'] ); console.log(table.toString()); // Outputs: (note the lack of the horizontal line between rows) //┌────────────┬─────┬──────┐ //│ foo │ bar │ baz │ //│ frobnicate │ bar │ quuz │ //└────────────┴─────┴──────┘ ``` By setting all chars to empty with the exception of 'middle' being set to a single space and by setting padding to zero, it's possible to get the most compact layout with no decorations: ```javascript var table = new Table({ chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': '' , 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': '' , 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': '' , 'right': '' , 'right-mid': '' , 'middle': ' ' }, style: { 'padding-left': 0, 'padding-right': 0 } }); table.push( ['foo', 'bar', 'baz'] , ['frobnicate', 'bar', 'quuz'] ); console.log(table.toString()); // Outputs: //foo bar baz //frobnicate bar quuz ``` ## Colors You can colorize the text in the table head and the table border with 16 named colors or with truecolor using a hex code. The color and styles can be combined. ### Base 16 colors and styles See the full list of [base named colors and styles](https://github.com/webdiscus/ansis#base-ansi-16-colors-and-styles). ### Truecolor To apply a custom `truecolor`, use the format `hex(CODE)` for foreground and `bgHex(CODE)` for background, where `CODE` is a 3- or 6-digit hexadecimal code, e.g., `hex(#FFA500)`, `hex(#A7E)`, `bgHex(#FF69B4)`, `bgHex(#49B)`. ### Colorizing table head Use the `style.head[]` option to style text in the table header. For example, the header text in base color `green` and styled as `bold` : ```js var table = new Table({ head: ['Name', 'Age'], style: { head: ['green', 'bold'], } }); table.push(['Walter White', '50']); console.log(table.toString()); ``` Outputs: ![Screenshot](https://i.imgur.com/mUNLz6l.png) For example, the header text in truecolor `orange` and styled as `italic`: ```js style: { head: ['hex(#FFA500)', 'italic'], } ``` ![Screenshot](https://i.imgur.com/mVieAvj.png) ### Colorizing table border Use the `style.border[]` option to specify a border color. For example, the header text in truecolor `orange` and the border in truecolor `gold`: ```js style: { border: ['hex(#FFD700)'], head: ['hex(#FFA500)', 'italic'], } ``` ![Screenshot](https://i.imgur.com/NWzB3nL.png) ### Colorizing table cells To colorize cells can be used [ansis](https://github.com/webdiscus/ansis), which is already included in this package as an optional dependency. For example, apply an individual color to the single cell in the table body: ```js var ansi = require('ansis'); var Table = require('cli-table3'); var table = new Table({ head: ['Name', 'Age'], style: { border: ['hex(#FFD700)'], head: ['hex(#FFA500)', 'italic'], } }); table.push( // apply a color to cells [ansi.green('Walter White'), ansi.red('50')], [ansi.hex('#FF69B4')('Jesse Pinkman'), ansi.blueBright('24')], ); console.log(table.toString()); ``` Outputs: ![Screenshot](https://i.imgur.com/EhnOn9i.png) For example, colorize the table body with one color: ```js var { green } = require('ansis'); // use a named import when using a few colors var Table = require('cli-table3'); var table = new Table({ head: ['Name', 'Age'], style: { border: ['hex(#FFD700)'], head: ['hex(#FFA500)', 'italic'], } }); table.push( ['Walter White', '50'], ['Jesse Pinkman', '24'], ); var output = green(table.toString()); // colorize the whole table console.log(output); ``` Outputs: ![Screenshot](https://i.imgur.com/TGUUzsB.png) ### Colorizing table background To specify the background color you can use a color name with the `bg` prefix, e.g., `bgGreen`, `bgGreenBright` or `gbHex()` for truecolor. For example, add a background color to the example above: ```js var { green } = require('ansis'); var Table = require('cli-table3'); var table = new Table({ head: ['Name', 'Age'], style: { border: ['hex(#FFD700)'], head: ['hex(#FFA500)', 'italic'], } }); table.push( ['Walter White', '50'], ['Jesse Pinkman', '24'], ); var output = green.bgHex('#3d239d')(table.toString()); // <- add a background to the table console.log(output); ``` Outputs: ![Screenshot](https://i.imgur.com/SY5ikmJ.png) ## Debugging Later versions of cli-table3 supporting debugging your table data. Enable and use debugging: ``` var table = new Table({ debug: 1 }); table.push([{}, {},}); // etc. console.log(table.toString()); table.messages.forEach((message) => console.log(message)); ``` If you are rendering multiple tables with debugging on run `Table.reset()` after rendering each table. ## Build Targets Clone the repository and run `yarn install` to install all its submodules, then run one of the following commands: ###### Run the tests with coverage reports. ```bash $ yarn test:coverage ``` ###### Run the tests every time a file changes. ```bash $ yarn test:watch ``` ###### Update the documentation. ```bash $ yarn docs ``` ## Credits - James Talmage - author <james.talmage@jrtechnical.com> ([jamestalmage](https://github.com/jamestalmage)) - Guillermo Rauch - author of the original cli-table <guillermo@learnboost.com> ([Rauchg](https://github.com/rauchg)) ## License (The MIT License) Copyright (c) 2014 James Talmage <james.talmage@jrtechnical.com> Original cli-table code/documentation: Copyright (c) 2010 LearnBoost <dev@learnboost.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.