1 | # text-table
|
---|
2 |
|
---|
3 | generate borderless text table strings suitable for printing to stdout
|
---|
4 |
|
---|
5 | [![build status](https://secure.travis-ci.org/substack/text-table.png)](http://travis-ci.org/substack/text-table)
|
---|
6 |
|
---|
7 | [![browser support](https://ci.testling.com/substack/text-table.png)](http://ci.testling.com/substack/text-table)
|
---|
8 |
|
---|
9 | # example
|
---|
10 |
|
---|
11 | ## default align
|
---|
12 |
|
---|
13 | ``` js
|
---|
14 | var table = require('text-table');
|
---|
15 | var t = table([
|
---|
16 | [ 'master', '0123456789abcdef' ],
|
---|
17 | [ 'staging', 'fedcba9876543210' ]
|
---|
18 | ]);
|
---|
19 | console.log(t);
|
---|
20 | ```
|
---|
21 |
|
---|
22 | ```
|
---|
23 | master 0123456789abcdef
|
---|
24 | staging fedcba9876543210
|
---|
25 | ```
|
---|
26 |
|
---|
27 | ## left-right align
|
---|
28 |
|
---|
29 | ``` js
|
---|
30 | var table = require('text-table');
|
---|
31 | var t = table([
|
---|
32 | [ 'beep', '1024' ],
|
---|
33 | [ 'boop', '33450' ],
|
---|
34 | [ 'foo', '1006' ],
|
---|
35 | [ 'bar', '45' ]
|
---|
36 | ], { align: [ 'l', 'r' ] });
|
---|
37 | console.log(t);
|
---|
38 | ```
|
---|
39 |
|
---|
40 | ```
|
---|
41 | beep 1024
|
---|
42 | boop 33450
|
---|
43 | foo 1006
|
---|
44 | bar 45
|
---|
45 | ```
|
---|
46 |
|
---|
47 | ## dotted align
|
---|
48 |
|
---|
49 | ``` js
|
---|
50 | var table = require('text-table');
|
---|
51 | var t = table([
|
---|
52 | [ 'beep', '1024' ],
|
---|
53 | [ 'boop', '334.212' ],
|
---|
54 | [ 'foo', '1006' ],
|
---|
55 | [ 'bar', '45.6' ],
|
---|
56 | [ 'baz', '123.' ]
|
---|
57 | ], { align: [ 'l', '.' ] });
|
---|
58 | console.log(t);
|
---|
59 | ```
|
---|
60 |
|
---|
61 | ```
|
---|
62 | beep 1024
|
---|
63 | boop 334.212
|
---|
64 | foo 1006
|
---|
65 | bar 45.6
|
---|
66 | baz 123.
|
---|
67 | ```
|
---|
68 |
|
---|
69 | ## centered
|
---|
70 |
|
---|
71 | ``` js
|
---|
72 | var table = require('text-table');
|
---|
73 | var t = table([
|
---|
74 | [ 'beep', '1024', 'xyz' ],
|
---|
75 | [ 'boop', '3388450', 'tuv' ],
|
---|
76 | [ 'foo', '10106', 'qrstuv' ],
|
---|
77 | [ 'bar', '45', 'lmno' ]
|
---|
78 | ], { align: [ 'l', 'c', 'l' ] });
|
---|
79 | console.log(t);
|
---|
80 | ```
|
---|
81 |
|
---|
82 | ```
|
---|
83 | beep 1024 xyz
|
---|
84 | boop 3388450 tuv
|
---|
85 | foo 10106 qrstuv
|
---|
86 | bar 45 lmno
|
---|
87 | ```
|
---|
88 |
|
---|
89 | # methods
|
---|
90 |
|
---|
91 | ``` js
|
---|
92 | var table = require('text-table')
|
---|
93 | ```
|
---|
94 |
|
---|
95 | ## var s = table(rows, opts={})
|
---|
96 |
|
---|
97 | Return a formatted table string `s` from an array of `rows` and some options
|
---|
98 | `opts`.
|
---|
99 |
|
---|
100 | `rows` should be an array of arrays containing strings, numbers, or other
|
---|
101 | printable values.
|
---|
102 |
|
---|
103 | options can be:
|
---|
104 |
|
---|
105 | * `opts.hsep` - separator to use between columns, default `' '`
|
---|
106 | * `opts.align` - array of alignment types for each column, default `['l','l',...]`
|
---|
107 | * `opts.stringLength` - callback function to use when calculating the string length
|
---|
108 |
|
---|
109 | alignment types are:
|
---|
110 |
|
---|
111 | * `'l'` - left
|
---|
112 | * `'r'` - right
|
---|
113 | * `'c'` - center
|
---|
114 | * `'.'` - decimal
|
---|
115 |
|
---|
116 | # install
|
---|
117 |
|
---|
118 | With [npm](https://npmjs.org) do:
|
---|
119 |
|
---|
120 | ```
|
---|
121 | npm install text-table
|
---|
122 | ```
|
---|
123 |
|
---|
124 | # Use with ANSI-colors
|
---|
125 |
|
---|
126 | Since the string length of ANSI color schemes does not equal the length
|
---|
127 | JavaScript sees internally it is necessary to pass the a custom string length
|
---|
128 | calculator during the main function call.
|
---|
129 |
|
---|
130 | See the `test/ansi-colors.js` file for an example.
|
---|
131 |
|
---|
132 | # license
|
---|
133 |
|
---|
134 | MIT
|
---|