-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtextTable.ts
78 lines (78 loc) · 2.28 KB
/
textTable.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
export function textTable(headings: string[], cells: string[][]): string {
const corners = [["┌", "┐"], ["└", "┘"]];
const hbar = "─";
const vbar = "│";
const ttop = "┬";
const tbottom = "┴";
const cross = "┼";
const tleft = "├";
const tright = "┤";
const maxWidths: number[] = headings.map((t) => t.length);
for (const row of cells) {
let colInd = 0;
for (const col of row) {
maxWidths[colInd] = Math.max(maxWidths[colInd], col.length);
++colInd;
}
}
const output: string[] = [];
// corner & top bars
{
const textrow: string[] = [];
textrow.push(corners[0][0]);
textrow.push(maxWidths.map((n) => hbar.repeat(n + 2)).join(ttop));
textrow.push(corners[0][1]);
output.push(textrow.join(""));
}
// mid
{
const textrow: string[] = [];
textrow.push(vbar);
textrow.push(
headings.map((h, i) => {
const curLength = h.length;
const maxWidth = maxWidths[i];
const curSpaces = maxWidth - curLength;
const spaceBefore = Math.floor(curSpaces / 2);
const spaceAfter = curSpaces - spaceBefore;
return " ".repeat(1 + spaceBefore) + h + " ".repeat(1 + spaceAfter);
}).join(vbar),
);
textrow.push(vbar);
output.push(textrow.join(""));
}
// cross bar
{
const textrow: string[] = [];
textrow.push(tleft);
textrow.push(maxWidths.map((n) => hbar.repeat(n + 2)).join(cross));
textrow.push(tright);
output.push(textrow.join(""));
}
// cells
for (const row of cells) {
const textrow: string[] = [];
textrow.push(vbar);
textrow.push(
row.map((t, i) => {
const curLength = t.length;
const maxWidth = maxWidths[i];
const curSpaces = maxWidth - curLength;
const spaceBefore = Math.floor(curSpaces / 2);
const spaceAfter = curSpaces - spaceBefore;
return " ".repeat(1 + spaceBefore) + t + " ".repeat(1 + spaceAfter);
}).join(vbar),
);
textrow.push(vbar);
output.push(textrow.join(""));
}
// corner & bottom bars
{
const textrow: string[] = [];
textrow.push(corners[1][0]);
textrow.push(maxWidths.map((n) => hbar.repeat(n + 2)).join(tbottom));
textrow.push(corners[1][1]);
output.push(textrow.join(""));
}
return output.join("\n");
}