@draht/tui 2026.9.5-1 → 2026.10.4-3

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 (167) 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.map +1 -1
  6. package/dist/autocomplete.js +45 -15
  7. package/dist/autocomplete.js.map +1 -1
  8. package/dist/colors.d.ts +73 -0
  9. package/dist/colors.d.ts.map +1 -0
  10. package/dist/colors.js +273 -0
  11. package/dist/colors.js.map +1 -0
  12. package/dist/components/alt-screen-flash.d.ts +13 -0
  13. package/dist/components/alt-screen-flash.d.ts.map +1 -0
  14. package/dist/components/alt-screen-flash.js +37 -0
  15. package/dist/components/alt-screen-flash.js.map +1 -0
  16. package/dist/components/box.d.ts +3 -1
  17. package/dist/components/box.d.ts.map +1 -1
  18. package/dist/components/box.js +43 -9
  19. package/dist/components/box.js.map +1 -1
  20. package/dist/components/cancellable-loader.js +3 -4
  21. package/dist/components/cancellable-loader.js.map +1 -1
  22. package/dist/components/editor.d.ts +6 -1
  23. package/dist/components/editor.d.ts.map +1 -1
  24. package/dist/components/editor.js +175 -70
  25. package/dist/components/editor.js.map +1 -1
  26. package/dist/components/h-stack.d.ts +7 -0
  27. package/dist/components/h-stack.d.ts.map +1 -0
  28. package/dist/components/h-stack.js +43 -0
  29. package/dist/components/h-stack.js.map +1 -0
  30. package/dist/components/image.js +8 -0
  31. package/dist/components/image.js.map +1 -1
  32. package/dist/components/input.d.ts +12 -1
  33. package/dist/components/input.d.ts.map +1 -1
  34. package/dist/components/input.js +57 -18
  35. package/dist/components/input.js.map +1 -1
  36. package/dist/components/loader.d.ts +2 -0
  37. package/dist/components/loader.d.ts.map +1 -1
  38. package/dist/components/loader.js +19 -10
  39. package/dist/components/loader.js.map +1 -1
  40. package/dist/components/markdown.d.ts +5 -0
  41. package/dist/components/markdown.d.ts.map +1 -1
  42. package/dist/components/markdown.js +148 -5
  43. package/dist/components/markdown.js.map +1 -1
  44. package/dist/components/mouse-region.d.ts +12 -0
  45. package/dist/components/mouse-region.d.ts.map +1 -0
  46. package/dist/components/mouse-region.js +21 -0
  47. package/dist/components/mouse-region.js.map +1 -0
  48. package/dist/components/scroll-view.d.ts +59 -0
  49. package/dist/components/scroll-view.d.ts.map +1 -0
  50. package/dist/components/scroll-view.js +195 -0
  51. package/dist/components/scroll-view.js.map +1 -0
  52. package/dist/components/select-list.d.ts +12 -1
  53. package/dist/components/select-list.d.ts.map +1 -1
  54. package/dist/components/select-list.js +72 -10
  55. package/dist/components/select-list.js.map +1 -1
  56. package/dist/components/settings-list.d.ts +13 -3
  57. package/dist/components/settings-list.d.ts.map +1 -1
  58. package/dist/components/settings-list.js +91 -13
  59. package/dist/components/settings-list.js.map +1 -1
  60. package/dist/components/spacer.js +1 -0
  61. package/dist/components/spacer.js.map +1 -1
  62. package/dist/components/stack.d.ts +32 -0
  63. package/dist/components/stack.d.ts.map +1 -0
  64. package/dist/components/stack.js +111 -0
  65. package/dist/components/stack.js.map +1 -0
  66. package/dist/components/text.js +8 -0
  67. package/dist/components/text.js.map +1 -1
  68. package/dist/components/truncated-text.js +3 -0
  69. package/dist/components/truncated-text.js.map +1 -1
  70. package/dist/components/v-stack.d.ts +8 -0
  71. package/dist/components/v-stack.d.ts.map +1 -0
  72. package/dist/components/v-stack.js +26 -0
  73. package/dist/components/v-stack.js.map +1 -0
  74. package/dist/fuzzy.d.ts.map +1 -1
  75. package/dist/fuzzy.js +23 -22
  76. package/dist/fuzzy.js.map +1 -1
  77. package/dist/index.d.ts +16 -4
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +18 -4
  80. package/dist/index.js.map +1 -1
  81. package/dist/keybindings.d.ts +84 -4
  82. package/dist/keybindings.d.ts.map +1 -1
  83. package/dist/keybindings.js +67 -6
  84. package/dist/keybindings.js.map +1 -1
  85. package/dist/kill-ring.js +1 -3
  86. package/dist/kill-ring.js.map +1 -1
  87. package/dist/latex.d.ts +10 -0
  88. package/dist/latex.d.ts.map +1 -0
  89. package/dist/latex.js +1378 -0
  90. package/dist/latex.js.map +1 -0
  91. package/dist/layout-node.d.ts +40 -0
  92. package/dist/layout-node.d.ts.map +1 -0
  93. package/dist/layout-node.js +6 -0
  94. package/dist/layout-node.js.map +1 -0
  95. package/dist/layout.d.ts +42 -0
  96. package/dist/layout.d.ts.map +1 -0
  97. package/dist/layout.js +344 -0
  98. package/dist/layout.js.map +1 -0
  99. package/dist/native-modifiers.d.ts +2 -1
  100. package/dist/native-modifiers.d.ts.map +1 -1
  101. package/dist/native-modifiers.js +3 -43
  102. package/dist/native-modifiers.js.map +1 -1
  103. package/dist/native-module-path.d.ts +7 -0
  104. package/dist/native-module-path.d.ts.map +1 -0
  105. package/dist/native-module-path.js +19 -0
  106. package/dist/native-module-path.js.map +1 -0
  107. package/dist/native-platform.d.ts +20 -0
  108. package/dist/native-platform.d.ts.map +1 -0
  109. package/dist/native-platform.js +42 -0
  110. package/dist/native-platform.js.map +1 -0
  111. package/dist/oklab.d.ts +43 -0
  112. package/dist/oklab.d.ts.map +1 -0
  113. package/dist/oklab.js +210 -0
  114. package/dist/oklab.js.map +1 -0
  115. package/dist/stdin-buffer.d.ts +1 -1
  116. package/dist/stdin-buffer.js +8 -5
  117. package/dist/stdin-buffer.js.map +1 -1
  118. package/dist/terminal-colors.d.ts +19 -2
  119. package/dist/terminal-colors.d.ts.map +1 -1
  120. package/dist/terminal-colors.js +12 -7
  121. package/dist/terminal-colors.js.map +1 -1
  122. package/dist/terminal-image.d.ts +26 -1
  123. package/dist/terminal-image.d.ts.map +1 -1
  124. package/dist/terminal-image.js +187 -13
  125. package/dist/terminal-image.js.map +1 -1
  126. package/dist/terminal.d.ts +4 -0
  127. package/dist/terminal.d.ts.map +1 -1
  128. package/dist/terminal.js +53 -63
  129. package/dist/terminal.js.map +1 -1
  130. package/dist/tui-alt-screen.d.ts +164 -0
  131. package/dist/tui-alt-screen.d.ts.map +1 -0
  132. package/dist/tui-alt-screen.js +1512 -0
  133. package/dist/tui-alt-screen.js.map +1 -0
  134. package/dist/tui-main-screen.d.ts +39 -0
  135. package/dist/tui-main-screen.d.ts.map +1 -0
  136. package/dist/tui-main-screen.js +594 -0
  137. package/dist/tui-main-screen.js.map +1 -0
  138. package/dist/tui.d.ts +178 -60
  139. package/dist/tui.d.ts.map +1 -1
  140. package/dist/tui.js +320 -715
  141. package/dist/tui.js.map +1 -1
  142. package/dist/undo-stack.js +1 -3
  143. package/dist/undo-stack.js.map +1 -1
  144. package/dist/utils.d.ts +16 -0
  145. package/dist/utils.d.ts.map +1 -1
  146. package/dist/utils.js +221 -73
  147. package/dist/utils.js.map +1 -1
  148. package/dist/wheel-scroll.d.ts +23 -0
  149. package/dist/wheel-scroll.d.ts.map +1 -0
  150. package/dist/wheel-scroll.js +73 -0
  151. package/dist/wheel-scroll.js.map +1 -0
  152. package/native/clipboard.h +208 -0
  153. package/native/darwin/build.sh +69 -0
  154. package/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  155. package/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  156. package/native/darwin/src/darwin-platform.m +132 -0
  157. package/native/linux/build.sh +65 -0
  158. package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  159. package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  160. package/native/linux/src/clipboard-worker.h +70 -0
  161. package/native/linux/src/linux-platform-x11.c +274 -0
  162. package/native/napi.h +91 -0
  163. package/native/win32/build.mjs +232 -0
  164. package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  165. package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  166. package/native/win32/src/win32-platform.c +241 -0
  167. 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);
