@visulima/pail 4.0.0-alpha.10 → 4.0.0-alpha.12
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/CHANGELOG.md +57 -0
- package/LICENSE.md +143 -931
- package/dist/error.d.ts +95 -96
- package/dist/index.browser.d.ts +557 -47
- package/dist/index.browser.js +2 -2
- package/dist/index.server.d.ts +722 -65
- package/dist/index.server.js +27 -410
- package/dist/middleware/elysia.d.ts +65 -63
- package/dist/middleware/elysia.js +2 -2
- package/dist/middleware/express.d.ts +73 -71
- package/dist/middleware/express.js +1 -1
- package/dist/middleware/fastify.d.ts +69 -67
- package/dist/middleware/fastify.js +2 -2
- package/dist/middleware/hono.d.ts +74 -72
- package/dist/middleware/next/handler.d.ts +101 -34
- package/dist/middleware/sveltekit.d.ts +100 -98
- package/dist/object-tree.d.ts +67 -70
- package/dist/packem_shared/{AbstractJsonReporter-DlugSJpY.js → AbstractJsonReporter-BO8Calb4.js} +1 -5
- package/dist/packem_shared/{AbstractJsonReporter-CjtVgHbU.js → AbstractJsonReporter-nOj0Ft1F.js} +1 -5
- package/dist/packem_shared/{JsonReporter-Dbw82ewj.js → JsonReporter-CCmj7oYL.js} +1 -1
- package/dist/packem_shared/{JsonReporter-BgPvIyC2.js → JsonReporter-Ck2PIAEw.js} +2 -2
- package/dist/packem_shared/{PrettyReporter-C2dCzIaf.js → PrettyReporter-CIbrmjUV.js} +2 -4
- package/dist/packem_shared/{PrettyReporter-gMqa7j_m.js → PrettyReporter-CNJEO9g7.js} +1250 -15
- package/dist/packem_shared/abstract-json-reporter.d-BAgznjyU.d.ts +61 -0
- package/dist/packem_shared/{abstract-pretty-reporter-szQO-IgK.js → abstract-pretty-reporter-CkqCt5hg.js} +2300 -2298
- package/dist/packem_shared/create-middleware-logger.d-DheMh8q4.d.ts +47 -0
- package/dist/packem_shared/{index-Bx3-C0j9.js → index-6cG1Kp0t.js} +2 -2
- package/dist/packem_shared/types.d-BeLumqgD.d.ts +246 -0
- package/dist/packem_shared/types.d-C51XNfQz.d.ts +194 -0
- package/dist/packem_shared/types.d-CM5ie2qm.d.ts +236 -0
- package/dist/packem_shared/wide-event.d-B-t8ZnhI.d.ts +704 -0
- package/dist/packem_shared/{write-stream-BuFtjATz.js → write-stream-MDqyXmc_.js} +1 -1
- package/dist/processor/caller/caller-processor.d.ts +49 -47
- package/dist/processor/caller/caller-processor.js +1 -1
- package/dist/processor/environment-processor.d.ts +113 -113
- package/dist/processor/message-formatter-processor.d.ts +51 -49
- package/dist/processor/message-formatter-processor.js +2 -2
- package/dist/processor/opentelemetry-processor.d.ts +69 -66
- package/dist/processor/redact-processor.d.ts +60 -41
- package/dist/processor/sampling-processor.d.ts +102 -101
- package/dist/reporter/file/json-file-reporter.d.ts +91 -42
- package/dist/reporter/file/json-file-reporter.js +1 -1
- package/dist/reporter/http/abstract-http-reporter.d.ts +213 -211
- package/dist/reporter/http/abstract-http-reporter.js +1 -1
- package/dist/reporter/http/http-reporter.d.ts +40 -36
- package/dist/reporter/http/http-reporter.edge-light.d.ts +430 -38
- package/dist/reporter/http/http-reporter.edge-light.js +1 -5
- package/dist/reporter/json/index.browser.d.ts +99 -3
- package/dist/reporter/json/index.browser.js +2 -2
- package/dist/reporter/json/index.d.ts +51 -3
- package/dist/reporter/json/index.js +2 -2
- package/dist/reporter/pretty/index.browser.d.ts +106 -2
- package/dist/reporter/pretty/index.browser.js +1 -1
- package/dist/reporter/pretty/index.d.ts +219 -2
- package/dist/reporter/pretty/index.js +1 -1
- package/dist/reporter/simple/simple-reporter.server.d.ts +168 -18
- package/dist/reporter/simple/simple-reporter.server.js +3 -2
- package/dist/wide-event.d.ts +5 -300
- package/dist/wide-event.js +3 -2
- package/package.json +9 -21
- package/dist/constants.d.ts +0 -37
- package/dist/interactive/index.d.ts +0 -2
- package/dist/interactive/index.js +0 -2
- package/dist/interactive/interactive-manager.d.ts +0 -108
- package/dist/interactive/interactive-stream-hook.d.ts +0 -68
- package/dist/middleware/next/middleware.d.ts +0 -59
- package/dist/middleware/next/storage.d.ts +0 -14
- package/dist/middleware/shared/create-middleware-logger.d.ts +0 -82
- package/dist/middleware/shared/headers.d.ts +0 -14
- package/dist/middleware/shared/routes.d.ts +0 -30
- package/dist/middleware/shared/storage.d.ts +0 -29
- package/dist/packem_shared/InteractiveManager-CowYA3Hx.js +0 -178
- package/dist/packem_shared/InteractiveStreamHook-BypRlYTX.js +0 -133
- package/dist/packem_shared/Spinner-Cokext9b.js +0 -2183
- package/dist/packem_shared/getBarChar-D7JfmdTr.js +0 -459
- package/dist/packem_shared/index-BEfVUy9P.js +0 -1256
- package/dist/pail.browser.d.ts +0 -412
- package/dist/pail.server.d.ts +0 -233
- package/dist/processor/caller/get-caller-filename.d.ts +0 -23
- package/dist/progress-bar.d.ts +0 -145
- package/dist/progress-bar.js +0 -459
- package/dist/reporter/file/utils/rotating-file-stream.d.ts +0 -48
- package/dist/reporter/http/utils/compression.d.ts +0 -7
- package/dist/reporter/http/utils/log-size-error.d.ts +0 -30
- package/dist/reporter/http/utils/retry.d.ts +0 -27
- package/dist/reporter/json/abstract-json-reporter.d.ts +0 -61
- package/dist/reporter/json/json-reporter.browser.d.ts +0 -40
- package/dist/reporter/json/json-reporter.server.d.ts +0 -50
- package/dist/reporter/pretty/abstract-pretty-reporter.d.ts +0 -83
- package/dist/reporter/pretty/pretty-reporter.browser.d.ts +0 -36
- package/dist/reporter/pretty/pretty-reporter.server.d.ts +0 -70
- package/dist/reporter/raw/raw-reporter.browser.d.ts +0 -5
- package/dist/reporter/raw/raw-reporter.server.d.ts +0 -13
- package/dist/reporter/utils/default-inspector-config.d.ts +0 -3
- package/dist/reporter/utils/format-label.d.ts +0 -3
- package/dist/spinner.d.ts +0 -220
- package/dist/spinner.js +0 -2183
- package/dist/types.d.ts +0 -241
- package/dist/utils/ansi-escapes.d.ts +0 -4
- package/dist/utils/arrayify.d.ts +0 -2
- package/dist/utils/get-longest-badge.d.ts +0 -4
- package/dist/utils/get-longest-label.d.ts +0 -4
- package/dist/utils/merge-types.d.ts +0 -4
- package/dist/utils/stream/safe-stream-handler.d.ts +0 -21
- package/dist/utils/write-console-log-based-on-level.d.ts +0 -4
- package/dist/utils/write-stream.d.ts +0 -2
|
@@ -1,1256 +0,0 @@
|
|
|
1
|
-
import { createRequire as __cjs_createRequire } from "node:module";
|
|
2
|
-
|
|
3
|
-
const __cjs_require = __cjs_createRequire(import.meta.url);
|
|
4
|
-
|
|
5
|
-
const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
|
|
6
|
-
|
|
7
|
-
const __cjs_getBuiltinModule = (module) => {
|
|
8
|
-
// Check if we're in Node.js and version supports getBuiltinModule
|
|
9
|
-
if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
|
|
10
|
-
const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
|
|
11
|
-
// Node.js 20.16.0+ and 22.3.0+
|
|
12
|
-
if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
|
|
13
|
-
return __cjs_getProcess.getBuiltinModule(module);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
// Fallback to createRequire
|
|
17
|
-
return __cjs_require(module);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const process = __cjs_getProcess;
|
|
21
|
-
const {
|
|
22
|
-
execFileSync
|
|
23
|
-
} = __cjs_getBuiltinModule("node:child_process");
|
|
24
|
-
const fs = __cjs_getBuiltinModule("node:fs");
|
|
25
|
-
const tty = __cjs_getBuiltinModule("node:tty");
|
|
26
|
-
|
|
27
|
-
const ambiguousRanges = [161, 161, 164, 164, 167, 168, 170, 170, 173, 174, 176, 180, 182, 186, 188, 191, 198, 198, 208, 208, 215, 216, 222, 225, 230, 230, 232, 234, 236, 237, 240, 240, 242, 243, 247, 250, 252, 252, 254, 254, 257, 257, 273, 273, 275, 275, 283, 283, 294, 295, 299, 299, 305, 307, 312, 312, 319, 322, 324, 324, 328, 331, 333, 333, 338, 339, 358, 359, 363, 363, 462, 462, 464, 464, 466, 466, 468, 468, 470, 470, 472, 472, 474, 474, 476, 476, 593, 593, 609, 609, 708, 708, 711, 711, 713, 715, 717, 717, 720, 720, 728, 731, 733, 733, 735, 735, 768, 879, 913, 929, 931, 937, 945, 961, 963, 969, 1025, 1025, 1040, 1103, 1105, 1105, 8208, 8208, 8211, 8214, 8216, 8217, 8220, 8221, 8224, 8226, 8228, 8231, 8240, 8240, 8242, 8243, 8245, 8245, 8251, 8251, 8254, 8254, 8308, 8308, 8319, 8319, 8321, 8324, 8364, 8364, 8451, 8451, 8453, 8453, 8457, 8457, 8467, 8467, 8470, 8470, 8481, 8482, 8486, 8486, 8491, 8491, 8531, 8532, 8539, 8542, 8544, 8555, 8560, 8569, 8585, 8585, 8592, 8601, 8632, 8633, 8658, 8658, 8660, 8660, 8679, 8679, 8704, 8704, 8706, 8707, 8711, 8712, 8715, 8715, 8719, 8719, 8721, 8721, 8725, 8725, 8730, 8730, 8733, 8736, 8739, 8739, 8741, 8741, 8743, 8748, 8750, 8750, 8756, 8759, 8764, 8765, 8776, 8776, 8780, 8780, 8786, 8786, 8800, 8801, 8804, 8807, 8810, 8811, 8814, 8815, 8834, 8835, 8838, 8839, 8853, 8853, 8857, 8857, 8869, 8869, 8895, 8895, 8978, 8978, 9312, 9449, 9451, 9547, 9552, 9587, 9600, 9615, 9618, 9621, 9632, 9633, 9635, 9641, 9650, 9651, 9654, 9655, 9660, 9661, 9664, 9665, 9670, 9672, 9675, 9675, 9678, 9681, 9698, 9701, 9711, 9711, 9733, 9734, 9737, 9737, 9742, 9743, 9756, 9756, 9758, 9758, 9792, 9792, 9794, 9794, 9824, 9825, 9827, 9829, 9831, 9834, 9836, 9837, 9839, 9839, 9886, 9887, 9919, 9919, 9926, 9933, 9935, 9939, 9941, 9953, 9955, 9955, 9960, 9961, 9963, 9969, 9972, 9972, 9974, 9977, 9979, 9980, 9982, 9983, 10045, 10045, 10102, 10111, 11094, 11097, 12872, 12879, 57344, 63743, 65024, 65039, 65533, 65533, 127232, 127242, 127248, 127277, 127280, 127337, 127344, 127373, 127375, 127376, 127387, 127404, 917760, 917999, 983040, 1048573, 1048576, 1114109];
|
|
28
|
-
const fullwidthRanges = [12288, 12288, 65281, 65376, 65504, 65510];
|
|
29
|
-
const halfwidthRanges = [8361, 8361, 65377, 65470, 65474, 65479, 65482, 65487, 65490, 65495, 65498, 65500, 65512, 65518];
|
|
30
|
-
const narrowRanges = [32, 126, 162, 163, 165, 166, 172, 172, 175, 175, 10214, 10221, 10629, 10630];
|
|
31
|
-
const wideRanges = [4352, 4447, 8986, 8987, 9001, 9002, 9193, 9196, 9200, 9200, 9203, 9203, 9725, 9726, 9748, 9749, 9776, 9783, 9800, 9811, 9855, 9855, 9866, 9871, 9875, 9875, 9889, 9889, 9898, 9899, 9917, 9918, 9924, 9925, 9934, 9934, 9940, 9940, 9962, 9962, 9970, 9971, 9973, 9973, 9978, 9978, 9981, 9981, 9989, 9989, 9994, 9995, 10024, 10024, 10060, 10060, 10062, 10062, 10067, 10069, 10071, 10071, 10133, 10135, 10160, 10160, 10175, 10175, 11035, 11036, 11088, 11088, 11093, 11093, 11904, 11929, 11931, 12019, 12032, 12245, 12272, 12287, 12289, 12350, 12353, 12438, 12441, 12543, 12549, 12591, 12593, 12686, 12688, 12773, 12783, 12830, 12832, 12871, 12880, 42124, 42128, 42182, 43360, 43388, 44032, 55203, 63744, 64255, 65040, 65049, 65072, 65106, 65108, 65126, 65128, 65131, 94176, 94180, 94192, 94198, 94208, 101589, 101631, 101662, 101760, 101874, 110576, 110579, 110581, 110587, 110589, 110590, 110592, 110882, 110898, 110898, 110928, 110930, 110933, 110933, 110948, 110951, 110960, 111355, 119552, 119638, 119648, 119670, 126980, 126980, 127183, 127183, 127374, 127374, 127377, 127386, 127488, 127490, 127504, 127547, 127552, 127560, 127568, 127569, 127584, 127589, 127744, 127776, 127789, 127797, 127799, 127868, 127870, 127891, 127904, 127946, 127951, 127955, 127968, 127984, 127988, 127988, 127992, 128062, 128064, 128064, 128066, 128252, 128255, 128317, 128331, 128334, 128336, 128359, 128378, 128378, 128405, 128406, 128420, 128420, 128507, 128591, 128640, 128709, 128716, 128716, 128720, 128722, 128725, 128728, 128732, 128735, 128747, 128748, 128756, 128764, 128992, 129003, 129008, 129008, 129292, 129338, 129340, 129349, 129351, 129535, 129648, 129660, 129664, 129674, 129678, 129734, 129736, 129736, 129741, 129756, 129759, 129770, 129775, 129784, 131072, 196605, 196608, 262141];
|
|
32
|
-
const isInRange = (ranges, codePoint) => {
|
|
33
|
-
let low = 0;
|
|
34
|
-
let high = Math.floor(ranges.length / 2) - 1;
|
|
35
|
-
while (low <= high) {
|
|
36
|
-
const mid = Math.floor((low + high) / 2);
|
|
37
|
-
const i = mid * 2;
|
|
38
|
-
if (codePoint < ranges[i]) {
|
|
39
|
-
high = mid - 1;
|
|
40
|
-
} else if (codePoint > ranges[i + 1]) {
|
|
41
|
-
low = mid + 1;
|
|
42
|
-
} else {
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
47
|
-
};
|
|
48
|
-
const minimumAmbiguousCodePoint = ambiguousRanges[0];
|
|
49
|
-
const maximumAmbiguousCodePoint = ambiguousRanges.at(-1);
|
|
50
|
-
const minimumFullWidthCodePoint = fullwidthRanges[0];
|
|
51
|
-
const maximumFullWidthCodePoint = fullwidthRanges.at(-1);
|
|
52
|
-
const minimumHalfWidthCodePoint = halfwidthRanges[0];
|
|
53
|
-
const maximumHalfWidthCodePoint = halfwidthRanges.at(-1);
|
|
54
|
-
const minimumNarrowCodePoint = narrowRanges[0];
|
|
55
|
-
const maximumNarrowCodePoint = narrowRanges.at(-1);
|
|
56
|
-
const minimumWideCodePoint = wideRanges[0];
|
|
57
|
-
const maximumWideCodePoint = wideRanges.at(-1);
|
|
58
|
-
const commonCjkCodePoint = 19968;
|
|
59
|
-
const [wideFastPathStart, wideFastPathEnd] = findWideFastPathRange(wideRanges);
|
|
60
|
-
function findWideFastPathRange(ranges) {
|
|
61
|
-
let fastPathStart = ranges[0];
|
|
62
|
-
let fastPathEnd = ranges[1];
|
|
63
|
-
for (let index = 0; index < ranges.length; index += 2) {
|
|
64
|
-
const start = ranges[index];
|
|
65
|
-
const end = ranges[index + 1];
|
|
66
|
-
if (commonCjkCodePoint >= start && commonCjkCodePoint <= end) {
|
|
67
|
-
return [start, end];
|
|
68
|
-
}
|
|
69
|
-
if (end - start > fastPathEnd - fastPathStart) {
|
|
70
|
-
fastPathStart = start;
|
|
71
|
-
fastPathEnd = end;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return [fastPathStart, fastPathEnd];
|
|
75
|
-
}
|
|
76
|
-
const isAmbiguous = (codePoint) => {
|
|
77
|
-
if (codePoint < minimumAmbiguousCodePoint || codePoint > maximumAmbiguousCodePoint) {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
return isInRange(ambiguousRanges, codePoint);
|
|
81
|
-
};
|
|
82
|
-
const isFullWidth = (codePoint) => {
|
|
83
|
-
if (codePoint < minimumFullWidthCodePoint || codePoint > maximumFullWidthCodePoint) {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
return isInRange(fullwidthRanges, codePoint);
|
|
87
|
-
};
|
|
88
|
-
const isHalfWidth = (codePoint) => {
|
|
89
|
-
if (codePoint < minimumHalfWidthCodePoint || codePoint > maximumHalfWidthCodePoint) {
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
return isInRange(halfwidthRanges, codePoint);
|
|
93
|
-
};
|
|
94
|
-
const isNarrow = (codePoint) => {
|
|
95
|
-
if (codePoint < minimumNarrowCodePoint || codePoint > maximumNarrowCodePoint) {
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
return isInRange(narrowRanges, codePoint);
|
|
99
|
-
};
|
|
100
|
-
const isWide = (codePoint) => {
|
|
101
|
-
if (codePoint >= wideFastPathStart && codePoint <= wideFastPathEnd) {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
if (codePoint < minimumWideCodePoint || codePoint > maximumWideCodePoint) {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
return isInRange(wideRanges, codePoint);
|
|
108
|
-
};
|
|
109
|
-
function getCategory(codePoint) {
|
|
110
|
-
if (isAmbiguous(codePoint)) {
|
|
111
|
-
return "ambiguous";
|
|
112
|
-
}
|
|
113
|
-
if (isFullWidth(codePoint)) {
|
|
114
|
-
return "fullwidth";
|
|
115
|
-
}
|
|
116
|
-
if (isHalfWidth(codePoint)) {
|
|
117
|
-
return "halfwidth";
|
|
118
|
-
}
|
|
119
|
-
if (isNarrow(codePoint)) {
|
|
120
|
-
return "narrow";
|
|
121
|
-
}
|
|
122
|
-
if (isWide(codePoint)) {
|
|
123
|
-
return "wide";
|
|
124
|
-
}
|
|
125
|
-
return "neutral";
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function validate(codePoint) {
|
|
129
|
-
if (!Number.isSafeInteger(codePoint)) {
|
|
130
|
-
throw new TypeError(`Expected a code point, got \`${typeof codePoint}\`.`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
function eastAsianWidthType(codePoint) {
|
|
134
|
-
validate(codePoint);
|
|
135
|
-
return getCategory(codePoint);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const r = String.raw;
|
|
139
|
-
const e = r`\p{Emoji}(?:\p{EMod}|[\u{E0020}-\u{E007E}]+\u{E007F}|\uFE0F?\u20E3?)`;
|
|
140
|
-
const emojiRegex = () => new RegExp(r`\p{RI}{2}|(?)${e}(?:\u200D${e})*`, "gu");
|
|
141
|
-
const ESCAPES = /* @__PURE__ */ new Set(["\x1B", ""]);
|
|
142
|
-
const ANSI_ESCAPE_BELL = "\x07";
|
|
143
|
-
const ANSI_CSI = "[";
|
|
144
|
-
const ANSI_SGR_TERMINATOR = "m";
|
|
145
|
-
const ANSI_ESCAPE_LINK = `]8;;`;
|
|
146
|
-
const END_CODE = 39;
|
|
147
|
-
const RE_ZERO_WIDTH = /[\u200B\uFEFF\u2060-\u2064]/g;
|
|
148
|
-
const RE_ESCAPE_PATTERN = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`);
|
|
149
|
-
const ANSI_RESET_CODES = Object.freeze(
|
|
150
|
-
/* @__PURE__ */ new Map([
|
|
151
|
-
[0, 0],
|
|
152
|
-
// Reset all
|
|
153
|
-
[1, 22],
|
|
154
|
-
// Bold → Not bold
|
|
155
|
-
[2, 22],
|
|
156
|
-
// Dim → Not bold
|
|
157
|
-
[3, 23],
|
|
158
|
-
// Italic → Not italic
|
|
159
|
-
[4, 24],
|
|
160
|
-
// Underline → Not underline
|
|
161
|
-
[7, 27],
|
|
162
|
-
// Inverse → Not inverse
|
|
163
|
-
[8, 28],
|
|
164
|
-
// Hidden → Not hidden
|
|
165
|
-
[9, 29],
|
|
166
|
-
// Strikethrough → Not strikethrough
|
|
167
|
-
[30, 39],
|
|
168
|
-
// Foreground colors → Default foreground
|
|
169
|
-
[31, 39],
|
|
170
|
-
[32, 39],
|
|
171
|
-
[33, 39],
|
|
172
|
-
[34, 39],
|
|
173
|
-
[35, 39],
|
|
174
|
-
[36, 39],
|
|
175
|
-
[37, 39],
|
|
176
|
-
[40, 49],
|
|
177
|
-
// Background colors → Default background
|
|
178
|
-
[41, 49],
|
|
179
|
-
[42, 49],
|
|
180
|
-
[43, 49],
|
|
181
|
-
[44, 49],
|
|
182
|
-
[45, 49],
|
|
183
|
-
[46, 49],
|
|
184
|
-
[47, 49],
|
|
185
|
-
[90, 39]
|
|
186
|
-
// Bright foreground → Default foreground
|
|
187
|
-
])
|
|
188
|
-
);
|
|
189
|
-
const RE_ANSI = /[\u001B\u009B](?:[[()#;?]{0,10}(?:\d{1,4}(?:;\d{0,4})*)?[0-9A-ORZcf-nqry=><]|\]8;;[^\u0007\u001B]{0,100}(?:\u0007|\u001B\\))/g;
|
|
190
|
-
const RE_CONTROL = /[\u0000-\u0008\n-\u001F\u007F-\u009F]{1,1000}/y;
|
|
191
|
-
const RE_EMOJI = emojiRegex();
|
|
192
|
-
|
|
193
|
-
const charWidthCache = /* @__PURE__ */ new Map();
|
|
194
|
-
const RE_LATIN_CHARS = /(?:[\u0020-\u007E\u00A0-\u00FF](?!\uFE0F)){1,1000}/y;
|
|
195
|
-
const getCharType = (codePoint) => {
|
|
196
|
-
if (codePoint >= 32 && codePoint <= 126) {
|
|
197
|
-
return "latin";
|
|
198
|
-
}
|
|
199
|
-
if (codePoint === 8203 || codePoint === 8204 || codePoint === 8205 || codePoint === 8288) {
|
|
200
|
-
return "zero";
|
|
201
|
-
}
|
|
202
|
-
if (codePoint <= 31 || codePoint >= 127 && codePoint <= 159) {
|
|
203
|
-
return "control";
|
|
204
|
-
}
|
|
205
|
-
if (codePoint >= 160 && codePoint <= 255) {
|
|
206
|
-
return "latin";
|
|
207
|
-
}
|
|
208
|
-
if (codePoint >= 9472 && codePoint <= 9599) {
|
|
209
|
-
return "latin";
|
|
210
|
-
}
|
|
211
|
-
if (codePoint >= 4352 && codePoint <= 4607 || codePoint >= 11904 && codePoint <= 40959 || codePoint >= 44032 && codePoint <= 55215 || codePoint >= 63744 && codePoint <= 64255 || codePoint >= 65280 && codePoint <= 65519 && !(codePoint >= 65377 && codePoint <= 65439) || codePoint >= 12352 && codePoint <= 12543) {
|
|
212
|
-
return "wide";
|
|
213
|
-
}
|
|
214
|
-
if (codePoint === 8230) {
|
|
215
|
-
return "latin";
|
|
216
|
-
}
|
|
217
|
-
return "other";
|
|
218
|
-
};
|
|
219
|
-
const getCachedCharWidth = (codePoint, config) => {
|
|
220
|
-
const highBits = Math.floor(codePoint / 65536);
|
|
221
|
-
const lowBits = codePoint % 65536;
|
|
222
|
-
let lowMap = charWidthCache.get(highBits);
|
|
223
|
-
if (!lowMap) {
|
|
224
|
-
lowMap = /* @__PURE__ */ new Map();
|
|
225
|
-
charWidthCache.set(highBits, lowMap);
|
|
226
|
-
}
|
|
227
|
-
if (lowMap.has(lowBits)) {
|
|
228
|
-
return lowMap.get(lowBits);
|
|
229
|
-
}
|
|
230
|
-
let width;
|
|
231
|
-
if (getCharType(codePoint) === "latin") {
|
|
232
|
-
width = config.width.regular;
|
|
233
|
-
} else if (getCharType(codePoint) === "control") {
|
|
234
|
-
width = config.width.control;
|
|
235
|
-
} else if (getCharType(codePoint) === "wide") {
|
|
236
|
-
width = config.width.wide;
|
|
237
|
-
} else {
|
|
238
|
-
const eaw = eastAsianWidthType(codePoint);
|
|
239
|
-
switch (eaw) {
|
|
240
|
-
case "ambiguous": {
|
|
241
|
-
width = config.width.ambiguousIsNarrow ? config.width.regular : config.width.wide;
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
case "fullwidth": {
|
|
245
|
-
width = config.width.fullWidth;
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
case "wide": {
|
|
249
|
-
width = config.width.wide;
|
|
250
|
-
break;
|
|
251
|
-
}
|
|
252
|
-
default: {
|
|
253
|
-
width = config.width.regular;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
lowMap.set(lowBits, width);
|
|
258
|
-
return width;
|
|
259
|
-
};
|
|
260
|
-
const isCombiningCharacter = (codePoint) => {
|
|
261
|
-
if (codePoint >= 768 && codePoint <= 879 || codePoint >= 6832 && codePoint <= 6911 || codePoint >= 7616 && codePoint <= 7679 || codePoint >= 8400 && codePoint <= 8447 || codePoint >= 65056 && codePoint <= 65071) {
|
|
262
|
-
return true;
|
|
263
|
-
}
|
|
264
|
-
if (codePoint >= 917760 && codePoint <= 917999 || codePoint >= 65024 && codePoint <= 65039) {
|
|
265
|
-
return true;
|
|
266
|
-
}
|
|
267
|
-
if (codePoint >= 3633 && codePoint <= 3642 || codePoint >= 3655 && codePoint <= 3662 || codePoint >= 3761 && codePoint <= 3769 || codePoint >= 3771 && codePoint <= 3772 || codePoint >= 3784 && codePoint <= 3789) {
|
|
268
|
-
return true;
|
|
269
|
-
}
|
|
270
|
-
if (codePoint >= 2304 && codePoint <= 2307 || codePoint >= 2362 && codePoint <= 2383 || codePoint >= 2385 && codePoint <= 2391 || codePoint >= 2402 && codePoint <= 2403 || codePoint >= 2433 && codePoint <= 2435 || codePoint >= 2492 && codePoint <= 2500 || codePoint >= 2509 && codePoint <= 2509 || codePoint >= 2561 && codePoint <= 2563 || codePoint >= 2620 && codePoint <= 2637) {
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
if (codePoint >= 1611 && codePoint <= 1631 || codePoint >= 1648 && codePoint <= 1648 || codePoint >= 1750 && codePoint <= 1773 || codePoint >= 2276 && codePoint <= 2302) {
|
|
274
|
-
return true;
|
|
275
|
-
}
|
|
276
|
-
if (codePoint >= 1425 && codePoint <= 1469 || codePoint >= 1471 && codePoint <= 1471 || codePoint >= 1473 && codePoint <= 1474 || codePoint >= 1476 && codePoint <= 1477 || codePoint >= 1479 && codePoint <= 1479) {
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
if (codePoint >= 3893 && codePoint <= 3893 || codePoint >= 3895 && codePoint <= 3895 || codePoint >= 3897 && codePoint <= 3897 || codePoint >= 3953 && codePoint <= 3966 || codePoint >= 3968 && codePoint <= 3972 || codePoint >= 3974 && codePoint <= 3975) {
|
|
280
|
-
return true;
|
|
281
|
-
}
|
|
282
|
-
return codePoint >= 768 && codePoint <= 777 || codePoint >= 803 && codePoint <= 803;
|
|
283
|
-
};
|
|
284
|
-
const getStringTruncatedWidth = (input, options = {}) => {
|
|
285
|
-
if (!input || input.length === 0) {
|
|
286
|
-
return { ellipsed: false, index: 0, truncated: false, width: 0 };
|
|
287
|
-
}
|
|
288
|
-
const config = {
|
|
289
|
-
truncation: {
|
|
290
|
-
countAnsiEscapeCodes: options.countAnsiEscapeCodes ?? false,
|
|
291
|
-
ellipsis: options.ellipsis ?? "",
|
|
292
|
-
ellipsisWidth: options.ellipsisWidth ?? (options.ellipsis ? getStringTruncatedWidth(options.ellipsis, {
|
|
293
|
-
...options,
|
|
294
|
-
ellipsis: "",
|
|
295
|
-
ellipsisWidth: 0,
|
|
296
|
-
limit: Number.POSITIVE_INFINITY
|
|
297
|
-
}).width : 0),
|
|
298
|
-
limit: options.limit ?? Number.POSITIVE_INFINITY
|
|
299
|
-
},
|
|
300
|
-
width: {
|
|
301
|
-
ambiguousIsNarrow: options.ambiguousIsNarrow ?? false,
|
|
302
|
-
ansi: options.ansiWidth ?? 0,
|
|
303
|
-
control: options.controlWidth ?? 0,
|
|
304
|
-
emoji: options.emojiWidth ?? 2,
|
|
305
|
-
fullWidth: options.fullWidth ?? 2,
|
|
306
|
-
halfWidth: options.halfWidth ?? 1,
|
|
307
|
-
regular: options.regularWidth ?? 1,
|
|
308
|
-
tab: options.tabWidth ?? 8,
|
|
309
|
-
wide: options.wideWidth ?? 2
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
const truncationLimit = Math.max(0, config.truncation.limit - config.truncation.ellipsisWidth);
|
|
313
|
-
const { length } = input;
|
|
314
|
-
const useCaching = length > 1e4;
|
|
315
|
-
let index = 0;
|
|
316
|
-
let width = 0;
|
|
317
|
-
let truncationIndex = length;
|
|
318
|
-
let truncationEnabled = false;
|
|
319
|
-
const hasAnsi = input.includes("\x1B") || input.includes("");
|
|
320
|
-
while (index < length) {
|
|
321
|
-
if (hasAnsi && (input[index] === "\x1B" || input[index] === "")) {
|
|
322
|
-
if (input.startsWith("\x1B]8;;", index)) {
|
|
323
|
-
const BELL = "\x07";
|
|
324
|
-
const OSC8_CLOSER = `\x1B]8;;${BELL}`;
|
|
325
|
-
const OSC8_CLOSER_LEN = OSC8_CLOSER.length;
|
|
326
|
-
const endOfParametersIndex = input.indexOf(BELL, index + 5);
|
|
327
|
-
if (endOfParametersIndex === -1) ;
|
|
328
|
-
else {
|
|
329
|
-
const startOfCloserIndex = input.indexOf(OSC8_CLOSER, endOfParametersIndex + 1);
|
|
330
|
-
if (startOfCloserIndex === -1) ;
|
|
331
|
-
else {
|
|
332
|
-
const endOfSequenceIndex = startOfCloserIndex + OSC8_CLOSER_LEN;
|
|
333
|
-
const linkText = input.slice(endOfParametersIndex + 1, startOfCloserIndex);
|
|
334
|
-
const strippedLinkText = linkText.replace(RE_ANSI, "");
|
|
335
|
-
const linkTextWidthResult = getStringTruncatedWidth(strippedLinkText, {
|
|
336
|
-
ambiguousIsNarrow: config.width.ambiguousIsNarrow,
|
|
337
|
-
ansiWidth: config.width.ansi,
|
|
338
|
-
controlWidth: config.width.control,
|
|
339
|
-
countAnsiEscapeCodes: false,
|
|
340
|
-
// Never count ANSI in link text width
|
|
341
|
-
ellipsis: config.truncation.ellipsis,
|
|
342
|
-
ellipsisWidth: config.truncation.ellipsisWidth,
|
|
343
|
-
emojiWidth: config.width.emoji,
|
|
344
|
-
fullWidth: config.width.fullWidth,
|
|
345
|
-
halfWidth: config.width.halfWidth,
|
|
346
|
-
limit: Math.max(0, truncationLimit - width),
|
|
347
|
-
regularWidth: config.width.regular,
|
|
348
|
-
tabWidth: config.width.tab,
|
|
349
|
-
wideWidth: config.width.wide
|
|
350
|
-
});
|
|
351
|
-
const textWidth = linkTextWidthResult.width;
|
|
352
|
-
if (linkTextWidthResult.truncated) {
|
|
353
|
-
truncationEnabled = true;
|
|
354
|
-
truncationIndex = Math.min(truncationIndex, index);
|
|
355
|
-
} else if (width + textWidth > truncationLimit) {
|
|
356
|
-
truncationIndex = Math.min(truncationIndex, index);
|
|
357
|
-
truncationEnabled = true;
|
|
358
|
-
if (width + textWidth > config.truncation.limit) {
|
|
359
|
-
break;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
width += textWidth;
|
|
363
|
-
index = endOfSequenceIndex;
|
|
364
|
-
if (truncationEnabled && width >= config.truncation.limit) {
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
367
|
-
continue;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
RE_ANSI.lastIndex = index;
|
|
372
|
-
if (RE_ANSI.test(input)) {
|
|
373
|
-
const ansiLength = RE_ANSI.lastIndex - index;
|
|
374
|
-
const ansiWidth = config.truncation.countAnsiEscapeCodes ? ansiLength : config.width.ansi;
|
|
375
|
-
if (width + ansiWidth > truncationLimit) {
|
|
376
|
-
truncationIndex = Math.min(truncationIndex, index);
|
|
377
|
-
if (width + ansiWidth > config.truncation.limit) {
|
|
378
|
-
truncationEnabled = true;
|
|
379
|
-
break;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
width += ansiWidth;
|
|
383
|
-
index = RE_ANSI.lastIndex;
|
|
384
|
-
continue;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
const charCode = input.codePointAt(index);
|
|
388
|
-
if (charCode === 8203 || charCode === 65279 || charCode >= 8288 && charCode <= 8292) {
|
|
389
|
-
index += 1;
|
|
390
|
-
continue;
|
|
391
|
-
}
|
|
392
|
-
if (charCode === 9) {
|
|
393
|
-
if (width + config.width.tab > truncationLimit) {
|
|
394
|
-
truncationIndex = Math.min(truncationIndex, index);
|
|
395
|
-
if (width + config.width.tab > config.truncation.limit) {
|
|
396
|
-
truncationEnabled = true;
|
|
397
|
-
break;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
width += config.width.tab;
|
|
401
|
-
index += 1;
|
|
402
|
-
continue;
|
|
403
|
-
}
|
|
404
|
-
RE_LATIN_CHARS.lastIndex = index;
|
|
405
|
-
if (RE_LATIN_CHARS.test(input)) {
|
|
406
|
-
const latinLength = RE_LATIN_CHARS.lastIndex - index;
|
|
407
|
-
const latinWidth = latinLength * config.width.regular;
|
|
408
|
-
if (width + latinWidth > truncationLimit) {
|
|
409
|
-
const charsToLimit = Math.floor((truncationLimit - width) / config.width.regular);
|
|
410
|
-
truncationIndex = Math.min(truncationIndex, index + charsToLimit);
|
|
411
|
-
if (width + latinWidth > config.truncation.limit) {
|
|
412
|
-
truncationEnabled = true;
|
|
413
|
-
break;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
width += latinWidth;
|
|
417
|
-
index = RE_LATIN_CHARS.lastIndex;
|
|
418
|
-
continue;
|
|
419
|
-
}
|
|
420
|
-
if (charCode <= 31 || charCode >= 127 && charCode <= 159) {
|
|
421
|
-
RE_CONTROL.lastIndex = index;
|
|
422
|
-
if (RE_CONTROL.test(input)) {
|
|
423
|
-
const controlLength = RE_CONTROL.lastIndex - index;
|
|
424
|
-
const controlWidth = controlLength * config.width.control;
|
|
425
|
-
if (width + controlWidth > truncationLimit) {
|
|
426
|
-
truncationIndex = Math.min(truncationIndex, index + Math.floor((truncationLimit - width) / config.width.control));
|
|
427
|
-
if (width + controlWidth > config.truncation.limit) {
|
|
428
|
-
truncationEnabled = true;
|
|
429
|
-
break;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
width += controlWidth;
|
|
433
|
-
index = RE_CONTROL.lastIndex;
|
|
434
|
-
continue;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
RE_EMOJI.lastIndex = index;
|
|
438
|
-
if (RE_EMOJI.test(input)) {
|
|
439
|
-
if (width + config.width.emoji > truncationLimit) {
|
|
440
|
-
truncationIndex = Math.min(truncationIndex, index);
|
|
441
|
-
if (width + config.width.emoji > config.truncation.limit) {
|
|
442
|
-
truncationEnabled = true;
|
|
443
|
-
break;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
width += config.width.emoji;
|
|
447
|
-
index = RE_EMOJI.lastIndex;
|
|
448
|
-
continue;
|
|
449
|
-
}
|
|
450
|
-
const codePoint = input.codePointAt(index) ?? 0;
|
|
451
|
-
if (isCombiningCharacter(codePoint)) {
|
|
452
|
-
index += codePoint > 65535 ? 2 : 1;
|
|
453
|
-
continue;
|
|
454
|
-
}
|
|
455
|
-
let charWidth;
|
|
456
|
-
if (useCaching) {
|
|
457
|
-
charWidth = getCachedCharWidth(codePoint, config);
|
|
458
|
-
} else {
|
|
459
|
-
const charType = getCharType(codePoint);
|
|
460
|
-
switch (charType) {
|
|
461
|
-
case "control": {
|
|
462
|
-
charWidth = config.width.control;
|
|
463
|
-
break;
|
|
464
|
-
}
|
|
465
|
-
case "latin": {
|
|
466
|
-
charWidth = config.width.regular;
|
|
467
|
-
break;
|
|
468
|
-
}
|
|
469
|
-
case "wide": {
|
|
470
|
-
charWidth = config.width.wide;
|
|
471
|
-
break;
|
|
472
|
-
}
|
|
473
|
-
case "zero": {
|
|
474
|
-
charWidth = 0;
|
|
475
|
-
break;
|
|
476
|
-
}
|
|
477
|
-
default: {
|
|
478
|
-
const eaw = eastAsianWidthType(codePoint);
|
|
479
|
-
switch (eaw) {
|
|
480
|
-
case "ambiguous": {
|
|
481
|
-
charWidth = config.width.ambiguousIsNarrow ? config.width.regular : config.width.wide;
|
|
482
|
-
break;
|
|
483
|
-
}
|
|
484
|
-
case "fullwidth": {
|
|
485
|
-
charWidth = config.width.fullWidth;
|
|
486
|
-
break;
|
|
487
|
-
}
|
|
488
|
-
case "wide": {
|
|
489
|
-
charWidth = config.width.wide;
|
|
490
|
-
break;
|
|
491
|
-
}
|
|
492
|
-
default: {
|
|
493
|
-
charWidth = config.width.regular;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
if (width + charWidth > truncationLimit) {
|
|
500
|
-
truncationIndex = Math.min(truncationIndex, index);
|
|
501
|
-
if (width + charWidth > config.truncation.limit) {
|
|
502
|
-
truncationEnabled = true;
|
|
503
|
-
break;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
width += charWidth;
|
|
507
|
-
index += codePoint > 65535 ? 2 : 1;
|
|
508
|
-
}
|
|
509
|
-
let finalWidth = width;
|
|
510
|
-
let ellipsed = false;
|
|
511
|
-
if (truncationEnabled && config.truncation.limit >= config.truncation.ellipsisWidth) {
|
|
512
|
-
finalWidth = config.truncation.limit;
|
|
513
|
-
ellipsed = true;
|
|
514
|
-
}
|
|
515
|
-
return {
|
|
516
|
-
ellipsed,
|
|
517
|
-
index: truncationEnabled ? truncationIndex : length,
|
|
518
|
-
truncated: truncationEnabled,
|
|
519
|
-
width: finalWidth
|
|
520
|
-
};
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
const getStringWidth = (input, options = {}) => getStringTruncatedWidth(input, { ...options, ellipsis: "", ellipsisWidth: 0, limit: Number.POSITIVE_INFINITY }).width;
|
|
524
|
-
|
|
525
|
-
const RE_ANSI_CODE = /\x1B\[(\d+)m/;
|
|
526
|
-
class AnsiStateTracker {
|
|
527
|
-
activeForeground = void 0;
|
|
528
|
-
activeBackground = void 0;
|
|
529
|
-
// Track other formatting (bold, italic, etc.)
|
|
530
|
-
activeFormatting = [];
|
|
531
|
-
/**
|
|
532
|
-
* Processes an escape sequence and updates the internal state
|
|
533
|
-
* @param sequence The escape sequence to process
|
|
534
|
-
*/
|
|
535
|
-
processEscape(sequence) {
|
|
536
|
-
const match = RE_ANSI_CODE.exec(sequence);
|
|
537
|
-
if (!match) {
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
|
-
const code = Number.parseInt(match[1], 10);
|
|
541
|
-
switch (code) {
|
|
542
|
-
case 0: {
|
|
543
|
-
this.activeForeground = void 0;
|
|
544
|
-
this.activeBackground = void 0;
|
|
545
|
-
this.activeFormatting = [];
|
|
546
|
-
break;
|
|
547
|
-
}
|
|
548
|
-
case 39: {
|
|
549
|
-
this.activeForeground = void 0;
|
|
550
|
-
break;
|
|
551
|
-
}
|
|
552
|
-
case 49: {
|
|
553
|
-
this.activeBackground = void 0;
|
|
554
|
-
break;
|
|
555
|
-
}
|
|
556
|
-
default: {
|
|
557
|
-
if (code >= 30 && code <= 37 || code >= 90 && code <= 97) {
|
|
558
|
-
this.activeForeground = sequence;
|
|
559
|
-
} else if (code >= 40 && code <= 47 || code >= 100 && code <= 107) {
|
|
560
|
-
this.activeBackground = sequence;
|
|
561
|
-
} else if ([1, 2, 3, 4, 7, 8, 9].includes(code)) {
|
|
562
|
-
this.activeFormatting.push(sequence);
|
|
563
|
-
} else if ([22, 23, 24, 27, 28, 29].includes(code)) {
|
|
564
|
-
const formatResetMap = {
|
|
565
|
-
22: "[1m",
|
|
566
|
-
// Reset bold
|
|
567
|
-
23: "[3m",
|
|
568
|
-
// Reset italic
|
|
569
|
-
24: "[4m",
|
|
570
|
-
// Reset underline
|
|
571
|
-
27: "[7m",
|
|
572
|
-
// Reset inverse
|
|
573
|
-
28: "[8m",
|
|
574
|
-
// Reset hidden
|
|
575
|
-
29: "[9m"
|
|
576
|
-
// Reset strikethrough
|
|
577
|
-
};
|
|
578
|
-
const formatToRemove = formatResetMap[code];
|
|
579
|
-
if (formatToRemove) {
|
|
580
|
-
this.activeFormatting = this.activeFormatting.filter((fmt) => !fmt.includes(formatToRemove));
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* Gets all active escape sequences to apply.
|
|
588
|
-
* @returns String with all active escapes
|
|
589
|
-
*/
|
|
590
|
-
getStartEscapesForAllActiveAttributes() {
|
|
591
|
-
return [this.activeBackground, this.activeForeground, ...this.activeFormatting].filter(Boolean).join("");
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* Gets all closing escape sequences for the currently active attributes.
|
|
595
|
-
* The order is generally reverse of application: formatting, foreground, background.
|
|
596
|
-
* @returns String with all necessary closing escapes.
|
|
597
|
-
*/
|
|
598
|
-
getEndEscapesForAllActiveAttributes() {
|
|
599
|
-
const closingEscapes = [];
|
|
600
|
-
if (this.activeFormatting.length > 0) {
|
|
601
|
-
const formatResetMap = {
|
|
602
|
-
"\x1B[1m": "\x1B[22m",
|
|
603
|
-
// Bold
|
|
604
|
-
"\x1B[2m": "\x1B[22m",
|
|
605
|
-
// Faint/Dim (also reset by 22)
|
|
606
|
-
"\x1B[3m": "\x1B[23m",
|
|
607
|
-
// Italic
|
|
608
|
-
"\x1B[4m": "\x1B[24m",
|
|
609
|
-
// Underline
|
|
610
|
-
"\x1B[7m": "\x1B[27m",
|
|
611
|
-
// Inverse
|
|
612
|
-
"\x1B[8m": "\x1B[28m",
|
|
613
|
-
// Hidden/Conceal
|
|
614
|
-
"\x1B[9m": "\x1B[29m"
|
|
615
|
-
// Strikethrough
|
|
616
|
-
};
|
|
617
|
-
[...this.activeFormatting].toReversed().forEach((formatCode) => {
|
|
618
|
-
const resetCode = formatResetMap[formatCode];
|
|
619
|
-
if (resetCode) {
|
|
620
|
-
closingEscapes.push(resetCode);
|
|
621
|
-
}
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
|
-
if (this.activeForeground) {
|
|
625
|
-
closingEscapes.push("\x1B[39m");
|
|
626
|
-
}
|
|
627
|
-
if (this.activeBackground) {
|
|
628
|
-
closingEscapes.push("\x1B[49m");
|
|
629
|
-
}
|
|
630
|
-
return closingEscapes.join("");
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
const checkEscapeSequence = (chars, index) => {
|
|
634
|
-
if (!ESCAPES.has(chars[index])) {
|
|
635
|
-
return { isInsideEscape: false, isInsideLinkEscape: false };
|
|
636
|
-
}
|
|
637
|
-
const isInsideEscape = true;
|
|
638
|
-
const possibleLink = chars.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join("");
|
|
639
|
-
const isInsideLinkEscape = possibleLink === ANSI_ESCAPE_LINK;
|
|
640
|
-
return { isInsideEscape, isInsideLinkEscape };
|
|
641
|
-
};
|
|
642
|
-
const processAnsiString = (string, options = {}) => {
|
|
643
|
-
const stateTracker = new AnsiStateTracker();
|
|
644
|
-
let currentText = "";
|
|
645
|
-
let isInsideEscape = false;
|
|
646
|
-
let escapeBuffer = "";
|
|
647
|
-
let currentUrl = "";
|
|
648
|
-
let isInHyperlink = false;
|
|
649
|
-
const chars = [...string];
|
|
650
|
-
for (let index = 0; index < chars.length; index++) {
|
|
651
|
-
const character = chars[index];
|
|
652
|
-
if (character && ESCAPES.has(character)) {
|
|
653
|
-
if (currentText) {
|
|
654
|
-
const width2 = options.getWidth?.(currentText) ?? 0;
|
|
655
|
-
const segment2 = {
|
|
656
|
-
isEscapeSequence: false,
|
|
657
|
-
isGrapheme: true,
|
|
658
|
-
text: currentText,
|
|
659
|
-
width: width2
|
|
660
|
-
};
|
|
661
|
-
if (isInHyperlink) {
|
|
662
|
-
segment2.isHyperlink = true;
|
|
663
|
-
segment2.hyperlinkUrl = currentUrl;
|
|
664
|
-
}
|
|
665
|
-
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
668
|
-
currentText = "";
|
|
669
|
-
}
|
|
670
|
-
isInsideEscape = true;
|
|
671
|
-
escapeBuffer = character;
|
|
672
|
-
const escapeInfo = checkEscapeSequence(chars, index);
|
|
673
|
-
const { isInsideLinkEscape } = escapeInfo;
|
|
674
|
-
if (isInsideLinkEscape) {
|
|
675
|
-
let urlEnd = index + 1;
|
|
676
|
-
currentUrl = "";
|
|
677
|
-
while (urlEnd < chars.length) {
|
|
678
|
-
const nextChar = chars[urlEnd];
|
|
679
|
-
if (nextChar === ANSI_ESCAPE_BELL) {
|
|
680
|
-
break;
|
|
681
|
-
}
|
|
682
|
-
currentUrl += nextChar;
|
|
683
|
-
urlEnd += 1;
|
|
684
|
-
}
|
|
685
|
-
currentUrl = currentUrl.slice(4);
|
|
686
|
-
const segment2 = {
|
|
687
|
-
hyperlinkUrl: currentUrl,
|
|
688
|
-
isEscapeSequence: true,
|
|
689
|
-
isGrapheme: false,
|
|
690
|
-
isHyperlink: true,
|
|
691
|
-
isHyperlinkStart: true,
|
|
692
|
-
width: 0
|
|
693
|
-
};
|
|
694
|
-
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
695
|
-
return;
|
|
696
|
-
}
|
|
697
|
-
index = urlEnd;
|
|
698
|
-
isInHyperlink = true;
|
|
699
|
-
isInsideEscape = false;
|
|
700
|
-
escapeBuffer = "";
|
|
701
|
-
continue;
|
|
702
|
-
}
|
|
703
|
-
if (index + 1 < chars.length && chars[index + 1] === "\\" && isInHyperlink) {
|
|
704
|
-
const segment2 = {
|
|
705
|
-
isEscapeSequence: true,
|
|
706
|
-
isGrapheme: false,
|
|
707
|
-
isHyperlink: true,
|
|
708
|
-
isHyperlinkEnd: true,
|
|
709
|
-
width: 0
|
|
710
|
-
};
|
|
711
|
-
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
712
|
-
return;
|
|
713
|
-
}
|
|
714
|
-
isInHyperlink = false;
|
|
715
|
-
currentUrl = "";
|
|
716
|
-
index += 1;
|
|
717
|
-
isInsideEscape = false;
|
|
718
|
-
escapeBuffer = "";
|
|
719
|
-
continue;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
if (isInsideEscape) {
|
|
723
|
-
if (escapeBuffer !== character) {
|
|
724
|
-
escapeBuffer += character;
|
|
725
|
-
}
|
|
726
|
-
if (character === ANSI_SGR_TERMINATOR) {
|
|
727
|
-
isInsideEscape = false;
|
|
728
|
-
stateTracker.processEscape(escapeBuffer);
|
|
729
|
-
const segment2 = {
|
|
730
|
-
isEscapeSequence: true,
|
|
731
|
-
isGrapheme: false,
|
|
732
|
-
text: escapeBuffer,
|
|
733
|
-
width: 0
|
|
734
|
-
};
|
|
735
|
-
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
736
|
-
return;
|
|
737
|
-
}
|
|
738
|
-
escapeBuffer = "";
|
|
739
|
-
}
|
|
740
|
-
continue;
|
|
741
|
-
}
|
|
742
|
-
currentText += character;
|
|
743
|
-
const width = options.getWidth?.(currentText) ?? 0;
|
|
744
|
-
const segment = {
|
|
745
|
-
isEscapeSequence: false,
|
|
746
|
-
isGrapheme: true,
|
|
747
|
-
text: currentText,
|
|
748
|
-
width
|
|
749
|
-
};
|
|
750
|
-
if (isInHyperlink) {
|
|
751
|
-
segment.isHyperlink = true;
|
|
752
|
-
segment.hyperlinkUrl = currentUrl;
|
|
753
|
-
}
|
|
754
|
-
if (options.onSegment?.(segment, stateTracker) === false) {
|
|
755
|
-
return;
|
|
756
|
-
}
|
|
757
|
-
currentText = "";
|
|
758
|
-
}
|
|
759
|
-
if (currentText) {
|
|
760
|
-
const width = options.getWidth?.(currentText) ?? 0;
|
|
761
|
-
const segment = {
|
|
762
|
-
isEscapeSequence: false,
|
|
763
|
-
isGrapheme: true,
|
|
764
|
-
text: currentText,
|
|
765
|
-
width
|
|
766
|
-
};
|
|
767
|
-
if (isInHyperlink) {
|
|
768
|
-
segment.isHyperlink = true;
|
|
769
|
-
segment.hyperlinkUrl = currentUrl;
|
|
770
|
-
}
|
|
771
|
-
options.onSegment?.(segment, stateTracker);
|
|
772
|
-
}
|
|
773
|
-
if (escapeBuffer) {
|
|
774
|
-
const segment = {
|
|
775
|
-
isEscapeSequence: true,
|
|
776
|
-
isGrapheme: false,
|
|
777
|
-
text: escapeBuffer,
|
|
778
|
-
width: 0
|
|
779
|
-
};
|
|
780
|
-
options.onSegment?.(segment, stateTracker);
|
|
781
|
-
}
|
|
782
|
-
};
|
|
783
|
-
const wrapAnsiCode = (code) => {
|
|
784
|
-
const escapeChar = ESCAPES.values().next().value;
|
|
785
|
-
return `${escapeChar}${ANSI_CSI}${String(code)}${ANSI_SGR_TERMINATOR}`;
|
|
786
|
-
};
|
|
787
|
-
const wrapAnsiHyperlink = (url) => {
|
|
788
|
-
const escapeChar = ESCAPES.values().next().value;
|
|
789
|
-
return `${escapeChar}${ANSI_ESCAPE_LINK}${url}${ANSI_ESCAPE_BELL}`;
|
|
790
|
-
};
|
|
791
|
-
const preserveAnsi = (rawLines) => {
|
|
792
|
-
if (rawLines.length === 0) {
|
|
793
|
-
return "";
|
|
794
|
-
}
|
|
795
|
-
if (rawLines.length === 1) {
|
|
796
|
-
return rawLines[0];
|
|
797
|
-
}
|
|
798
|
-
let returnValue = "";
|
|
799
|
-
let escapeCode;
|
|
800
|
-
let escapeUrl;
|
|
801
|
-
const preString = rawLines.join("\n");
|
|
802
|
-
const pre = [...preString];
|
|
803
|
-
let preStringIndex = 0;
|
|
804
|
-
for (const [index, character] of pre.entries()) {
|
|
805
|
-
returnValue += character;
|
|
806
|
-
if (ESCAPES.has(character)) {
|
|
807
|
-
const match = RE_ESCAPE_PATTERN.exec(preString.slice(preStringIndex));
|
|
808
|
-
const groups = match?.groups ?? {};
|
|
809
|
-
if (groups.code !== void 0) {
|
|
810
|
-
const code2 = Number.parseFloat(groups.code);
|
|
811
|
-
escapeCode = code2 === END_CODE ? void 0 : code2;
|
|
812
|
-
} else if (groups.uri !== void 0) {
|
|
813
|
-
escapeUrl = groups.uri.length === 0 ? void 0 : groups.uri;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
const code = ANSI_RESET_CODES.get(Number(escapeCode));
|
|
817
|
-
if (pre[index + 1] === "\n") {
|
|
818
|
-
if (escapeUrl) {
|
|
819
|
-
returnValue += wrapAnsiHyperlink("");
|
|
820
|
-
}
|
|
821
|
-
if (escapeCode && code) {
|
|
822
|
-
returnValue += wrapAnsiCode(code);
|
|
823
|
-
}
|
|
824
|
-
} else if (character === "\n") {
|
|
825
|
-
if (escapeCode && code) {
|
|
826
|
-
returnValue += wrapAnsiCode(escapeCode);
|
|
827
|
-
}
|
|
828
|
-
if (escapeUrl) {
|
|
829
|
-
returnValue += wrapAnsiHyperlink(escapeUrl);
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
preStringIndex += character.length;
|
|
833
|
-
}
|
|
834
|
-
return returnValue;
|
|
835
|
-
};
|
|
836
|
-
const RE_SPLIT_WHITESPACE = /(?=\s)|(?<=\s)/;
|
|
837
|
-
const RE_WHITESPACE_ONLY = /^\s+$/;
|
|
838
|
-
const resetAnsiAtLineBreak = (currentLine) => {
|
|
839
|
-
if (!currentLine.includes("\x1B")) {
|
|
840
|
-
return currentLine;
|
|
841
|
-
}
|
|
842
|
-
let result = currentLine;
|
|
843
|
-
if (currentLine.includes("\x1B[30m")) {
|
|
844
|
-
result += "\x1B[39m";
|
|
845
|
-
}
|
|
846
|
-
if (currentLine.includes("\x1B[42m")) {
|
|
847
|
-
result += "\x1B[49m";
|
|
848
|
-
}
|
|
849
|
-
return result;
|
|
850
|
-
};
|
|
851
|
-
const stringVisibleTrimSpacesRight = (string) => {
|
|
852
|
-
const words = string.split(" ");
|
|
853
|
-
let last = words.length;
|
|
854
|
-
while (last > 0 && getStringWidth(words[last - 1]) === 0) {
|
|
855
|
-
last--;
|
|
856
|
-
}
|
|
857
|
-
if (last === words.length) {
|
|
858
|
-
return string;
|
|
859
|
-
}
|
|
860
|
-
return words.slice(0, last).join(" ") + words.slice(last).join("");
|
|
861
|
-
};
|
|
862
|
-
const wrapWithBreakAtWidth = (string, width, trim) => {
|
|
863
|
-
if (string.length === 0) {
|
|
864
|
-
return [""];
|
|
865
|
-
}
|
|
866
|
-
if (width <= 0) {
|
|
867
|
-
return [string];
|
|
868
|
-
}
|
|
869
|
-
const rows = [];
|
|
870
|
-
const ansiTracker = new AnsiStateTracker();
|
|
871
|
-
let currentLine = "";
|
|
872
|
-
let currentWidth = 0;
|
|
873
|
-
let isInsideEscape = false;
|
|
874
|
-
let isInsideLinkEscape = false;
|
|
875
|
-
let escapeBuffer = "";
|
|
876
|
-
let index = 0;
|
|
877
|
-
while (index < string.length) {
|
|
878
|
-
const char = string[index];
|
|
879
|
-
if (ESCAPES.has(char)) {
|
|
880
|
-
isInsideEscape = true;
|
|
881
|
-
escapeBuffer = char;
|
|
882
|
-
currentLine += char;
|
|
883
|
-
const escapeInfo = checkEscapeSequence([...string], index);
|
|
884
|
-
isInsideLinkEscape = escapeInfo.isInsideLinkEscape;
|
|
885
|
-
index += 1;
|
|
886
|
-
continue;
|
|
887
|
-
}
|
|
888
|
-
if (isInsideEscape) {
|
|
889
|
-
escapeBuffer += char;
|
|
890
|
-
currentLine += char;
|
|
891
|
-
if (isInsideLinkEscape) {
|
|
892
|
-
if (char === ANSI_ESCAPE_BELL) {
|
|
893
|
-
isInsideEscape = isInsideLinkEscape = false;
|
|
894
|
-
}
|
|
895
|
-
} else if (char === ANSI_SGR_TERMINATOR) {
|
|
896
|
-
isInsideEscape = false;
|
|
897
|
-
ansiTracker.processEscape(escapeBuffer);
|
|
898
|
-
}
|
|
899
|
-
index += 1;
|
|
900
|
-
continue;
|
|
901
|
-
}
|
|
902
|
-
const charWidth = getStringWidth(char);
|
|
903
|
-
const isSpace = char === " ";
|
|
904
|
-
if (charWidth === 0) {
|
|
905
|
-
currentLine += char;
|
|
906
|
-
index += 1;
|
|
907
|
-
continue;
|
|
908
|
-
}
|
|
909
|
-
if (currentWidth + charWidth > width) {
|
|
910
|
-
if (currentLine) {
|
|
911
|
-
rows.push(currentLine + ansiTracker.getEndEscapesForAllActiveAttributes());
|
|
912
|
-
}
|
|
913
|
-
currentLine = ansiTracker.getStartEscapesForAllActiveAttributes();
|
|
914
|
-
currentWidth = getStringWidth(currentLine);
|
|
915
|
-
if (isSpace && trim) {
|
|
916
|
-
while (index < string.length && string[index] === " ") {
|
|
917
|
-
index += 1;
|
|
918
|
-
}
|
|
919
|
-
continue;
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
currentLine += char;
|
|
923
|
-
currentWidth += charWidth;
|
|
924
|
-
if (currentWidth === width && index < string.length - 1) {
|
|
925
|
-
rows.push(currentLine + ansiTracker.getEndEscapesForAllActiveAttributes());
|
|
926
|
-
currentLine = ansiTracker.getStartEscapesForAllActiveAttributes();
|
|
927
|
-
currentWidth = getStringWidth(currentLine);
|
|
928
|
-
if (index + 1 < string.length && string[index + 1] === " " && trim) {
|
|
929
|
-
index += 1;
|
|
930
|
-
while (index < string.length && string[index] === " ") {
|
|
931
|
-
index += 1;
|
|
932
|
-
}
|
|
933
|
-
continue;
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
index += 1;
|
|
937
|
-
}
|
|
938
|
-
if (currentLine) {
|
|
939
|
-
rows.push(currentLine + ansiTracker.getEndEscapesForAllActiveAttributes());
|
|
940
|
-
}
|
|
941
|
-
return trim ? rows.map((element) => stringVisibleTrimSpacesRight(element)) : rows;
|
|
942
|
-
};
|
|
943
|
-
const wrapCharByChar = (string, width, trim) => {
|
|
944
|
-
if (string.length === 0) {
|
|
945
|
-
return [];
|
|
946
|
-
}
|
|
947
|
-
const inputToProcess = trim ? string.trim() : string;
|
|
948
|
-
if (inputToProcess.length === 0) {
|
|
949
|
-
return [];
|
|
950
|
-
}
|
|
951
|
-
const rows = [];
|
|
952
|
-
let currentLine = "";
|
|
953
|
-
let currentWidth = 0;
|
|
954
|
-
processAnsiString(inputToProcess, {
|
|
955
|
-
getWidth: getStringWidth,
|
|
956
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity,sonarjs/no-invariant-returns
|
|
957
|
-
onSegment: (segment, stateTracker) => {
|
|
958
|
-
const segText = segment.text ?? "";
|
|
959
|
-
if (segment.isEscapeSequence) {
|
|
960
|
-
currentLine += segText;
|
|
961
|
-
} else {
|
|
962
|
-
const isSpace = segText === " ";
|
|
963
|
-
if (segment.width === 0) {
|
|
964
|
-
currentLine += segText;
|
|
965
|
-
return true;
|
|
966
|
-
}
|
|
967
|
-
if (currentWidth + segment.width > width) {
|
|
968
|
-
if (currentLine) {
|
|
969
|
-
rows.push(currentLine);
|
|
970
|
-
}
|
|
971
|
-
currentLine = stateTracker.getStartEscapesForAllActiveAttributes();
|
|
972
|
-
currentWidth = 0;
|
|
973
|
-
if (isSpace) {
|
|
974
|
-
if (trim) {
|
|
975
|
-
return true;
|
|
976
|
-
}
|
|
977
|
-
rows.push(stateTracker.getStartEscapesForAllActiveAttributes() + segText);
|
|
978
|
-
return true;
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
currentLine += segText;
|
|
982
|
-
currentWidth += segment.width;
|
|
983
|
-
}
|
|
984
|
-
return true;
|
|
985
|
-
}
|
|
986
|
-
});
|
|
987
|
-
if (currentLine) {
|
|
988
|
-
rows.push(currentLine);
|
|
989
|
-
}
|
|
990
|
-
return trim ? rows.map((row) => stringVisibleTrimSpacesRight(row)) : rows;
|
|
991
|
-
};
|
|
992
|
-
const wrapWithWordBoundaries = (string, width, trim) => {
|
|
993
|
-
if (string.length === 0) {
|
|
994
|
-
return [];
|
|
995
|
-
}
|
|
996
|
-
const inputToProcess = trim ? string.trim() : string;
|
|
997
|
-
if (inputToProcess.length === 0) {
|
|
998
|
-
return [];
|
|
999
|
-
}
|
|
1000
|
-
const tokens = inputToProcess.split(RE_SPLIT_WHITESPACE);
|
|
1001
|
-
const rows = [];
|
|
1002
|
-
let currentLine = "";
|
|
1003
|
-
let currentWidth = 0;
|
|
1004
|
-
let index = 0;
|
|
1005
|
-
while (index < tokens.length) {
|
|
1006
|
-
const token = tokens[index];
|
|
1007
|
-
const isSpace = RE_WHITESPACE_ONLY.test(token);
|
|
1008
|
-
const tokenVisibleWidth = getStringWidth(token);
|
|
1009
|
-
if (token.length === 0) {
|
|
1010
|
-
index += 1;
|
|
1011
|
-
continue;
|
|
1012
|
-
}
|
|
1013
|
-
if (trim && isSpace && currentWidth === 0) {
|
|
1014
|
-
index += 1;
|
|
1015
|
-
continue;
|
|
1016
|
-
}
|
|
1017
|
-
if (currentWidth + tokenVisibleWidth > width && currentWidth > 0) {
|
|
1018
|
-
if (trim) {
|
|
1019
|
-
rows.push(stringVisibleTrimSpacesRight(currentLine));
|
|
1020
|
-
} else {
|
|
1021
|
-
rows.push(currentLine);
|
|
1022
|
-
}
|
|
1023
|
-
currentLine = "";
|
|
1024
|
-
currentWidth = 0;
|
|
1025
|
-
continue;
|
|
1026
|
-
}
|
|
1027
|
-
currentLine += token;
|
|
1028
|
-
currentWidth += tokenVisibleWidth;
|
|
1029
|
-
index += 1;
|
|
1030
|
-
}
|
|
1031
|
-
if (currentLine) {
|
|
1032
|
-
if (trim) {
|
|
1033
|
-
rows.push(stringVisibleTrimSpacesRight(currentLine));
|
|
1034
|
-
} else {
|
|
1035
|
-
rows.push(currentLine);
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
return rows;
|
|
1039
|
-
};
|
|
1040
|
-
const wrapAndBreakWords = (string, width, trim) => {
|
|
1041
|
-
if (string.length === 0) {
|
|
1042
|
-
return [];
|
|
1043
|
-
}
|
|
1044
|
-
const inputToProcess = trim ? string.trim() : string;
|
|
1045
|
-
if (inputToProcess.length === 0) {
|
|
1046
|
-
return [];
|
|
1047
|
-
}
|
|
1048
|
-
const tokens = inputToProcess.split(RE_SPLIT_WHITESPACE);
|
|
1049
|
-
const rows = [];
|
|
1050
|
-
let currentLine = "";
|
|
1051
|
-
let currentWidth = 0;
|
|
1052
|
-
let index = 0;
|
|
1053
|
-
while (index < tokens.length) {
|
|
1054
|
-
const token = tokens[index];
|
|
1055
|
-
const isSpace = RE_WHITESPACE_ONLY.test(token);
|
|
1056
|
-
const tokenVisibleWidth = getStringWidth(token);
|
|
1057
|
-
if (token.length === 0) {
|
|
1058
|
-
index += 1;
|
|
1059
|
-
continue;
|
|
1060
|
-
}
|
|
1061
|
-
if (trim && isSpace && currentWidth === 0) {
|
|
1062
|
-
index += 1;
|
|
1063
|
-
continue;
|
|
1064
|
-
}
|
|
1065
|
-
if (tokenVisibleWidth > width) {
|
|
1066
|
-
if (currentLine) {
|
|
1067
|
-
rows.push(resetAnsiAtLineBreak(trim ? stringVisibleTrimSpacesRight(currentLine) : currentLine));
|
|
1068
|
-
}
|
|
1069
|
-
const brokenLines = wrapWithBreakAtWidth(token, width, trim);
|
|
1070
|
-
if (brokenLines.length > 0) {
|
|
1071
|
-
for (let brokenLineIndex = 0; brokenLineIndex < brokenLines.length - 1; brokenLineIndex += 1) {
|
|
1072
|
-
rows.push(brokenLines[brokenLineIndex]);
|
|
1073
|
-
}
|
|
1074
|
-
currentLine = brokenLines.at(-1);
|
|
1075
|
-
currentWidth = getStringWidth(currentLine);
|
|
1076
|
-
} else {
|
|
1077
|
-
currentLine = "";
|
|
1078
|
-
currentWidth = 0;
|
|
1079
|
-
}
|
|
1080
|
-
index += 1;
|
|
1081
|
-
continue;
|
|
1082
|
-
}
|
|
1083
|
-
if (currentWidth + tokenVisibleWidth > width && currentWidth > 0) {
|
|
1084
|
-
rows.push(resetAnsiAtLineBreak(trim ? stringVisibleTrimSpacesRight(currentLine) : currentLine));
|
|
1085
|
-
currentLine = "";
|
|
1086
|
-
currentWidth = 0;
|
|
1087
|
-
if (trim && isSpace) {
|
|
1088
|
-
index += 1;
|
|
1089
|
-
continue;
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
currentLine += token;
|
|
1093
|
-
currentWidth += tokenVisibleWidth;
|
|
1094
|
-
index += 1;
|
|
1095
|
-
}
|
|
1096
|
-
if (currentLine) {
|
|
1097
|
-
rows.push(resetAnsiAtLineBreak(trim ? stringVisibleTrimSpacesRight(currentLine) : currentLine));
|
|
1098
|
-
}
|
|
1099
|
-
return rows;
|
|
1100
|
-
};
|
|
1101
|
-
const WrapMode = {
|
|
1102
|
-
/**
|
|
1103
|
-
* Breaks words at character boundaries to fit the width
|
|
1104
|
-
*/
|
|
1105
|
-
BREAK_AT_CHARACTERS: "BREAK_AT_CHARACTERS",
|
|
1106
|
-
/**
|
|
1107
|
-
* Breaks lines at word boundaries. If a word is longer than the width, it will be broken.
|
|
1108
|
-
*/
|
|
1109
|
-
BREAK_WORDS: "BREAK_WORDS",
|
|
1110
|
-
/**
|
|
1111
|
-
* Preserves word boundaries, words are kept intact even if they exceed width
|
|
1112
|
-
*/
|
|
1113
|
-
PRESERVE_WORDS: "PRESERVE_WORDS",
|
|
1114
|
-
/**
|
|
1115
|
-
* Enforces strict adherence to the width limit by breaking at exact width
|
|
1116
|
-
*/
|
|
1117
|
-
STRICT_WIDTH: "STRICT_WIDTH"
|
|
1118
|
-
};
|
|
1119
|
-
const wordWrap = (string, options = {}) => {
|
|
1120
|
-
const { removeZeroWidthCharacters = true, trim = true, width = 80, wrapMode = WrapMode.PRESERVE_WORDS } = options;
|
|
1121
|
-
if (trim && string.trim() === "") {
|
|
1122
|
-
return "";
|
|
1123
|
-
}
|
|
1124
|
-
let normalizedString = string.normalize("NFC").replaceAll("\r\n", "\n");
|
|
1125
|
-
if (removeZeroWidthCharacters) {
|
|
1126
|
-
normalizedString = normalizedString.replaceAll(RE_ZERO_WIDTH, "");
|
|
1127
|
-
}
|
|
1128
|
-
const result = normalizedString.split("\n").map((line) => {
|
|
1129
|
-
if (trim && line.trim() === "") {
|
|
1130
|
-
return "";
|
|
1131
|
-
}
|
|
1132
|
-
let wrappedLines;
|
|
1133
|
-
switch (wrapMode) {
|
|
1134
|
-
case WrapMode.BREAK_AT_CHARACTERS: {
|
|
1135
|
-
wrappedLines = wrapCharByChar(line, width, trim);
|
|
1136
|
-
break;
|
|
1137
|
-
}
|
|
1138
|
-
case WrapMode.BREAK_WORDS: {
|
|
1139
|
-
wrappedLines = wrapAndBreakWords(line, width, trim);
|
|
1140
|
-
break;
|
|
1141
|
-
}
|
|
1142
|
-
case WrapMode.STRICT_WIDTH: {
|
|
1143
|
-
wrappedLines = wrapWithBreakAtWidth(line, width, trim);
|
|
1144
|
-
break;
|
|
1145
|
-
}
|
|
1146
|
-
default: {
|
|
1147
|
-
wrappedLines = wrapWithWordBoundaries(line, width, trim);
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
return preserveAnsi(wrappedLines);
|
|
1151
|
-
});
|
|
1152
|
-
return result.join("\n");
|
|
1153
|
-
};
|
|
1154
|
-
|
|
1155
|
-
const defaultColumns = 80;
|
|
1156
|
-
const defaultRows = 24;
|
|
1157
|
-
const exec = (command, arguments_, { shell, env } = {}) => execFileSync(command, arguments_, {
|
|
1158
|
-
encoding: "utf8",
|
|
1159
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
1160
|
-
timeout: 500,
|
|
1161
|
-
shell,
|
|
1162
|
-
env
|
|
1163
|
-
}).trim();
|
|
1164
|
-
const create = (columns, rows) => ({
|
|
1165
|
-
columns: Number.parseInt(columns, 10),
|
|
1166
|
-
rows: Number.parseInt(rows, 10)
|
|
1167
|
-
});
|
|
1168
|
-
const createIfNotDefault = (maybeColumns, maybeRows) => {
|
|
1169
|
-
const { columns, rows } = create(maybeColumns, maybeRows);
|
|
1170
|
-
if (Number.isNaN(columns) || Number.isNaN(rows)) {
|
|
1171
|
-
return;
|
|
1172
|
-
}
|
|
1173
|
-
if (columns === defaultColumns && rows === defaultRows) {
|
|
1174
|
-
return;
|
|
1175
|
-
}
|
|
1176
|
-
return { columns, rows };
|
|
1177
|
-
};
|
|
1178
|
-
const isForegroundProcess = () => {
|
|
1179
|
-
if (process.platform !== "linux") {
|
|
1180
|
-
return true;
|
|
1181
|
-
}
|
|
1182
|
-
try {
|
|
1183
|
-
const statContents = fs.readFileSync("/proc/self/stat", "utf8");
|
|
1184
|
-
const closingParenthesisIndex = statContents.lastIndexOf(") ");
|
|
1185
|
-
if (closingParenthesisIndex === -1) {
|
|
1186
|
-
return false;
|
|
1187
|
-
}
|
|
1188
|
-
const statFields = statContents.slice(closingParenthesisIndex + 2).trim().split(/\s+/);
|
|
1189
|
-
const processGroupId = Number.parseInt(statFields[2], 10);
|
|
1190
|
-
const foregroundProcessGroupId = Number.parseInt(statFields[5], 10);
|
|
1191
|
-
if (Number.isNaN(processGroupId) || Number.isNaN(foregroundProcessGroupId)) {
|
|
1192
|
-
return false;
|
|
1193
|
-
}
|
|
1194
|
-
if (foregroundProcessGroupId <= 0) {
|
|
1195
|
-
return false;
|
|
1196
|
-
}
|
|
1197
|
-
return processGroupId === foregroundProcessGroupId;
|
|
1198
|
-
} catch {
|
|
1199
|
-
return false;
|
|
1200
|
-
}
|
|
1201
|
-
};
|
|
1202
|
-
function terminalSize() {
|
|
1203
|
-
const { env, stdout, stderr } = process;
|
|
1204
|
-
if (stdout?.columns && stdout?.rows) {
|
|
1205
|
-
return create(stdout.columns, stdout.rows);
|
|
1206
|
-
}
|
|
1207
|
-
if (stderr?.columns && stderr?.rows) {
|
|
1208
|
-
return create(stderr.columns, stderr.rows);
|
|
1209
|
-
}
|
|
1210
|
-
if (env.COLUMNS && env.LINES) {
|
|
1211
|
-
return create(env.COLUMNS, env.LINES);
|
|
1212
|
-
}
|
|
1213
|
-
const fallback = {
|
|
1214
|
-
columns: defaultColumns,
|
|
1215
|
-
rows: defaultRows
|
|
1216
|
-
};
|
|
1217
|
-
if (process.platform === "win32") {
|
|
1218
|
-
return tput() ?? fallback;
|
|
1219
|
-
}
|
|
1220
|
-
if (process.platform === "darwin") {
|
|
1221
|
-
return devTty() ?? tput() ?? fallback;
|
|
1222
|
-
}
|
|
1223
|
-
return devTty() ?? tput() ?? resize() ?? fallback;
|
|
1224
|
-
}
|
|
1225
|
-
const devTty = () => {
|
|
1226
|
-
try {
|
|
1227
|
-
const flags = process.platform === "darwin" ? fs.constants.O_EVTONLY | fs.constants.O_NONBLOCK : fs.constants.O_NONBLOCK;
|
|
1228
|
-
const { columns, rows } = tty.WriteStream(fs.openSync("/dev/tty", flags));
|
|
1229
|
-
return { columns, rows };
|
|
1230
|
-
} catch {
|
|
1231
|
-
}
|
|
1232
|
-
};
|
|
1233
|
-
const tput = () => {
|
|
1234
|
-
try {
|
|
1235
|
-
const columns = exec("tput", ["cols"], { env: { TERM: "dumb", ...process.env } });
|
|
1236
|
-
const rows = exec("tput", ["lines"], { env: { TERM: "dumb", ...process.env } });
|
|
1237
|
-
if (columns && rows) {
|
|
1238
|
-
return createIfNotDefault(columns, rows);
|
|
1239
|
-
}
|
|
1240
|
-
} catch {
|
|
1241
|
-
}
|
|
1242
|
-
};
|
|
1243
|
-
const resize = () => {
|
|
1244
|
-
try {
|
|
1245
|
-
if (!isForegroundProcess()) {
|
|
1246
|
-
return;
|
|
1247
|
-
}
|
|
1248
|
-
const size = exec("resize", ["-u"]).match(/\d+/g);
|
|
1249
|
-
if (size.length === 2) {
|
|
1250
|
-
return createIfNotDefault(size[0], size[1]);
|
|
1251
|
-
}
|
|
1252
|
-
} catch {
|
|
1253
|
-
}
|
|
1254
|
-
};
|
|
1255
|
-
|
|
1256
|
-
export { WrapMode as W, getStringWidth as g, terminalSize as t, wordWrap as w };
|