ansi.client.spec.ts 24 KB

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