@@ -162,8 +173,16 @@ function finalizeTruncatedResult(prefix, prefixWidth, ellipsis, ellipsisWidth, m
162
173
  * check to avoid running the RGI_Emoji regex unnecessarily.
163
174
  */
164
175
  function graphemeWidth(segment) {
165
- if (segment === "\t") {
166
- 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;
167
186
  }
168
187
  // Zero-width clusters
169
188
  if (zeroWidthRegex.test(segment)) {
@@ -186,16 +205,30 @@ function graphemeWidth(segment) {
186
205
  return 2;
187
206
  }
188
207
  let width = eastAsianWidth(cp);
189
- // Trailing halfwidth/fullwidth forms and AM vowels that segment with a base.
190
- if (segment.length > 1) {
191
- 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)) {
192
223
  const c = char.codePointAt(0);
193
- if (c >= 0xff00 && c <= 0xffef) {
224
+ if (followsMark || (c >= 0xff00 && c <= 0xffef)) {
225
+ // halfwidth + fullwidth forms
194
226
  width += eastAsianWidth(c);
195
227
  }
196
228
  else if (c === 0x0e33 || c === 0x0eb3) {
197
229
  width += 1;
198
230
  }
231
+ followsMark = false;
199
232
  }
200
233
  }
201
234
  return width;
@@ -207,9 +240,11 @@ export function visibleWidth(str) {
207
240
  if (str.length === 0) {
208
241
  return 0;
209
242
  }
210
- // Fast path: pure ASCII printable
211
- if (isPrintableAscii(str)) {
212
- 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;
213
248
  }
214
249
  // Check cache
215
250
  const cached = widthCache.get(str);
@@ -221,22 +256,26 @@ export function visibleWidth(str) {
221
256
  if (str.includes("\t")) {
222
257
  clean = clean.replace(/\t/g, " ");
223
258
  }
224
- if (clean.includes("\x1b")) {
259
+ let escapeIndex = clean.indexOf("\x1b");
260
+ if (escapeIndex !== -1) {
225
261
  // Strip supported ANSI/OSC/APC escape sequences in one pass.
226
262
  // This covers CSI styling/cursor codes, OSC hyperlinks and prompt markers,
227
263
  // and APC sequences like CURSOR_MARKER.
228
264
  let stripped = "";
229
- let i = 0;
230
- while (i < clean.length) {
231
- const ansi = extractAnsiCode(clean, i);
232
- if (ansi) {
233
- i += ansi.length;
234
- 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;
235
272
  }
236
- stripped += clean[i];
237
- i++;
273
+ else {
274
+ escapeIndex++;
275
+ }
276
+ escapeIndex = clean.indexOf("\x1b", escapeIndex);
238
277
  }
239
- clean = stripped;
278
+ clean = stripped + clean.slice(copyFrom);
240
279
  }
241
280
  // Calculate width
242
281
  let width = 0;
@@ -253,6 +292,74 @@ export function visibleWidth(str) {
253
292
  widthCache.set(str, width);
254
293
  return width;
255
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
+ }
256
363
  /**
257
364
  * Normalize text for terminal output without changing logical editor content.
258
365
  * Some terminals render precomposed Thai/Lao AM vowels inconsistently during
@@ -288,45 +395,68 @@ export function normalizeTerminalOutput(str) {
288
395
  * Extract ANSI escape sequences from a string at the given position.
289
396
  */
290
397
  export function extractAnsiCode(str, pos) {
291
- if (pos >= str.length || str[pos] !== "\x1b")
292
- 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;
293
434
  const next = str[pos + 1];
294
435
  // CSI sequence: ESC [ ... m/G/K/H/J
295
436
  if (next === "[") {
296
- let j = pos + 2;
297
- while (j < str.length && !/[mGKHJ]/.test(str[j]))
298
- j++;
299
- if (j < str.length)
300
- return { code: str.substring(pos, j + 1), length: j + 1 - pos };
301
- 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;
302
444
  }
303
445
  // OSC sequence: ESC ] ... BEL or ESC ] ... ST (ESC \)
304
446
  // Used for hyperlinks (OSC 8), window titles, etc.
305
- if (next === "]") {
306
- let j = pos + 2;
307
- while (j < str.length) {
308
- if (str[j] === "\x07")
309
- return { code: str.substring(pos, j + 1), length: j + 1 - pos };
310
- if (str[j] === "\x1b" && str[j + 1] === "\\")
311
- return { code: str.substring(pos, j + 2), length: j + 2 - pos };
312
- j++;
313
- }
314
- return null;
315
- }
316
447
  // APC sequence: ESC _ ... BEL or ESC _ ... ST (ESC \)
317
448
  // Used for cursor marker and application-specific commands
318
- if (next === "_") {
319
- let j = pos + 2;
320
- while (j < str.length) {
321
- if (str[j] === "\x07")
322
- return { code: str.substring(pos, j + 1), length: j + 1 - pos };
323
- if (str[j] === "\x1b" && str[j + 1] === "\\")
324
- return { code: str.substring(pos, j + 2), length: j + 2 - pos };
325
- 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;
326
456
  }
327
- return null;
457
+ return 0;
328
458
  }
329
- return null;
459
+ return 0;
330
460
  }
331
461
  function parseOsc8Hyperlink(ansiCode) {
332
462
  if (!ansiCode.startsWith("\x1b]8;")) {
@@ -376,20 +506,18 @@ function getActiveOsc8Close(prefix) {
376
506
  * Track active ANSI SGR codes to preserve styling across line breaks.
377
507
  */
378
508
  class AnsiCodeTracker {
379
- constructor() {
380
- // Track individual attributes separately so we can reset them specifically
381
- this.bold = false;
382
- this.dim = false;
383
- this.italic = false;
384
- this.underline = false;
385
- this.blink = false;
386
- this.inverse = false;
387
- this.hidden = false;
388
- this.strikethrough = false;
389
- this.fgColor = null; // Stores the full code like "31" or "38;5;240"
390
- this.bgColor = null; // Stores the full code like "41" or "48;5;240"
391
- this.activeHyperlink = null;
392
- }
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;
393
521
  process(ansiCode) {
394
522
  // OSC 8 hyperlink: \x1b]8;;<url>\x1b\\ (open) or \x1b]8;;\x1b\\ (close).
395
523
  // Preserve the original terminator because some terminals only make BEL-terminated
@@ -567,6 +695,9 @@ class AnsiCodeTracker {
567
695
  }
568
696
  return result;
569
697
  }
698
+ getActiveBackgroundCode() {
699
+ return this.bgColor ? `\x1b[${this.bgColor}m` : "";
700
+ }
570
701
  hasActiveCodes() {
571
702
  return (this.bold ||
572
703
  this.dim ||
@@ -598,18 +729,29 @@ class AnsiCodeTracker {
598
729
  }
599
730
  }
600
731
  function updateTrackerFromText(text, tracker) {
601
- let i = 0;
602
- while (i < text.length) {
603
- const ansiResult = extractAnsiCode(text, i);
604
- if (ansiResult) {
605
- tracker.process(ansiResult.code);
606
- 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;
607
738
  }
608
739
  else {
609
740
  i++;
610
741
  }
742
+ i = text.indexOf("\x1b", i);
611
743
  }
612
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
+ }
613
755
  /**
614
756
  * Split text into words while keeping ANSI codes attached.
615
757
  */
@@ -635,13 +777,19 @@ function splitIntoTokensWithAnsi(text) {
635
777
  i += ansiResult.length;
636
778
  continue;
637
779
  }
638
- let end = i;
639
- while (end < text.length && !extractAnsiCode(text, end)) {
640
- 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);
641
784
  }
642
- 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)) {
643
791
  const segmentIsSpace = segment === " ";
644
- if (!segmentIsSpace && cjkBreakRegex.test(segment)) {
792
+ if (!ascii && !segmentIsSpace && cjkBreakRegex.test(segment)) {
645
793
  flushCurrent();
646
794
  const token = pendingAnsi + segment;
647
795
  pendingAnsi = "";