-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathformat.ts
More file actions
194 lines (176 loc) · 6.35 KB
/
format.ts
File metadata and controls
194 lines (176 loc) · 6.35 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
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
import {
getColumnWidths,
getContentWidth,
getHeadingWidth,
getStringWidth,
renderContentLine,
renderHeaderOpen,
renderRowSeparator,
renderSectionClose,
} from "./internal/format.js";
export interface TableTitle {
type: "title";
text: string;
}
export interface TableSectionHeader {
type: "section-header";
text: string;
subtitle?: string;
}
export interface TableHeader {
type: "header";
cells: string[];
}
export interface TableRow {
type: "row";
cells: string[];
}
export type TableItem =
| TableTitle
| TableSectionHeader
| TableHeader
| TableRow;
/**
* Formats an array of titles, section headers, headers, and rows into a table
* string with box-drawing characters.
*
* Features:
* - Titles are centered in a standalone box with double borders (╔═╗)
* - Section headers group related content with automatic closing
* - Headers and rows can have different numbers of cells
* - Rows with fewer cells than max columns are handled with special rendering
*
* @param items An array of table items (titles, section headers, headers, and rows).
* Sections are automatically closed when a new section-header or title appears, or
* at the end of the table.
* @returns The formatted table as a string, ready to be rendered.
*
* @example
* ```ts
* formatTable([
* { type: "title", text: "My Table" },
* { type: "section-header", text: "User Data" },
* { type: "header", cells: ["Name", "Age", "City"] },
* { type: "row", cells: ["Alice", "30", "NYC"] },
* { type: "row", cells: ["Bob", "25", "LA"] },
* { type: "section-header", text: "Summary" },
* { type: "header", cells: ["Total", "Count"] },
* { type: "row", cells: ["55", "2"] }
* ]);
*
* // =>
* // ╔═══════════════════╗
* // ║ My Table ║
* // ╚═══════════════════╝
* // ╔═══════════════════╗
* // ║ User Data ║
* // ╟───────┬─────┬─────╢
* // ║ Name │ Age │ City║
* // ╟───────┼─────┼─────╢
* // ║ Alice │ 30 │ NYC ║
* // ╟───────┼─────┼─────╢
* // ║ Bob │ 25 │ LA ║
* // ╚═══════╧═════╧═════╝
* // ╔═══════════════════╗
* // ║ Summary ║
* // ╟───────┬───────────╢
* // ║ Total │ Count ║
* // ╟───────┼───────────╢
* // ║ 55 │ 2 ║
* // ╚═══════╧═══════════╝
* ```
*/
export function formatTable(items: TableItem[]): string {
if (items.length === 0) {
return "";
}
const columnWidths = getColumnWidths(items);
const contentWidth = getContentWidth(columnWidths);
const headingWidth = getHeadingWidth(items);
// If heading is wider than content, expand last column to fit
if (headingWidth > contentWidth && columnWidths.length > 0) {
const extraSpace = headingWidth - contentWidth;
columnWidths[columnWidths.length - 1] += extraSpace;
}
const tableWidth = Math.max(contentWidth, headingWidth);
const lines: string[] = [];
let previousCellCount = 0; // Keep track of previous row/header cell count
let inSection = false;
for (let i = 0; i < items.length; i++) {
const [previous, current] = [items[i - 1], items[i]];
if (current.type === "title") {
if (inSection) {
lines.push(renderSectionClose(columnWidths, previousCellCount));
inSection = false;
}
lines.push("╔" + "═".repeat(tableWidth) + "╗");
const titleDisplayWidth = getStringWidth(current.text);
const titleActualLength = current.text.length;
const centeredTitle = current.text
.padStart(
(tableWidth + titleDisplayWidth) / 2 +
(titleActualLength - titleDisplayWidth),
)
.padEnd(tableWidth + (titleActualLength - titleDisplayWidth));
lines.push("║" + centeredTitle + "║");
lines.push("╚" + "═".repeat(tableWidth) + "╝");
} else if (current.type === "section-header") {
if (inSection) {
lines.push(renderSectionClose(columnWidths, previousCellCount));
}
lines.push("╔" + "═".repeat(tableWidth) + "╗");
const headerDisplayWidth = getStringWidth(current.text);
const headerActualLength = current.text.length;
const paddedHeader = current.text.padEnd(
tableWidth - 2 + (headerActualLength - headerDisplayWidth),
);
lines.push("║ " + paddedHeader + " ║");
if (current.subtitle !== undefined) {
const subtitleText = " " + current.subtitle;
const subtitleDisplayWidth = getStringWidth(subtitleText);
const subtitleActualLength = subtitleText.length;
const paddedSubtitle = subtitleText.padEnd(
tableWidth - 2 + (subtitleActualLength - subtitleDisplayWidth),
);
lines.push("║ " + paddedSubtitle + " ║");
}
inSection = true;
} else if (current.type === "header") {
const currentCellCount = current.cells.length;
const innerJoiner =
previous !== undefined && previous.type === "section-header"
? "┬"
: "┼";
const needsTransition =
previous !== undefined &&
previous.type !== "section-header" &&
currentCellCount < previousCellCount;
lines.push(
renderHeaderOpen(
columnWidths,
currentCellCount,
innerJoiner,
needsTransition,
),
);
lines.push(
renderContentLine(current.cells, columnWidths, currentCellCount),
);
previousCellCount = currentCellCount;
} else if (current.type === "row") {
const currentCellCount = current.cells.length;
// Only add separator if previous wasn't a row
if (previous === undefined || previous.type !== "row") {
lines.push(renderRowSeparator(columnWidths, currentCellCount));
}
lines.push(
renderContentLine(current.cells, columnWidths, currentCellCount),
);
previousCellCount = currentCellCount;
}
}
if (inSection) {
lines.push(renderSectionClose(columnWidths, previousCellCount));
}
return lines.join("\n");
}