-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathformat.ts
More file actions
264 lines (251 loc) · 8.25 KB
/
format.ts
File metadata and controls
264 lines (251 loc) · 8.25 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
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
import type { TableItem } from "../format.js";
/**
* Calculate the display width of a string by removing ANSI escape codes.
*
* NOTE: This implementation only removes basic ANSI color/style codes and may
* not handle all escape sequences (e.g., cursor movement, complex control
* sequences).
*/
export function getStringWidth(str: string): number {
// Remove ANSI escape codes if present
const stripped = str.replace(/\u001b\[[0-9;]*m/g, "");
return stripped.length;
}
/**
* Calculates the minimum width needed by each column in the table
* to fit its content (accounting for ANSI color codes).
*/
export function getColumnWidths(items: TableItem[]): number[] {
const columnWidths: number[] = [];
for (const item of items) {
if (item.type === "row" || item.type === "header") {
item.cells.forEach((cell, i) => {
columnWidths[i] = Math.max(columnWidths[i] ?? 0, getStringWidth(cell));
});
}
}
return columnWidths;
}
/**
* Calculates the inner width needed to fit the rows and headers
* (excludes borders, which are added during rendering).
*
* Each column is padded by 1 space on each side, and columns are
* separated by " │ " (3 spaces).
*/
export function getContentWidth(columnWidths: number[]): number {
return (
columnWidths.reduce((sum, w) => sum + w, 0) +
(columnWidths.length - 1) * 3 +
2
);
}
/**
* Calculates the inner width needed to fit titles and section headers
* (excludes borders, which are added during rendering).
*
* Each title/header is padded by 1 space on each side.
* Accounts for ANSI color codes.
*/
export function getHeadingWidth(items: TableItem[]): number {
let headingWidth = 0;
for (const item of items) {
if (item.type === "section-header" || item.type === "title") {
headingWidth = Math.max(headingWidth, getStringWidth(item.text) + 2);
}
if (item.type === "section-header" && item.subtitle !== undefined) {
// +4 accounts for "║ " prefix (2) and " " indent (2)
headingWidth = Math.max(headingWidth, getStringWidth(item.subtitle) + 4);
}
}
return headingWidth;
}
/**
* Calculates the width needed for unused columns when a row/header has fewer
* cells than the total column count (e.g., if table has 6 columns but row
* only has 2 cells, calculates space for the remaining 4 columns).
*/
export function getUnusedColumnsWidth(
columnWidths: number[],
previousCellCount: number,
): number {
const remainingWidths = columnWidths.slice(previousCellCount);
return remainingWidths.reduce((sum, w) => sum + w + 3, 0) - 3;
}
/**
* Renders a horizontal rule segment by repeating a character for each column
* with padding, joined by a separator (e.g., "─────┼─────┼─────").
*/
export function renderRuleSegment(
columnWidths: number[],
char: string,
joiner: string,
): string {
return columnWidths.map((w) => char.repeat(w + 2)).join(joiner);
}
/**
* Renders a complete horizontal rule with left and right borders
* (e.g., "╟─────┼─────┼─────╢").
*/
export function renderHorizontalRule(
leftBorder: string,
columnWidths: number[],
char: string,
joiner: string,
rightBorder: string,
): string {
return (
leftBorder + renderRuleSegment(columnWidths, char, joiner) + rightBorder
);
}
/**
* Renders a content line containing cells from either a header or row.
*
* Handles two cases:
* - Full width: When all columns are used, cells are separated by " │ " and
* line ends with " ║" (e.g., "║ cell1 │ cell2 │ cell3 ║")
* - Short line: When fewer columns are used, active cells are followed by
* " │ " and empty space, ending with "║" (e.g., "║ cell1 │ cell2 │ ║")
*
* Accounts for ANSI color codes when padding cells.
*/
export function renderContentLine(
cells: string[],
columnWidths: number[],
currentCellCount: number,
): string {
if (currentCellCount === columnWidths.length) {
return (
"║ " +
cells
.map((cell, j) => {
const displayWidth = getStringWidth(cell);
const actualLength = cell.length;
// Adjust padding to account for ANSI escape codes
return cell.padEnd(columnWidths[j] + actualLength - displayWidth);
})
.join(" │ ") +
" ║"
);
} else {
const usedWidths = columnWidths.slice(0, currentCellCount);
const remainingWidth = getUnusedColumnsWidth(
columnWidths,
currentCellCount,
);
return (
"║ " +
cells
.map((cell, j) => {
const displayWidth = getStringWidth(cell);
const actualLength = cell.length;
// Adjust padding to account for ANSI escape codes
return cell.padEnd(usedWidths[j] + actualLength - displayWidth);
})
.join(" │ ") +
" │ " +
" ".repeat(remainingWidth + 1) +
"║"
);
}
}
/**
* Renders the horizontal rule that appears above a header row.
*
* Handles three cases:
* - Transition rule: When going from more columns to fewer, shows ┴ marks
* where columns collapse (e.g., "╟───┼───┼───┴───┴───╢")
* - Full width: When header uses all columns (e.g., "╟───┬───┬───╢" or "╟───┼───┼───╢")
* - Short header: When header uses fewer columns than max (e.g., "╟───┬─────────╢")
*
* The innerJoiner determines the separator character: ┬ after section-header, ┼ otherwise.
*/
export function renderHeaderOpen(
columnWidths: number[],
currentCellCount: number,
innerJoiner: string,
needsTransition: boolean,
): string {
if (needsTransition) {
const usedWidths = columnWidths.slice(0, currentCellCount);
const collapsingWidths = columnWidths.slice(currentCellCount);
return (
"╟" +
renderRuleSegment(usedWidths, "─", "┼") +
"┼" +
renderRuleSegment(collapsingWidths, "─", "┴") +
"╢"
);
} else if (currentCellCount === columnWidths.length) {
return renderHorizontalRule("╟", columnWidths, "─", innerJoiner, "╢");
} else {
const usedWidths = columnWidths.slice(0, currentCellCount);
const remainingWidth = getUnusedColumnsWidth(
columnWidths,
currentCellCount,
);
return (
"╟" +
renderRuleSegment(usedWidths, "─", innerJoiner) +
innerJoiner +
"─".repeat(remainingWidth + 2) +
"╢"
);
}
}
/**
* Renders the horizontal rule that appears above a row.
*
* Handles two cases:
* - Full width: When row uses all columns, renders with ┼ joiners and
* ends with ╢ (e.g., "╟───┼───┼───╢")
* - Short row: When row uses fewer columns, renders active columns with
* ┼ joiners, ends with ┤, then fills remaining space and ends with ║
* (e.g., "╟───┼───┤ ║")
*/
export function renderRowSeparator(
columnWidths: number[],
currentCellCount: number,
): string {
if (currentCellCount === columnWidths.length) {
return renderHorizontalRule("╟", columnWidths, "─", "┼", "╢");
} else {
// Short row - ends with ┤ instead of ╢
const usedWidths = columnWidths.slice(0, currentCellCount);
const remainingWidth = getUnusedColumnsWidth(
columnWidths,
currentCellCount,
);
return (
"╟" +
renderRuleSegment(usedWidths, "─", "┼") +
"┤" +
" ".repeat(remainingWidth + 2) +
"║"
);
}
}
/**
* Renders the section's bottom border, placing ╧ marks under column
* separators where the last row/header had cells (e.g., if the last row
* looked like "║ a │ b │ ║", the bottom border would be
* "╚═══╧═══╧═══════╝").
*/
export function renderSectionClose(
columnWidths: number[],
previousCellCount: number,
): string {
if (previousCellCount === columnWidths.length) {
return renderHorizontalRule("╚", columnWidths, "═", "╧", "╝");
} else {
const usedWidths = columnWidths.slice(0, previousCellCount);
const unusedWidth = getUnusedColumnsWidth(columnWidths, previousCellCount);
return (
"╚" +
renderRuleSegment(usedWidths, "═", "╧") +
"╧" +
renderRuleSegment([unusedWidth], "═", "") +
"╝"
);
}
}