UNPKG

5.82 kBMarkdownView Raw
1CLI Table [![NPM Version](http://badge.fury.io/js/cli-table.svg)](http://badge.fury.io/js/cli-table) [![Build Status](https://secure.travis-ci.org/Automattic/cli-table.svg)](http://travis-ci.org/Automattic/cli-table)
2=========
3
4This utility allows you to render unicode-aided tables on the command line from
5your node.js scripts.
6
7![Screenshot](http://i.imgur.com/sYq4T.png)
8
9## Features
10
11- Customizable characters that constitute the table.
12- Color/background styling in the header through
13 [colors.js](http://github.com/marak/colors.js)
14- Column width customization
15- Text truncation based on predefined widths
16- Text alignment (left, right, center)
17- Padding (left, right)
18- Easy-to-use API
19
20## Installation
21
22```bash
23npm install cli-table
24```
25
26## How to use
27
28### Horizontal Tables
29```javascript
30var Table = require('cli-table');
31
32// instantiate
33var table = new Table({
34 head: ['TH 1 label', 'TH 2 label']
35 , colWidths: [100, 200]
36});
37
38// table is an Array, so you can `push`, `unshift`, `splice` and friends
39table.push(
40 ['First value', 'Second value']
41 , ['First value', 'Second value']
42);
43
44console.log(table.toString());
45```
46
47### Initializing rows in the constructore
48
49Optionally you can initialize rows in the constructors directly - comes in handy for smaller tables:
50
51```
52new Table({
53 rows: [
54 ['foo', '7 minutes ago']
55 , ['bar', '8 minutes ago']
56 ]
57})
58```
59
60### Vertical Tables
61```javascript
62var Table = require('cli-table');
63var table = new Table();
64
65table.push(
66 { 'Some key': 'Some value' }
67 , { 'Another key': 'Another value' }
68);
69
70console.log(table.toString());
71```
72### Cross Tables
73Cross tables are very similar to vertical tables, with two key differences:
74
751. They require a `head` setting when instantiated that has an empty string as the first header
762. The individual rows take the general form of { "Header": ["Row", "Values"] }
77
78```javascript
79var Table = require('cli-table');
80var table = new Table({ head: ["", "Top Header 1", "Top Header 2"] });
81
82table.push(
83 { 'Left Header 1': ['Value Row 1 Col 1', 'Value Row 1 Col 2'] }
84 , { 'Left Header 2': ['Value Row 2 Col 1', 'Value Row 2 Col 2'] }
85);
86
87console.log(table.toString());
88```
89
90### Custom styles
91The ```chars``` property controls how the table is drawn:
92```javascript
93var table = new Table({
94 chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
95 , 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
96 , 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
97 , 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
98});
99
100table.push(
101 ['foo', 'bar', 'baz']
102 , ['frob', 'bar', 'quuz']
103);
104
105console.log(table.toString());
106// Outputs:
107//
108//╔══════╤═════╤══════╗
109//║ foo │ bar │ baz ║
110//╟──────┼─────┼──────╢
111//║ frob │ bar │ quuz ║
112//╚══════╧═════╧══════╝
113```
114
115Empty decoration lines will be skipped, to avoid vertical separator rows just
116set the 'mid', 'left-mid', 'mid-mid', 'right-mid' to the empty string:
117```javascript
118var table = new Table({ chars: {'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''} });
119table.push(
120 ['foo', 'bar', 'baz']
121 , ['frobnicate', 'bar', 'quuz']
122);
123
124console.log(table.toString());
125// Outputs: (note the lack of the horizontal line between rows)
126//┌────────────┬─────┬──────┐
127//│ foo │ bar │ baz │
128//│ frobnicate │ bar │ quuz │
129//└────────────┴─────┴──────┘
130```
131
132By setting all chars to empty with the exception of 'middle' being set to a
133single space and by setting padding to zero, it's possible to get the most
134compact layout with no decorations:
135```javascript
136var table = new Table({
137 chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': ''
138 , 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': ''
139 , 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': ''
140 , 'right': '' , 'right-mid': '' , 'middle': ' ' },
141 style: { 'padding-left': 0, 'padding-right': 0 }
142});
143
144table.push(
145 ['foo', 'bar', 'baz']
146 , ['frobnicate', 'bar', 'quuz']
147);
148
149console.log(table.toString());
150// Outputs:
151//foo bar baz
152//frobnicate bar quuz
153```
154
155## Running tests
156
157Clone the repository with all its submodules and run:
158
159```bash
160$ make test
161```
162
163## Credits
164
165- Guillermo Rauch <guillermo@learnboost.com> ([Guille](http://github.com/guille))
166
167[![huntr](https://cdn.huntr.dev/huntr_security_badge_mono.svg)](https://huntr.dev)
168
169## License
170
171(The MIT License)
172
173Copyright (c) 2010 LearnBoost <dev@learnboost.com>
174
175Permission is hereby granted, free of charge, to any person obtaining
176a copy of this software and associated documentation files (the
177'Software'), to deal in the Software without restriction, including
178without limitation the rights to use, copy, modify, merge, publish,
179distribute, sublicense, and/or sell copies of the Software, and to
180permit persons to whom the Software is furnished to do so, subject to
181the following conditions:
182
183The above copyright notice and this permission notice shall be
184included in all copies or substantial portions of the Software.
185
186THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
187EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
188MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
189IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
190CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
191TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
192SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.