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
, compact : false
46
}
47
, head: []
48
}, options);
49
};
50
51
/**
52
* Inherit from Array.
53
*/
54
55
Table.prototype.__proto__ = Array.prototype;
56
57
/**
58
* Width getter
59
*
60
* @return {Number} width
61
* @api public
62
*/
63
64
Table.prototype.__defineGetter__('width', function (){
65
var str = this.toString().split("\n");
66
if (str.length) return str[0].length;
67
return 0;
68
});
69
70
/**
71
* Render to a string.
72
*
73
* @return {String} table representation
74
* @api public
75
*/
76
77
Table.prototype.render
78
Table.prototype.toString = function (){
79
var ret = ''
80
, options = this.options
81
, style = options.style
82
, head = options.head
83
, chars = options.chars
84
, truncater = options.truncate
85
, colWidths = options.colWidths || new Array(this.head.length)
86
, totalWidth = 0;
87
88
if (!head.length && !this.length) return '';
89
90
if (!colWidths.length){
91
this.slice(0).concat([head]).forEach(function(cells){
92
cells.forEach(function(cell, i){
93
var width = typeof cell == 'object' && cell.width != undefined
94
? cell.width
95
: ((typeof cell == 'object' ? utils.strlen(cell.text) : utils.strlen(cell)) + (style['padding-left'] || 0) + (style['padding-right'] || 0))
96
colWidths[i] = Math.max(colWidths[i] || 0, width || 0);
97
});
98
});
99
};
100
101
totalWidth = (colWidths.length == 1 ? colWidths[0] : colWidths.reduce(
102
function (a, b){
103
return a + b
104
})) + colWidths.length + 1;
105
106
// draws a line
107
function line (line, left, right, intersection){
108
var width = 0
109
, line =
110
left
111
+ repeat(line, totalWidth - 2)
112
+ right;
113
114
colWidths.forEach(function (w, i){
115
if (i == colWidths.length - 1) return;
116
width += w + 1;
117
line = line.substr(0, width) + intersection + line.substr(width + 1);
118
});
119
120
ret += line;
121
};
122
123
// draws the top line
124
function lineTop (){
125
line(chars.top
126
, chars['top-left'] || chars.top
127
, chars['top-right'] || chars.top
128
, chars['top-mid']);
129
ret += "\n";
130
};
131
132
// renders a string, by padding it or truncating it
133
function string (str, index){
134
var str = String(typeof str == 'object' && str.text ? str.text : str)
135
, length = utils.strlen(str)
136
, width = colWidths[index]
137
- (style['padding-left'] || 0)
138
- (style['padding-right'] || 0)
139
, align = options.colAligns[index] || 'left';
140
141
return repeat(' ', style['padding-left'] || 0)
142
+ (length == width ? str :
143
(length < width
144
? pad(str, ( width + (str.length - length) ), ' ', align == 'left' ? 'right' :
145
(align == 'middle' ? 'both' : 'left'))
146
: (truncater ? truncate(str, width, truncater) : str))
147
)
148
+ repeat(' ', style['padding-right'] || 0);
149
};
150
151
if (head.length){
152
lineTop();
153
154
ret += chars.left;
155
156
head.forEach(function (th, index){
157
var text = string(th, index);
158
if (style.head){
159
style.head.forEach(function(style){
160
text = text[style];
161
});
162
}
163
164
ret += text;
165
ret += chars.right;
166
});
167
168
ret += "\n";
169
}
170
171
if (this.length)
172
this.forEach(function (cells, i){
173
if (!head.length && i == 0)
174
lineTop();
175
else {
176
if (!style.compact || i<(!!head.length) ?1:0 || cells.length == 0){
177
line(chars.mid
178
, chars['left-mid']
179
, chars['right-mid']
180
, chars['mid-mid']);
181
182
ret += "\n"
183
}
184
if (!cells.length)
185
return;
186
187
ret += chars.left;
188
189
cells.forEach(function(cell, i){
190
ret += string(cell, i);
191
ret += chars.right;
192
});
193
194
ret += "\n";
195
}
196
});
197
198
line(chars.bottom
199
, chars['bottom-left'] || chars.bottom
200
, chars['bottom-right'] || chars.bottom
201
, chars['bottom-mid']);
202
203
return ret;
204
};
205
206
/**
207
* Module exports.
208
*/
209
210
module.exports = Table;
211
212
module.exports.version = '0.0.1';
213
214