@draht/tui 2026.7.30 → 2026.10.4-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.
Files changed (170) hide show
  1. package/dist/alt-screen-search.d.ts +46 -0
  2. package/dist/alt-screen-search.d.ts.map +1 -0
  3. package/dist/alt-screen-search.js +282 -0
  4. package/dist/alt-screen-search.js.map +1 -0
  5. package/dist/autocomplete.d.ts +1 -0
  6. package/dist/autocomplete.d.ts.map +1 -1
  7. package/dist/autocomplete.js +81 -18
  8. package/dist/autocomplete.js.map +1 -1
  9. package/dist/colors.d.ts +73 -0
  10. package/dist/colors.d.ts.map +1 -0
  11. package/dist/colors.js +273 -0
  12. package/dist/colors.js.map +1 -0
  13. package/dist/components/alt-screen-flash.d.ts +13 -0
  14. package/dist/components/alt-screen-flash.d.ts.map +1 -0
  15. package/dist/components/alt-screen-flash.js +37 -0
  16. package/dist/components/alt-screen-flash.js.map +1 -0
  17. package/dist/components/box.d.ts +3 -1
  18. package/dist/components/box.d.ts.map +1 -1
  19. package/dist/components/box.js +43 -9
  20. package/dist/components/box.js.map +1 -1
  21. package/dist/components/cancellable-loader.js +3 -4
  22. package/dist/components/cancellable-loader.js.map +1 -1
  23. package/dist/components/editor.d.ts +6 -1
  24. package/dist/components/editor.d.ts.map +1 -1
  25. package/dist/components/editor.js +175 -70
  26. package/dist/components/editor.js.map +1 -1
  27. package/dist/components/h-stack.d.ts +7 -0
  28. package/dist/components/h-stack.d.ts.map +1 -0
  29. package/dist/components/h-stack.js +43 -0
  30. package/dist/components/h-stack.js.map +1 -0
  31. package/dist/components/image.js +8 -0
  32. package/dist/components/image.js.map +1 -1
  33. package/dist/components/input.d.ts +12 -1
  34. package/dist/components/input.d.ts.map +1 -1
  35. package/dist/components/input.js +57 -18
  36. package/dist/components/input.js.map +1 -1
  37. package/dist/components/loader.d.ts +2 -0
  38. package/dist/components/loader.d.ts.map +1 -1
  39. package/dist/components/loader.js +19 -10
  40. package/dist/components/loader.js.map +1 -1
  41. package/dist/components/markdown.d.ts +5 -0
  42. package/dist/components/markdown.d.ts.map +1 -1
  43. package/dist/components/markdown.js +157 -9
  44. package/dist/components/markdown.js.map +1 -1
  45. package/dist/components/mouse-region.d.ts +12 -0
  46. package/dist/components/mouse-region.d.ts.map +1 -0
  47. package/dist/components/mouse-region.js +21 -0
  48. package/dist/components/mouse-region.js.map +1 -0
  49. package/dist/components/scroll-view.d.ts +59 -0
  50. package/dist/components/scroll-view.d.ts.map +1 -0
  51. package/dist/components/scroll-view.js +195 -0
  52. package/dist/components/scroll-view.js.map +1 -0
  53. package/dist/components/select-list.d.ts +12 -1
  54. package/dist/components/select-list.d.ts.map +1 -1
  55. package/dist/components/select-list.js +72 -10
  56. package/dist/components/select-list.js.map +1 -1
  57. package/dist/components/settings-list.d.ts +13 -3
  58. package/dist/components/settings-list.d.ts.map +1 -1
  59. package/dist/components/settings-list.js +94 -19
  60. package/dist/components/settings-list.js.map +1 -1
  61. package/dist/components/spacer.js +1 -0
  62. package/dist/components/spacer.js.map +1 -1
  63. package/dist/components/stack.d.ts +32 -0
  64. package/dist/components/stack.d.ts.map +1 -0
  65. package/dist/components/stack.js +111 -0
  66. package/dist/components/stack.js.map +1 -0
  67. package/dist/components/text.d.ts.map +1 -1
  68. package/dist/components/text.js +13 -4
  69. package/dist/components/text.js.map +1 -1
  70. package/dist/components/truncated-text.js +3 -0
  71. package/dist/components/truncated-text.js.map +1 -1
  72. package/dist/components/v-stack.d.ts +8 -0
  73. package/dist/components/v-stack.d.ts.map +1 -0
  74. package/dist/components/v-stack.js +26 -0
  75. package/dist/components/v-stack.js.map +1 -0
  76. package/dist/fuzzy.d.ts.map +1 -1
  77. package/dist/fuzzy.js +23 -22
  78. package/dist/fuzzy.js.map +1 -1
  79. package/dist/index.d.ts +16 -4
  80. package/dist/index.d.ts.map +1 -1
  81. package/dist/index.js +18 -4
  82. package/dist/index.js.map +1 -1
  83. package/dist/keybindings.d.ts +84 -4
  84. package/dist/keybindings.d.ts.map +1 -1
  85. package/dist/keybindings.js +67 -6
  86. package/dist/keybindings.js.map +1 -1
  87. package/dist/kill-ring.js +1 -3
  88. package/dist/kill-ring.js.map +1 -1
  89. package/dist/latex.d.ts +10 -0
  90. package/dist/latex.d.ts.map +1 -0
  91. package/dist/latex.js +1378 -0
  92. package/dist/latex.js.map +1 -0
  93. package/dist/layout-node.d.ts +40 -0
  94. package/dist/layout-node.d.ts.map +1 -0
  95. package/dist/layout-node.js +6 -0
  96. package/dist/layout-node.js.map +1 -0
  97. package/dist/layout.d.ts +42 -0
  98. package/dist/layout.d.ts.map +1 -0
  99. package/dist/layout.js +344 -0
  100. package/dist/layout.js.map +1 -0
  101. package/dist/native-modifiers.d.ts +2 -1
  102. package/dist/native-modifiers.d.ts.map +1 -1
  103. package/dist/native-modifiers.js +3 -43
  104. package/dist/native-modifiers.js.map +1 -1
  105. package/dist/native-module-path.d.ts +7 -0
  106. package/dist/native-module-path.d.ts.map +1 -0
  107. package/dist/native-module-path.js +19 -0
  108. package/dist/native-module-path.js.map +1 -0
  109. package/dist/native-platform.d.ts +20 -0
  110. package/dist/native-platform.d.ts.map +1 -0
  111. package/dist/native-platform.js +42 -0
  112. package/dist/native-platform.js.map +1 -0
  113. package/dist/oklab.d.ts +43 -0
  114. package/dist/oklab.d.ts.map +1 -0
  115. package/dist/oklab.js +210 -0
  116. package/dist/oklab.js.map +1 -0
  117. package/dist/stdin-buffer.d.ts +8 -2
  118. package/dist/stdin-buffer.d.ts.map +1 -1
  119. package/dist/stdin-buffer.js +13 -6
  120. package/dist/stdin-buffer.js.map +1 -1
  121. package/dist/terminal-colors.d.ts +19 -2
  122. package/dist/terminal-colors.d.ts.map +1 -1
  123. package/dist/terminal-colors.js +13 -8
  124. package/dist/terminal-colors.js.map +1 -1
  125. package/dist/terminal-image.d.ts +26 -1
  126. package/dist/terminal-image.d.ts.map +1 -1
  127. package/dist/terminal-image.js +199 -18
  128. package/dist/terminal-image.js.map +1 -1
  129. package/dist/terminal.d.ts +16 -0
  130. package/dist/terminal.d.ts.map +1 -1
  131. package/dist/terminal.js +88 -68
  132. package/dist/terminal.js.map +1 -1
  133. package/dist/tui-alt-screen.d.ts +164 -0
  134. package/dist/tui-alt-screen.d.ts.map +1 -0
  135. package/dist/tui-alt-screen.js +1512 -0
  136. package/dist/tui-alt-screen.js.map +1 -0
  137. package/dist/tui-main-screen.d.ts +39 -0
  138. package/dist/tui-main-screen.d.ts.map +1 -0
  139. package/dist/tui-main-screen.js +594 -0
  140. package/dist/tui-main-screen.js.map +1 -0
  141. package/dist/tui.d.ts +178 -60
  142. package/dist/tui.d.ts.map +1 -1
  143. package/dist/tui.js +322 -658
  144. package/dist/tui.js.map +1 -1
  145. package/dist/undo-stack.js +1 -3
  146. package/dist/undo-stack.js.map +1 -1
  147. package/dist/utils.d.ts +16 -0
  148. package/dist/utils.d.ts.map +1 -1
  149. package/dist/utils.js +245 -75
  150. package/dist/utils.js.map +1 -1
  151. package/dist/wheel-scroll.d.ts +23 -0
  152. package/dist/wheel-scroll.d.ts.map +1 -0
  153. package/dist/wheel-scroll.js +73 -0
  154. package/dist/wheel-scroll.js.map +1 -0
  155. package/native/clipboard.h +208 -0
  156. package/native/darwin/build.sh +69 -0
  157. package/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  158. package/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  159. package/native/darwin/src/darwin-platform.m +132 -0
  160. package/native/linux/build.sh +65 -0
  161. package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  162. package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  163. package/native/linux/src/clipboard-worker.h +70 -0
  164. package/native/linux/src/linux-platform-x11.c +274 -0
  165. package/native/napi.h +91 -0
  166. package/native/win32/build.mjs +232 -0
  167. package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  168. package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  169. package/native/win32/src/win32-platform.c +241 -0
  170. package/package.json +16 -2
