@bash0816/claude-code 2.1.270 → 2.1.276-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/config/claude-native-audited-versions.json +249 -1
- package/config/claude-termux-release-manifest.json +3 -3
- package/lib/bun-cellsegmenter-shim.js +569 -0
- package/lib/bunfs-esm-loader.mjs +156 -0
- package/lib/bunfs-esm-loader.test.js +885 -0
- package/lib/bunfs-extract.js +10 -1
- package/lib/bunfs-extract.test.js +36 -3
- package/lib/hooks-standalone-pattern.js +7 -0
- package/lib/hooks-standalone-pattern.test.js +115 -0
- package/lib/termux-run-claude-native.sh +52 -0
- package/lib/termux-run-claude-native.test.js +467 -0
- package/lib/test-support/vendor-cellsegmenter-harness.js +142 -0
- package/package.json +1 -1
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
module.exports = function createBunCellSegmenterShim({ graphemeWidth }) {
|
|
2
|
+
// Constants and bit masks (from vendor bundle analysis)
|
|
3
|
+
const pn = 3; // width mask (2 bits)
|
|
4
|
+
const Ub = 10; // runIndex shift
|
|
5
|
+
const fC = 255; // width mask (8 bits)
|
|
6
|
+
const dC = 256; // tab flag bit
|
|
7
|
+
|
|
8
|
+
// BiDi control character codepoint ranges (U+061C, U+202A-U+202E, U+2066-U+2069)
|
|
9
|
+
const aXe = [[1564, 1564], [8234, 8238], [8294, 8297]];
|
|
10
|
+
|
|
11
|
+
// Build regex for BiDi control character detection
|
|
12
|
+
const Nc = aXe.map(([n, s]) => `\\u{${n.toString(16)}}` + (s > n ? `-\\u{${s.toString(16)}}` : '')).join('');
|
|
13
|
+
const mSn = new RegExp(`[${Nc}]`, 'gu');
|
|
14
|
+
|
|
15
|
+
// Intl.Segmenter instance for grapheme segmentation (reuse across calls)
|
|
16
|
+
const globalSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
17
|
+
|
|
18
|
+
// ============================================================
|
|
19
|
+
// sliceAnsi: Split string by display columns using grapheme units
|
|
20
|
+
// ============================================================
|
|
21
|
+
function sliceAnsi(str, start, end) {
|
|
22
|
+
let visibleCol = 0;
|
|
23
|
+
let result = '';
|
|
24
|
+
let i = 0;
|
|
25
|
+
|
|
26
|
+
// Regex for non-SGR CSI sequences: ESC [ ... (@ to ~, excluding m)
|
|
27
|
+
const csiNonSgrRegex = /^\x1b\[[0-?]*[ -/]*[@-~]/;
|
|
28
|
+
|
|
29
|
+
while (i < str.length) {
|
|
30
|
+
// Invariant: must advance i by at least 1 per iteration
|
|
31
|
+
// Four-way dispatch with catch-all fallback (§9-6)
|
|
32
|
+
|
|
33
|
+
// 1. Complete SGR code: \x1b[...m with params (\d, ;, :)
|
|
34
|
+
if (str[i] === '\x1b' && str[i + 1] === '[') {
|
|
35
|
+
let j = i + 2;
|
|
36
|
+
// Find 'm' terminator, accepting digits, ;, and : (colon for kitty underline)
|
|
37
|
+
while (j < str.length && /[\d;:]/.test(str[j])) {
|
|
38
|
+
j++;
|
|
39
|
+
}
|
|
40
|
+
if (j < str.length && str[j] === 'm') {
|
|
41
|
+
// Match complete SGR
|
|
42
|
+
result += str.slice(i, j + 1);
|
|
43
|
+
i = j + 1;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
// Otherwise fall through to catch-all
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 2. Complete OSC8 sequence: \x1b]8;;...BEL or \x1b]8...ST
|
|
50
|
+
if (str[i] === '\x1b' && str[i + 1] === ']' && str[i + 2] === '8' && str[i + 3] === ';') {
|
|
51
|
+
let j = i + 4;
|
|
52
|
+
let foundEnd = false;
|
|
53
|
+
while (j < str.length) {
|
|
54
|
+
if (str[j] === '\x07') {
|
|
55
|
+
// BEL terminator
|
|
56
|
+
result += str.slice(i, j + 1);
|
|
57
|
+
i = j + 1;
|
|
58
|
+
foundEnd = true;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
if (str[j] === '\x1b' && j + 1 < str.length && str[j + 1] === '\\') {
|
|
62
|
+
// ST terminator
|
|
63
|
+
result += str.slice(i, j + 2);
|
|
64
|
+
i = j + 2;
|
|
65
|
+
foundEnd = true;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
j++;
|
|
69
|
+
}
|
|
70
|
+
if (foundEnd) continue;
|
|
71
|
+
// Otherwise fall through to catch-all (incomplete OSC8)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 3. Non-SGR CSI sequence: match and skip (skip without adding to result)
|
|
75
|
+
if (str[i] === '\x1b' && str[i + 1] === '[') {
|
|
76
|
+
const remaining = str.slice(i);
|
|
77
|
+
const csiMatch = remaining.match(csiNonSgrRegex);
|
|
78
|
+
if (csiMatch) {
|
|
79
|
+
// Skip CSI sequence without adding to result (consumed but not output)
|
|
80
|
+
i += csiMatch[0].length;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
// Otherwise fall through to catch-all (malformed CSI)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 4. Catch-all: any other character (ESC alone or normal char)
|
|
87
|
+
// Distinguish between lone ESC and normal text
|
|
88
|
+
if (str[i] === '\x1b') {
|
|
89
|
+
// Single ESC character (incomplete sequence) - consume it as-is
|
|
90
|
+
result += str[i];
|
|
91
|
+
i += 1;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Normal character: find the next ESC and segment everything up to it
|
|
96
|
+
let chunkEnd = str.length; // Default: rest of string (no more ESCs)
|
|
97
|
+
for (let j = i + 1; j < str.length; j++) {
|
|
98
|
+
if (str[j] === '\x1b') {
|
|
99
|
+
chunkEnd = j;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const chunk = str.slice(i, chunkEnd);
|
|
105
|
+
const graphemes = Array.from(globalSegmenter.segment(chunk)).map(seg => seg.segment);
|
|
106
|
+
|
|
107
|
+
for (const grapheme of graphemes) {
|
|
108
|
+
const charWidth = graphemeWidth(grapheme);
|
|
109
|
+
const nextCol = visibleCol + charWidth;
|
|
110
|
+
|
|
111
|
+
// Include if entirely within [start, end)
|
|
112
|
+
if (nextCol <= end && visibleCol < end) {
|
|
113
|
+
if (visibleCol >= start) {
|
|
114
|
+
result += grapheme;
|
|
115
|
+
}
|
|
116
|
+
visibleCol = nextCol;
|
|
117
|
+
} else if (visibleCol >= end) {
|
|
118
|
+
break;
|
|
119
|
+
} else {
|
|
120
|
+
visibleCol = nextCol;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (visibleCol >= end) {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
i = chunkEnd;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ============================================================
|
|
135
|
+
// sleepSync: Sleep using Atomics.wait (blocking)
|
|
136
|
+
// ============================================================
|
|
137
|
+
function sleepSync(ms) {
|
|
138
|
+
const buf = new Int32Array(new SharedArrayBuffer(4));
|
|
139
|
+
Atomics.wait(buf, 0, 0, ms);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ============================================================
|
|
143
|
+
// CellSegmenter: Main text segmentation and painting class
|
|
144
|
+
// ============================================================
|
|
145
|
+
class CellSegmenter {
|
|
146
|
+
constructor(config) {
|
|
147
|
+
this.ambiguousIsNarrow = config.ambiguousIsNarrow ?? true;
|
|
148
|
+
this.substitute = config.substitute || [];
|
|
149
|
+
this.screen = config.screen || {
|
|
150
|
+
widthMask: pn,
|
|
151
|
+
narrow: 0,
|
|
152
|
+
wide: 1,
|
|
153
|
+
spacerTail: 2,
|
|
154
|
+
spacerHead: 3,
|
|
155
|
+
emptyCharIndex: 0,
|
|
156
|
+
spacerCharIndex: 1,
|
|
157
|
+
emptyWord: 0,
|
|
158
|
+
tabWidth: 8,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// GraphemeIndex to charPoolIndex mapping (per-instance state)
|
|
162
|
+
this.graphemes = [];
|
|
163
|
+
this.sgrKeys = ['']; // Index 0 = default (no style applied)
|
|
164
|
+
this.sgrCloseKeys = [''];
|
|
165
|
+
this.uris = ['']; // Index 0 = no link (reserved dummy), per §9-2
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ============================================================
|
|
169
|
+
// setCell: Write single cell to destination buffer
|
|
170
|
+
// ============================================================
|
|
171
|
+
setCell(destCells, destWidth, x, y, charPoolIndex, packedStyleHyperlinkWidth) {
|
|
172
|
+
// Clamp to screen width
|
|
173
|
+
if (x < 0 || x >= destWidth || y < 0) {
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Calculate cell index: must bind the shift result before adding 1
|
|
178
|
+
const idx = (y * destWidth + x) << 1;
|
|
179
|
+
if (idx + 1 >= destCells.length) {
|
|
180
|
+
return 0; // Out of bounds
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
destCells[idx] = charPoolIndex;
|
|
184
|
+
destCells[idx + 1] = packedStyleHyperlinkWidth;
|
|
185
|
+
|
|
186
|
+
// Return damage rect: xStart * 2^20 + endCol * 2^36
|
|
187
|
+
const xStart = x;
|
|
188
|
+
const endCol = x + 1;
|
|
189
|
+
return endCol + xStart * (2 ** 20) + endCol * (2 ** 36);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ============================================================
|
|
193
|
+
// segment: Parse text into grapheme cells, SGR styles, and URIs (BL-1,2,3,4,5,6,7 fixes)
|
|
194
|
+
// ============================================================
|
|
195
|
+
segment(text, cells, runs, reordered) {
|
|
196
|
+
// Apply BiDi character substitution
|
|
197
|
+
const processedText = text.replace(mSn, '�');
|
|
198
|
+
|
|
199
|
+
// Pre-allocate capacity check
|
|
200
|
+
const estimatedGraphemes = processedText.length;
|
|
201
|
+
if (cells.length < estimatedGraphemes * 2) {
|
|
202
|
+
return -(estimatedGraphemes);
|
|
203
|
+
}
|
|
204
|
+
if (runs.length < estimatedGraphemes * 2) {
|
|
205
|
+
return -(estimatedGraphemes);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let graphemeIndex = this.graphemes.length;
|
|
209
|
+
let cellCount = 0;
|
|
210
|
+
let localRunIndex = 0;
|
|
211
|
+
let currentUri = 0;
|
|
212
|
+
|
|
213
|
+
// Track current active style attributes by category (§9-4, BL-6)
|
|
214
|
+
let fgColor = null; // Current foreground color code (null = none)
|
|
215
|
+
let bgColor = null; // Current background color code (null = none)
|
|
216
|
+
const booleanAttrs = new Set(); // bold, dim, italic, underline, blink, reverse, hidden, strikethrough
|
|
217
|
+
|
|
218
|
+
// Track run states to fill runs array later
|
|
219
|
+
const runToState = new Map(); // Map of runIdx -> {sgrCodes, uriIdx}
|
|
220
|
+
|
|
221
|
+
// Helper: build SGR code array from current state
|
|
222
|
+
const buildSgrCodes = () => {
|
|
223
|
+
const codes = [];
|
|
224
|
+
if (fgColor !== null) codes.push(fgColor);
|
|
225
|
+
if (bgColor !== null) codes.push(bgColor);
|
|
226
|
+
for (const attr of booleanAttrs) codes.push(attr);
|
|
227
|
+
return codes.sort();
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// Helper: get close codes corresponding to open codes (§9-5)
|
|
231
|
+
// Returns codes in escape sequence format: \x1b[...m (not raw codes)
|
|
232
|
+
const getCloseCodes = (openCodes) => {
|
|
233
|
+
const closeMap = {
|
|
234
|
+
'1': '22', '2': '22',
|
|
235
|
+
'3': '23', '4': '24', '5': '25', '6': '25',
|
|
236
|
+
'7': '27', '8': '28', '9': '29'
|
|
237
|
+
};
|
|
238
|
+
return openCodes.map(code => {
|
|
239
|
+
let closeCode = '';
|
|
240
|
+
// Check for composite color codes first
|
|
241
|
+
if (/^38/.test(code)) closeCode = '39'; // Foreground color (38;5;n or 38;2;r;g;b)
|
|
242
|
+
else if (/^48/.test(code)) closeCode = '49'; // Background color (48;5;n or 48;2;r;g;b)
|
|
243
|
+
// Check for basic foreground colors (30-37, 90-97)
|
|
244
|
+
else if (/^(30|31|32|33|34|35|36|37|90|91|92|93|94|95|96|97)$/.test(code)) closeCode = '39';
|
|
245
|
+
// Check for basic background colors (40-47, 100-107)
|
|
246
|
+
else if (/^(40|41|42|43|44|45|46|47|100|101|102|103|104|105|106|107)$/.test(code)) closeCode = '49';
|
|
247
|
+
// Check for other attributes
|
|
248
|
+
else if (/^\d+$/.test(code)) closeCode = closeMap[code] || '';
|
|
249
|
+
// Return as escape sequence: \x1b[...m, or empty if no mapping
|
|
250
|
+
return closeCode ? `\x1b[${closeCode}m` : '';
|
|
251
|
+
});
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// Helper: parse SGR parameter and update state (BL-6)
|
|
255
|
+
const applySgrParam = (param) => {
|
|
256
|
+
if (param === '0' || param === '') {
|
|
257
|
+
fgColor = null;
|
|
258
|
+
bgColor = null;
|
|
259
|
+
booleanAttrs.clear();
|
|
260
|
+
} else if (param === '1' || param === '2') {
|
|
261
|
+
booleanAttrs.add(param);
|
|
262
|
+
} else if (param === '3') {
|
|
263
|
+
booleanAttrs.add(param);
|
|
264
|
+
} else if (param === '4') {
|
|
265
|
+
booleanAttrs.add(param);
|
|
266
|
+
} else if (param === '5' || param === '6') {
|
|
267
|
+
booleanAttrs.add(param);
|
|
268
|
+
} else if (param === '7') {
|
|
269
|
+
booleanAttrs.add(param);
|
|
270
|
+
} else if (param === '8') {
|
|
271
|
+
booleanAttrs.add(param);
|
|
272
|
+
} else if (param === '9') {
|
|
273
|
+
booleanAttrs.add(param);
|
|
274
|
+
} else if (param === '22') {
|
|
275
|
+
booleanAttrs.delete('1');
|
|
276
|
+
booleanAttrs.delete('2');
|
|
277
|
+
} else if (param === '23') {
|
|
278
|
+
booleanAttrs.delete('3');
|
|
279
|
+
} else if (param === '24') {
|
|
280
|
+
booleanAttrs.delete('4');
|
|
281
|
+
} else if (param === '25') {
|
|
282
|
+
booleanAttrs.delete('5');
|
|
283
|
+
booleanAttrs.delete('6');
|
|
284
|
+
} else if (param === '27') {
|
|
285
|
+
booleanAttrs.delete('7');
|
|
286
|
+
} else if (param === '28') {
|
|
287
|
+
booleanAttrs.delete('8');
|
|
288
|
+
} else if (param === '29') {
|
|
289
|
+
booleanAttrs.delete('9');
|
|
290
|
+
} else if (param === '39') {
|
|
291
|
+
fgColor = null;
|
|
292
|
+
} else if (param === '49') {
|
|
293
|
+
bgColor = null;
|
|
294
|
+
} else if (/^(30|31|32|33|34|35|36|37|90|91|92|93|94|95|96|97)$/.test(param)) {
|
|
295
|
+
fgColor = param;
|
|
296
|
+
} else if (/^(40|41|42|43|44|45|46|47|100|101|102|103|104|105|106|107)$/.test(param)) {
|
|
297
|
+
bgColor = param;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// Helper: parse full SGR sequence
|
|
302
|
+
const parseSgrSequence = (paramStr) => {
|
|
303
|
+
if (!paramStr) {
|
|
304
|
+
applySgrParam('0');
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const parts = paramStr.split(';');
|
|
308
|
+
let k = 0;
|
|
309
|
+
while (k < parts.length) {
|
|
310
|
+
const p = parts[k];
|
|
311
|
+
if ((p === '38' || p === '48') && parts[k + 1] === '5' && k + 2 < parts.length) {
|
|
312
|
+
const composite = `${p};5;${parts[k + 2]}`;
|
|
313
|
+
if (p === '38') fgColor = composite;
|
|
314
|
+
else bgColor = composite;
|
|
315
|
+
k += 3;
|
|
316
|
+
continue;
|
|
317
|
+
} else if ((p === '38' || p === '48') && parts[k + 1] === '2' && k + 4 < parts.length) {
|
|
318
|
+
const composite = `${p};2;${parts[k + 2]};${parts[k + 3]};${parts[k + 4]}`;
|
|
319
|
+
if (p === '38') fgColor = composite;
|
|
320
|
+
else bgColor = composite;
|
|
321
|
+
k += 5;
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
applySgrParam(p);
|
|
325
|
+
k++;
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
// Regex for non-SGR CSI sequences (with ^ anchor to match at current position)
|
|
330
|
+
const csiNonSgrRegex = /^\x1b\[[0-?]*[ -/]*[@-~]/;
|
|
331
|
+
|
|
332
|
+
let i = 0;
|
|
333
|
+
let prevState = null; // Previous SGR+URI state for BL-3 (monotonic run allocation)
|
|
334
|
+
|
|
335
|
+
while (i < processedText.length) {
|
|
336
|
+
// Invariant: must advance i by at least 1 per iteration (§9-6)
|
|
337
|
+
// Four-way dispatch with catch-all fallback
|
|
338
|
+
|
|
339
|
+
// 1. Complete SGR code: \x1b[...m
|
|
340
|
+
if (processedText[i] === '\x1b' && processedText[i + 1] === '[') {
|
|
341
|
+
let j = i + 2;
|
|
342
|
+
while (j < processedText.length && /[\d;:]/.test(processedText[j])) {
|
|
343
|
+
j++;
|
|
344
|
+
}
|
|
345
|
+
if (j < processedText.length && processedText[j] === 'm') {
|
|
346
|
+
const params = processedText.slice(i + 2, j);
|
|
347
|
+
parseSgrSequence(params);
|
|
348
|
+
i = j + 1;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// 2. Complete OSC8 sequence: \x1b]8;;...
|
|
354
|
+
if (processedText[i] === '\x1b' && processedText[i + 1] === ']' && processedText[i + 2] === '8' && processedText[i + 3] === ';') {
|
|
355
|
+
let j = i + 4;
|
|
356
|
+
let foundEnd = false;
|
|
357
|
+
while (j < processedText.length) {
|
|
358
|
+
if (processedText[j] === '\x07') {
|
|
359
|
+
const oscSeq = processedText.slice(i + 4, j);
|
|
360
|
+
const semicolonPos = oscSeq.indexOf(';');
|
|
361
|
+
if (semicolonPos >= 0) {
|
|
362
|
+
const url = oscSeq.slice(semicolonPos + 1);
|
|
363
|
+
if (url) {
|
|
364
|
+
let uriIdx = this.uris.indexOf(url);
|
|
365
|
+
if (uriIdx === -1) {
|
|
366
|
+
uriIdx = this.uris.length;
|
|
367
|
+
this.uris.push(url);
|
|
368
|
+
}
|
|
369
|
+
currentUri = uriIdx;
|
|
370
|
+
} else {
|
|
371
|
+
currentUri = 0;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
i = j + 1;
|
|
375
|
+
foundEnd = true;
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
if (processedText[j] === '\x1b' && j + 1 < processedText.length && processedText[j + 1] === '\\') {
|
|
379
|
+
const oscSeq = processedText.slice(i + 4, j);
|
|
380
|
+
const semicolonPos = oscSeq.indexOf(';');
|
|
381
|
+
if (semicolonPos >= 0) {
|
|
382
|
+
const url = oscSeq.slice(semicolonPos + 1);
|
|
383
|
+
if (url) {
|
|
384
|
+
let uriIdx = this.uris.indexOf(url);
|
|
385
|
+
if (uriIdx === -1) {
|
|
386
|
+
uriIdx = this.uris.length;
|
|
387
|
+
this.uris.push(url);
|
|
388
|
+
}
|
|
389
|
+
currentUri = uriIdx;
|
|
390
|
+
} else {
|
|
391
|
+
currentUri = 0;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
i = j + 2;
|
|
395
|
+
foundEnd = true;
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
j++;
|
|
399
|
+
}
|
|
400
|
+
if (foundEnd) continue;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 3. Non-SGR CSI sequence: match and skip
|
|
404
|
+
if (processedText[i] === '\x1b' && processedText[i + 1] === '[') {
|
|
405
|
+
const remaining = processedText.slice(i);
|
|
406
|
+
const csiMatch = remaining.match(csiNonSgrRegex);
|
|
407
|
+
if (csiMatch) {
|
|
408
|
+
i += csiMatch[0].length;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// 4. Catch-all: lone ESC or normal character
|
|
414
|
+
// Distinguish between lone ESC and normal text
|
|
415
|
+
if (processedText[i] === '\x1b') {
|
|
416
|
+
// Single ESC character (incomplete sequence) - consume it as-is, don't segment
|
|
417
|
+
i += 1;
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Normal character: find the next ESC and segment everything up to it
|
|
422
|
+
let chunkEnd = processedText.length; // Default: rest of string (no more ESCs)
|
|
423
|
+
for (let j = i + 1; j < processedText.length; j++) {
|
|
424
|
+
if (processedText[j] === '\x1b') {
|
|
425
|
+
chunkEnd = j;
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const chunk = processedText.slice(i, chunkEnd);
|
|
431
|
+
const graphemeArray = Array.from(globalSegmenter.segment(chunk)).map(seg => seg.segment);
|
|
432
|
+
|
|
433
|
+
for (const grapheme of graphemeArray) {
|
|
434
|
+
// Determine run index: compare current state with previous (BL-3)
|
|
435
|
+
const currentSgrCodes = buildSgrCodes();
|
|
436
|
+
const currentState = JSON.stringify({ sgr: currentSgrCodes, uri: currentUri });
|
|
437
|
+
|
|
438
|
+
let runIdx;
|
|
439
|
+
if (prevState !== currentState) {
|
|
440
|
+
runIdx = localRunIndex++;
|
|
441
|
+
prevState = currentState;
|
|
442
|
+
|
|
443
|
+
// Register in sgrKeys/sgrCloseKeys pool if needed
|
|
444
|
+
if (currentSgrCodes.length > 0) {
|
|
445
|
+
const poolEntry = currentSgrCodes.map(code => `\x1b[${code}m`).join('\x00');
|
|
446
|
+
let poolIdx = this.sgrKeys.indexOf(poolEntry);
|
|
447
|
+
if (poolIdx === -1) {
|
|
448
|
+
poolIdx = this.sgrKeys.length;
|
|
449
|
+
this.sgrKeys.push(poolEntry);
|
|
450
|
+
const closeCodes = getCloseCodes(currentSgrCodes);
|
|
451
|
+
this.sgrCloseKeys.push(closeCodes.join('\x00'));
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Store state for this run
|
|
456
|
+
runToState.set(runIdx, { sgrCodes: currentSgrCodes, uriIdx: currentUri });
|
|
457
|
+
} else {
|
|
458
|
+
// Reuse previous run
|
|
459
|
+
runIdx = localRunIndex - 1;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Add grapheme to pool
|
|
463
|
+
this.graphemes.push(grapheme);
|
|
464
|
+
const width = graphemeWidth(grapheme);
|
|
465
|
+
|
|
466
|
+
cells[cellCount * 2] = graphemeIndex;
|
|
467
|
+
cells[cellCount * 2 + 1] = (runIdx << Ub) | (width & fC);
|
|
468
|
+
|
|
469
|
+
if (grapheme === '\t') {
|
|
470
|
+
cells[cellCount * 2 + 1] |= dC;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
graphemeIndex++;
|
|
474
|
+
cellCount++;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
i = chunkEnd;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// Build runs array from tracked run states
|
|
481
|
+
for (let runIdx = 0; runIdx < localRunIndex; runIdx++) {
|
|
482
|
+
const state = runToState.get(runIdx) || { sgrCodes: [], uriIdx: 0 };
|
|
483
|
+
let poolIdx = 0;
|
|
484
|
+
if (state.sgrCodes.length > 0) {
|
|
485
|
+
const poolEntry = state.sgrCodes.map(code => `\x1b[${code}m`).join('\x00');
|
|
486
|
+
poolIdx = this.sgrKeys.indexOf(poolEntry);
|
|
487
|
+
if (poolIdx === -1) poolIdx = 0;
|
|
488
|
+
}
|
|
489
|
+
runs[runIdx * 2] = poolIdx;
|
|
490
|
+
runs[runIdx * 2 + 1] = state.uriIdx;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return cellCount;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// ============================================================
|
|
497
|
+
// paint: Render segmented cells to destination screen buffer
|
|
498
|
+
// ============================================================
|
|
499
|
+
paint(destCells, destWidth, x, y, srcCells, srcCount, unused, charIndices, runWords) {
|
|
500
|
+
let col = x;
|
|
501
|
+
let maxX = x;
|
|
502
|
+
|
|
503
|
+
for (let i = 0; i < srcCount; i++) {
|
|
504
|
+
if (col < 0) {
|
|
505
|
+
col++;
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (col >= destWidth) {
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const graphemeIndex = srcCells[i * 2];
|
|
514
|
+
const width = srcCells[i * 2 + 1] & fC;
|
|
515
|
+
const tab = (srcCells[i * 2 + 1] & dC) !== 0;
|
|
516
|
+
const runIndex = srcCells[i * 2 + 1] >>> Ub;
|
|
517
|
+
const word = runWords[runIndex] || 0;
|
|
518
|
+
|
|
519
|
+
// Skip wide characters that would overshoot screen width
|
|
520
|
+
if (width === 2 && col + 1 >= destWidth) {
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// Calculate cell index: bind result to variable before adding 1
|
|
525
|
+
const idx = (y * destWidth + col) << 1;
|
|
526
|
+
|
|
527
|
+
if (tab) {
|
|
528
|
+
// Fill tab stops with spaces (use this.screen.emptyCharIndex for space)
|
|
529
|
+
const tabWidth = this.screen.tabWidth || 8;
|
|
530
|
+
const nextTabStop = Math.min(((Math.floor(col / tabWidth) + 1) * tabWidth), destWidth);
|
|
531
|
+
while (col < nextTabStop && col < destWidth) {
|
|
532
|
+
const cellIdx = (y * destWidth + col) << 1;
|
|
533
|
+
destCells[cellIdx] = this.screen.emptyCharIndex;
|
|
534
|
+
destCells[cellIdx + 1] = word | 0; // width code 0 for space
|
|
535
|
+
col++;
|
|
536
|
+
}
|
|
537
|
+
maxX = col;
|
|
538
|
+
} else {
|
|
539
|
+
destCells[idx] = charIndices[graphemeIndex] || 0;
|
|
540
|
+
destCells[idx + 1] = word | (width === 2 ? 1 : 0);
|
|
541
|
+
|
|
542
|
+
if (width === 2) {
|
|
543
|
+
// Wide character: write spacer tail to next cell
|
|
544
|
+
const spacerIdx = idx + 2;
|
|
545
|
+
if (col + 1 < destWidth && spacerIdx + 1 < destCells.length) {
|
|
546
|
+
destCells[spacerIdx] = this.screen.spacerCharIndex;
|
|
547
|
+
destCells[spacerIdx + 1] = word | 2; // width code 2 (spacerTail)
|
|
548
|
+
}
|
|
549
|
+
col += 2;
|
|
550
|
+
maxX = col;
|
|
551
|
+
} else {
|
|
552
|
+
col++;
|
|
553
|
+
maxX = col;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Return damage rect: must use * and + only, not <<
|
|
559
|
+
const endCol = Math.min(col, destWidth);
|
|
560
|
+
const xStart = x;
|
|
561
|
+
return endCol + xStart * (2 ** 20) + endCol * (2 ** 36);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// ============================================================
|
|
566
|
+
// Return factory exports
|
|
567
|
+
// ============================================================
|
|
568
|
+
return { sliceAnsi, sleepSync, CellSegmenter };
|
|
569
|
+
};
|