-
Notifications
You must be signed in to change notification settings - Fork 5
/
columns.js
171 lines (151 loc) · 5.36 KB
/
columns.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var t = require('typical');
var arrayify = require('array-back');
var Column = require('./column');
var wrap = require('wordwrapjs');
var Cell = require('./cell');
var ansi = require('./ansi');
var _maxWidth = new WeakMap();
var Columns = function () {
function Columns(columns) {
_classCallCheck(this, Columns);
this.list = [];
arrayify(columns).forEach(this.add.bind(this));
}
_createClass(Columns, [{
key: 'totalWidth',
value: function totalWidth() {
return this.list.length ? this.list.map(function (col) {
return col.generatedWidth;
}).reduce(function (a, b) {
return a + b;
}) : 0;
}
}, {
key: 'totalFixedWidth',
value: function totalFixedWidth() {
return this.getFixed().map(function (col) {
return col.generatedWidth;
}).reduce(function (a, b) {
return a + b;
}, 0);
}
}, {
key: 'get',
value: function get(columnName) {
return this.list.find(function (column) {
return column.name === columnName;
});
}
}, {
key: 'getResizable',
value: function getResizable() {
return this.list.filter(function (column) {
return column.isResizable();
});
}
}, {
key: 'getFixed',
value: function getFixed() {
return this.list.filter(function (column) {
return column.isFixed();
});
}
}, {
key: 'add',
value: function add(column) {
var col = column instanceof Column ? column : new Column(column);
this.list.push(col);
return col;
}
}, {
key: 'autoSize',
value: function autoSize() {
var _this = this;
var maxWidth = _maxWidth.get(this);
this.list.forEach(function (column) {
column.generateWidth();
column.generateMinWidth();
});
this.list.forEach(function (column) {
if (t.isDefined(column.maxWidth) && column.generatedWidth > column.maxWidth) {
column.generatedWidth = column.maxWidth;
}
if (t.isDefined(column.minWidth) && column.generatedWidth < column.minWidth) {
column.generatedWidth = column.minWidth;
}
});
var width = {
total: this.totalWidth(),
view: maxWidth,
diff: this.totalWidth() - maxWidth,
totalFixed: this.totalFixedWidth(),
totalResizable: Math.max(maxWidth - this.totalFixedWidth(), 0)
};
if (width.diff > 0) {
(function () {
var resizableColumns = _this.getResizable();
resizableColumns.forEach(function (column) {
column.generatedWidth = Math.floor(width.totalResizable / resizableColumns.length);
});
var grownColumns = _this.list.filter(function (column) {
return column.generatedWidth > column.contentWidth;
});
var shrunkenColumns = _this.list.filter(function (column) {
return column.generatedWidth < column.contentWidth;
});
var salvagedSpace = 0;
grownColumns.forEach(function (column) {
var currentGeneratedWidth = column.generatedWidth;
column.generateWidth();
salvagedSpace += currentGeneratedWidth - column.generatedWidth;
});
shrunkenColumns.forEach(function (column) {
column.generatedWidth += Math.floor(salvagedSpace / shrunkenColumns.length);
});
})();
}
return this;
}
}, {
key: 'maxWidth',
set: function set(val) {
_maxWidth.set(this, val);
}
}], [{
key: 'getColumns',
value: function getColumns(rows) {
var columns = new Columns();
arrayify(rows).forEach(function (row) {
for (var columnName in row) {
var column = columns.get(columnName);
if (!column) {
column = columns.add({ name: columnName, contentWidth: 0, minContentWidth: 0 });
}
var cell = new Cell(row[columnName], column);
var cellValue = cell.value;
if (ansi.has(cellValue)) {
cellValue = ansi.remove(cellValue);
}
if (cellValue.length > column.contentWidth) column.contentWidth = cellValue.length;
var longestWord = getLongestWord(cellValue);
if (longestWord > column.minContentWidth) {
column.minContentWidth = longestWord;
}
if (!column.contentWrappable) column.contentWrappable = wrap.isWrappable(cellValue);
}
});
return columns;
}
}]);
return Columns;
}();
function getLongestWord(line) {
var words = wrap.getChunks(line);
return words.reduce(function (max, word) {
return Math.max(word.length, max);
}, 0);
}
module.exports = Columns;