|
| 1 | +import { describe, expect, it } from "./suite.ts"; |
| 2 | +import { Virtualizer } from "../mod.ts"; |
| 3 | + |
| 4 | +function charMeasure(text: string): number { |
| 5 | + let cp = text.codePointAt(0)!; |
| 6 | + if (cp >= 0x4e00 && cp <= 0x9fff) return 2; |
| 7 | + if (cp < 0x20) return 0; |
| 8 | + return 1; |
| 9 | +} |
| 10 | + |
| 11 | +describe("G.ANSI — ANSI golden fixtures", () => { |
| 12 | + it("G.ANSI.simple-sgr — SGR sequence does not add sub-rows", () => { |
| 13 | + let v = new Virtualizer({ measureWidth: charMeasure, columns: 80, rows: 24 }); |
| 14 | + let idx = v.appendLine("\x1b[31mred\x1b[0m"); |
| 15 | + let vp = v.resolveViewport(); |
| 16 | + expect(vp.entries[0].totalSubRows).toBe(1); |
| 17 | + expect(v.getLineDisplayWidth(idx)).toBe(3); |
| 18 | + }); |
| 19 | + |
| 20 | + it("G.ANSI.sgr-at-wrap-boundary — wrap occurs at visible char boundary, not inside SGR", () => { |
| 21 | + let v = new Virtualizer({ measureWidth: charMeasure, columns: 5, rows: 24 }); |
| 22 | + v.appendLine("abcde\x1b[31mfghij"); |
| 23 | + let vp = v.resolveViewport(); |
| 24 | + let entry = vp.entries[0]; |
| 25 | + // Visible: "abcde" (5) then "fghij" (5). CSI "\x1b[31m" at indices 5–9 (5 chars). |
| 26 | + // Wrap after 5 visible chars → wrap point at index 10 (start of 'f', after CSI) |
| 27 | + expect(entry.wrapPoints).toEqual([10]); |
| 28 | + let slices = [entry.text.slice(0, 10), entry.text.slice(10)]; |
| 29 | + expect(slices[0]).toBe("abcde\x1b[31m"); |
| 30 | + expect(slices[1]).toBe("fghij"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("G.ANSI.osc-with-bel — OSC with BEL terminator", () => { |
| 34 | + let v = new Virtualizer({ measureWidth: charMeasure, columns: 80, rows: 24 }); |
| 35 | + let idx = v.appendLine("\x1b]0;title\x07visible"); |
| 36 | + let vp = v.resolveViewport(); |
| 37 | + expect(vp.entries[0].totalSubRows).toBe(1); |
| 38 | + expect(v.getLineDisplayWidth(idx)).toBe(7); |
| 39 | + }); |
| 40 | + |
| 41 | + it("G.ANSI.osc-with-st — OSC with ST terminator", () => { |
| 42 | + let v = new Virtualizer({ measureWidth: charMeasure, columns: 80, rows: 24 }); |
| 43 | + let idx = v.appendLine("\x1b]0;title\x1b\\visible"); |
| 44 | + let vp = v.resolveViewport(); |
| 45 | + expect(vp.entries[0].totalSubRows).toBe(1); |
| 46 | + expect(v.getLineDisplayWidth(idx)).toBe(7); |
| 47 | + }); |
| 48 | + |
| 49 | + it("G.ANSI.nested-csi-in-wrapped-line — multiple CSI in wrapping line, no wrap inside CSI", () => { |
| 50 | + let v = new Virtualizer({ measureWidth: charMeasure, columns: 3, rows: 24 }); |
| 51 | + // "a\x1b[1mb\x1b[0mc\x1b[32md\x1b[0me" — 5 visible chars: a, b, c, d, e |
| 52 | + v.appendLine("a\x1b[1mb\x1b[0mc\x1b[32md\x1b[0me"); |
| 53 | + let vp = v.resolveViewport(); |
| 54 | + let entry = vp.entries[0]; |
| 55 | + // 5 visible chars at columns 3 → 2 sub-rows |
| 56 | + expect(entry.totalSubRows).toBe(2); |
| 57 | + // No wrap point should fall inside any CSI sequence |
| 58 | + let text = entry.text; |
| 59 | + for (let wp of entry.wrapPoints) { |
| 60 | + expect(text.charCodeAt(wp)).not.toBe(0x5b); // not '[' |
| 61 | + // wp should not be between ESC and final byte |
| 62 | + if (wp > 0 && text.charCodeAt(wp - 1) === 0x1b) { |
| 63 | + // wp right after ESC — that's inside the sequence |
| 64 | + expect(true).toBe(false); |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | +}); |
0 commit comments