package/dist/utils.js CHANGED
@@ -33,6 +33,13 @@ function couldBeEmoji(segment) {
33
33
  // Regexes for character classification (same as string-width library, without ES2024 /v flags)
34
34
  const zeroWidthRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Mark}|\p{Surrogate})+$/u;
35
35
  const leadingNonPrintingRegex = /^[\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Mark}\p{Surrogate}]+/u;
36
+ const nonPrintingCharRegex = /^(?:\p{Default_Ignorable_Code_Point}|\p{Control}|\p{Format}|\p{Mark}|\p{Surrogate})$/u;
37
+ const markCharRegex = /^\p{Mark}$/u;
38
+ // Marks that terminals allocate cells for when attached to a base character. This includes
39
+ // Unicode spacing marks (excluding \u1734, \u302E, \u302F via lookahead, since /u regexes can't
40
+ // express set subtraction inside a character class the way /v's `--` operator does) and
41
+ // non-spacing exceptions in legacy wcwidth tables.
42
+ const terminalSpacingMarkRegex = /^(?:(?:(?!\u1734|\u302E|\u302F)\p{Spacing_Mark})|[\u065F\u0F7F\u102B\u102C\u1031\u1033-\u1035\u1038\u103A-\u103E])+$/u;
36
43
  const extendedPictographicRegex = /\p{Extended_Pictographic}/u;
