TablePrinter.js
1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
const Table = require('cli-table2');
const MIN_CELL_WIDTH = 15;
class TablePrinter {
constructor(opts) {
this.opts = opts;
this._hasWritten = false;
this.colWidths;
}
push(csv) {
const lines = csv.split(this.opts.eol);
const chars = {
'bottom': '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': ''
};
if (!this._hasWritten) {
this.colWidths = this.getColumnWidths(lines[0]);
if (this.opts.header) {
const head = lines.shift().split(this.opts.delimiter);
const table = new Table({ head, colWidths: this.colWidths, chars });
this.print(table, []);
this._hasWritten = true;
}
} else {
chars['top-left'] = '├';
chars['top-mid'] = '┼';
chars['top-right'] = '┤';
}
if (!lines.length) return;
const table = new Table({ colWidths: this.colWidths, chars });
this.print(table, lines);
this._hasWritten = true;
}
end(csv) {
const lines = csv.split(this.opts.eol);
const chars = { 'top-left': '├' , 'top-mid': '┼', 'top-right': '┤' };
const table = new Table({ colWidths: this.colWidths, chars });
this.print(table, lines);
}
printCSV(csv) {
let lines = csv.split(this.opts.eol);
this.colWidths = this.getColumnWidths(lines[0]);
const head = this.opts.header
? lines.shift().split(this.opts.delimiter)
: undefined;
const table = new Table(head
? { head, colWidths: this.colWidths }
: { colWidths: this.colWidths });
this.print(table, lines);
}
getColumnWidths(line) {
return line
.split(this.opts.delimiter)
.map(elem => Math.max(elem.length * 2, MIN_CELL_WIDTH));
}
print(table, lines) {
lines.forEach(line => table.push(line.split(this.opts.delimiter)));
// eslint-disable-next-line no-console
console.log(table.toString());
}
}
module.exports = TablePrinter;