/
table.ts
337 lines (249 loc) 路 10.8 KB
/
table.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { encode } from 'https://deno.land/std@v0.61.0/encoding/utf8.ts';
import { border } from './border.ts';
import { Cell, ICell } from './cell.ts';
import { CELL_PADDING, MAX_CELL_WIDTH, MIN_CELL_WIDTH } from './const.ts';
import { IRow, Row } from './row.ts';
import { consumeWords, longest, stripeColors } from './utils.ts';
export interface ITableOptions {
indent?: number;
border?: boolean;
maxCellWidth?: number | number[];
minCellWidth?: number | number[];
padding?: number | number[];
}
export type ITable = IRow[] | Table;
export interface ICalc {
padding: number[],
width: number[],
columns: number
}
export class Table extends Array<IRow> {
protected options: ITableOptions = {};
public static from( rows: ITable ): Table {
const table = new Table( ...rows.map( cells => [ ...cells ] ) );
if ( rows instanceof Table ) {
table.options = Object.create( rows.options );
}
return table;
}
public static render( rows: ITable ): void {
Table.from( rows ).render();
}
public render(): void {
Deno.stdout.writeSync( encode( this.toString() ) );
}
/**
* Calc:
*/
protected calc(): ICalc {
const padding: number[] = [];
const width: number[] = [];
const columns: number = Math.max( ...this.map( row => Object.keys( row ).length ) );
for ( let colIndex: number = 0; colIndex < columns; colIndex++ ) {
let minCellWidth: number = Array.isArray( this.options.minCellWidth ) ? this.options.minCellWidth[ colIndex ] :
( typeof this.options.minCellWidth === 'undefined' ? MIN_CELL_WIDTH : this.options.minCellWidth );
let maxCellWidth: number = Array.isArray( this.options.maxCellWidth ) ? this.options.maxCellWidth[ colIndex ] :
( typeof this.options.maxCellWidth === 'undefined' ? MAX_CELL_WIDTH : this.options.maxCellWidth );
padding[ colIndex ] = Array.isArray( this.options.padding ) ? this.options.padding[ colIndex ] :
( typeof this.options.padding === 'undefined' ? CELL_PADDING : this.options.padding );
let cellWidth: number = longest( colIndex, this, maxCellWidth );
// let cellWidth: number = longest( colIndex, this );
width[ colIndex ] = Math.min( maxCellWidth, Math.max( minCellWidth, cellWidth ) );
}
return { padding, width, columns };
}
protected getRows(): Row<Cell>[] {
return this.map( ( iRow: IRow ) => {
const row: Row = Row.from( iRow );
if ( this.options.border ) {
row.border( this.options.border, false );
}
row.forEach( ( iCell: ICell, i: number ) => {
const cell = Cell.from( iCell );
if ( row.getBorder() ) {
cell.border( row.getBorder() as boolean, false );
}
row[ i ] = cell;
} );
return row as Row<Cell>;
} );
}
/**
* Render:
*/
public toString(): string {
const calc: ICalc = this.calc();
const rows: Row<Cell>[] = this.getRows();
return this.renderRows( calc, rows );
}
protected renderRows( opts: ICalc, rows: Row<Cell>[], _rowGroupIndex: number = 0 ) {
let result: string = '';
result += this.renderRow( rows[ _rowGroupIndex ], rows[ _rowGroupIndex - 1 ], rows[ _rowGroupIndex + 1 ], opts, _rowGroupIndex );
if ( _rowGroupIndex < this.length - 1 ) {
result += this.renderRows( opts, rows, ++_rowGroupIndex );
}
return result;
}
protected renderRow( row: Row<Cell>, prevRow: Row<Cell> | undefined, nextRow: Row<Cell> | undefined, opts: ICalc, _rowGroupIndex: number = 0, _rowIndex: number = 0 ): string {
let result: string = '';
if ( row.getBorder() && _rowGroupIndex === 0 && _rowIndex === 0 ) {
result += this.renderBorderTopRow( row, prevRow, nextRow, opts );
}
const { cells, isMultilineRow } = this.renderCells( row, opts );
result += cells;
if ( isMultilineRow ) {
result += this.renderRow( row, prevRow, nextRow, opts, _rowGroupIndex, ++_rowIndex );
} else if ( _rowGroupIndex < this.length - 1 ) {
if ( row.getBorder() ) {
result += this.renderBorderMidRow( row, prevRow, nextRow, opts );
}
} else if ( _rowGroupIndex === this.length - 1 ) {
if ( row.getBorder() ) {
result += this.renderBorderBottomRow( row, prevRow, nextRow, opts );
}
} else {
throw new Error( `Invalid row index: ${ _rowGroupIndex }` );
}
return result;
}
protected renderCells( row: Row<Cell>, opts: ICalc ) {
let cells: string = ' '.repeat( this.options.indent || 0 );
let isMultilineRow: boolean = false;
const rowCount = row.length;
let prev: Cell | undefined;
for ( let i = 0; i < rowCount; i++ ) {
const cell: Cell = row[ i ];
if ( i === 0 ) {
if ( cell.getBorder() ) {
cells += border.left;
} else {
// cells += ' ';
}
}
const { current, next } = this.renderCell( cell, opts.width[ i ] );
// it's required to call explicilty .length because next is a String class and not a string type.
next.length && ( isMultilineRow = true );
row[ i ] = next;
if ( i > 0 ) {
if ( cell.getBorder() && ( !prev || prev.getBorder() ) ) {
cells += border.middle;
} else {
// cells += ' ';
}
}
if ( cell.getBorder() ) {
cells += ' '.repeat( opts.padding[ i ] );
}
cells += current;
if ( cell.getBorder() || i < rowCount - 1 ) {
cells += ' '.repeat( opts.padding[ i ] );
}
if ( i === rowCount - 1 ) {
if ( cell.getBorder() ) {
cells += border.right;
} else {
// cells += ' ';
}
}
prev = cell;
}
cells += '\n';
return { cells, isMultilineRow };
}
protected renderCell( cell: Cell, maxLength: number ): { current: string, next: Cell } {
const length: number = Math.min( maxLength, stripeColors( cell.toString() ).length );
let words: string = consumeWords( length, cell.toString() );
// break word if word is longer than max length
const breakWord = stripeColors( words ).length > length;
if ( breakWord ) {
words = words.slice( 0, length );
}
// get next content and remove leading space if breakWord is not true
const next = cell.slice( words.length + ( breakWord ? 0 : 1 ) );
const fillLength = maxLength - stripeColors( words ).length;
const current = words + ' '.repeat( fillLength );
return {
current,
next: cell.clone( next )
};
}
/**
* Border:
*/
protected renderBorderTopRow( row: Row<Cell>, prevRow: Row<Cell> | undefined, nextRow: Row<Cell> | undefined, opts: ICalc ): string {
let cells = [];
for ( let i = 0; i < opts.columns; i++ ) {
const cell: Cell = row[ i ];
cells.push( ( cell.getBorder() ? border.top : ' ' ).repeat( opts.padding[ i ] + opts.width[ i ] + opts.padding[ i ] ) );
}
return ' '.repeat( this.options.indent || 0 ) + border.topLeft + cells.join( border.topMid ) + ( border.topRight ) + '\n';
}
protected renderBorderMidRow( row: Row<Cell>, prevRow: Row<Cell> | undefined, nextRow: Row<Cell> | undefined, opts: ICalc ): string {
let cells = [];
for ( let i = 0; i < opts.columns; i++ ) {
const cell: Cell = row[ i ];
const nextCell = nextRow && nextRow[ i ];
cells.push( ( cell.getBorder() && nextCell?.getBorder() !== false ? border.mid : ' ' ).repeat( opts.padding[ i ] + opts.width[ i ] + opts.padding[ i ] ) );
}
return ' '.repeat( this.options.indent || 0 ) + border.leftMid + cells.join( border.midMid ) + ( border.rightMid ) + '\n';
}
protected renderBorderBottomRow( row: Row<Cell>, prevRow: Row<Cell> | undefined, nextRow: Row<Cell> | undefined, opts: ICalc ): string {
let cells = [];
for ( let i = 0; i < opts.columns; i++ ) {
const cell: Cell = row[ i ];
cells.push( ( cell.getBorder() ? border.bottom : ' ' ).repeat( opts.padding[ i ] + opts.width[ i ] + opts.padding[ i ] ) );
}
return ' '.repeat( this.options.indent || 0 ) + border.bottomLeft + cells.join( border.bottomMid ) + ( border.bottomRight ) + '\n';
}
/**
* Setter:
*/
public indent( width: number, override: boolean = true ): this {
if ( override || typeof this.options.indent === 'undefined' ) {
this.options.indent = width;
}
return this;
}
public maxCellWidth( width: number | number[], override: boolean = true ): this {
if ( override || typeof this.options.maxCellWidth === 'undefined' ) {
this.options.maxCellWidth = width;
}
return this;
}
public minCellWidth( width: number | number[], override: boolean = true ): this {
if ( override || typeof this.options.minCellWidth === 'undefined' ) {
this.options.minCellWidth = width;
}
return this;
}
public padding( padding: number | number[], override: boolean = true ): this {
if ( override || typeof this.options.padding === 'undefined' ) {
this.options.padding = padding;
}
return this;
}
public border( enable: boolean, override: boolean = true ): this {
if ( override || typeof this.options.border === 'undefined' ) {
this.options.border = enable;
}
return this;
}
/**
* Getter:
*/
public getIndent( defaultValue?: number ): number | undefined {
return this.options.indent ?? defaultValue;
}
public getMaxCellWidth( defaultValue?: number | number[] ): number | number[] | undefined {
return this.options.maxCellWidth ?? defaultValue;
}
public getMinCellWidth( defaultValue?: number | number[] ): number | number[] | undefined {
return this.options.minCellWidth ?? defaultValue;
}
public getPadding( defaultValue?: number | number[] ): number | number[] | undefined {
return this.options.padding ?? defaultValue;
}
public getBorder( defaultValue?: boolean ): boolean | undefined {
return this.options.border ?? defaultValue;
}
}