ansi.client.spec.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. import { describe, expect, it } from 'vitest'
  2. import { parseAnsiLines } from '../src/ansi.ts'
  3. const ESC = '\u001b'
  4. const BS = '\u0008'
  5. /** A combining acute accent: zero-width, so it takes no terminal column. */
  6. const ACCENT = '\u0301'
  7. function sgr(codes: string, text: string): string {
  8. return `${ESC}[${codes}m${text}${ESC}[0m`
  9. }
  10. function onlySpan(text: string) {
  11. const lines = parseAnsiLines(text)
  12. expect(lines).toHaveLength(1)
  13. expect(lines[0]).toHaveLength(1)
  14. return lines[0]![0]!
  15. }
  16. describe('parseAnsiLines: text without SGR state', () => {
  17. it('leaves plain text as one unstyled span', () => {
  18. expect(parseAnsiLines('hello')).toEqual([[{ text: 'hello', style: undefined }]])
  19. })
  20. it('returns exactly one empty line for empty input', () => {
  21. expect(parseAnsiLines('')).toEqual([[]])
  22. })
  23. it('splits a multi-line run and drops the empty line between two blocks', () => {
  24. expect(parseAnsiLines('a\n\nb')).toEqual([
  25. [{ text: 'a', style: undefined }],
  26. [],
  27. [{ text: 'b', style: undefined }],
  28. ])
  29. })
  30. it('keeps tabs, which the terminal surface needs for column layout', () => {
  31. expect(onlySpan('a\tb')).toEqual({ text: 'a\tb', style: undefined })
  32. })
  33. })
  34. describe('parseAnsiLines: basic colors mapped onto theme tokens', () => {
  35. it.each<[string, string, string]>([
  36. ['30', 'black', 'var(--dsw-alias-label-primary)'],
  37. ['37', 'white', 'var(--dsw-alias-label-primary)'],
  38. ['90', 'bright black', 'var(--dsw-alias-label-tertiary)'],
  39. ['31', 'red', 'var(--dsw-alias-state-error-primary)'],
  40. ['91', 'bright red', 'var(--dsw-alias-state-error-secondary)'],
  41. ['32', 'green', 'var(--dsw-alias-state-success-primary)'],
  42. ['92', 'bright green', 'var(--dsw-alias-state-success-secondary)'],
  43. ['33', 'yellow', 'var(--dsw-alias-state-warn-primary)'],
  44. ['93', 'bright yellow', 'var(--dsw-alias-state-warn-secondary)'],
  45. ['34', 'blue', 'var(--dsw-alias-state-business-primary)'],
  46. ['94', 'bright blue', 'var(--dsw-static-blue-400)'],
  47. ])('SGR %s (%s) resolves to %s', (code, _name, token) => {
  48. expect(onlySpan(sgr(code, 'x'))).toEqual({ text: 'x', style: { color: token } })
  49. })
  50. })
  51. describe('parseAnsiLines: colors with no token equivalent', () => {
  52. it.each<[string, string, string]>([
  53. ['35', 'magenta', 'rgb(187, 0, 187)'],
  54. ['36', 'cyan', 'rgb(0, 187, 187)'],
  55. ['38;5;208', '256-palette orange', 'rgb(255, 135, 0)'],
  56. ['38;2;10;20;30', 'truecolor', 'rgb(10, 20, 30)'],
  57. ])('SGR %s (%s) falls through to %s', (code, _name, literal) => {
  58. expect(onlySpan(sgr(code, 'x')).style).toEqual({ color: literal })
  59. })
  60. })
  61. describe('parseAnsiLines: backgrounds', () => {
  62. it('sets backgroundColor for a background-only run', () => {
  63. expect(onlySpan(sgr('44', 'x')).style).toEqual({ backgroundColor: 'rgb(0, 0, 187)' })
  64. })
  65. it('keeps the literal foreground when the run paints its own background', () => {
  66. expect(onlySpan(sgr('41;37', 'x')).style).toEqual({
  67. backgroundColor: 'rgb(187, 0, 0)',
  68. color: 'rgb(255,255,255)',
  69. })
  70. })
  71. it('renders reverse video as the swapped pair anser reports', () => {
  72. expect(onlySpan(sgr('31;7', 'x')).style).toEqual({
  73. backgroundColor: 'rgb(187, 0, 0)',
  74. color: 'rgb(0, 0, 0)',
  75. })
  76. })
  77. })
  78. describe('parseAnsiLines: decorations', () => {
  79. it.each<[string, string, Record<string, unknown>]>([
  80. ['1', 'bold', { fontWeight: 700 }],
  81. ['2', 'dim', { opacity: 0.7 }],
  82. ['3', 'italic', { fontStyle: 'italic' }],
  83. ['4', 'underline', { textDecoration: 'underline' }],
  84. ['9', 'strikethrough', { textDecoration: 'line-through' }],
  85. ['8', 'hidden', { visibility: 'hidden' }],
  86. ])('SGR %s (%s) resolves to %o', (code, _name, style) => {
  87. expect(onlySpan(sgr(code, 'x')).style).toEqual(style)
  88. })
  89. it('lets the later textDecoration win when a run declares underline and strikethrough', () => {
  90. expect(onlySpan(sgr('4;9', 'x')).style).toEqual({ textDecoration: 'line-through' })
  91. expect(onlySpan(sgr('9;4', 'x')).style).toEqual({ textDecoration: 'underline' })
  92. })
  93. it('combines a color with several decorations in one style', () => {
  94. expect(onlySpan(sgr('1;3;31', 'x')).style).toEqual({
  95. color: 'var(--dsw-alias-state-error-primary)',
  96. fontWeight: 700,
  97. fontStyle: 'italic',
  98. })
  99. })
  100. it('reproduces no animation for blink, leaving the run unstyled', () => {
  101. expect(onlySpan(sgr('5', 'x'))).toEqual({ text: 'x', style: undefined })
  102. })
  103. })
  104. describe('parseAnsiLines: sequences that carry no color', () => {
  105. it('removes an OSC string with its BEL terminator', () => {
  106. expect(onlySpan(`a${ESC}]0;window title\u0007b`)).toEqual({ text: 'ab', style: undefined })
  107. })
  108. it('removes an OSC string terminated by ST', () => {
  109. expect(onlySpan(`a${ESC}]8;;https://example.com${ESC}\\b`)).toEqual({ text: 'ab', style: undefined })
  110. })
  111. it('removes non-CSI escapes such as charset selection and reset', () => {
  112. expect(onlySpan(`x${ESC}(By${ESC}cz`)).toEqual({ text: 'xyz', style: undefined })
  113. })
  114. it('removes inert C0 controls', () => {
  115. expect(onlySpan('\u0000ab\u001fc\u007f')).toEqual({ text: 'abc', style: undefined })
  116. })
  117. it('keeps CSI sequences that only move the cursor out of the text', () => {
  118. expect(onlySpan(`${ESC}[2K${ESC}[1Adone`)).toEqual({ text: 'done', style: undefined })
  119. })
  120. })
  121. describe('parseAnsiLines: carriage returns', () => {
  122. it('keeps only the last redraw of a line', () => {
  123. expect(onlySpan('10%\r55%\r100%')).toEqual({ text: '100%', style: undefined })
  124. })
  125. it('leaves the tail of a longer frame standing under a shorter redraw', () => {
  126. // Verified against a real terminal: `100%\rOK` paints `OK0%`. A carriage
  127. // return only moves the cursor, so the two columns the redraw never reaches
  128. // still hold the frame beneath — truncating to the last `\r` would lose them.
  129. expect(onlySpan('100%\rOK')).toEqual({ text: 'OK0%', style: undefined })
  130. expect(onlySpan('abcdef\rXY')).toEqual({ text: 'XYcdef', style: undefined })
  131. })
  132. it('clamps a backspace run at the line start rather than going negative', () => {
  133. // More backspaces than characters: the cursor stops at column 0, so the
  134. // following write simply overwrites from there.
  135. expect(onlySpan(`ab${BS}${BS}${BS}${BS}xyz`)).toEqual({ text: 'xyz', style: undefined })
  136. })
  137. it('keeps SGR state in force across a redraw, as a terminal does', () => {
  138. // Verified against a real terminal: `\x1b[31mgone\rkept` paints `kept` RED.
  139. // A carriage return moves the cursor; it does not reset the graphic state,
  140. // so the redraw inherits the color the discarded frame was written with.
  141. expect(onlySpan(`${ESC}[31mgone\rkept`))
  142. .toEqual({ text: 'kept', style: { color: 'var(--dsw-alias-state-error-primary)' } })
  143. })
  144. it('preserves both lines of a CRLF pair instead of treating it as a redraw', () => {
  145. expect(parseAnsiLines('a\r\r\nb\r\n')).toEqual([
  146. [{ text: 'a', style: undefined }],
  147. [{ text: 'b', style: undefined }],
  148. [],
  149. ])
  150. })
  151. it('applies the redraw per line, not across the whole text', () => {
  152. expect(parseAnsiLines('one\rtwo\nthree')).toEqual([
  153. [{ text: 'two', style: undefined }],
  154. [{ text: 'three', style: undefined }],
  155. ])
  156. })
  157. })
  158. describe('parseAnsiLines: backspaces', () => {
  159. it('applies a backspace as the overwrite a terminal draws', () => {
  160. // `abc` then two backspaces then `XY` shows as `aXY`, not `abcXY`.
  161. expect(onlySpan(`abc${BS}${BS}XY`)).toEqual({ text: 'aXY', style: undefined })
  162. })
  163. it('stops at the line start instead of eating the newline before it', () => {
  164. expect(parseAnsiLines(`ab\n${BS}${BS}${BS}cd`)).toEqual([
  165. [{ text: 'ab', style: undefined }],
  166. [{ text: 'cd', style: undefined }],
  167. ])
  168. })
  169. it('treats a trailing backspace as a cursor move, not a delete', () => {
  170. // Verified against a real terminal: `abc\b` still shows `abc`. Only a later
  171. // write overwrites; a backspace with nothing after it erases nothing.
  172. expect(onlySpan(`abc${BS}`)).toEqual({ text: 'abc', style: undefined })
  173. // Same at a line boundary: the newline ends the line before any overwrite.
  174. expect(parseAnsiLines(`abc${BS}\ndef`)).toEqual([
  175. [{ text: 'abc', style: undefined }],
  176. [{ text: 'def', style: undefined }],
  177. ])
  178. })
  179. it('steps over an SGR sequence instead of erasing its bytes', () => {
  180. // `abc` reset then two backspaces then `XY`: erasing the reset's bytes would
  181. // corrupt it and repaint the rest of the line with whatever the remainder
  182. // parses as. The visible result is `aXY`, still red, with the reset intact.
  183. expect(parseAnsiLines(`${sgr('31', 'abc')}${BS}${BS}XY`)).toEqual([[
  184. { text: 'a', style: { color: 'var(--dsw-alias-state-error-primary)' } },
  185. { text: 'XY', style: undefined },
  186. ]])
  187. })
  188. it('erases across a style boundary without dropping the styles between', () => {
  189. // The backspace reaches back past the reset to the last printed character.
  190. expect(parseAnsiLines(`${sgr('32', 'ok')}${ESC}[31m${BS}bad`)).toEqual([[
  191. { text: 'o', style: { color: 'var(--dsw-alias-state-success-primary)' } },
  192. { text: 'bad', style: { color: 'var(--dsw-alias-state-error-primary)' } },
  193. ]])
  194. })
  195. it('replays a redraw and a trailing backspace as pure cursor moves', () => {
  196. // Verified against a real terminal: `old\rnew\b` shows `new`. The redraw
  197. // repaints all three columns and the trailing backspace only moves the
  198. // cursor left — nothing overwrites the `w`, so nothing is lost.
  199. expect(onlySpan(`old\rnew${BS}`)).toEqual({ text: 'new', style: undefined })
  200. })
  201. it('overwrites only the columns the later write reaches, keeping the rest styled', () => {
  202. // Verified against a real terminal: red `bad`, three backspaces, then `ok`
  203. // shows `okd` — the cursor returned to column 0 and `ok` overwrote two of
  204. // the three columns, so the untouched `d` keeps the run's red.
  205. expect(parseAnsiLines(`${sgr('31', 'bad')}${BS}${BS}${BS}ok`)).toEqual([[
  206. { text: 'ok', style: undefined },
  207. { text: 'd', style: { color: 'var(--dsw-alias-state-error-primary)' } },
  208. ]])
  209. })
  210. })
  211. describe('parseAnsiLines: erase and column arithmetic', () => {
  212. it('erases the rest of the line, the fixed companion of a redraw', () => {
  213. // Verified in a real terminal: `100%\r\x1b[KOK` shows `OK`. Every spinner and
  214. // progress bar writes `\r\x1b[K`; without the erase the previous frame's tail
  215. // stands and the card shows text the terminal never displayed.
  216. expect(onlySpan(`100%\r${ESC}[KOK`)).toEqual({ text: 'OK', style: undefined })
  217. // The parameterless form and `0` are the same erase.
  218. expect(onlySpan(`100%\r${ESC}[0KOK`)).toEqual({ text: 'OK', style: undefined })
  219. })
  220. it('erases the whole line for the 2K form and to the cursor for 1K', () => {
  221. expect(onlySpan(`ab\r${ESC}[2Kxy`)).toEqual({ text: 'xy', style: undefined })
  222. // 1K clears left of the cursor without moving it, so those columns read as
  223. // blanks — verified in a real terminal, which shows ` |` for this input.
  224. expect(onlySpan(`abcd${ESC}[1K|`)).toEqual({ text: ' |', style: undefined })
  225. })
  226. it('paints columns a 2K dropped as blanks when a later write lands past them', () => {
  227. // 2K clears the line but leaves the cursor where it was, so writing there
  228. // leaves the columns before it unwritten — blanks, as a terminal shows.
  229. expect(onlySpan(`abcd${ESC}[2Kx`)).toEqual({ text: ' x', style: undefined })
  230. })
  231. it('advances a redraw cursor by tab stops, leaving a tabbed column standing', () => {
  232. // Verified in a real terminal: `a\tb\rXY` shows `XY b` — the `b` sits at
  233. // column 8, which a two-character redraw cannot reach. Counting the tab as
  234. // one column would have produced `XYb` and destroyed the alignment.
  235. expect(onlySpan('a\tb\rXY')).toEqual({ text: 'XY b', style: undefined })
  236. })
  237. it('counts a wide character as the two columns a terminal advances', () => {
  238. // `中` occupies two cells, so a two-character redraw covers exactly it.
  239. expect(onlySpan('中x\rab')).toEqual({ text: 'abx', style: undefined })
  240. })
  241. it('does not accumulate a cursor or erase sequence into a cell style', () => {
  242. // Only SGR carries graphic state. An erase folded into the style string
  243. // would grow it per redraw and emit boundaries anser has to discard.
  244. expect(parseAnsiLines(`${ESC}[31ma\r${ESC}[Kb`)).toEqual([[
  245. { text: 'b', style: { color: 'var(--dsw-alias-state-error-primary)' } },
  246. ]])
  247. })
  248. })
  249. describe('parseAnsiLines: line-end state and column widths', () => {
  250. it('closes a run whose reset lands after the last written cell', () => {
  251. // Verified in a real terminal: `\x1b[32mdone\rok\x1b[0m` then `plain` shows
  252. // `okne` GREEN and `plain` in the DEFAULT color. The reset changes no cell,
  253. // so returning the last cell's state leaked green onto every later line —
  254. // and this exact shape (`\r\x1b[K\x1b[32m✓ built\x1b[0m`) is what every
  255. // build tool writes.
  256. expect(parseAnsiLines(`${ESC}[32mdone\rok${ESC}[0m\nplain`)).toEqual([
  257. [{ text: 'okne', style: { color: 'var(--dsw-alias-state-success-primary)' } }],
  258. [{ text: 'plain', style: undefined }],
  259. ])
  260. })
  261. it('erases through the cursor column for 1K, not up to it', () => {
  262. // Verified in a real terminal: `abcd\b\x1b[1K|` shows ` |` — the `d` under
  263. // the cursor is erased too, which the CSI spec calls inclusive.
  264. expect(onlySpan(`abcd${BS}${ESC}[1K|`)).toEqual({ text: ' |', style: undefined })
  265. })
  266. it('gives a combining mark no column of its own', () => {
  267. // Verified in a real terminal: `é` (e + U+0301) then `x`, redrawn with `YZ`,
  268. // shows `YZ`. Counting the mark as a column left the `x` standing.
  269. expect(onlySpan('e\u0301x\rYZ')).toEqual({ text: 'YZ', style: undefined })
  270. })
  271. it('drops a combining mark left with no cell to attach to by a redraw', () => {
  272. // Verified in a real terminal: `ab` then CR then U+0301 then `x` shows `xb`.
  273. // The redraw puts the cursor at column 0, so the mark has no preceding cell
  274. // and the terminal shows nothing for it rather than a lone accent.
  275. expect(onlySpan(`ab\r${ACCENT}x`)).toEqual({ text: 'xb', style: undefined })
  276. // A mark with no movement on its line never reaches the replay at all: it
  277. // is width business, not a cursor move, so it stays as authored.
  278. expect(onlySpan(`${ACCENT}abc`)).toEqual({ text: `${ACCENT}abc`, style: undefined })
  279. })
  280. it('carries a colour opened after the last write onto the next line', () => {
  281. // The mirror of the reset case, verified in a real terminal: `ab` CR `X` then
  282. // `\x1b[31m` with nothing after it shows `Xb` UNSTYLED and the next line red.
  283. // The scan ends styled while the last cell is not, so the convergence has to
  284. // open the run at the line end for it to reach the following line.
  285. expect(parseAnsiLines(`ab\rX${ESC}[31m\nnext`)).toEqual([
  286. [{ text: 'Xb', style: undefined }],
  287. [{ text: 'next', style: { color: 'var(--dsw-alias-state-error-primary)' } }],
  288. ])
  289. })
  290. it('blanks a wide character\'s spacer once its lead cell is overwritten', () => {
  291. // Verified in a real terminal: `中x` redrawn with `A` shows `A x` — the wide
  292. // glyph's second cell becomes a blank rather than closing the gap, so the
  293. // `x` keeps column 3.
  294. expect(onlySpan('中x\rA')).toEqual({ text: 'A x', style: undefined })
  295. // Covering both of its columns leaves no spacer behind.
  296. expect(onlySpan('中x\rab')).toEqual({ text: 'abx', style: undefined })
  297. })
  298. it('replays an erase whose parameters carry a semicolon', () => {
  299. // The replay guard has to match the same CSI shape the parser accepts, or a
  300. // form like `\x1b[1;2K` skips the replay and its erase never happens.
  301. expect(onlySpan(`abcd${ESC}[1;2K|`)).toEqual({ text: ' |', style: undefined })
  302. })
  303. })
  304. describe('parseAnsiLines: bounded state and true widths', () => {
  305. it('emits one canonical sequence per boundary however the state was reached', () => {
  306. // Colors that never fully reset would, under a raw per-cell sequence
  307. // history, re-emit the whole chain at every boundary: 3200 such cells
  308. // produce 25 MB and eventually a RangeError. Normalized state keeps
  309. // the emitted text linear in the number of cells.
  310. let input = ''
  311. for (let index = 0; index < 2000; index += 1) input += `${ESC}[3${index % 6 + 1}mx`
  312. const emitted = parseAnsiLines(`${input}\rz`)[0] ?? []
  313. expect(emitted.reduce((total, span) => total + span.text.length, 0)).toBe(2000)
  314. })
  315. it('closes an attribute with its closer instead of appending to the state', () => {
  316. // `1` then `22` is bold then not-bold, which every chalk-based tool writes;
  317. // appending both left the cell bold and grew the chain.
  318. // Verified in a real terminal: the `22` closes the bold, so the `x` written
  319. // after the redraw is PLAIN. Appending both left it bold and grew the chain.
  320. expect(parseAnsiLines(`${ESC}[1mbold${ESC}[22mplain\r${ESC}[Kx`)).toEqual([[
  321. { text: 'x', style: undefined },
  322. ]])
  323. expect(parseAnsiLines(`${ESC}[1mA${ESC}[22mB`)).toEqual([[
  324. { text: 'A', style: { fontWeight: 700 } },
  325. { text: 'B', style: undefined },
  326. ]])
  327. })
  328. it('folds extended colors, backgrounds and every attribute closer', () => {
  329. // The 256-palette and truecolor forms consume their own arguments, so the
  330. // fold has to take them whole rather than as separate codes.
  331. expect(parseAnsiLines(`${ESC}[38;5;208mA\r${ESC}[KB`)).toEqual([[
  332. { text: 'B', style: { color: 'rgb(255, 135, 0)' } },
  333. ]])
  334. expect(parseAnsiLines(`${ESC}[38;2;10;20;30mA\r${ESC}[KB`)).toEqual([[
  335. { text: 'B', style: { color: 'rgb(10, 20, 30)' } },
  336. ]])
  337. // A background survives the same way, and `49` closes it.
  338. expect(parseAnsiLines(`${ESC}[41mA${ESC}[49mB\r${ESC}[KC`)).toEqual([[
  339. { text: 'C', style: undefined },
  340. ]])
  341. // Each closer drops only its own attribute: `4` underline closed by `24`
  342. // while the italic opened before it stays in force.
  343. expect(parseAnsiLines(`${ESC}[3;4mA${ESC}[24mB\r${ESC}[KC`)).toEqual([[
  344. { text: 'C', style: { fontStyle: 'italic' } },
  345. ]])
  346. // `39` closes a foreground without touching the background.
  347. expect(parseAnsiLines(`${ESC}[31;42mA${ESC}[39mB\r${ESC}[KC`)).toEqual([[
  348. { text: 'C', style: { backgroundColor: 'rgb(0, 187, 0)' } },
  349. ]])
  350. })
  351. it('folds the remaining SGR shapes the model has to carry', () => {
  352. // A 48-background in extended form, so the `48` arm and the `2`-span both run.
  353. expect(parseAnsiLines(`${ESC}[48;2;1;2;3mA\r${ESC}[KB`)).toEqual([[
  354. { text: 'B', style: { backgroundColor: 'rgb(1, 2, 3)' } },
  355. ]])
  356. // A bright foreground and a bright background, the 90-97 / 100-107 arms.
  357. expect(parseAnsiLines(`${ESC}[91mA\r${ESC}[KB`)).toEqual([[
  358. { text: 'B', style: { color: 'var(--dsw-alias-state-error-secondary)' } },
  359. ]])
  360. expect(parseAnsiLines(`${ESC}[101mA\r${ESC}[KB`)).toEqual([[
  361. { text: 'B', style: { backgroundColor: 'rgb(255, 85, 85)' } },
  362. ]])
  363. // An extended form with no recognized kind byte consumes nothing extra.
  364. expect(parseAnsiLines(`${ESC}[38mA\r${ESC}[KB`)).toEqual([[{ text: 'B', style: undefined }]])
  365. // Re-opening an attribute already in force does not duplicate it, and a bare
  366. // `\x1b[m` resets exactly as `\x1b[0m` does.
  367. expect(parseAnsiLines(`${ESC}[1m${ESC}[1mA${ESC}[mB\r${ESC}[KC`)).toEqual([[
  368. { text: 'C', style: undefined },
  369. ]])
  370. })
  371. it('treats a text-presentation symbol as one column', () => {
  372. // Verified in a real terminal: `A✓B` redrawn with `XY` shows `XYB`, so the
  373. // check mark is ONE column. Taking the whole U+2600-U+27BF block as wide
  374. // misaligned exactly the progress output this card exists to show.
  375. expect(onlySpan('A\u2713B\rXY')).toEqual({ text: 'XYB', style: undefined })
  376. // An emoji-presentation character is two, so the same redraw leaves a blank.
  377. expect(onlySpan('A\u{1f600}B\rXY')).toEqual({ text: 'XY B', style: undefined })
  378. })
  379. it('clears a wide pair from either side, including through an erase', () => {
  380. // Verified in a real terminal (`A x`): the redraw puts the cursor at column
  381. // 0, the backspace clamps there, and writing `A` over the wide lead blanks
  382. // its spacer rather than letting the `x` slide left.
  383. expect(onlySpan(`\u4e2dx\r${BS}A`)).toEqual({ text: 'A x', style: undefined })
  384. // An erase reaching the lead blanks its spacer through the same helper.
  385. // Verified in a real terminal (` |`): 1K blanks through the cursor column,
  386. // so the wide glyph's two cells and the `x` all become blanks.
  387. expect(onlySpan(`\u4e2dx${ESC}[1K|`)).toEqual({ text: ' |', style: undefined })
  388. })
  389. it('clears the lead when the write lands on the spacer itself', () => {
  390. // Two backspaces from after `中x` stop ON the wide glyph's second cell;
  391. // writing there blanks the lead through the spacer side of the pair clear,
  392. // so the glyph cannot survive as half a character.
  393. expect(onlySpan(`中x${BS}${BS}A`)).toEqual({ text: ' Ax', style: undefined })
  394. })
  395. it('keeps a surviving spacer as a blank when its lead was replaced by a spacer', () => {
  396. // `好` written over the first glyph's spacer puts its own spacer on the
  397. // second glyph's lead cell — a write that goes down without a pair clear.
  398. // The second glyph's spacer survives with a dead lead and must emit a
  399. // blank, or everything after it shifts one column left.
  400. expect(onlySpan(`中中${BS}${BS}${BS}好`)).toEqual({ text: ' 好 ', style: undefined })
  401. })
  402. it('blanks both halves of a wide pair when either is overwritten', () => {
  403. // A terminal cannot leave one cell of a two-cell glyph standing, so writing
  404. // over the spacer clears the lead as well.
  405. // Verified in a real terminal: two wide chars, CR, then `A` shows `A ` and
  406. // the second glyph — writing the lead cell blanks its spacer, so the column
  407. // stays occupied rather than collapsing.
  408. expect(onlySpan('\u4e2d\u4e2d\rA')).toEqual({ text: 'A \u4e2d', style: undefined })
  409. })
  410. })
  411. describe('parseAnsiLines: SGR across lines', () => {
  412. it('carries active state past a newline, as a terminal does', () => {
  413. // Verified in a real terminal: `\x1b[31mabc\rX\nnext` paints BOTH lines red.
  414. // A newline does not reset the graphic state, so a replayed line must hand
  415. // its state to the next one instead of closing it off.
  416. expect(parseAnsiLines(`${ESC}[31mabc\rX\nnext`)).toEqual([
  417. [{ text: 'Xbc', style: { color: 'var(--dsw-alias-state-error-primary)' } }],
  418. [{ text: 'next', style: { color: 'var(--dsw-alias-state-error-primary)' } }],
  419. ])
  420. })
  421. it('tracks state through a line that needs no replay', () => {
  422. // The middle line has no movement, so it is not replayed — but its own SGR
  423. // still has to reach the line after it.
  424. expect(parseAnsiLines(`a\r${ESC}[32mb\nplain\nc`)).toEqual([
  425. [{ text: 'b', style: { color: 'var(--dsw-alias-state-success-primary)' } }],
  426. [{ text: 'plain', style: { color: 'var(--dsw-alias-state-success-primary)' } }],
  427. [{ text: 'c', style: { color: 'var(--dsw-alias-state-success-primary)' } }],
  428. ])
  429. })
  430. })
  431. describe('parseAnsiLines: runs spanning lines', () => {
  432. it('carries one run\'s style onto every line it covers', () => {
  433. expect(parseAnsiLines(sgr('32', 'first\nsecond'))).toEqual([
  434. [{ text: 'first', style: { color: 'var(--dsw-alias-state-success-primary)' } }],
  435. [{ text: 'second', style: { color: 'var(--dsw-alias-state-success-primary)' } }],
  436. ])
  437. })
  438. it('keeps several runs of one line in order', () => {
  439. expect(parseAnsiLines(`plain${sgr('31', 'red')}tail`)).toEqual([[
  440. { text: 'plain', style: undefined },
  441. { text: 'red', style: { color: 'var(--dsw-alias-state-error-primary)' } },
  442. { text: 'tail', style: undefined },
  443. ]])
  444. })
  445. })