xonecas.io
70160
Ce

cel-tui / commit 78e4393

fix(core): wide character rendering — continuation markers and emitter skip

Paint now writes continuation markers (char="") in the trailing cells
of wide characters (CJK, emoji). The emitter skips these cells so the
terminal cursor advances naturally from the wide character output,
preventing the right half from being overwritten by a stray space.

Both emitBuffer and emitDiff handle continuation cells. Updated paint
tests to verify continuation markers, added emitter tests for wide chars.

xonecas2026-04-04 78e4393b8c54362445c663ffcf448d882bb4e91d parent fbafce6

4 files changed

packages/core/src/emitter.test.ts+29−0
@@ -125,6 +125,20 @@ describe("emitBuffer", () => {
125125 const output = emitBuffer(buf);
126126 expect(output).toContain(" ");
127127 });
128+
129+ test("wide character continuation cells are skipped", () => {
130+ const buf = new CellBuffer(5, 1);
131+ // Simulate a wide char at col 0 with continuation at col 1
132+ buf.set(0, 0, cell("\u4e16")); // CJK char (2 cols)
133+ buf.set(1, 0, cell("")); // continuation marker
134+ buf.set(2, 0, cell("x"));
135+ const output = emitBuffer(buf);
136+ // The output should contain the wide char and "x" without
137+ // an extra space between them (continuation is skipped)
138+ expect(output).toContain("\u4e16x");
139+ // Should NOT contain "\u4e16 x" (space from continuation)
140+ expect(output).not.toContain("\u4e16 x");
141+ });
128142 });
129143
130144 describe("emitDiff", () => {
@@ -183,4 +197,19 @@ describe("emitDiff", () => {
183197 const cursorMoves = output.match(/\x1b\[\d+;\d+H/g) || [];
184198 expect(cursorMoves.length).toBe(1);
185199 });
200+
201+ test("wide character continuation cells are skipped in diff", () => {
202+ const prev = new CellBuffer(5, 1);
203+ const next = new CellBuffer(5, 1);
204+ // Wide char at col 0 with continuation at col 1
205+ next.set(0, 0, cell("\u4e16"));
206+ next.set(1, 0, cell("")); // continuation
207+ next.set(2, 0, cell("x"));
208+ const output = emitDiff(prev, next);
209+ // Should emit the wide char and x without extra space
210+ expect(output).toContain("\u4e16");
211+ expect(output).toContain("x");
212+ // Continuation cell should not produce any output character
213+ expect(output).not.toContain("\u4e16 ");
214+ });
186215 });
packages/core/src/emitter.ts+6−0
@@ -106,6 +106,9 @@ export function emitBuffer(buf: CellBuffer): string {
106106 for (let x = 0; x < buf.width; x++) {
107107 const cell = buf.get(x, y);
108108
109+ // Skip continuation cells (trailing half of wide characters)
110+ if (cell.char === "") continue;
111+
109112 // Emit style change if needed
110113 if (lastStyle === null || !styleEquals(cell, lastStyle)) {
111114 // Reset before applying new style
@@ -156,6 +159,9 @@ export function emitDiff(prev: CellBuffer, next: CellBuffer): string {
156159 for (const { x, y } of changes) {
157160 const cell = next.get(x, y);
158161
162+ // Skip continuation cells (trailing half of wide characters)
163+ if (cell.char === "") continue;
164+
159165 // Position cursor if not consecutive
160166 if (y !== lastY || x !== lastX + 1) {
161167 // Reset style before repositioning
packages/core/src/paint.test.ts+5−2
@@ -165,9 +165,11 @@ describe("paint", () => {
165165 const ln = layout(node, 20, 1);
166166 const buf = new CellBuffer(20, 1);
167167 paint(ln, buf);
168- // \u4e16 at col 0 (wide), \u754c at col 2 (wide)
168+ // \u4e16 at col 0 (wide), continuation at col 1, \u754c at col 2 (wide), continuation at col 3
169169 expect(buf.get(0, 0).char).toBe("\u4e16");
170+ expect(buf.get(1, 0).char).toBe(""); // continuation marker
170171 expect(buf.get(2, 0).char).toBe("\u754c");
172+ expect(buf.get(3, 0).char).toBe(""); // continuation marker
171173 });
172174
173175 test("emoji occupies 2 cells", () => {
@@ -175,8 +177,9 @@ describe("paint", () => {
175177 const ln = layout(node, 20, 1);
176178 const buf = new CellBuffer(20, 1);
177179 paint(ln, buf);
178- // emoji at col 0 (wide), x at col 2
180+ // emoji at col 0 (wide), continuation at col 1, x at col 2
179181 expect(buf.get(0, 0).char).toBe("\ud83d\ude00");
182+ expect(buf.get(1, 0).char).toBe(""); // continuation marker
180183 expect(buf.get(2, 0).char).toBe("x");
181184 });
182185
packages/core/src/paint.ts+14−0
@@ -361,6 +361,20 @@ function paintLineGraphemes(
361361 if (absX + gw > clipLeft) {
362362 // At least partially visible in clip rect
363363 buf.set(absX, y, makeCell(segment, props));
364+ // Write continuation markers for wide characters (2+ columns)
365+ for (let w = 1; w < gw; w++) {
366+ const cx = absX + w;
367+ if (cx < clipRight) {
368+ buf.set(cx, y, {
369+ char: "",
370+ fgColor: props.fgColor ?? null,
371+ bgColor: props.bgColor ?? null,
372+ bold: props.bold ?? false,
373+ italic: props.italic ?? false,
374+ underline: props.underline ?? false,
375+ });
376+ }
377+ }
364378 }
365379 col += gw;
366380 }