37
44
  const emojiJoinerRegex = /^(?:\p{Emoji_Modifier}|\p{Mark}|\u200D|\uFE0F|\u20E3)$/u;
38
45
  function isRegionalIndicator(codePoint) {
@@ -63,6 +70,10 @@ function isEmojiGrapheme(segment) {
63
70
  const WIDTH_CACHE_SIZE = 512;
64
71
  const widthCache = new Map();
65
72
  export const cjkBreakRegex = /[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}\p{Script_Extensions=Hangul}\p{Script_Extensions=Bopomofo}]/u;
73
+ // CJK letters remain part of words and paths; only punctuation can separate prose from completions.
74
+ export const cjkPunctuationRegex = new RegExp(`(?:(?=\\p{Punctuation})${cjkBreakRegex.source}|[,.:;!?()[]{}“”‘’…—])`, "u");
75
+ export const autocompleteSeparatorRegex = new RegExp(`(?:\\s|${cjkPunctuationRegex.source})`, "u");
76
+ export const autocompleteBoundaryRegex = new RegExp(`(?:^|${autocompleteSeparatorRegex.source})`, "u");
66
77
  function isPrintableAscii(str) {
67
78
  for (let i = 0; i < str.length; i++) {
68
79
  const code = str.charCodeAt(i);
@@ -145,13 +156,14 @@ function truncateFragmentToWidth(text, maxWidth) {
145
156
  }
146
157
  function finalizeTruncatedResult(prefix, prefixWidth, ellipsis, ellipsisWidth, maxWidth, pad) {
147
158
  const reset = "\x1b[0m";
159
+ const hyperlinkClose = getActiveOsc8Close(prefix);
148
160
  const visibleWidth = prefixWidth + ellipsisWidth;
149
161
  let result;
150
162
  if (ellipsis.length > 0) {
151
- result = `${prefix}${reset}${ellipsis}${reset}`;
163
+ result = `${prefix}${hyperlinkClose}${reset}${ellipsis}${reset}`;
152
164
  }
153
165
  else {
154
- result = `${prefix}${reset}`;
166
+ result = `${prefix}${hyperlinkClose}${reset}`;
155
167
  }
156
168
  return pad ? result + " ".repeat(Math.max(0, maxWidth - visibleWidth)) : result;
157
169
  }
@@ -161,8 +173,16 @@ function finalizeTruncatedResult(prefix, prefixWidth, ellipsis, ellipsisWidth, m
161
173
  * check to avoid running the RGI_Emoji regex unnecessarily.
162
174
  */
163
175
  function graphemeWidth(segment) {
164
- if (segment === "\t") {
165
- return 3;
176
+ if (segment.length === 1) {
177
+ const code = segment.charCodeAt(0);
178
+ if (code >= 0x20 && code <= 0x7e)
179
+ return 1;
180
+ if (code === 0x09)
181
+ return 3;
182
+ }
183
+ // Some marks occupy cells even without a base character.
184
+ if (terminalSpacingMarkRegex.test(segment)) {
185
+ return [...segment].length;
166
186
  }
167
187
  // Zero-width clusters
168
188
  if (zeroWidthRegex.test(segment)) {
@@ -185,16 +205,30 @@ function graphemeWidth(segment) {
185
205
  return 2;
186
206
  }
187
207
  let width = eastAsianWidth(cp);
188
- // Trailing halfwidth/fullwidth forms and AM vowels that segment with a base.
189
- if (segment.length > 1) {
190
- for (const char of segment.slice(1)) {
208
+ // Intl.Segmenter can group multiple terminal-spacing code points into one
209
+ // grapheme. Count trailing visible code points that terminals may allocate
210
+ // cells for: Indic consonants after marks, halfwidth/fullwidth forms, and
211
+ // Thai/Lao AM vowels.
212
+ let followsMark = false;
213
+ const chars = [...base];
214
+ for (const char of chars.slice(1)) {
215
+ if (terminalSpacingMarkRegex.test(char)) {
216
+ width += 1;
217
+ followsMark = false;
218
+ }
219
+ else if (markCharRegex.test(char)) {
220
+ followsMark = true;
221
+ }
222
+ else if (!nonPrintingCharRegex.test(char)) {
191
223
  const c = char.codePointAt(0);
192
- if (c >= 0xff00 && c <= 0xffef) {
224
+ if (followsMark || (c >= 0xff00 && c <= 0xffef)) {
225
+ // halfwidth + fullwidth forms
193
226
  width += eastAsianWidth(c);
194
227
  }
195
228
  else if (c === 0x0e33 || c === 0x0eb3) {
196
229
  width += 1;
197
230
  }
231
+ followsMark = false;
198
232
  }
199
233
  }
200
234
  return width;
@@ -206,9 +240,11 @@ export function visibleWidth(str) {
206
240
  if (str.length === 0) {
207
241
  return 0;
208
242
  }
209
- // Fast path: pure ASCII printable
210
- if (isPrintableAscii(str)) {
211
- return str.length;
243
+ // Fast path: printable ASCII, tabs, and ANSI escape sequences. Styled lines take this path, so
244
+ // re-rendering after a theme change does not run grapheme segmentation on every line.
245
+ const asciiWidth = asciiVisibleWidth(str);
246
+ if (asciiWidth !== -1) {
247
+ return asciiWidth;
212
248
  }
213
249
  // Check cache
214
250
  const cached = widthCache.get(str);
@@ -220,22 +256,26 @@ export function visibleWidth(str) {
220
256
  if (str.includes("\t")) {
221
257
  clean = clean.replace(/\t/g, " ");
222
258
  }
223
- if (clean.includes("\x1b")) {
259
+ let escapeIndex = clean.indexOf("\x1b");
260
+ if (escapeIndex !== -1) {
224
261
  // Strip supported ANSI/OSC/APC escape sequences in one pass.
225
262
  // This covers CSI styling/cursor codes, OSC hyperlinks and prompt markers,
226
263
  // and APC sequences like CURSOR_MARKER.
227
264
  let stripped = "";
228
- let i = 0;
229
- while (i < clean.length) {
230
- const ansi = extractAnsiCode(clean, i);
231
- if (ansi) {
232
- i += ansi.length;
233
- continue;
265
+ let copyFrom = 0;
266
+ while (escapeIndex !== -1) {
267
+ const length = ansiCodeLength(clean, escapeIndex);
268
+ if (length > 0) {
269
+ stripped += clean.slice(copyFrom, escapeIndex);
270
+ escapeIndex += length;
271
+ copyFrom = escapeIndex;
234
272
  }
235
- stripped += clean[i];
236
- i++;
273
+ else {
274
+ escapeIndex++;
275
+ }
276
+ escapeIndex = clean.indexOf("\x1b", escapeIndex);
237
277
  }
238
- clean = stripped;
278
+ clean = stripped + clean.slice(copyFrom);
239
279
  }
240
280
  // Calculate width
241
281
  let width = 0;
@@ -252,6 +292,74 @@ export function visibleWidth(str) {
252
292
  widthCache.set(str, width);
253
293
  return width;
254
294
  }
295
+ /** Remove ANSI, OSC, and APC control sequences while preserving visible text. */
296
+ export function stripTerminalSequences(str) {
297
+ if (!str.includes("\x1b"))
298
+ return str;
299
+ let result = "";
300
+ let i = 0;
301
+ while (i < str.length) {
302
+ const ansi = extractAnsiCode(str, i);
303
+ if (ansi) {
304
+ i += ansi.length;
305
+ continue;
306
+ }
307
+ result += str[i];
308
+ i++;
309
+ }
310
+ return result;
311
+ }
312
+ /** Return the terminal-cell range occupied by the grapheme at a visible column. */
313
+ export function getGraphemeCellRange(line, column) {
314
+ let currentCol = 0;
315
+ let i = 0;
316
+ while (i < line.length) {
317
+ const ansi = extractAnsiCode(line, i);
318
+ if (ansi) {
319
+ i += ansi.length;
320
+ continue;
321
+ }
322
+ let textEnd = i;
323
+ while (textEnd < line.length && !extractAnsiCode(line, textEnd))
324
+ textEnd++;
325
+ for (const { segment } of graphemeSegmenter.segment(line.slice(i, textEnd))) {
326
+ const width = graphemeWidth(segment);
327
+ if (width > 0 && column >= currentCol && column < currentCol + width) {
328
+ return { start: currentCol, end: currentCol + width };
329
+ }
330
+ currentCol += width;
331
+ }
332
+ i = textEnd;
333
+ }
334
+ return undefined;
335
+ }
336
+ /** Return the OSC 8 hyperlink covering a visible terminal column. */
337
+ export function getOsc8LinkAtColumn(line, column) {
338
+ let activeUrl;
339
+ let currentCol = 0;
340
+ let i = 0;
341
+ while (i < line.length) {
342
+ const ansi = extractAnsiCode(line, i);
343
+ if (ansi) {
344
+ const hyperlink = /^\x1b\]8;[^;]*;([^\x07\x1b]*)(?:\x07|\x1b\\)$/.exec(ansi.code);
345
+ if (hyperlink)
346
+ activeUrl = hyperlink[1] || undefined;
347
+ i += ansi.length;
348
+ continue;
349
+ }
350
+ let textEnd = i;
351
+ while (textEnd < line.length && !extractAnsiCode(line, textEnd))
352
+ textEnd++;
353
+ for (const { segment } of graphemeSegmenter.segment(line.slice(i, textEnd))) {
354
+ const width = segment === "\t" ? 3 : graphemeWidth(segment);
355
+ if (column >= currentCol && column < currentCol + width)
356
+ return activeUrl;
357
+ currentCol += width;
358
+ }
359
+ i = textEnd;
360
+ }
361
+ return undefined;
362
+ }
255
363
  /**
256
364
  * Normalize text for terminal output without changing logical editor content.
257
365
  * Some terminals render precomposed Thai/Lao AM vowels inconsistently during
@@ -287,45 +395,68 @@ export function normalizeTerminalOutput(str) {
287
395
  * Extract ANSI escape sequences from a string at the given position.
288
396
  */
289
397
  export function extractAnsiCode(str, pos) {
290
- if (pos >= str.length || str[pos] !== "\x1b")
291
- return null;
398
+ const length = ansiCodeLength(str, pos);
399
+ return length > 0 ? { code: str.substring(pos, pos + length), length } : null;
400
+ }
401
+ /**
402
+ * Width of a string made of printable ASCII, tabs, and ANSI escape sequences, or -1 if it contains
403
+ * anything else. Matches `visibleWidth` for those strings without allocating.
404
+ */
405
+ function asciiVisibleWidth(str) {
406
+ let width = 0;
407
+ let i = 0;
408
+ while (i < str.length) {
409
+ const code = str.charCodeAt(i);
410
+ if (code >= 0x20 && code <= 0x7e) {
411
+ width++;
412
+ i++;
413
+ }
414
+ else if (code === 0x09) {
415
+ width += 3;
416
+ i++;
417
+ }
418
+ else if (code === 0x1b) {
419
+ const length = ansiCodeLength(str, i);
420
+ if (length === 0)
421
+ return -1;
422
+ i += length;
423
+ }
424
+ else {
425
+ return -1;
426
+ }
427
+ }
428
+ return width;
429
+ }
430
+ /** Length of the ANSI/OSC/APC escape sequence starting at `pos`, or 0 if there is none. */
431
+ function ansiCodeLength(str, pos) {
432
+ if (pos >= str.length || str.charCodeAt(pos) !== 0x1b)
433
+ return 0;
292
434
  const next = str[pos + 1];
293
435
  // CSI sequence: ESC [ ... m/G/K/H/J
294
436
  if (next === "[") {
295
- let j = pos + 2;
296
- while (j < str.length && !/[mGKHJ]/.test(str[j]))
297
- j++;
298
- if (j < str.length)
299
- return { code: str.substring(pos, j + 1), length: j + 1 - pos };
300
- return null;
437
+ for (let j = pos + 2; j < str.length; j++) {
438
+ const c = str.charCodeAt(j);
439
+ // m, G, K, H, J
440
+ if (c === 0x6d || c === 0x47 || c === 0x4b || c === 0x48 || c === 0x4a)
441
+ return j + 1 - pos;
442
+ }
443
+ return 0;
301
444
  }
302
445
  // OSC sequence: ESC ] ... BEL or ESC ] ... ST (ESC \)
303
446
  // Used for hyperlinks (OSC 8), window titles, etc.
304
- if (next === "]") {
305
- let j = pos + 2;
306
- while (j < str.length) {
307
- if (str[j] === "\x07")
308
- return { code: str.substring(pos, j + 1), length: j + 1 - pos };
309
- if (str[j] === "\x1b" && str[j + 1] === "\\")
310
- return { code: str.substring(pos, j + 2), length: j + 2 - pos };
311
- j++;
312
- }
313
- return null;
314
- }
315
447
  // APC sequence: ESC _ ... BEL or ESC _ ... ST (ESC \)
316
448
  // Used for cursor marker and application-specific commands
317
- if (next === "_") {
318
- let j = pos + 2;
319
- while (j < str.length) {
320
- if (str[j] === "\x07")
321
- return { code: str.substring(pos, j + 1), length: j + 1 - pos };
322
- if (str[j] === "\x1b" && str[j + 1] === "\\")
323
- return { code: str.substring(pos, j + 2), length: j + 2 - pos };
324
- j++;
449
+ if (next === "]" || next === "_") {
450
+ for (let j = pos + 2; j < str.length; j++) {
451
+ const c = str.charCodeAt(j);
452
+ if (c === 0x07)
453
+ return j + 1 - pos;
454
+ if (c === 0x1b && str[j + 1] === "\\")
455
+ return j + 2 - pos;
325
456
  }
326
- return null;
457
+ return 0;
327
458
  }
328
- return null;
459
+ return 0;
329
460
  }
330
461
  function parseOsc8Hyperlink(ansiCode) {
331
462
  if (!ansiCode.startsWith("\x1b]8;")) {
@@ -350,24 +481,43 @@ function formatOsc8Hyperlink(hyperlink) {
350
481
  function formatOsc8Close(terminator) {
351
482
  return `\x1b]8;;${terminator}`;
352
483
  }
484
+ function getActiveOsc8Close(prefix) {
485
+ if (!prefix.includes("\x1b]8;")) {
486
+ return "";
487
+ }
488
+ let activeHyperlink = null;
489
+ let i = 0;
490
+ while (i < prefix.length) {
491
+ const ansi = extractAnsiCode(prefix, i);
492
+ if (ansi) {
493
+ const hyperlink = parseOsc8Hyperlink(ansi.code);
494
+ if (hyperlink !== undefined) {
495
+ activeHyperlink = hyperlink;
496
+ }
497
+ i += ansi.length;
498
+ }
499
+ else {
500
+ i++;
501
+ }
502
+ }
503
+ return activeHyperlink ? formatOsc8Close(activeHyperlink.terminator) : "";
504
+ }
353
505
  /**
354
506
  * Track active ANSI SGR codes to preserve styling across line breaks.
355
507
  */
356
508
  class AnsiCodeTracker {
357
- constructor() {
358
- // Track individual attributes separately so we can reset them specifically
359
- this.bold = false;
360
- this.dim = false;
361
- this.italic = false;
362
- this.underline = false;
363
- this.blink = false;
364
- this.inverse = false;
365
- this.hidden = false;
366
- this.strikethrough = false;
367
- this.fgColor = null; // Stores the full code like "31" or "38;5;240"
368
- this.bgColor = null; // Stores the full code like "41" or "48;5;240"
369
- this.activeHyperlink = null;
370
- }
509
+ // Track individual attributes separately so we can reset them specifically
510
+ bold = false;
511
+ dim = false;
512
+ italic = false;
513
+ underline = false;
514
+ blink = false;
515
+ inverse = false;
516
+ hidden = false;
517
+ strikethrough = false;
518
+ fgColor = null; // Stores the full code like "31" or "38;5;240"
519
+ bgColor = null; // Stores the full code like "41" or "48;5;240"
520
+ activeHyperlink = null;
371
521
  process(ansiCode) {
372
522
  // OSC 8 hyperlink: \x1b]8;;<url>\x1b\\ (open) or \x1b]8;;\x1b\\ (close).
373
523
  // Preserve the original terminator because some terminals only make BEL-terminated
@@ -545,6 +695,9 @@ class AnsiCodeTracker {
545
695
  }
546
696
  return result;
547
697
  }
698
+ getActiveBackgroundCode() {
699
+ return this.bgColor ? `\x1b[${this.bgColor}m` : "";
700
+ }
548
701
  hasActiveCodes() {
549
702
  return (this.bold ||
550
703
  this.dim ||
@@ -576,18 +729,29 @@ class AnsiCodeTracker {
576
729
  }
577
730
  }
578
731
  function updateTrackerFromText(text, tracker) {
579
- let i = 0;
580
- while (i < text.length) {
581
- const ansiResult = extractAnsiCode(text, i);
582
- if (ansiResult) {
583
- tracker.process(ansiResult.code);
584
- i += ansiResult.length;
732
+ let i = text.indexOf("\x1b");
733
+ while (i !== -1) {
734
+ const length = ansiCodeLength(text, i);
735
+ if (length > 0) {
736
+ tracker.process(text.substring(i, i + length));
737
+ i += length;
585
738
  }
586
739
  else {
587
740
  i++;
588
741
  }
742
+ i = text.indexOf("\x1b", i);
589
743
  }
590
744
  }
745
+ /** Return only the background color active at the end of an ANSI-styled string. */
746
+ export function getActiveBackgroundAnsi(text) {
747
+ const tracker = new AnsiCodeTracker();
748
+ updateTrackerFromText(text, tracker);
749
+ return tracker.getActiveBackgroundCode();
750
+ }
751
+ function* graphemeSegments(text) {
752
+ for (const { segment } of graphemeSegmenter.segment(text))
753
+ yield segment;
754
+ }
591
755
  /**
592
756
  * Split text into words while keeping ANSI codes attached.
593
757
  */
@@ -613,13 +777,19 @@ function splitIntoTokensWithAnsi(text) {
613
777
  i += ansiResult.length;
614
778
  continue;
615
779
  }
616
- let end = i;
617
- while (end < text.length && !extractAnsiCode(text, end)) {
618
- end++;
780
+ // Visible text runs up to the next escape sequence.
781
+ let end = text.indexOf("\x1b", i + 1);
782
+ while (end !== -1 && ansiCodeLength(text, end) === 0) {
783
+ end = text.indexOf("\x1b", end + 1);
619
784
  }
620
- for (const { segment } of graphemeSegmenter.segment(text.slice(i, end))) {
785
+ if (end === -1)
786
+ end = text.length;
787
+ const chunk = text.slice(i, end);
788
+ // Printable ASCII characters are single graphemes, so skip the segmenter for them.
789
+ const ascii = isPrintableAscii(chunk);
790
+ for (const segment of ascii ? chunk : graphemeSegments(chunk)) {
621
791
  const segmentIsSpace = segment === " ";
622
- if (!segmentIsSpace && cjkBreakRegex.test(segment)) {
792
+ if (!ascii && !segmentIsSpace && cjkBreakRegex.test(segment)) {
623
793
  flushCurrent();
624
794
  const token = pendingAnsi + segment;
625
795
  pendingAnsi = "";