@visulima/string 1.0.0
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 +5 -0
- package/LICENSE.md +77 -0
- package/README.md +880 -0
- package/dist/case/camel-case.cjs +59 -0
- package/dist/case/camel-case.d.cts +7 -0
- package/dist/case/camel-case.d.mts +7 -0
- package/dist/case/camel-case.d.ts +7 -0
- package/dist/case/camel-case.mjs +57 -0
- package/dist/case/capital-case.cjs +51 -0
- package/dist/case/capital-case.d.cts +7 -0
- package/dist/case/capital-case.d.mts +7 -0
- package/dist/case/capital-case.d.ts +7 -0
- package/dist/case/capital-case.mjs +49 -0
- package/dist/case/constant-case.cjs +11 -0
- package/dist/case/constant-case.d.cts +7 -0
- package/dist/case/constant-case.d.mts +7 -0
- package/dist/case/constant-case.d.ts +7 -0
- package/dist/case/constant-case.mjs +9 -0
- package/dist/case/dot-case.cjs +11 -0
- package/dist/case/dot-case.d.cts +7 -0
- package/dist/case/dot-case.d.mts +7 -0
- package/dist/case/dot-case.d.ts +7 -0
- package/dist/case/dot-case.mjs +9 -0
- package/dist/case/flat-case.cjs +11 -0
- package/dist/case/flat-case.d.cts +7 -0
- package/dist/case/flat-case.d.mts +7 -0
- package/dist/case/flat-case.d.ts +7 -0
- package/dist/case/flat-case.mjs +9 -0
- package/dist/case/flip-case.cjs +61 -0
- package/dist/case/flip-case.d.cts +8 -0
- package/dist/case/flip-case.d.mts +8 -0
- package/dist/case/flip-case.d.ts +8 -0
- package/dist/case/flip-case.mjs +57 -0
- package/dist/case/identify-case.cjs +30 -0
- package/dist/case/identify-case.d.cts +7 -0
- package/dist/case/identify-case.d.mts +7 -0
- package/dist/case/identify-case.d.ts +7 -0
- package/dist/case/identify-case.mjs +28 -0
- package/dist/case/index.cjs +47 -0
- package/dist/case/index.d.cts +26 -0
- package/dist/case/index.d.mts +26 -0
- package/dist/case/index.d.ts +26 -0
- package/dist/case/index.mjs +20 -0
- package/dist/case/kebab-case.cjs +61 -0
- package/dist/case/kebab-case.d.cts +11 -0
- package/dist/case/kebab-case.d.mts +11 -0
- package/dist/case/kebab-case.d.ts +11 -0
- package/dist/case/kebab-case.mjs +57 -0
- package/dist/case/lower-first.cjs +13 -0
- package/dist/case/lower-first.d.cts +7 -0
- package/dist/case/lower-first.d.mts +7 -0
- package/dist/case/lower-first.d.ts +7 -0
- package/dist/case/lower-first.mjs +11 -0
- package/dist/case/no-case.cjs +11 -0
- package/dist/case/no-case.d.cts +7 -0
- package/dist/case/no-case.d.mts +7 -0
- package/dist/case/no-case.d.ts +7 -0
- package/dist/case/no-case.mjs +9 -0
- package/dist/case/pascal-case.cjs +52 -0
- package/dist/case/pascal-case.d.cts +7 -0
- package/dist/case/pascal-case.d.mts +7 -0
- package/dist/case/pascal-case.d.ts +7 -0
- package/dist/case/pascal-case.mjs +50 -0
- package/dist/case/pascal-snake-case.cjs +32 -0
- package/dist/case/pascal-snake-case.d.cts +7 -0
- package/dist/case/pascal-snake-case.d.mts +7 -0
- package/dist/case/pascal-snake-case.d.ts +7 -0
- package/dist/case/pascal-snake-case.mjs +30 -0
- package/dist/case/path-case.cjs +11 -0
- package/dist/case/path-case.d.cts +7 -0
- package/dist/case/path-case.d.mts +7 -0
- package/dist/case/path-case.d.ts +7 -0
- package/dist/case/path-case.mjs +9 -0
- package/dist/case/sentence-case.cjs +58 -0
- package/dist/case/sentence-case.d.cts +7 -0
- package/dist/case/sentence-case.d.mts +7 -0
- package/dist/case/sentence-case.d.ts +7 -0
- package/dist/case/sentence-case.mjs +56 -0
- package/dist/case/snake-case.cjs +11 -0
- package/dist/case/snake-case.d.cts +7 -0
- package/dist/case/snake-case.d.mts +7 -0
- package/dist/case/snake-case.d.ts +7 -0
- package/dist/case/snake-case.mjs +9 -0
- package/dist/case/split-by-case.cjs +638 -0
- package/dist/case/split-by-case.d.cts +16 -0
- package/dist/case/split-by-case.d.mts +16 -0
- package/dist/case/split-by-case.d.ts +16 -0
- package/dist/case/split-by-case.mjs +634 -0
- package/dist/case/title-case.cjs +47 -0
- package/dist/case/title-case.d.cts +7 -0
- package/dist/case/title-case.d.mts +7 -0
- package/dist/case/title-case.d.ts +7 -0
- package/dist/case/title-case.mjs +45 -0
- package/dist/case/train-case.cjs +44 -0
- package/dist/case/train-case.d.cts +7 -0
- package/dist/case/train-case.d.mts +7 -0
- package/dist/case/train-case.d.ts +7 -0
- package/dist/case/train-case.mjs +42 -0
- package/dist/case/upper-first.cjs +13 -0
- package/dist/case/upper-first.d.cts +7 -0
- package/dist/case/upper-first.d.mts +7 -0
- package/dist/case/upper-first.d.ts +7 -0
- package/dist/case/upper-first.mjs +11 -0
- package/dist/get-string-truncated-width.cjs +386 -0
- package/dist/get-string-truncated-width.d.cts +25 -0
- package/dist/get-string-truncated-width.d.mts +25 -0
- package/dist/get-string-truncated-width.d.ts +25 -0
- package/dist/get-string-truncated-width.mjs +382 -0
- package/dist/get-string-width.cjs +11 -0
- package/dist/get-string-width.d.cts +6 -0
- package/dist/get-string-width.d.mts +6 -0
- package/dist/get-string-width.d.ts +6 -0
- package/dist/get-string-width.mjs +7 -0
- package/dist/index.cjs +60 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.mts +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.mjs +26 -0
- package/dist/native-string-types.d.cts +128 -0
- package/dist/native-string-types.d.mts +128 -0
- package/dist/native-string-types.d.ts +128 -0
- package/dist/outdent.cjs +122 -0
- package/dist/outdent.d.cts +15 -0
- package/dist/outdent.d.mts +15 -0
- package/dist/outdent.d.ts +15 -0
- package/dist/outdent.mjs +118 -0
- package/dist/packem_shared/LRUCache-Crb-m7cw.cjs +53 -0
- package/dist/packem_shared/LRUCache-udNErhWw.mjs +51 -0
- package/dist/packem_shared/WrapMode-BgOOthJV.mjs +545 -0
- package/dist/packem_shared/WrapMode-ZqTeXK8X.cjs +550 -0
- package/dist/packem_shared/constants-BnVGLrDr.mjs +99 -0
- package/dist/packem_shared/constants-HevSYUjU.cjs +149 -0
- package/dist/packem_shared/generate-cache-key-D8f1auDB.cjs +10 -0
- package/dist/packem_shared/generate-cache-key-YSju5pj2.mjs +8 -0
- package/dist/packem_shared/joinSegments-BH1CT4dK.cjs +48 -0
- package/dist/packem_shared/joinSegments-BHY8PPnk.mjs +46 -0
- package/dist/packem_shared/normalize-german-eszett-DJK0S2qj.cjs +11 -0
- package/dist/packem_shared/normalize-german-eszett-fpJ4lZL6.mjs +9 -0
- package/dist/packem_shared/types-BAd3WDHo.d.mts +59 -0
- package/dist/packem_shared/types-BNbQJg02.d.cts +41 -0
- package/dist/packem_shared/types-BNbQJg02.d.mts +41 -0
- package/dist/packem_shared/types-BNbQJg02.d.ts +41 -0
- package/dist/packem_shared/types-BttWSSIk.d.ts +59 -0
- package/dist/packem_shared/types-D2-6p7t1.d.cts +59 -0
- package/dist/slice.cjs +413 -0
- package/dist/slice.d.cts +10 -0
- package/dist/slice.d.mts +10 -0
- package/dist/slice.d.ts +10 -0
- package/dist/slice.mjs +409 -0
- package/dist/test/utils.cjs +77 -0
- package/dist/test/utils.d.cts +30 -0
- package/dist/test/utils.d.mts +30 -0
- package/dist/test/utils.d.ts +30 -0
- package/dist/test/utils.mjs +71 -0
- package/dist/test/vitest.cjs +11 -0
- package/dist/test/vitest.d.cts +8 -0
- package/dist/test/vitest.d.mts +8 -0
- package/dist/test/vitest.d.ts +8 -0
- package/dist/test/vitest.mjs +7 -0
- package/dist/truncate.cjs +90 -0
- package/dist/truncate.d.cts +12 -0
- package/dist/truncate.d.mts +12 -0
- package/dist/truncate.d.ts +12 -0
- package/dist/truncate.mjs +86 -0
- package/dist/utils.cjs +9 -0
- package/dist/utils.d.cts +14 -0
- package/dist/utils.d.mts +14 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.mjs +1 -0
- package/package.json +468 -0
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import { N as ESCAPES, O as ANSI_ESCAPE_LINK, P as ANSI_SGR_TERMINATOR, Q as ANSI_ESCAPE_BELL, S as ANSI_CSI, T as RE_ESCAPE_PATTERN, U as ANSI_RESET_CODES, V as END_CODE, W as RE_ZERO_WIDTH } from './constants-BnVGLrDr.mjs';
|
|
2
|
+
import { getStringWidth } from '../get-string-width.mjs';
|
|
3
|
+
|
|
4
|
+
var __defProp$5 = Object.defineProperty;
|
|
5
|
+
var __name$5 = (target, value) => __defProp$5(target, "name", { value, configurable: true });
|
|
6
|
+
class AnsiStateTracker {
|
|
7
|
+
static {
|
|
8
|
+
__name$5(this, "AnsiStateTracker");
|
|
9
|
+
}
|
|
10
|
+
activeForeground = null;
|
|
11
|
+
activeBackground = null;
|
|
12
|
+
// Track other formatting (bold, italic, etc.)
|
|
13
|
+
activeFormatting = [];
|
|
14
|
+
/**
|
|
15
|
+
* Processes an escape sequence and updates the internal state
|
|
16
|
+
* @param sequence - The escape sequence to process
|
|
17
|
+
*/
|
|
18
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
19
|
+
processEscape(sequence) {
|
|
20
|
+
const match = /\x1B\[(\d+)m/.exec(sequence);
|
|
21
|
+
if (!match) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const code = Number.parseInt(match[1], 10);
|
|
25
|
+
switch (code) {
|
|
26
|
+
case 0: {
|
|
27
|
+
this.activeForeground = null;
|
|
28
|
+
this.activeBackground = null;
|
|
29
|
+
this.activeFormatting = [];
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 39: {
|
|
33
|
+
this.activeForeground = null;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case 49: {
|
|
37
|
+
this.activeBackground = null;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
default: {
|
|
41
|
+
if (code >= 30 && code <= 37 || code >= 90 && code <= 97) {
|
|
42
|
+
this.activeForeground = sequence;
|
|
43
|
+
} else if (code >= 40 && code <= 47 || code >= 100 && code <= 107) {
|
|
44
|
+
this.activeBackground = sequence;
|
|
45
|
+
} else if ([1, 2, 3, 4, 7, 8, 9].includes(code)) {
|
|
46
|
+
this.activeFormatting.push(sequence);
|
|
47
|
+
} else if ([22, 23, 24, 27, 28, 29].includes(code)) {
|
|
48
|
+
const formatResetMap = {
|
|
49
|
+
22: "[1m",
|
|
50
|
+
// Reset bold
|
|
51
|
+
23: "[3m",
|
|
52
|
+
// Reset italic
|
|
53
|
+
24: "[4m",
|
|
54
|
+
// Reset underline
|
|
55
|
+
27: "[7m",
|
|
56
|
+
// Reset inverse
|
|
57
|
+
28: "[8m",
|
|
58
|
+
// Reset hidden
|
|
59
|
+
29: "[9m"
|
|
60
|
+
// Reset strikethrough
|
|
61
|
+
};
|
|
62
|
+
const formatToRemove = formatResetMap[code];
|
|
63
|
+
if (formatToRemove) {
|
|
64
|
+
this.activeFormatting = this.activeFormatting.filter((fmt) => !fmt.includes(formatToRemove));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets all active escape sequences to apply
|
|
72
|
+
* @returns String with all active escapes
|
|
73
|
+
*/
|
|
74
|
+
getActiveEscapes() {
|
|
75
|
+
return [this.activeBackground, this.activeForeground, ...this.activeFormatting].filter(Boolean).join("");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
var __defProp$4 = Object.defineProperty;
|
|
80
|
+
var __name$4 = (target, value) => __defProp$4(target, "name", { value, configurable: true });
|
|
81
|
+
const checkEscapeSequence = /* @__PURE__ */ __name$4((chars, index) => {
|
|
82
|
+
if (!ESCAPES.has(chars[index])) {
|
|
83
|
+
return { isInsideEscape: false, isInsideLinkEscape: false };
|
|
84
|
+
}
|
|
85
|
+
const isInsideEscape = true;
|
|
86
|
+
const possibleLink = chars.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join("");
|
|
87
|
+
const isInsideLinkEscape = possibleLink === ANSI_ESCAPE_LINK;
|
|
88
|
+
return { isInsideEscape, isInsideLinkEscape };
|
|
89
|
+
}, "checkEscapeSequence");
|
|
90
|
+
const processAnsiString = /* @__PURE__ */ __name$4((string, options = {}) => {
|
|
91
|
+
const stateTracker = new AnsiStateTracker();
|
|
92
|
+
let currentText = "";
|
|
93
|
+
let isInsideEscape = false;
|
|
94
|
+
let isInsideLinkEscape = false;
|
|
95
|
+
let escapeBuffer = "";
|
|
96
|
+
let currentUrl = "";
|
|
97
|
+
let isInHyperlink = false;
|
|
98
|
+
const chars = [...string];
|
|
99
|
+
for (let index = 0; index < chars.length; index++) {
|
|
100
|
+
const character = chars[index];
|
|
101
|
+
if (character && ESCAPES.has(character)) {
|
|
102
|
+
if (currentText) {
|
|
103
|
+
const width2 = options.getWidth?.(currentText) ?? 0;
|
|
104
|
+
const segment2 = {
|
|
105
|
+
isEscapeSequence: false,
|
|
106
|
+
isGrapheme: true,
|
|
107
|
+
text: currentText,
|
|
108
|
+
width: width2
|
|
109
|
+
};
|
|
110
|
+
if (isInHyperlink) {
|
|
111
|
+
segment2.isHyperlink = true;
|
|
112
|
+
segment2.hyperlinkUrl = currentUrl;
|
|
113
|
+
}
|
|
114
|
+
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
currentText = "";
|
|
118
|
+
}
|
|
119
|
+
isInsideEscape = true;
|
|
120
|
+
escapeBuffer = character;
|
|
121
|
+
const escapeInfo = checkEscapeSequence(chars, index);
|
|
122
|
+
isInsideLinkEscape = escapeInfo.isInsideLinkEscape;
|
|
123
|
+
if (isInsideLinkEscape) {
|
|
124
|
+
let urlEnd = index + 1;
|
|
125
|
+
currentUrl = "";
|
|
126
|
+
while (urlEnd < chars.length) {
|
|
127
|
+
const nextChar = chars[urlEnd];
|
|
128
|
+
if (nextChar === ANSI_ESCAPE_BELL) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
currentUrl += nextChar;
|
|
132
|
+
urlEnd++;
|
|
133
|
+
}
|
|
134
|
+
currentUrl = currentUrl.slice(4);
|
|
135
|
+
const segment2 = {
|
|
136
|
+
hyperlinkUrl: currentUrl,
|
|
137
|
+
isEscapeSequence: true,
|
|
138
|
+
isGrapheme: false,
|
|
139
|
+
isHyperlink: true,
|
|
140
|
+
isHyperlinkStart: true,
|
|
141
|
+
width: 0
|
|
142
|
+
};
|
|
143
|
+
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
index = urlEnd;
|
|
147
|
+
isInHyperlink = true;
|
|
148
|
+
isInsideEscape = false;
|
|
149
|
+
isInsideLinkEscape = false;
|
|
150
|
+
escapeBuffer = "";
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (index + 1 < chars.length && chars[index + 1] === "\\" && isInHyperlink) {
|
|
154
|
+
const segment2 = {
|
|
155
|
+
isEscapeSequence: true,
|
|
156
|
+
isGrapheme: false,
|
|
157
|
+
isHyperlink: true,
|
|
158
|
+
isHyperlinkEnd: true,
|
|
159
|
+
width: 0
|
|
160
|
+
};
|
|
161
|
+
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
isInHyperlink = false;
|
|
165
|
+
currentUrl = "";
|
|
166
|
+
index++;
|
|
167
|
+
isInsideEscape = false;
|
|
168
|
+
escapeBuffer = "";
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (isInsideEscape) {
|
|
173
|
+
if (escapeBuffer !== character) {
|
|
174
|
+
escapeBuffer += character;
|
|
175
|
+
}
|
|
176
|
+
if (character === ANSI_SGR_TERMINATOR) {
|
|
177
|
+
isInsideEscape = false;
|
|
178
|
+
stateTracker.processEscape(escapeBuffer);
|
|
179
|
+
const segment2 = {
|
|
180
|
+
isEscapeSequence: true,
|
|
181
|
+
isGrapheme: false,
|
|
182
|
+
text: escapeBuffer,
|
|
183
|
+
width: 0
|
|
184
|
+
};
|
|
185
|
+
if (options.onSegment?.(segment2, stateTracker) === false) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
escapeBuffer = "";
|
|
189
|
+
}
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
currentText += character;
|
|
193
|
+
const width = options.getWidth?.(currentText) ?? 0;
|
|
194
|
+
const segment = {
|
|
195
|
+
isEscapeSequence: false,
|
|
196
|
+
isGrapheme: true,
|
|
197
|
+
text: currentText,
|
|
198
|
+
width
|
|
199
|
+
};
|
|
200
|
+
if (isInHyperlink) {
|
|
201
|
+
segment.isHyperlink = true;
|
|
202
|
+
segment.hyperlinkUrl = currentUrl;
|
|
203
|
+
}
|
|
204
|
+
if (options.onSegment?.(segment, stateTracker) === false) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
currentText = "";
|
|
208
|
+
}
|
|
209
|
+
if (currentText) {
|
|
210
|
+
const width = options.getWidth?.(currentText) ?? 0;
|
|
211
|
+
const segment = {
|
|
212
|
+
isEscapeSequence: false,
|
|
213
|
+
isGrapheme: true,
|
|
214
|
+
text: currentText,
|
|
215
|
+
width
|
|
216
|
+
};
|
|
217
|
+
if (isInHyperlink) {
|
|
218
|
+
segment.isHyperlink = true;
|
|
219
|
+
segment.hyperlinkUrl = currentUrl;
|
|
220
|
+
}
|
|
221
|
+
options.onSegment?.(segment, stateTracker);
|
|
222
|
+
}
|
|
223
|
+
if (escapeBuffer) {
|
|
224
|
+
const segment = {
|
|
225
|
+
isEscapeSequence: true,
|
|
226
|
+
isGrapheme: false,
|
|
227
|
+
text: escapeBuffer,
|
|
228
|
+
width: 0
|
|
229
|
+
};
|
|
230
|
+
options.onSegment?.(segment, stateTracker);
|
|
231
|
+
}
|
|
232
|
+
}, "processAnsiString");
|
|
233
|
+
|
|
234
|
+
var __defProp$3 = Object.defineProperty;
|
|
235
|
+
var __name$3 = (target, value) => __defProp$3(target, "name", { value, configurable: true });
|
|
236
|
+
const wrapAnsiCode = /* @__PURE__ */ __name$3((code) => {
|
|
237
|
+
const escapeChar = ESCAPES.values().next().value;
|
|
238
|
+
return `${escapeChar}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
|
|
239
|
+
}, "wrapAnsiCode");
|
|
240
|
+
|
|
241
|
+
var __defProp$2 = Object.defineProperty;
|
|
242
|
+
var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true });
|
|
243
|
+
const wrapAnsiHyperlink = /* @__PURE__ */ __name$2((url) => {
|
|
244
|
+
const escapeChar = ESCAPES.values().next().value;
|
|
245
|
+
return `${escapeChar}${ANSI_ESCAPE_LINK}${url}${ANSI_ESCAPE_BELL}`;
|
|
246
|
+
}, "wrapAnsiHyperlink");
|
|
247
|
+
|
|
248
|
+
var __defProp$1 = Object.defineProperty;
|
|
249
|
+
var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
|
|
250
|
+
const preserveAnsi = /* @__PURE__ */ __name$1((rawLines) => {
|
|
251
|
+
if (rawLines.length === 0) {
|
|
252
|
+
return "";
|
|
253
|
+
}
|
|
254
|
+
if (rawLines.length === 1) {
|
|
255
|
+
return rawLines[0];
|
|
256
|
+
}
|
|
257
|
+
let returnValue = "";
|
|
258
|
+
let escapeCode;
|
|
259
|
+
let escapeUrl;
|
|
260
|
+
const preString = rawLines.join("\n");
|
|
261
|
+
const pre = [...preString];
|
|
262
|
+
let preStringIndex = 0;
|
|
263
|
+
for (const [index, character] of pre.entries()) {
|
|
264
|
+
returnValue += character;
|
|
265
|
+
if (ESCAPES.has(character)) {
|
|
266
|
+
const match = RE_ESCAPE_PATTERN.exec(preString.slice(preStringIndex));
|
|
267
|
+
const groups = match?.groups ?? {};
|
|
268
|
+
if (groups.code !== void 0) {
|
|
269
|
+
const code2 = Number.parseFloat(groups.code);
|
|
270
|
+
escapeCode = code2 === END_CODE ? void 0 : code2;
|
|
271
|
+
} else if (groups.uri !== void 0) {
|
|
272
|
+
escapeUrl = groups.uri.length === 0 ? void 0 : groups.uri;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const code = ANSI_RESET_CODES.get(Number(escapeCode));
|
|
276
|
+
if (pre[index + 1] === "\n") {
|
|
277
|
+
if (escapeUrl) {
|
|
278
|
+
returnValue += wrapAnsiHyperlink("");
|
|
279
|
+
}
|
|
280
|
+
if (escapeCode && code) {
|
|
281
|
+
returnValue += wrapAnsiCode(code);
|
|
282
|
+
}
|
|
283
|
+
} else if (character === "\n") {
|
|
284
|
+
if (escapeCode && code) {
|
|
285
|
+
returnValue += wrapAnsiCode(escapeCode);
|
|
286
|
+
}
|
|
287
|
+
if (escapeUrl) {
|
|
288
|
+
returnValue += wrapAnsiHyperlink(escapeUrl);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
preStringIndex += character.length;
|
|
292
|
+
}
|
|
293
|
+
return returnValue;
|
|
294
|
+
}, "preserveAnsi");
|
|
295
|
+
|
|
296
|
+
var __defProp = Object.defineProperty;
|
|
297
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
298
|
+
const resetAnsiAtLineBreak = /* @__PURE__ */ __name((currentLine) => {
|
|
299
|
+
if (!currentLine.includes("\x1B")) {
|
|
300
|
+
return currentLine;
|
|
301
|
+
}
|
|
302
|
+
let result = currentLine;
|
|
303
|
+
if (currentLine.includes("\x1B[30m")) {
|
|
304
|
+
result += "\x1B[39m";
|
|
305
|
+
}
|
|
306
|
+
if (currentLine.includes("\x1B[42m")) {
|
|
307
|
+
result += "\x1B[49m";
|
|
308
|
+
}
|
|
309
|
+
return result;
|
|
310
|
+
}, "resetAnsiAtLineBreak");
|
|
311
|
+
const stringVisibleTrimSpacesRight = /* @__PURE__ */ __name((string) => {
|
|
312
|
+
const words = string.split(" ");
|
|
313
|
+
let last = words.length;
|
|
314
|
+
while (last > 0 && getStringWidth(words[last - 1]) === 0) {
|
|
315
|
+
last--;
|
|
316
|
+
}
|
|
317
|
+
if (last === words.length) {
|
|
318
|
+
return string;
|
|
319
|
+
}
|
|
320
|
+
return words.slice(0, last).join(" ") + words.slice(last).join("");
|
|
321
|
+
}, "stringVisibleTrimSpacesRight");
|
|
322
|
+
const wrapWithBreakAtWidth = /* @__PURE__ */ __name((string, width, trim) => {
|
|
323
|
+
if (string.length === 0) {
|
|
324
|
+
return [""];
|
|
325
|
+
}
|
|
326
|
+
if (width <= 0) {
|
|
327
|
+
return [string];
|
|
328
|
+
}
|
|
329
|
+
const rows = [];
|
|
330
|
+
let currentLine = "";
|
|
331
|
+
let currentWidth = 0;
|
|
332
|
+
const ansiTracker = new AnsiStateTracker();
|
|
333
|
+
let isInsideEscape = false;
|
|
334
|
+
let isInsideLinkEscape = false;
|
|
335
|
+
let escapeBuffer = "";
|
|
336
|
+
for (let index = 0; index < string.length; index++) {
|
|
337
|
+
const char = string[index];
|
|
338
|
+
if (ESCAPES.has(char)) {
|
|
339
|
+
isInsideEscape = true;
|
|
340
|
+
escapeBuffer = char;
|
|
341
|
+
currentLine += char;
|
|
342
|
+
const escapeInfo = checkEscapeSequence([...string], index);
|
|
343
|
+
isInsideLinkEscape = escapeInfo.isInsideLinkEscape;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (isInsideEscape) {
|
|
347
|
+
escapeBuffer += char;
|
|
348
|
+
currentLine += char;
|
|
349
|
+
if (isInsideLinkEscape) {
|
|
350
|
+
if (char === ANSI_ESCAPE_BELL) {
|
|
351
|
+
isInsideEscape = isInsideLinkEscape = false;
|
|
352
|
+
}
|
|
353
|
+
} else if (char === ANSI_SGR_TERMINATOR) {
|
|
354
|
+
isInsideEscape = false;
|
|
355
|
+
ansiTracker.processEscape(escapeBuffer);
|
|
356
|
+
}
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const charWidth = getStringWidth(char);
|
|
360
|
+
const isSpace = char === " ";
|
|
361
|
+
if (charWidth === 0) {
|
|
362
|
+
currentLine += char;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (currentWidth + charWidth > width) {
|
|
366
|
+
if (currentLine) {
|
|
367
|
+
rows.push(resetAnsiAtLineBreak(currentLine));
|
|
368
|
+
}
|
|
369
|
+
currentLine = ansiTracker.getActiveEscapes();
|
|
370
|
+
currentWidth = 0;
|
|
371
|
+
if (isSpace && trim) {
|
|
372
|
+
while (index < string.length && string[index] === " ") {
|
|
373
|
+
index++;
|
|
374
|
+
if (index >= string.length) {
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (index < string.length) {
|
|
379
|
+
index--;
|
|
380
|
+
}
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
currentLine += char;
|
|
385
|
+
currentWidth += charWidth;
|
|
386
|
+
if (currentWidth === width && index < string.length - 1) {
|
|
387
|
+
rows.push(resetAnsiAtLineBreak(currentLine));
|
|
388
|
+
currentLine = ansiTracker.getActiveEscapes();
|
|
389
|
+
currentWidth = 0;
|
|
390
|
+
if (index + 1 < string.length && string[index + 1] === " " && trim) {
|
|
391
|
+
index++;
|
|
392
|
+
while (index < string.length && string[index] === " ") {
|
|
393
|
+
index++;
|
|
394
|
+
}
|
|
395
|
+
index--;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (currentLine) {
|
|
400
|
+
rows.push(currentLine);
|
|
401
|
+
}
|
|
402
|
+
return trim ? rows.map((element) => stringVisibleTrimSpacesRight(element)) : rows;
|
|
403
|
+
}, "wrapWithBreakAtWidth");
|
|
404
|
+
const wrapCharByChar = /* @__PURE__ */ __name((string, width, trim) => {
|
|
405
|
+
if (string.length === 0) {
|
|
406
|
+
return [];
|
|
407
|
+
}
|
|
408
|
+
const inputToProcess = trim ? string.trim() : string;
|
|
409
|
+
if (inputToProcess.length === 0) {
|
|
410
|
+
return [];
|
|
411
|
+
}
|
|
412
|
+
const rows = [];
|
|
413
|
+
let currentLine = "";
|
|
414
|
+
let currentWidth = 0;
|
|
415
|
+
processAnsiString(inputToProcess, {
|
|
416
|
+
getWidth: getStringWidth,
|
|
417
|
+
onSegment: /* @__PURE__ */ __name((segment, stateTracker) => {
|
|
418
|
+
if (segment.isEscapeSequence) {
|
|
419
|
+
currentLine += segment.text;
|
|
420
|
+
} else {
|
|
421
|
+
const isSpace = segment.text === " ";
|
|
422
|
+
if (segment.width === 0) {
|
|
423
|
+
currentLine += segment.text;
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
if (currentWidth + segment.width > width) {
|
|
427
|
+
if (currentLine) {
|
|
428
|
+
rows.push(resetAnsiAtLineBreak(currentLine));
|
|
429
|
+
}
|
|
430
|
+
currentLine = stateTracker.getActiveEscapes();
|
|
431
|
+
currentWidth = 0;
|
|
432
|
+
if (isSpace) {
|
|
433
|
+
if (trim) {
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
rows.push(stateTracker.getActiveEscapes() + segment.text);
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
currentLine += segment.text;
|
|
441
|
+
currentWidth += segment.width;
|
|
442
|
+
}
|
|
443
|
+
return true;
|
|
444
|
+
}, "onSegment")
|
|
445
|
+
});
|
|
446
|
+
if (currentLine) {
|
|
447
|
+
rows.push(currentLine);
|
|
448
|
+
}
|
|
449
|
+
return trim ? rows.map((row) => stringVisibleTrimSpacesRight(row)) : rows;
|
|
450
|
+
}, "wrapCharByChar");
|
|
451
|
+
const wrapWithWordBoundaries = /* @__PURE__ */ __name((string, width, trim) => {
|
|
452
|
+
if (string.length === 0) {
|
|
453
|
+
return [];
|
|
454
|
+
}
|
|
455
|
+
const inputToProcess = trim ? string.trim() : string;
|
|
456
|
+
if (inputToProcess.length === 0) {
|
|
457
|
+
return [];
|
|
458
|
+
}
|
|
459
|
+
const tokens = inputToProcess.split(/(?=\s)|(?<=\s)/);
|
|
460
|
+
const rows = [];
|
|
461
|
+
let currentLine = "";
|
|
462
|
+
let currentWidth = 0;
|
|
463
|
+
let index = 0;
|
|
464
|
+
while (index < tokens.length) {
|
|
465
|
+
const token = tokens[index];
|
|
466
|
+
const isSpace = /^\s+$/.test(token);
|
|
467
|
+
const tokenVisibleWidth = getStringWidth(token);
|
|
468
|
+
if (token.length === 0) {
|
|
469
|
+
index++;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (trim && isSpace && currentWidth === 0) {
|
|
473
|
+
index++;
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (currentWidth + tokenVisibleWidth > width && currentWidth > 0) {
|
|
477
|
+
if (trim) {
|
|
478
|
+
rows.push(stringVisibleTrimSpacesRight(currentLine));
|
|
479
|
+
} else {
|
|
480
|
+
rows.push(currentLine);
|
|
481
|
+
}
|
|
482
|
+
currentLine = "";
|
|
483
|
+
currentWidth = 0;
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
currentLine += token;
|
|
487
|
+
currentWidth += tokenVisibleWidth;
|
|
488
|
+
index++;
|
|
489
|
+
}
|
|
490
|
+
if (currentLine) {
|
|
491
|
+
if (trim) {
|
|
492
|
+
rows.push(stringVisibleTrimSpacesRight(currentLine));
|
|
493
|
+
} else {
|
|
494
|
+
rows.push(currentLine);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return rows;
|
|
498
|
+
}, "wrapWithWordBoundaries");
|
|
499
|
+
const WrapMode = {
|
|
500
|
+
/**
|
|
501
|
+
* Breaks words at character boundaries to fit the width
|
|
502
|
+
*/
|
|
503
|
+
BREAK_AT_CHARACTERS: "BREAK_AT_CHARACTERS",
|
|
504
|
+
/**
|
|
505
|
+
* Preserves word boundaries, words are kept intact even if they exceed width
|
|
506
|
+
*/
|
|
507
|
+
PRESERVE_WORDS: "PRESERVE_WORDS",
|
|
508
|
+
/**
|
|
509
|
+
* Enforces strict adherence to the width limit by breaking at exact width
|
|
510
|
+
*/
|
|
511
|
+
STRICT_WIDTH: "STRICT_WIDTH"
|
|
512
|
+
};
|
|
513
|
+
const wordWrap = /* @__PURE__ */ __name((string, options = {}) => {
|
|
514
|
+
const { removeZeroWidthCharacters = true, trim = true, width = 80, wrapMode = WrapMode.PRESERVE_WORDS } = options;
|
|
515
|
+
if (trim && string.trim() === "") {
|
|
516
|
+
return "";
|
|
517
|
+
}
|
|
518
|
+
let normalizedString = String(string).normalize("NFC").replaceAll("\r\n", "\n");
|
|
519
|
+
if (removeZeroWidthCharacters) {
|
|
520
|
+
normalizedString = normalizedString.replaceAll(RE_ZERO_WIDTH, "");
|
|
521
|
+
}
|
|
522
|
+
const result = normalizedString.split("\n").map((line) => {
|
|
523
|
+
if (trim && line.trim() === "") {
|
|
524
|
+
return "";
|
|
525
|
+
}
|
|
526
|
+
let wrappedLines;
|
|
527
|
+
switch (wrapMode) {
|
|
528
|
+
case WrapMode.STRICT_WIDTH: {
|
|
529
|
+
wrappedLines = wrapWithBreakAtWidth(line, width, trim);
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
case WrapMode.BREAK_AT_CHARACTERS: {
|
|
533
|
+
wrappedLines = wrapCharByChar(line, width, trim);
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
default: {
|
|
537
|
+
wrappedLines = wrapWithWordBoundaries(line, width, trim);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return preserveAnsi(wrappedLines);
|
|
541
|
+
});
|
|
542
|
+
return result.join("\n");
|
|
543
|
+
}, "wordWrap");
|
|
544
|
+
|
|
545
|
+
export { WrapMode, wordWrap };
|