Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80687 views
1
2
/**
3
* Module dependencies.
4
*/
5
6
var utils = require('./utils')
7
, repeat = utils.repeat
8
, truncate = utils.truncate
9
, pad = utils.pad;
10
11
require('../../../colors');
12
13
/**
14
* Table constructor
15
*
16
* @param {Object} options
17
* @api public
18
*/
19
20
function Table (options){
21
this.options = utils.options({
22
chars: {
23
'top': '━'
24
, 'top-mid': '┳'
25
, 'top-left': '┏'
26
, 'top-right': '┓'
27
, 'bottom': '━'
28
, 'bottom-mid': '┻'
29
, 'bottom-left': '┗'
30
, 'bottom-right': '┛'
31
, 'left': '┃'
32
, 'left-mid': '┣'
33
, 'mid': '━'
34
, 'mid-mid': '╋'
35
, 'right': '┃'
36
, 'right-mid': '┫'
37
}
38
, truncate: '…'
39
, colWidths: []
40
, colAligns: []
41
, style: {
42
'padding-left': 1
43
, 'padding-right': 1
44
, head: ['cyan']
45
}
46
, head: []
47
}, options);
48
};
49
50
/**
51
* Inherit from Array.
52
*/
53
54
Table.prototype.__proto__ = Array.prototype;
55
56
/**
57
* Width getter
58
*
59
* @return {Number} width
60
* @api public
61
*/
62
63
Table.prototype.__defineGetter__('width', function (){
64
var str = this.toString().split("\n");
65
if (str.length) return str[0].length;
66
return 0;
67
});
68
69
/**
70
* Render to a string.
71
*
72
* @return {String} table representation
73
* @api public
74
*/
75
76
Table.prototype.render
77
Table.prototype.toString = function (){
78
var ret = ''
79
, options = this.options
80
, style = options.style
81
, head = options.head
82
, chars = options.chars
83
, truncater = options.truncate
84
, colWidths = options.colWidths || new Array(this.head.length)
85
, totalWidth = 0;
86
87
if (!head.length && !this.length) return '';
88
89
if (!colWidths.length){
90
this.slice(0).concat([head]).forEach(function(cells){
91
cells.forEach(function(cell, i){
92
var width = typeof cell == 'object' && cell.width != undefined
93
? cell.width
94
: ((typeof cell == 'object' ? utils.strlen(cell.text) : utils.strlen(cell)) + (style['padding-left'] || 0) + (style['padding-right'] || 0))
95
colWidths[i] = Math.max(colWidths[i] || 0, width || 0);
96
});
97
});
98
};
99
100
totalWidth = (colWidths.length == 1 ? colWidths[0] : colWidths.reduce(
101
function (a, b){
102
return a + b
103
})) + colWidths.length + 1;
104
105
// draws a line
106
function line (line, left, right, intersection){
107
var width = 0
108
, line =
109
left
110
+ repeat(line, totalWidth - 2)
111
+ right;
112
113
colWidths.forEach(function (w, i){
114
if (i == colWidths.length - 1) return;
115
width += w + 1;
116
line = line.substr(0, width) + intersection + line.substr(width + 1);
117
});
118
119
ret += line;
120
};
121
122
// draws the top line
123
function lineTop (){
124
line(chars.top
125
, chars['top-left'] || chars.top
126
, chars['top-right'] || chars.top
127
, chars['top-mid']);
128
ret += "\n";
129
};
130
131
// renders a string, by padding it or truncating it
132
function string (str, index){
133
var str = String(typeof str == 'object' && str.text ? str.text : str)
134
, length = utils.strlen(str)
135
, width = colWidths[index]
136
- (style['padding-left'] || 0)
137
- (style['padding-right'] || 0)
138
, align = options.colAligns[index] || 'left';
139
140
return repeat(' ', style['padding-left'] || 0)
141
+ (length == width ? str :
142
(length < width
143
? pad(str, ( width + (str.length - length) ), ' ', align == 'left' ? 'right' :
144
(align == 'middle' ? 'both' : 'left'))
145
: (truncater ? truncate(str, width, truncater) : str))
146
)
147
+ repeat(' ', style['padding-right'] || 0);
148
};
149
150
if (head.length){
151
lineTop();
152
153
ret += chars.left;
154
155
head.forEach(function (th, index){
156
var text = string(th, index);
157
if (style.head){
158
style.head.forEach(function(style){
159
text = text[style];
160
});
161
}
162
163
ret += text;
164
ret += chars.right;
165
});
166
167
ret += "\n";
168
}
169
170
if (this.length)
171
this.forEach(function (cells, i){
172
if (!head.length && i == 0)
173
lineTop();
174
else {
175
line(chars.mid
176
, chars['left-mid']
177
, chars['right-mid']
178
, chars['mid-mid']);
179
180
ret += "\n" + chars.left;
181
182
cells.forEach(function(cell, i){
183
ret += string(cell, i);
184
ret += chars.right;
185
});
186
187
ret += "\n";
188
}
189
});
190
191
line(chars.bottom
192
, chars['bottom-left'] || chars.bottom
193
, chars['bottom-right'] || chars.bottom
194
, chars['bottom-mid']);
195
196
return ret;
197
};
198
199
/**
200
* Module exports.
201
*/
202
203
module.exports = Table;
204
205
module.exports.version = '0.0.1';
206