forked from susisu/mte-kernel
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
formatter.ts
721 lines (696 loc) · 17.4 KB
/
formatter.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import { Alignment, DefaultAlignment, HeaderAlignment } from './alignment';
import { Options } from './options';
import { Table } from './table';
import { TableCell } from './table-cell';
import { TableRow } from './table-row';
import { getEAW } from 'meaw';
export interface FormattedTable {
/**
* A formatted table object.
*/
table: Table;
/**
* The common left margin of the formatted table.
*/
marginLeft: string;
}
export interface CompletedTable {
/**
* A completed table object.
*/
table: Table;
/**
* `true` if a delimiter row is inserted.
*/
delimiterInserted: boolean;
}
/**
* An object containing options for computing text widths.
*/
export interface TextWidthOptions {
/**
* Normalizes text before computing text widths.
* @public
*/
normalize: boolean;
/**
* A set of characters that should be treated as wide.
* @public
*/
wideChars: Set<string>;
/**
* A set of characters that should be treated as narrow.
* @public
*/
narrowChars: Set<string>;
/**
* Treats East Asian Ambiguous characters as wide.
* @public
*/
ambiguousAsWide: boolean;
}
/**
* Creates a delimiter text.
*
* @private
* @param width - Width of the horizontal bar of delimiter.
* @throws {Error} Unknown alignment.
*/
export const _delimiterText = (alignment: Alignment, width: number): string => {
const bar = '-'.repeat(width);
switch (alignment) {
case Alignment.NONE:
return ` ${bar} `;
case Alignment.LEFT:
return `:${bar} `;
case Alignment.RIGHT:
return ` ${bar}:`;
case Alignment.CENTER:
return `:${bar}:`;
default:
throw new Error('Unknown alignment: ' + alignment);
}
};
/**
* Extends array size.
*
* @private
* @param callback - Callback function to fill newly created cells.
* @returns Extended array.
*/
export const _extendArray = <T>(
arr: T[],
size: number,
callback: (i: number, arr: T[]) => T,
): T[] => {
const extended = arr.slice();
for (let i = arr.length; i < size; i++) {
extended.push(callback(i, arr));
}
return extended;
};
/**
* Completes a table by adding missing delimiter and cells.
* After completion, all rows in the table have the same width.
*
* @private
*
* @throws {Error} Empty table.
*/
export const completeTable = (
table: Table,
options: Options,
): CompletedTable => {
const tableHeight = table.getHeight();
const tableWidth = table.getWidth();
if (tableHeight === 0) {
throw new Error('Empty table');
}
const rows = table.getRows();
const newRows = [];
// header
const headerRow = rows[0];
const headerCells = headerRow.getCells();
newRows.push(
new TableRow(
_extendArray(
headerCells,
tableWidth,
(j) =>
new TableCell(j === headerCells.length ? headerRow.marginRight : ''),
),
headerRow.marginLeft,
headerCells.length < tableWidth ? '' : headerRow.marginRight,
),
);
// delimiter
const delimiterRow = table.getDelimiterRow();
if (delimiterRow !== undefined) {
const delimiterCells = delimiterRow.getCells();
newRows.push(
new TableRow(
_extendArray(
delimiterCells,
tableWidth,
(j) =>
new TableCell(
_delimiterText(
Alignment.NONE,
j === delimiterCells.length
? Math.max(
options.minDelimiterWidth,
delimiterRow.marginRight.length - 2,
)
: options.minDelimiterWidth,
),
),
),
delimiterRow.marginLeft,
delimiterCells.length < tableWidth ? '' : delimiterRow.marginRight,
),
);
} else {
newRows.push(
new TableRow(
_extendArray(
[],
tableWidth,
() =>
new TableCell(
_delimiterText(Alignment.NONE, options.minDelimiterWidth),
),
),
'',
'',
),
);
}
// body
for (let i = delimiterRow !== undefined ? 2 : 1; i < tableHeight; i++) {
const row = rows[i];
const cells = row.getCells();
newRows.push(
new TableRow(
_extendArray(
cells,
tableWidth,
(j) => new TableCell(j === cells.length ? row.marginRight : ''),
),
row.marginLeft,
cells.length < tableWidth ? '' : row.marginRight,
),
);
}
return {
table: new Table(newRows),
delimiterInserted: delimiterRow === undefined,
};
};
/**
* Calculates the width of a text based on characters' EAW properties.
*
* @private
*
* @returns Calculated width of the text.
*/
export const _computeTextWidth = (
text: string,
options: TextWidthOptions,
): number => {
const normalized = options.normalize ? text.normalize('NFC') : text;
let w = 0;
for (const char of normalized) {
if (options.wideChars.has(char)) {
w += 2;
continue;
}
if (options.narrowChars.has(char)) {
w += 1;
continue;
}
switch (getEAW(char)) {
case 'F':
case 'W':
w += 2;
break;
case 'A':
w += options.ambiguousAsWide ? 2 : 1;
break;
default:
w += 1;
}
}
return w;
};
/**
* Returns a aligned cell content.
*
* @throws {Error} Unknown alignment.
* @throws {Error} Unexpected default alignment.
*/
export const _alignText = (
text: string,
width: number,
alignment: Alignment,
options: TextWidthOptions,
): string => {
const space = width - _computeTextWidth(text, options);
if (space < 0) {
return text;
}
switch (alignment) {
case Alignment.NONE:
throw new Error('Unexpected default alignment');
case Alignment.LEFT:
return text + ' '.repeat(space);
case Alignment.RIGHT:
return ' '.repeat(space) + text;
case Alignment.CENTER:
return (
' '.repeat(Math.floor(space / 2)) +
text +
' '.repeat(Math.ceil(space / 2))
);
default:
throw new Error('Unknown alignment: ' + alignment);
}
};
/**
* Just adds one space paddings to both sides of a text.
*
* @private
*/
export const _padText = (text: string): string => ` ${text} `;
/**
* Formats a table.
*
* @private
*/
export const _formatTable = (
table: Table,
options: Options,
): FormattedTable => {
const tableHeight = table.getHeight();
const tableWidth = table.getWidth();
if (tableHeight === 0) {
return {
table,
marginLeft: '',
};
}
const marginLeft = table.getRows()[0].marginLeft;
if (tableWidth === 0) {
const rows = new Array(tableHeight).fill(new TableRow([], marginLeft, ''));
return {
table: new Table(rows),
marginLeft,
};
}
// compute column widths
const delimiterRow = table.getDelimiterRow();
const columnWidths = new Array(tableWidth).fill(0);
if (delimiterRow !== undefined) {
const delimiterRowWidth = delimiterRow.getWidth();
for (let j = 0; j < delimiterRowWidth; j++) {
columnWidths[j] = options.minDelimiterWidth;
}
}
for (let i = 0; i < tableHeight; i++) {
if (delimiterRow !== undefined && i === 1) {
continue;
}
const row = table.getRows()[i];
const rowWidth = row.getWidth();
for (let j = 0; j < rowWidth; j++) {
columnWidths[j] = Math.max(
columnWidths[j],
_computeTextWidth(row.getCellAt(j)!.content, options.textWidthOptions),
);
}
}
// get column alignments
const alignments =
delimiterRow !== undefined
? _extendArray(
delimiterRow.getCells().map((cell) => cell.getAlignment()),
tableWidth,
// Safe conversion because DefaultAlignment is a subset of Alignment
() => (options.defaultAlignment as unknown) as Alignment,
)
: new Array(tableWidth).fill(options.defaultAlignment);
// format
const rows = [];
// header
const headerRow = table.getRows()[0];
rows.push(
new TableRow(
headerRow
.getCells()
.map(
(cell, j) =>
new TableCell(
_padText(
_alignText(
cell.content,
columnWidths[j],
options.headerAlignment === HeaderAlignment.FOLLOW
? alignments[j] === Alignment.NONE
? options.defaultAlignment
: alignments[j]
: options.headerAlignment,
options.textWidthOptions,
),
),
),
),
marginLeft,
'',
),
);
// delimiter
if (delimiterRow !== undefined) {
rows.push(
new TableRow(
delimiterRow
.getCells()
.map(
(cell, j) =>
new TableCell(_delimiterText(alignments[j], columnWidths[j])),
),
marginLeft,
'',
),
);
}
// body
for (let i = delimiterRow !== undefined ? 2 : 1; i < tableHeight; i++) {
const row = table.getRows()[i];
rows.push(
new TableRow(
row
.getCells()
.map(
(cell, j) =>
new TableCell(
_padText(
_alignText(
cell.content,
columnWidths[j],
alignments[j] === Alignment.NONE
? options.defaultAlignment
: alignments[j],
options.textWidthOptions,
),
),
),
),
marginLeft,
'',
),
);
}
return {
table: new Table(rows),
marginLeft,
};
};
/**
* Formats a table weakly.
* Rows are formatted independently to each other, cell contents are just trimmed and not aligned.
* This is useful when using a non-monospaced font or dealing with wide tables.
*
* @private
*/
export const _weakFormatTable = (
table: Table,
options: Options,
): FormattedTable => {
const tableHeight = table.getHeight();
const tableWidth = table.getWidth();
if (tableHeight === 0) {
return {
table,
marginLeft: '',
};
}
const marginLeft = table.getRows()[0].marginLeft;
if (tableWidth === 0) {
const rows = new Array(tableHeight).fill(new TableRow([], marginLeft, ''));
return {
table: new Table(rows),
marginLeft,
};
}
const delimiterRow = table.getDelimiterRow();
// format
const rows = [];
// header
const headerRow = table.getRows()[0];
rows.push(
new TableRow(
headerRow.getCells().map((cell) => new TableCell(_padText(cell.content))),
marginLeft,
'',
),
);
// delimiter
if (delimiterRow !== undefined) {
rows.push(
new TableRow(
delimiterRow
.getCells()
.map(
(cell) =>
new TableCell(
_delimiterText(cell.getAlignment()!, options.minDelimiterWidth),
),
),
marginLeft,
'',
),
);
}
// body
for (let i = delimiterRow !== undefined ? 2 : 1; i < tableHeight; i++) {
const row = table.getRows()[i];
rows.push(
new TableRow(
row.getCells().map((cell) => new TableCell(_padText(cell.content))),
marginLeft,
'',
),
);
}
return {
table: new Table(rows),
marginLeft,
};
};
/**
* Represents table format type.
*
* - `FormatType.NORMAL` - Formats table normally.
* - `FormatType.WEAK` - Formats table weakly, rows are formatted independently to each other, cell
* contents are just trimmed and not aligned.
*/
export enum FormatType {
NORMAL = 'normal',
WEAK = 'weak',
}
/**
* Formats a table.
*
* @private
*
* @throws {Error} Unknown format type.
*/
export const formatTable = (table: Table, options: Options): FormattedTable => {
switch (options.formatType) {
case FormatType.NORMAL:
return _formatTable(table, options);
case FormatType.WEAK:
return _weakFormatTable(table, options);
default:
throw new Error('Unknown format type: ' + options.formatType);
}
};
/**
* Alters a column's alignment of a table.
*
* @private
* @param table - A completed non-empty table.
* @param columnIndex - An index of the column.
* @param alignment - A new alignment of the column.
* @param options - An object containing options for completion.
* @returns {Table} An altered table object.
* If the column index is out of range, returns the original table.
*/
export const alterAlignment = (
table: Table,
columnIndex: number,
alignment: Alignment,
options: Options,
): Table => {
if (table.getHeight() < 1) {
return table;
}
const delimiterRow = table.getRows()[1];
if (columnIndex < 0 || delimiterRow.getWidth() - 1 < columnIndex) {
return table;
}
const delimiterCells = delimiterRow.getCells();
delimiterCells[columnIndex] = new TableCell(
_delimiterText(alignment, options.minDelimiterWidth),
);
const rows = table.getRows();
rows[1] = new TableRow(
delimiterCells,
delimiterRow.marginLeft,
delimiterRow.marginRight,
);
return new Table(rows);
};
/**
* Inserts a row to a table.
* The row is always inserted after the header and the delimiter rows, even if the index specifies
* the header or the delimiter.
*
* @private
* @param table - A completed non-empty table.
* @param rowIndex - An row index at which a new row will be inserted.
* @param row - A table row to be inserted.
* @returns An altered table obejct.
*/
export const insertRow = (
table: Table,
rowIndex: number,
row: TableRow,
): Table => {
const rows = table.getRows();
rows.splice(Math.max(rowIndex, 2), 0, row);
return new Table(rows);
};
/**
* Deletes a row in a table.
* If the index specifies the header row, the cells are emptied but the row will not be removed.
* If the index specifies the delimiter row, it does nothing.
*
* @private
* @param table - A completed non-empty table.
* @param rowIndex - An index of the row to be deleted.
* @returns An altered table obejct.
*/
export const deleteRow = (table: Table, rowIndex: number): Table => {
if (rowIndex === 1) {
return table;
}
const rows = table.getRows();
if (rowIndex === 0) {
const headerRow = rows[0];
rows[0] = new TableRow(
new Array(headerRow.getWidth()).fill(new TableCell('')),
headerRow.marginLeft,
headerRow.marginRight,
);
} else {
rows.splice(rowIndex, 1);
}
return new Table(rows);
};
/**
* Moves a row at the index to the specified destination.
*
* @private
* @param table - A completed non-empty table.
* @param rowIndex - Index of the row to be moved.
* @param destIndex - Index of the destination.
* @returns An altered table object.
*/
export const moveRow = (
table: Table,
rowIndex: number,
destIndex: number,
): Table => {
if (rowIndex <= 1 || destIndex <= 1 || rowIndex === destIndex) {
return table;
}
const rows = table.getRows();
const row = rows[rowIndex];
rows.splice(rowIndex, 1);
rows.splice(destIndex, 0, row);
return new Table(rows);
};
/**
* Inserts a column to a table.
*
* @private
* @param table - A completed non-empty table.
* @param columnIndex - An column index at which the new column will be inserted.
* @param column - An array of cells.
* @param options - An object containing options for completion.
* @returns An altered table obejct.
*/
export const insertColumn = (
table: Table,
columnIndex: number,
column: TableCell[],
options: Options,
): Table => {
const rows = table.getRows();
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const cells = rows[i].getCells();
const cell =
i === 1
? new TableCell(
_delimiterText(Alignment.NONE, options.minDelimiterWidth),
)
: column[i > 1 ? i - 1 : i];
cells.splice(columnIndex, 0, cell);
rows[i] = new TableRow(cells, row.marginLeft, row.marginRight);
}
return new Table(rows);
};
/**
* Deletes a column in a table.
* If there will be no columns after the deletion, the cells are emptied but the column will not be
* removed.
*
* @private
* @param table - A completed non-empty table.
* @param columnIndex - An index of the column to be deleted.
* @param options - An object containing options for completion.
* @returns An altered table object.
*/
export const deleteColumn = (
table: Table,
columnIndex: number,
options: Options,
): Table => {
const rows = table.getRows();
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
let cells = row.getCells();
if (cells.length <= 1) {
cells = [
new TableCell(
i === 1
? _delimiterText(Alignment.NONE, options.minDelimiterWidth)
: '',
),
];
} else {
cells.splice(columnIndex, 1);
}
rows[i] = new TableRow(cells, row.marginLeft, row.marginRight);
}
return new Table(rows);
};
/**
* Moves a column at the index to the specified destination.
*
* @private
* @param table - A completed non-empty table.
* @param columnIndex - Index of the column to be moved.
* @param destIndex - Index of the destination.
* @returns An altered table object.
*/
export const moveColumn = (
table: Table,
columnIndex: number,
destIndex: number,
): Table => {
if (columnIndex === destIndex) {
return table;
}
const rows = table.getRows();
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const cells = row.getCells();
const cell = cells[columnIndex];
cells.splice(columnIndex, 1);
cells.splice(destIndex, 0, cell);
rows[i] = new TableRow(cells, row.marginLeft, row.marginRight);
}
return new Table(rows);
};