@crustjs/style 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +227 -0
- package/dist/index.d.ts +965 -0
- package/dist/index.js +721 -0
- package/package.json +51 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/ansiCodes.ts
|
|
3
|
+
function pair(open, close) {
|
|
4
|
+
return { open: `\x1B[${open}m`, close: `\x1B[${close}m` };
|
|
5
|
+
}
|
|
6
|
+
var reset = pair(0, 0);
|
|
7
|
+
var bold = pair(1, 22);
|
|
8
|
+
var dim = pair(2, 22);
|
|
9
|
+
var italic = pair(3, 23);
|
|
10
|
+
var underline = pair(4, 24);
|
|
11
|
+
var inverse = pair(7, 27);
|
|
12
|
+
var hidden = pair(8, 28);
|
|
13
|
+
var strikethrough = pair(9, 29);
|
|
14
|
+
var black = pair(30, 39);
|
|
15
|
+
var red = pair(31, 39);
|
|
16
|
+
var green = pair(32, 39);
|
|
17
|
+
var yellow = pair(33, 39);
|
|
18
|
+
var blue = pair(34, 39);
|
|
19
|
+
var magenta = pair(35, 39);
|
|
20
|
+
var cyan = pair(36, 39);
|
|
21
|
+
var white = pair(37, 39);
|
|
22
|
+
var gray = pair(90, 39);
|
|
23
|
+
var brightRed = pair(91, 39);
|
|
24
|
+
var brightGreen = pair(92, 39);
|
|
25
|
+
var brightYellow = pair(93, 39);
|
|
26
|
+
var brightBlue = pair(94, 39);
|
|
27
|
+
var brightMagenta = pair(95, 39);
|
|
28
|
+
var brightCyan = pair(96, 39);
|
|
29
|
+
var brightWhite = pair(97, 39);
|
|
30
|
+
var bgBlack = pair(40, 49);
|
|
31
|
+
var bgRed = pair(41, 49);
|
|
32
|
+
var bgGreen = pair(42, 49);
|
|
33
|
+
var bgYellow = pair(43, 49);
|
|
34
|
+
var bgBlue = pair(44, 49);
|
|
35
|
+
var bgMagenta = pair(45, 49);
|
|
36
|
+
var bgCyan = pair(46, 49);
|
|
37
|
+
var bgWhite = pair(47, 49);
|
|
38
|
+
var bgBrightBlack = pair(100, 49);
|
|
39
|
+
var bgBrightRed = pair(101, 49);
|
|
40
|
+
var bgBrightGreen = pair(102, 49);
|
|
41
|
+
var bgBrightYellow = pair(103, 49);
|
|
42
|
+
var bgBrightBlue = pair(104, 49);
|
|
43
|
+
var bgBrightMagenta = pair(105, 49);
|
|
44
|
+
var bgBrightCyan = pair(106, 49);
|
|
45
|
+
var bgBrightWhite = pair(107, 49);
|
|
46
|
+
// src/text/stripAnsi.ts
|
|
47
|
+
var ANSI_REGEX = /[\x1b\x9b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><~]/g;
|
|
48
|
+
function stripAnsi(text) {
|
|
49
|
+
return text.replace(ANSI_REGEX, "");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/text/width.ts
|
|
53
|
+
function isFullWidth(codePoint) {
|
|
54
|
+
return codePoint >= 19968 && codePoint <= 40959 || codePoint >= 13312 && codePoint <= 19903 || codePoint >= 131072 && codePoint <= 173791 || codePoint >= 63744 && codePoint <= 64255 || codePoint >= 65281 && codePoint <= 65376 || codePoint >= 65504 && codePoint <= 65510 || codePoint >= 11904 && codePoint <= 12031 || codePoint >= 12032 && codePoint <= 12255 || codePoint >= 12288 && codePoint <= 12351 || codePoint >= 12352 && codePoint <= 12447 || codePoint >= 12448 && codePoint <= 12543 || codePoint >= 12544 && codePoint <= 12591 || codePoint >= 12592 && codePoint <= 12687 || codePoint >= 12800 && codePoint <= 13055 || codePoint >= 13056 && codePoint <= 13311 || codePoint >= 44032 && codePoint <= 55215 || codePoint >= 65072 && codePoint <= 65103;
|
|
55
|
+
}
|
|
56
|
+
function visibleWidth(text) {
|
|
57
|
+
const plain = stripAnsi(text);
|
|
58
|
+
let width = 0;
|
|
59
|
+
for (const char of plain) {
|
|
60
|
+
const codePoint = char.codePointAt(0);
|
|
61
|
+
if (codePoint === undefined) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
width += isFullWidth(codePoint) ? 2 : 1;
|
|
65
|
+
}
|
|
66
|
+
return width;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/blocks/lists.ts
|
|
70
|
+
function indentMultiline(text, contentIndent) {
|
|
71
|
+
const lines = text.split(`
|
|
72
|
+
`);
|
|
73
|
+
if (lines.length <= 1) {
|
|
74
|
+
return text;
|
|
75
|
+
}
|
|
76
|
+
const padding = " ".repeat(contentIndent);
|
|
77
|
+
return lines.map((line, idx) => idx === 0 ? line : padding + line).join(`
|
|
78
|
+
`);
|
|
79
|
+
}
|
|
80
|
+
function unorderedList(items, options) {
|
|
81
|
+
const marker = options?.marker ?? "\u2022";
|
|
82
|
+
const markerGap = options?.markerGap ?? 1;
|
|
83
|
+
const indent = options?.indent ?? 0;
|
|
84
|
+
const prefix = " ".repeat(indent);
|
|
85
|
+
const gap = " ".repeat(markerGap);
|
|
86
|
+
const markerWidth = visibleWidth(marker);
|
|
87
|
+
const contentIndent = indent + markerWidth + markerGap;
|
|
88
|
+
return items.map((item) => {
|
|
89
|
+
const adjusted = indentMultiline(item, contentIndent);
|
|
90
|
+
return `${prefix}${marker}${gap}${adjusted}`;
|
|
91
|
+
}).join(`
|
|
92
|
+
`);
|
|
93
|
+
}
|
|
94
|
+
function orderedList(items, options) {
|
|
95
|
+
const start = options?.start ?? 1;
|
|
96
|
+
const markerGap = options?.markerGap ?? 1;
|
|
97
|
+
const indent = options?.indent ?? 0;
|
|
98
|
+
if (items.length === 0) {
|
|
99
|
+
return "";
|
|
100
|
+
}
|
|
101
|
+
const prefix = " ".repeat(indent);
|
|
102
|
+
const gap = " ".repeat(markerGap);
|
|
103
|
+
const lastIndex = start + items.length - 1;
|
|
104
|
+
const maxMarkerWidth = `${lastIndex}.`.length;
|
|
105
|
+
return items.map((item, idx) => {
|
|
106
|
+
const index = start + idx;
|
|
107
|
+
const markerText = `${index}.`;
|
|
108
|
+
const paddedMarker = markerText.padStart(maxMarkerWidth, " ");
|
|
109
|
+
const contentIndent = indent + maxMarkerWidth + markerGap;
|
|
110
|
+
const adjusted = indentMultiline(item, contentIndent);
|
|
111
|
+
return `${prefix}${paddedMarker}${gap}${adjusted}`;
|
|
112
|
+
}).join(`
|
|
113
|
+
`);
|
|
114
|
+
}
|
|
115
|
+
function taskList(items, options) {
|
|
116
|
+
const checkedMarker = options?.checkedMarker ?? "[x]";
|
|
117
|
+
const uncheckedMarker = options?.uncheckedMarker ?? "[ ]";
|
|
118
|
+
const markerGap = options?.markerGap ?? 1;
|
|
119
|
+
const indent = options?.indent ?? 0;
|
|
120
|
+
const prefix = " ".repeat(indent);
|
|
121
|
+
const gap = " ".repeat(markerGap);
|
|
122
|
+
const markerWidth = Math.max(visibleWidth(checkedMarker), visibleWidth(uncheckedMarker));
|
|
123
|
+
const contentIndent = indent + markerWidth + markerGap;
|
|
124
|
+
return items.map((item) => {
|
|
125
|
+
const marker = item.checked ? checkedMarker : uncheckedMarker;
|
|
126
|
+
const adjusted = indentMultiline(item.text, contentIndent);
|
|
127
|
+
return `${prefix}${marker}${gap}${adjusted}`;
|
|
128
|
+
}).join(`
|
|
129
|
+
`);
|
|
130
|
+
}
|
|
131
|
+
// src/text/pad.ts
|
|
132
|
+
function padStart(text, width, fillChar = " ") {
|
|
133
|
+
const currentWidth = visibleWidth(text);
|
|
134
|
+
if (currentWidth >= width) {
|
|
135
|
+
return text;
|
|
136
|
+
}
|
|
137
|
+
const padding = fillChar.repeat(width - currentWidth);
|
|
138
|
+
return padding + text;
|
|
139
|
+
}
|
|
140
|
+
function padEnd(text, width, fillChar = " ") {
|
|
141
|
+
const currentWidth = visibleWidth(text);
|
|
142
|
+
if (currentWidth >= width) {
|
|
143
|
+
return text;
|
|
144
|
+
}
|
|
145
|
+
const padding = fillChar.repeat(width - currentWidth);
|
|
146
|
+
return text + padding;
|
|
147
|
+
}
|
|
148
|
+
function center(text, width, fillChar = " ") {
|
|
149
|
+
const currentWidth = visibleWidth(text);
|
|
150
|
+
if (currentWidth >= width) {
|
|
151
|
+
return text;
|
|
152
|
+
}
|
|
153
|
+
const totalPadding = width - currentWidth;
|
|
154
|
+
const leftPadding = Math.floor(totalPadding / 2);
|
|
155
|
+
const rightPadding = totalPadding - leftPadding;
|
|
156
|
+
return fillChar.repeat(leftPadding) + text + fillChar.repeat(rightPadding);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/blocks/tables.ts
|
|
160
|
+
function computeColumnWidths(headers, rows, minWidth) {
|
|
161
|
+
const columnCount = headers.length;
|
|
162
|
+
const widths = new Array(columnCount).fill(minWidth);
|
|
163
|
+
for (let col = 0;col < columnCount; col++) {
|
|
164
|
+
const header = headers[col];
|
|
165
|
+
if (header !== undefined) {
|
|
166
|
+
widths[col] = Math.max(widths[col] ?? 0, visibleWidth(header));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
for (const row of rows) {
|
|
170
|
+
for (let col = 0;col < columnCount; col++) {
|
|
171
|
+
const cell = row[col];
|
|
172
|
+
if (cell !== undefined) {
|
|
173
|
+
widths[col] = Math.max(widths[col] ?? 0, visibleWidth(cell));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return widths;
|
|
178
|
+
}
|
|
179
|
+
function alignCell(value, width, alignment) {
|
|
180
|
+
switch (alignment) {
|
|
181
|
+
case "right":
|
|
182
|
+
return padStart(value, width);
|
|
183
|
+
case "center":
|
|
184
|
+
return center(value, width);
|
|
185
|
+
default:
|
|
186
|
+
return padEnd(value, width);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function formatRow(cells, columnWidths, alignments, cellPadding, borderChar) {
|
|
190
|
+
const pad = " ".repeat(cellPadding);
|
|
191
|
+
const formattedCells = columnWidths.map((width, col) => {
|
|
192
|
+
const cell = cells[col] ?? "";
|
|
193
|
+
const alignment = alignments[col] ?? "left";
|
|
194
|
+
const aligned = alignCell(cell, width, alignment);
|
|
195
|
+
return `${pad}${aligned}${pad}`;
|
|
196
|
+
});
|
|
197
|
+
return `${borderChar}${formattedCells.join(borderChar)}${borderChar}`;
|
|
198
|
+
}
|
|
199
|
+
function formatSeparator(columnWidths, cellPadding, separatorChar, borderChar) {
|
|
200
|
+
const segments = columnWidths.map((width) => {
|
|
201
|
+
return separatorChar.repeat(width + cellPadding * 2);
|
|
202
|
+
});
|
|
203
|
+
return `${borderChar}${segments.join(borderChar)}${borderChar}`;
|
|
204
|
+
}
|
|
205
|
+
function table(headers, rows, options) {
|
|
206
|
+
const alignments = options?.align ?? [];
|
|
207
|
+
const minColumnWidth = options?.minColumnWidth ?? 0;
|
|
208
|
+
const cellPadding = options?.cellPadding ?? 1;
|
|
209
|
+
const separatorChar = options?.separatorChar ?? "-";
|
|
210
|
+
const borderChar = options?.borderChar ?? "|";
|
|
211
|
+
const columnWidths = computeColumnWidths(headers, rows, minColumnWidth);
|
|
212
|
+
const lines = [];
|
|
213
|
+
lines.push(formatRow(headers, columnWidths, alignments, cellPadding, borderChar));
|
|
214
|
+
lines.push(formatSeparator(columnWidths, cellPadding, separatorChar, borderChar));
|
|
215
|
+
for (const row of rows) {
|
|
216
|
+
lines.push(formatRow(row, columnWidths, alignments, cellPadding, borderChar));
|
|
217
|
+
}
|
|
218
|
+
return lines.join(`
|
|
219
|
+
`);
|
|
220
|
+
}
|
|
221
|
+
// src/capability.ts
|
|
222
|
+
function resolveCapability(mode, overrides) {
|
|
223
|
+
if (mode === "always") {
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
if (mode === "never") {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
const isTTY = overrides?.isTTY ?? process.stdout?.isTTY ?? false;
|
|
230
|
+
const noColor = overrides?.noColor !== undefined ? overrides.noColor : process.env.NO_COLOR;
|
|
231
|
+
if (noColor !== undefined) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return isTTY;
|
|
235
|
+
}
|
|
236
|
+
// src/styleEngine.ts
|
|
237
|
+
function applyStyle(text, style) {
|
|
238
|
+
if (text === "") {
|
|
239
|
+
return "";
|
|
240
|
+
}
|
|
241
|
+
const { open, close } = style;
|
|
242
|
+
if (text.includes(close)) {
|
|
243
|
+
text = text.replaceAll(close, close + open);
|
|
244
|
+
}
|
|
245
|
+
return open + text + close;
|
|
246
|
+
}
|
|
247
|
+
function composeStyles(...styles) {
|
|
248
|
+
return {
|
|
249
|
+
open: styles.map((s) => s.open).join(""),
|
|
250
|
+
close: styles.map((s) => s.close).reverse().join("")
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// src/colors.ts
|
|
255
|
+
function black2(text) {
|
|
256
|
+
return applyStyle(text, black);
|
|
257
|
+
}
|
|
258
|
+
function red2(text) {
|
|
259
|
+
return applyStyle(text, red);
|
|
260
|
+
}
|
|
261
|
+
function green2(text) {
|
|
262
|
+
return applyStyle(text, green);
|
|
263
|
+
}
|
|
264
|
+
function yellow2(text) {
|
|
265
|
+
return applyStyle(text, yellow);
|
|
266
|
+
}
|
|
267
|
+
function blue2(text) {
|
|
268
|
+
return applyStyle(text, blue);
|
|
269
|
+
}
|
|
270
|
+
function magenta2(text) {
|
|
271
|
+
return applyStyle(text, magenta);
|
|
272
|
+
}
|
|
273
|
+
function cyan2(text) {
|
|
274
|
+
return applyStyle(text, cyan);
|
|
275
|
+
}
|
|
276
|
+
function white2(text) {
|
|
277
|
+
return applyStyle(text, white);
|
|
278
|
+
}
|
|
279
|
+
function gray2(text) {
|
|
280
|
+
return applyStyle(text, gray);
|
|
281
|
+
}
|
|
282
|
+
function brightRed2(text) {
|
|
283
|
+
return applyStyle(text, brightRed);
|
|
284
|
+
}
|
|
285
|
+
function brightGreen2(text) {
|
|
286
|
+
return applyStyle(text, brightGreen);
|
|
287
|
+
}
|
|
288
|
+
function brightYellow2(text) {
|
|
289
|
+
return applyStyle(text, brightYellow);
|
|
290
|
+
}
|
|
291
|
+
function brightBlue2(text) {
|
|
292
|
+
return applyStyle(text, brightBlue);
|
|
293
|
+
}
|
|
294
|
+
function brightMagenta2(text) {
|
|
295
|
+
return applyStyle(text, brightMagenta);
|
|
296
|
+
}
|
|
297
|
+
function brightCyan2(text) {
|
|
298
|
+
return applyStyle(text, brightCyan);
|
|
299
|
+
}
|
|
300
|
+
function brightWhite2(text) {
|
|
301
|
+
return applyStyle(text, brightWhite);
|
|
302
|
+
}
|
|
303
|
+
function bgBlack2(text) {
|
|
304
|
+
return applyStyle(text, bgBlack);
|
|
305
|
+
}
|
|
306
|
+
function bgRed2(text) {
|
|
307
|
+
return applyStyle(text, bgRed);
|
|
308
|
+
}
|
|
309
|
+
function bgGreen2(text) {
|
|
310
|
+
return applyStyle(text, bgGreen);
|
|
311
|
+
}
|
|
312
|
+
function bgYellow2(text) {
|
|
313
|
+
return applyStyle(text, bgYellow);
|
|
314
|
+
}
|
|
315
|
+
function bgBlue2(text) {
|
|
316
|
+
return applyStyle(text, bgBlue);
|
|
317
|
+
}
|
|
318
|
+
function bgMagenta2(text) {
|
|
319
|
+
return applyStyle(text, bgMagenta);
|
|
320
|
+
}
|
|
321
|
+
function bgCyan2(text) {
|
|
322
|
+
return applyStyle(text, bgCyan);
|
|
323
|
+
}
|
|
324
|
+
function bgWhite2(text) {
|
|
325
|
+
return applyStyle(text, bgWhite);
|
|
326
|
+
}
|
|
327
|
+
function bgBrightBlack2(text) {
|
|
328
|
+
return applyStyle(text, bgBrightBlack);
|
|
329
|
+
}
|
|
330
|
+
function bgBrightRed2(text) {
|
|
331
|
+
return applyStyle(text, bgBrightRed);
|
|
332
|
+
}
|
|
333
|
+
function bgBrightGreen2(text) {
|
|
334
|
+
return applyStyle(text, bgBrightGreen);
|
|
335
|
+
}
|
|
336
|
+
function bgBrightYellow2(text) {
|
|
337
|
+
return applyStyle(text, bgBrightYellow);
|
|
338
|
+
}
|
|
339
|
+
function bgBrightBlue2(text) {
|
|
340
|
+
return applyStyle(text, bgBrightBlue);
|
|
341
|
+
}
|
|
342
|
+
function bgBrightMagenta2(text) {
|
|
343
|
+
return applyStyle(text, bgBrightMagenta);
|
|
344
|
+
}
|
|
345
|
+
function bgBrightCyan2(text) {
|
|
346
|
+
return applyStyle(text, bgBrightCyan);
|
|
347
|
+
}
|
|
348
|
+
function bgBrightWhite2(text) {
|
|
349
|
+
return applyStyle(text, bgBrightWhite);
|
|
350
|
+
}
|
|
351
|
+
// src/createStyle.ts
|
|
352
|
+
function makeStyleFn(pair2, enabled) {
|
|
353
|
+
if (enabled) {
|
|
354
|
+
return (text) => applyStyle(text, pair2);
|
|
355
|
+
}
|
|
356
|
+
return (text) => text;
|
|
357
|
+
}
|
|
358
|
+
function createStyle(options) {
|
|
359
|
+
const mode = options?.mode ?? "auto";
|
|
360
|
+
const enabled = resolveCapability(mode, options?.overrides);
|
|
361
|
+
const instance = {
|
|
362
|
+
enabled,
|
|
363
|
+
apply: enabled ? (text, pair2) => applyStyle(text, pair2) : (text, _pair) => text,
|
|
364
|
+
bold: makeStyleFn(bold, enabled),
|
|
365
|
+
dim: makeStyleFn(dim, enabled),
|
|
366
|
+
italic: makeStyleFn(italic, enabled),
|
|
367
|
+
underline: makeStyleFn(underline, enabled),
|
|
368
|
+
inverse: makeStyleFn(inverse, enabled),
|
|
369
|
+
hidden: makeStyleFn(hidden, enabled),
|
|
370
|
+
strikethrough: makeStyleFn(strikethrough, enabled),
|
|
371
|
+
black: makeStyleFn(black, enabled),
|
|
372
|
+
red: makeStyleFn(red, enabled),
|
|
373
|
+
green: makeStyleFn(green, enabled),
|
|
374
|
+
yellow: makeStyleFn(yellow, enabled),
|
|
375
|
+
blue: makeStyleFn(blue, enabled),
|
|
376
|
+
magenta: makeStyleFn(magenta, enabled),
|
|
377
|
+
cyan: makeStyleFn(cyan, enabled),
|
|
378
|
+
white: makeStyleFn(white, enabled),
|
|
379
|
+
gray: makeStyleFn(gray, enabled),
|
|
380
|
+
brightRed: makeStyleFn(brightRed, enabled),
|
|
381
|
+
brightGreen: makeStyleFn(brightGreen, enabled),
|
|
382
|
+
brightYellow: makeStyleFn(brightYellow, enabled),
|
|
383
|
+
brightBlue: makeStyleFn(brightBlue, enabled),
|
|
384
|
+
brightMagenta: makeStyleFn(brightMagenta, enabled),
|
|
385
|
+
brightCyan: makeStyleFn(brightCyan, enabled),
|
|
386
|
+
brightWhite: makeStyleFn(brightWhite, enabled),
|
|
387
|
+
bgBlack: makeStyleFn(bgBlack, enabled),
|
|
388
|
+
bgRed: makeStyleFn(bgRed, enabled),
|
|
389
|
+
bgGreen: makeStyleFn(bgGreen, enabled),
|
|
390
|
+
bgYellow: makeStyleFn(bgYellow, enabled),
|
|
391
|
+
bgBlue: makeStyleFn(bgBlue, enabled),
|
|
392
|
+
bgMagenta: makeStyleFn(bgMagenta, enabled),
|
|
393
|
+
bgCyan: makeStyleFn(bgCyan, enabled),
|
|
394
|
+
bgWhite: makeStyleFn(bgWhite, enabled),
|
|
395
|
+
bgBrightBlack: makeStyleFn(bgBrightBlack, enabled),
|
|
396
|
+
bgBrightRed: makeStyleFn(bgBrightRed, enabled),
|
|
397
|
+
bgBrightGreen: makeStyleFn(bgBrightGreen, enabled),
|
|
398
|
+
bgBrightYellow: makeStyleFn(bgBrightYellow, enabled),
|
|
399
|
+
bgBrightBlue: makeStyleFn(bgBrightBlue, enabled),
|
|
400
|
+
bgBrightMagenta: makeStyleFn(bgBrightMagenta, enabled),
|
|
401
|
+
bgBrightCyan: makeStyleFn(bgBrightCyan, enabled),
|
|
402
|
+
bgBrightWhite: makeStyleFn(bgBrightWhite, enabled)
|
|
403
|
+
};
|
|
404
|
+
return Object.freeze(instance);
|
|
405
|
+
}
|
|
406
|
+
var style = createStyle();
|
|
407
|
+
// src/modifiers.ts
|
|
408
|
+
function bold2(text) {
|
|
409
|
+
return applyStyle(text, bold);
|
|
410
|
+
}
|
|
411
|
+
function dim2(text) {
|
|
412
|
+
return applyStyle(text, dim);
|
|
413
|
+
}
|
|
414
|
+
function italic2(text) {
|
|
415
|
+
return applyStyle(text, italic);
|
|
416
|
+
}
|
|
417
|
+
function underline2(text) {
|
|
418
|
+
return applyStyle(text, underline);
|
|
419
|
+
}
|
|
420
|
+
function inverse2(text) {
|
|
421
|
+
return applyStyle(text, inverse);
|
|
422
|
+
}
|
|
423
|
+
function hidden2(text) {
|
|
424
|
+
return applyStyle(text, hidden);
|
|
425
|
+
}
|
|
426
|
+
function strikethrough2(text) {
|
|
427
|
+
return applyStyle(text, strikethrough);
|
|
428
|
+
}
|
|
429
|
+
// src/text/wrap.ts
|
|
430
|
+
var ANSI_SEQUENCE = /[\x1b\x9b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><~]/;
|
|
431
|
+
function isFullWidth2(codePoint) {
|
|
432
|
+
return codePoint >= 19968 && codePoint <= 40959 || codePoint >= 13312 && codePoint <= 19903 || codePoint >= 131072 && codePoint <= 173791 || codePoint >= 63744 && codePoint <= 64255 || codePoint >= 65281 && codePoint <= 65376 || codePoint >= 65504 && codePoint <= 65510 || codePoint >= 11904 && codePoint <= 12031 || codePoint >= 12032 && codePoint <= 12255 || codePoint >= 12288 && codePoint <= 12351 || codePoint >= 12352 && codePoint <= 12447 || codePoint >= 12448 && codePoint <= 12543 || codePoint >= 12544 && codePoint <= 12591 || codePoint >= 12592 && codePoint <= 12687 || codePoint >= 12800 && codePoint <= 13055 || codePoint >= 13056 && codePoint <= 13311 || codePoint >= 44032 && codePoint <= 55215 || codePoint >= 65072 && codePoint <= 65103;
|
|
433
|
+
}
|
|
434
|
+
function isReset(seq) {
|
|
435
|
+
return seq === "\x1B[0m";
|
|
436
|
+
}
|
|
437
|
+
function isSGR(seq) {
|
|
438
|
+
return seq.endsWith("m") && seq.startsWith("\x1B[");
|
|
439
|
+
}
|
|
440
|
+
function wrapText(text, width, options) {
|
|
441
|
+
if (width <= 0) {
|
|
442
|
+
return text;
|
|
443
|
+
}
|
|
444
|
+
const wordBreak = options?.wordBreak ?? true;
|
|
445
|
+
const inputLines = text.split(`
|
|
446
|
+
`);
|
|
447
|
+
const resultLines = [];
|
|
448
|
+
let activeStyles = [];
|
|
449
|
+
for (const inputLine of inputLines) {
|
|
450
|
+
const wrapped = wrapLine(inputLine, width, wordBreak, activeStyles);
|
|
451
|
+
resultLines.push(...wrapped.lines);
|
|
452
|
+
activeStyles = wrapped.activeStyles;
|
|
453
|
+
}
|
|
454
|
+
return resultLines.join(`
|
|
455
|
+
`);
|
|
456
|
+
}
|
|
457
|
+
function wrapLine(line, width, wordBreak, initialStyles) {
|
|
458
|
+
const activeStyles = [...initialStyles];
|
|
459
|
+
const lines = [];
|
|
460
|
+
let currentLine = activeStyles.length > 0 ? activeStyles.join("") : "";
|
|
461
|
+
let currentWidth = 0;
|
|
462
|
+
let lastSpaceIdx = -1;
|
|
463
|
+
let lastSpaceWidth = 0;
|
|
464
|
+
let lastSpaceStyleSnapshot = [];
|
|
465
|
+
let i = 0;
|
|
466
|
+
while (i < line.length) {
|
|
467
|
+
const ansiMatch = line.slice(i).match(ANSI_SEQUENCE);
|
|
468
|
+
if (ansiMatch && ansiMatch.index === 0) {
|
|
469
|
+
const seq = ansiMatch[0];
|
|
470
|
+
if (isSGR(seq)) {
|
|
471
|
+
if (isReset(seq)) {
|
|
472
|
+
activeStyles.length = 0;
|
|
473
|
+
} else {
|
|
474
|
+
activeStyles.push(seq);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
currentLine += seq;
|
|
478
|
+
i += seq.length;
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
const char = line[i];
|
|
482
|
+
if (char === undefined) {
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
const codePoint = char.codePointAt(0) ?? 0;
|
|
486
|
+
const charWidth = isFullWidth2(codePoint) ? 2 : 1;
|
|
487
|
+
if (currentWidth + charWidth > width) {
|
|
488
|
+
if (wordBreak && lastSpaceIdx !== -1) {
|
|
489
|
+
const beforeSpace = currentLine.slice(0, lastSpaceIdx);
|
|
490
|
+
const afterSpace = currentLine.slice(lastSpaceIdx + 1);
|
|
491
|
+
const closeSeq = activeStyles.length > 0 ? "\x1B[0m" : "";
|
|
492
|
+
lines.push(beforeSpace + closeSeq);
|
|
493
|
+
const reopenSeq = lastSpaceStyleSnapshot.length > 0 ? lastSpaceStyleSnapshot.join("") : "";
|
|
494
|
+
currentLine = reopenSeq + afterSpace;
|
|
495
|
+
currentWidth = currentWidth - lastSpaceWidth;
|
|
496
|
+
lastSpaceIdx = -1;
|
|
497
|
+
lastSpaceWidth = 0;
|
|
498
|
+
lastSpaceStyleSnapshot = [];
|
|
499
|
+
if (currentWidth + charWidth > width) {
|
|
500
|
+
const closeSeq2 = activeStyles.length > 0 ? "\x1B[0m" : "";
|
|
501
|
+
lines.push(currentLine + closeSeq2);
|
|
502
|
+
const reopenSeq2 = activeStyles.length > 0 ? activeStyles.join("") : "";
|
|
503
|
+
currentLine = reopenSeq2 + char;
|
|
504
|
+
currentWidth = charWidth;
|
|
505
|
+
} else {
|
|
506
|
+
currentLine += char;
|
|
507
|
+
currentWidth += charWidth;
|
|
508
|
+
}
|
|
509
|
+
} else {
|
|
510
|
+
const closeSeq = activeStyles.length > 0 ? "\x1B[0m" : "";
|
|
511
|
+
lines.push(currentLine + closeSeq);
|
|
512
|
+
const reopenSeq = activeStyles.length > 0 ? activeStyles.join("") : "";
|
|
513
|
+
currentLine = reopenSeq + char;
|
|
514
|
+
currentWidth = charWidth;
|
|
515
|
+
lastSpaceIdx = -1;
|
|
516
|
+
lastSpaceWidth = 0;
|
|
517
|
+
lastSpaceStyleSnapshot = [];
|
|
518
|
+
}
|
|
519
|
+
} else {
|
|
520
|
+
if (wordBreak && char === " ") {
|
|
521
|
+
currentLine += char;
|
|
522
|
+
currentWidth += charWidth;
|
|
523
|
+
lastSpaceIdx = currentLine.length - 1;
|
|
524
|
+
lastSpaceWidth = currentWidth;
|
|
525
|
+
lastSpaceStyleSnapshot = [...activeStyles];
|
|
526
|
+
} else {
|
|
527
|
+
currentLine += char;
|
|
528
|
+
currentWidth += charWidth;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
i += char.length;
|
|
532
|
+
}
|
|
533
|
+
if (currentLine.length > 0) {
|
|
534
|
+
const hasVisibleContent = currentLine.replace(/[\x1b\x9b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><~]/g, "");
|
|
535
|
+
if (hasVisibleContent.length > 0 || currentLine.length > 0) {
|
|
536
|
+
lines.push(currentLine);
|
|
537
|
+
}
|
|
538
|
+
} else {
|
|
539
|
+
lines.push("");
|
|
540
|
+
}
|
|
541
|
+
return { lines, activeStyles };
|
|
542
|
+
}
|
|
543
|
+
// src/theme/markdownTheme.ts
|
|
544
|
+
function buildDefaultMarkdownTheme(s) {
|
|
545
|
+
const theme = {
|
|
546
|
+
heading1: (value) => s.bold(s.underline(value)),
|
|
547
|
+
heading2: (value) => s.bold(value),
|
|
548
|
+
heading3: (value) => s.bold(s.yellow(value)),
|
|
549
|
+
heading4: (value) => s.yellow(value),
|
|
550
|
+
heading5: (value) => s.dim(s.yellow(value)),
|
|
551
|
+
heading6: (value) => s.dim(value),
|
|
552
|
+
text: (value) => value,
|
|
553
|
+
emphasis: (value) => s.italic(value),
|
|
554
|
+
strong: (value) => s.bold(value),
|
|
555
|
+
strongEmphasis: (value) => s.bold(s.italic(value)),
|
|
556
|
+
strikethrough: (value) => s.strikethrough(value),
|
|
557
|
+
inlineCode: (value) => s.cyan(value),
|
|
558
|
+
linkText: (value) => s.blue(s.underline(value)),
|
|
559
|
+
linkUrl: (value) => s.dim(s.underline(value)),
|
|
560
|
+
autolink: (value) => s.blue(s.underline(value)),
|
|
561
|
+
blockquoteMarker: (value) => s.dim(s.green(value)),
|
|
562
|
+
blockquoteText: (value) => s.italic(value),
|
|
563
|
+
listMarker: (value) => s.dim(value),
|
|
564
|
+
orderedListMarker: (value) => s.dim(value),
|
|
565
|
+
taskChecked: (value) => s.green(value),
|
|
566
|
+
taskUnchecked: (value) => s.dim(value),
|
|
567
|
+
codeFence: (value) => s.dim(value),
|
|
568
|
+
codeInfo: (value) => s.dim(s.italic(value)),
|
|
569
|
+
codeText: (value) => s.cyan(value),
|
|
570
|
+
thematicBreak: (value) => s.dim(value),
|
|
571
|
+
tableHeader: (value) => s.bold(value),
|
|
572
|
+
tableCell: (value) => value,
|
|
573
|
+
tableBorder: (value) => s.dim(value),
|
|
574
|
+
imageAltText: (value) => s.italic(s.magenta(value)),
|
|
575
|
+
imageUrl: (value) => s.dim(s.underline(value))
|
|
576
|
+
};
|
|
577
|
+
return Object.freeze(theme);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// src/theme/createMarkdownTheme.ts
|
|
581
|
+
function createMarkdownTheme(options) {
|
|
582
|
+
const styleInstance = createStyle(options?.style);
|
|
583
|
+
const base = buildDefaultMarkdownTheme(styleInstance);
|
|
584
|
+
const overrides = options?.overrides;
|
|
585
|
+
if (!overrides) {
|
|
586
|
+
return base;
|
|
587
|
+
}
|
|
588
|
+
const merged = {
|
|
589
|
+
heading1: overrides.heading1 ?? base.heading1,
|
|
590
|
+
heading2: overrides.heading2 ?? base.heading2,
|
|
591
|
+
heading3: overrides.heading3 ?? base.heading3,
|
|
592
|
+
heading4: overrides.heading4 ?? base.heading4,
|
|
593
|
+
heading5: overrides.heading5 ?? base.heading5,
|
|
594
|
+
heading6: overrides.heading6 ?? base.heading6,
|
|
595
|
+
text: overrides.text ?? base.text,
|
|
596
|
+
emphasis: overrides.emphasis ?? base.emphasis,
|
|
597
|
+
strong: overrides.strong ?? base.strong,
|
|
598
|
+
strongEmphasis: overrides.strongEmphasis ?? base.strongEmphasis,
|
|
599
|
+
strikethrough: overrides.strikethrough ?? base.strikethrough,
|
|
600
|
+
inlineCode: overrides.inlineCode ?? base.inlineCode,
|
|
601
|
+
linkText: overrides.linkText ?? base.linkText,
|
|
602
|
+
linkUrl: overrides.linkUrl ?? base.linkUrl,
|
|
603
|
+
autolink: overrides.autolink ?? base.autolink,
|
|
604
|
+
blockquoteMarker: overrides.blockquoteMarker ?? base.blockquoteMarker,
|
|
605
|
+
blockquoteText: overrides.blockquoteText ?? base.blockquoteText,
|
|
606
|
+
listMarker: overrides.listMarker ?? base.listMarker,
|
|
607
|
+
orderedListMarker: overrides.orderedListMarker ?? base.orderedListMarker,
|
|
608
|
+
taskChecked: overrides.taskChecked ?? base.taskChecked,
|
|
609
|
+
taskUnchecked: overrides.taskUnchecked ?? base.taskUnchecked,
|
|
610
|
+
codeFence: overrides.codeFence ?? base.codeFence,
|
|
611
|
+
codeInfo: overrides.codeInfo ?? base.codeInfo,
|
|
612
|
+
codeText: overrides.codeText ?? base.codeText,
|
|
613
|
+
thematicBreak: overrides.thematicBreak ?? base.thematicBreak,
|
|
614
|
+
tableHeader: overrides.tableHeader ?? base.tableHeader,
|
|
615
|
+
tableCell: overrides.tableCell ?? base.tableCell,
|
|
616
|
+
tableBorder: overrides.tableBorder ?? base.tableBorder,
|
|
617
|
+
imageAltText: overrides.imageAltText ?? base.imageAltText,
|
|
618
|
+
imageUrl: overrides.imageUrl ?? base.imageUrl
|
|
619
|
+
};
|
|
620
|
+
return Object.freeze(merged);
|
|
621
|
+
}
|
|
622
|
+
var defaultTheme = createMarkdownTheme();
|
|
623
|
+
export {
|
|
624
|
+
yellow as yellowCode,
|
|
625
|
+
yellow2 as yellow,
|
|
626
|
+
wrapText,
|
|
627
|
+
white as whiteCode,
|
|
628
|
+
white2 as white,
|
|
629
|
+
visibleWidth,
|
|
630
|
+
unorderedList,
|
|
631
|
+
underline as underlineCode,
|
|
632
|
+
underline2 as underline,
|
|
633
|
+
taskList,
|
|
634
|
+
table,
|
|
635
|
+
style,
|
|
636
|
+
stripAnsi,
|
|
637
|
+
strikethrough as strikethroughCode,
|
|
638
|
+
strikethrough2 as strikethrough,
|
|
639
|
+
resolveCapability,
|
|
640
|
+
reset,
|
|
641
|
+
red as redCode,
|
|
642
|
+
red2 as red,
|
|
643
|
+
padStart,
|
|
644
|
+
padEnd,
|
|
645
|
+
orderedList,
|
|
646
|
+
magenta as magentaCode,
|
|
647
|
+
magenta2 as magenta,
|
|
648
|
+
italic as italicCode,
|
|
649
|
+
italic2 as italic,
|
|
650
|
+
inverse as inverseCode,
|
|
651
|
+
inverse2 as inverse,
|
|
652
|
+
hidden as hiddenCode,
|
|
653
|
+
hidden2 as hidden,
|
|
654
|
+
green as greenCode,
|
|
655
|
+
green2 as green,
|
|
656
|
+
gray as grayCode,
|
|
657
|
+
gray2 as gray,
|
|
658
|
+
dim as dimCode,
|
|
659
|
+
dim2 as dim,
|
|
660
|
+
defaultTheme,
|
|
661
|
+
cyan as cyanCode,
|
|
662
|
+
cyan2 as cyan,
|
|
663
|
+
createStyle,
|
|
664
|
+
createMarkdownTheme,
|
|
665
|
+
composeStyles,
|
|
666
|
+
center,
|
|
667
|
+
buildDefaultMarkdownTheme,
|
|
668
|
+
brightYellow as brightYellowCode,
|
|
669
|
+
brightYellow2 as brightYellow,
|
|
670
|
+
brightWhite as brightWhiteCode,
|
|
671
|
+
brightWhite2 as brightWhite,
|
|
672
|
+
brightRed as brightRedCode,
|
|
673
|
+
brightRed2 as brightRed,
|
|
674
|
+
brightMagenta as brightMagentaCode,
|
|
675
|
+
brightMagenta2 as brightMagenta,
|
|
676
|
+
brightGreen as brightGreenCode,
|
|
677
|
+
brightGreen2 as brightGreen,
|
|
678
|
+
brightCyan as brightCyanCode,
|
|
679
|
+
brightCyan2 as brightCyan,
|
|
680
|
+
brightBlue as brightBlueCode,
|
|
681
|
+
brightBlue2 as brightBlue,
|
|
682
|
+
bold as boldCode,
|
|
683
|
+
bold2 as bold,
|
|
684
|
+
blue as blueCode,
|
|
685
|
+
blue2 as blue,
|
|
686
|
+
black as blackCode,
|
|
687
|
+
black2 as black,
|
|
688
|
+
bgYellow as bgYellowCode,
|
|
689
|
+
bgYellow2 as bgYellow,
|
|
690
|
+
bgWhite as bgWhiteCode,
|
|
691
|
+
bgWhite2 as bgWhite,
|
|
692
|
+
bgRed as bgRedCode,
|
|
693
|
+
bgRed2 as bgRed,
|
|
694
|
+
bgMagenta as bgMagentaCode,
|
|
695
|
+
bgMagenta2 as bgMagenta,
|
|
696
|
+
bgGreen as bgGreenCode,
|
|
697
|
+
bgGreen2 as bgGreen,
|
|
698
|
+
bgCyan as bgCyanCode,
|
|
699
|
+
bgCyan2 as bgCyan,
|
|
700
|
+
bgBrightYellow as bgBrightYellowCode,
|
|
701
|
+
bgBrightYellow2 as bgBrightYellow,
|
|
702
|
+
bgBrightWhite as bgBrightWhiteCode,
|
|
703
|
+
bgBrightWhite2 as bgBrightWhite,
|
|
704
|
+
bgBrightRed as bgBrightRedCode,
|
|
705
|
+
bgBrightRed2 as bgBrightRed,
|
|
706
|
+
bgBrightMagenta as bgBrightMagentaCode,
|
|
707
|
+
bgBrightMagenta2 as bgBrightMagenta,
|
|
708
|
+
bgBrightGreen as bgBrightGreenCode,
|
|
709
|
+
bgBrightGreen2 as bgBrightGreen,
|
|
710
|
+
bgBrightCyan as bgBrightCyanCode,
|
|
711
|
+
bgBrightCyan2 as bgBrightCyan,
|
|
712
|
+
bgBrightBlue as bgBrightBlueCode,
|
|
713
|
+
bgBrightBlue2 as bgBrightBlue,
|
|
714
|
+
bgBrightBlack as bgBrightBlackCode,
|
|
715
|
+
bgBrightBlack2 as bgBrightBlack,
|
|
716
|
+
bgBlue as bgBlueCode,
|
|
717
|
+
bgBlue2 as bgBlue,
|
|
718
|
+
bgBlack as bgBlackCode,
|
|
719
|
+
bgBlack2 as bgBlack,
|
|
720
|
+
applyStyle
|
|
721
|
+
};
|