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