@oevery/rsp 3.1.0-beta.1 → 3.1.0-beta.2

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.
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/tui/terminal.ts
4
+ function openTerminalSession(stdout, options) {
5
+ let cleaned = false;
6
+ if (!options.screenReader)
7
+ stdout.write("\x1B[?1049h\x1B[?25l");
8
+ return {
9
+ cleanup() {
10
+ if (cleaned)
11
+ return;
12
+ cleaned = true;
13
+ if (!options.screenReader)
14
+ stdout.write("\x1B[?25h\x1B[?1049l");
15
+ }
16
+ };
17
+ }
18
+
19
+ // src/tui/display.ts
20
+ var segmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
21
+ function isWide(codePoint) {
22
+ return codePoint >= 4352 && (codePoint <= 4447 || codePoint === 9001 || codePoint === 9002 || codePoint >= 11904 && codePoint <= 42191 && codePoint !== 12351 || codePoint >= 44032 && codePoint <= 55203 || codePoint >= 63744 && codePoint <= 64255 || codePoint >= 65040 && codePoint <= 65049 || codePoint >= 65072 && codePoint <= 65135 || codePoint >= 65280 && codePoint <= 65376 || codePoint >= 65504 && codePoint <= 65510 || codePoint >= 127744 && codePoint <= 129791 || codePoint >= 131072 && codePoint <= 262141);
23
+ }
24
+ function displayWidth(value) {
25
+ let width = 0;
26
+ for (const { segment } of segmenter.segment(value)) {
27
+ const codePoint = segment.codePointAt(0) ?? 0;
28
+ if (/^\p{Mark}$/u.test(segment) || codePoint === 8205)
29
+ continue;
30
+ width += isWide(codePoint) ? 2 : 1;
31
+ }
32
+ return width;
33
+ }
34
+ function truncateDisplay(value, maxWidth) {
35
+ if (displayWidth(value) <= maxWidth)
36
+ return value;
37
+ if (maxWidth <= 1)
38
+ return "\u2026".slice(0, maxWidth);
39
+ let result = "";
40
+ for (const { segment } of segmenter.segment(value)) {
41
+ if (displayWidth(result + segment) > maxWidth - 1)
42
+ break;
43
+ result += segment;
44
+ }
45
+ return `${result}\u2026`;
46
+ }
47
+ function wrapDisplay(value, maxWidth, firstPrefix = "", continuationPrefix = firstPrefix) {
48
+ const width = Math.max(1, maxWidth);
49
+ if (displayWidth(firstPrefix) >= width)
50
+ return [truncateDisplay(`${firstPrefix}${value}`, width)];
51
+ const lines = [];
52
+ let prefix = firstPrefix;
53
+ let line = prefix;
54
+ const pushLine = () => {
55
+ lines.push(line);
56
+ prefix = continuationPrefix;
57
+ line = prefix;
58
+ };
59
+ const appendUnbroken = (token) => {
60
+ for (const { segment } of segmenter.segment(token)) {
61
+ if (displayWidth(line + segment) > width && line !== prefix)
62
+ pushLine();
63
+ if (displayWidth(line + segment) > width) {
64
+ lines.push(truncateDisplay(line + segment, width));
65
+ prefix = continuationPrefix;
66
+ line = prefix;
67
+ continue;
68
+ }
69
+ line += segment;
70
+ }
71
+ };
72
+ let pendingWhitespace = "";
73
+ for (const token of value.split(/(\s+)/u).filter(Boolean)) {
74
+ if (token.trim().length === 0) {
75
+ if (line !== prefix)
76
+ pendingWhitespace += token;
77
+ continue;
78
+ }
79
+ const separator = line === prefix ? "" : pendingWhitespace;
80
+ if (displayWidth(line + separator + token) <= width) {
81
+ line += `${separator}${token}`;
82
+ } else {
83
+ if (line !== prefix && separator)
84
+ pushLine();
85
+ appendUnbroken(token);
86
+ }
87
+ pendingWhitespace = "";
88
+ }
89
+ if (line !== prefix || lines.length === 0)
90
+ lines.push(line);
91
+ return lines;
92
+ }
93
+
94
+ export {
95
+ openTerminalSession,
96
+ displayWidth,
97
+ truncateDisplay,
98
+ wrapDisplay
99
+ };