The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 13:30 UTC
fix(shared): preserve Unicode during command output overwrites (#34332)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Lakshmi Tanmay committed
Nov 15, 2026 at 01:07 UTC
2bee33eb193a0430c9a0d8e24f8f684163f045a4
4 files changed
+179
-59
packages/shared/package.json
+1
@@ -582,6 +582,7 @@
582
"@t3tools/contracts": "workspace:*",
583
"effect": "catalog:",
584
"ffi-rs": "1.3.2",
585
+ "get-east-asian-width": "^1.6.0",
586
"mdast-util-directive": "catalog:",
587
"mdast-util-to-hast": "^13.2.1",
588
"micromark-extension-directive": "catalog:",
packages/shared/src/terminalOutput.test.ts
+60
-14
@@ -24,6 +24,31 @@ describe("appendTerminalOutput", () => {
24
expect(feed(["[1/3]", "\r[2/3]", "\r[3/3] done\nnext"]).text).toBe("[3/3] done\nnext");
25
});
26
27
+ it.each([
28
+ ["😀\u001b[1Gx", "x "],
29
+ ["界a\u001b[3GZ", "界Z"],
30
+ ["e\u0301a\u001b[2GZ", "e\u0301Z"],
31
+ ["𐐀a\u001b[2GZ", "𐐀Z"],
32
+ ["界a\bZ", "界Z"],
33
+ ["界a\u001b[2GZ", " Za"],
34
+ ["a界b\rXY", "XY b"],
35
+ ["e\u0301a\rZ", "Za"],
36
+ ["界a\u001b[2G\u001b[K", " "],
37
+ ["界a\u001b[1G\u001b[1K", " a"],
38
+ ["\u001b[31m界\u001b[32ma\u001b[3GZ", "界Z"],
39
+ ["e\u001b[31m\u0301a\u001b[1GZ", "Za"],
40
+ ])("overwrites Unicode at terminal columns: %j", (input, expected) => {
41
+ expect(terminalOutputPlainText(feed([input]).text)).toBe(expected);
42
+ for (let cut = 1; cut < input.length; cut += 1) {
43
+ const source = feed([input.slice(0, cut)]);
44
+ const chunk = input.slice(cut);
45
+ expect(terminalOutputPlainText(appendTerminalOutput(source, chunk).text)).toBe(expected);
46
+ expect(terminalOutputPlainText(feed([terminalOutputResumeText(source), chunk]).text)).toBe(
47
+ expected,
48
+ );
49
+ }
50
+ });
51
+
52
it("redraws several lines in place when the cursor moves up", () => {
53
const frames = [
54
"layer a: 10%\nlayer b: 10%\n",
@@ -60,20 +85,38 @@ describe("appendTerminalOutput", () => {
85
expect(feed(["abcdefgh\nijk\nlmnop"], 8).text).toBe("jk\nlmnop");
86
});
87
63
- it("stays bounded across many appends of huge output, including one endless line", () => {
64
- let state = EMPTY_TERMINAL_OUTPUT;
65
- for (let index = 0; index < 5_000; index += 1) {
66
- state = appendTerminalOutput(state, "y\n".repeat(200), 4_096);
67
- }
68
- expect(state.text.length).toBeLessThanOrEqual(4_096);
69
- expect(state.truncated).toBe(true);
70
- for (let index = 0; index < 1_000; index += 1) {
71
- state = appendTerminalOutput(state, ".".repeat(100), 4_096);
72
- }
73
- expect(state.text.length).toBeLessThanOrEqual(4_096);
74
- expect(state.text.endsWith(".")).toBe(true);
88
+ it("keeps Unicode tails bounded without shifting saved cursor columns", () => {
89
+ const source = feed(["界界e\u0301\u001b7ab"], 4);
90
+ expect(source.text).toBe("e\u0301ab");
91
+ expect(source.screen.col).toBe(3);
92
+ expect(source.screen.saved?.col).toBe(1);
93
+ expect(appendTerminalOutput(source, "\u001b8Z", 4).text).toBe("e\u0301Zb");
94
+ expect(feed([terminalOutputResumeText(source), "\u001b8Z"], 4).text).toBe("e\u0301Zb");
95
+ expect(feed(["𐐀a"], 2).text).toBe("a");
96
+ expect(feed(["e\u0301ab"], 3).text).toBe("ab");
97
+ const wide = feed(["界".repeat(40_000), "\u001b7"]);
98
+ expect(feed([terminalOutputResumeText(wide)]).screen.col).toBe(wide.screen.col);
99
});
100
101
+ it.each([".", "界", "😀", "\u001b[31m界\u001b[0m"])(
102
+ "stays bounded across many appends of huge output, including one endless line of %s",
103
+ (character) => {
104
+ let state = EMPTY_TERMINAL_OUTPUT;
105
+ for (let index = 0; index < 5_000; index += 1) {
106
+ state = appendTerminalOutput(state, "y\n".repeat(200), 4_096);
107
+ }
108
+ expect(state.text.length).toBeLessThanOrEqual(4_096);
109
+ expect(state.truncated).toBe(true);
110
+ for (let index = 0; index < 1_000; index += 1) {
111
+ state = appendTerminalOutput(state, character.repeat(100), 4_096);
112
+ }
113
+ expect(terminalOutputPlainText(state.text).length).toBeLessThanOrEqual(4_096);
114
+ expect(terminalOutputPlainText(state.text).endsWith(terminalOutputPlainText(character))).toBe(
115
+ true,
116
+ );
117
+ },
118
+ );
119
+
120
it("hides a long string sequence's payload however it is split into chunks", () => {
121
const input = `a\u001b]0;${"t".repeat(100)}\u0007b`;
122
expect(feed([input]).text).toBe("ab");
@@ -161,6 +204,9 @@ describe("appendTerminalOutput edge cases from review", () => {
204
);
205
expect(windowChars).toBeLessThanOrEqual(4_096);
206
expect(state.text.length).toBeLessThanOrEqual(4_096);
207
+ for (const prefix of ["😀", "e\u0301"]) {
208
+ expect(feed([`a\n${prefix}bx\nZ\u001b[2A`], 4).text).toBe("bx\nZ");
209
+ }
210
});
211
});
212
@@ -228,12 +274,12 @@ describe("terminalOutputResumeText", () => {
274
275
describe("terminalOutputResumeText with a saved cursor or trimmed output", () => {
276
it("restores a cursor saved before the resync", () => {
231
- const source = feed(["abc\u001b7def"]);
277
+ const source = feed(["界e\u0301\u001b7def"]);
278
const viewer = appendTerminalOutput(
279
appendTerminalOutput(EMPTY_TERMINAL_OUTPUT, terminalOutputResumeText(source)),
280
"\u001b8X",
281
);
236
- expect(viewer.text).toBe("abcXef");
282
+ expect(viewer.text).toBe("界e\u0301Xef");
283
});
284
285
it("stays on the right line when early output was trimmed away", () => {
packages/shared/src/terminalOutput.ts
+115
-45
@@ -1,10 +1,12 @@
1
+import { eastAsianWidth } from "get-east-asian-width";
2
+
3
/** Server/client folds must agree: only the last LIVE_LINES are cursor-addressable, with no wrapping.
4
* SGR resets at line boundaries for safe tail cuts; unfinished escapes carry through pending. */
5
export interface TerminalOutputState {
6
/** Fixed lines followed by the live window; contains only SGR escapes. */
7
readonly text: string;
8
readonly truncated: boolean;
7
- /** An unfinished escape sequence awaiting the next chunk. */
9
+ /** An unfinished escape sequence or high surrogate awaiting the next chunk. */
10
readonly pending: string;
11
readonly screen: TerminalScreen;
12
}
@@ -60,22 +62,71 @@ function lineLength(line: Line): number {
62
return length;
63
}
64
65
+const lineWidths = new WeakMap<Line, number>();
66
+const ZERO_WIDTH = /[\p{Mark}\p{Default_Ignorable_Code_Point}]/u;
67
+const ASCII = /^[\x00-\x7f]*$/u;
68
+
69
+function characterWidth(character: string): number {
70
+ return ZERO_WIDTH.test(character) ? 0 : eastAsianWidth(character.codePointAt(0)!);
71
+}
72
+
73
+function textWidth(text: string): number {
74
+ if (ASCII.test(text)) return text.length;
75
+ let width = 0;
76
+ for (const character of text) width += characterWidth(character);
77
+ return width;
78
+}
79
+
80
+function lineWidth(line: Line): number {
81
+ const cached = lineWidths.get(line);
82
+ if (cached !== undefined) return cached;
83
+ let width = 0;
84
+ for (const run of line) width += textWidth(run.text);
85
+ lineWidths.set(line, width);
86
+ return width;
87
+}
88
+
89
// Strip oldest styles to bound work on color-per-character output.
90
const MAX_LINE_RUNS = 2_048;
91
92
function sliceLine(line: Line, start: number, end: number): Array<Run> {
93
const out: Array<Run> = [];
68
- let offset = 0;
94
+ let col = 0;
95
+ let base = 0;
96
+ let baseEnd = 0;
97
for (const run of line) {
70
- const runEnd = offset + run.text.length;
71
- if (runEnd > start && offset < end) {
72
- out.push({
73
- pen: run.pen,
74
- text: run.text.slice(Math.max(0, start - offset), Math.min(run.text.length, end - offset)),
75
- });
98
+ if (ASCII.test(run.text)) {
99
+ const runEnd = col + run.text.length;
100
+ if (runEnd > start && col < end) {
101
+ pushRun(out, {
102
+ pen: run.pen,
103
+ text: run.text.slice(Math.max(0, start - col), Math.min(run.text.length, end - col)),
104
+ });
105
+ }
106
+ base = runEnd - 1;
107
+ baseEnd = runEnd;
108
+ col = runEnd;
109
+ continue;
110
+ }
111
+ let text = "";
112
+ for (const character of run.text) {
113
+ const width = characterWidth(character);
114
+ if (width > 0) {
115
+ base = col;
116
+ baseEnd = col + width;
117
+ }
118
+ if (width === 0) {
119
+ if (base >= start && base < end && baseEnd <= end) text += character;
120
+ } else if (col < end && col + width > start) {
121
+ // Overwriting either half of a wide glyph clears that glyph's other cell.
122
+ text +=
123
+ col >= start && col + width <= end
124
+ ? character
125
+ : " ".repeat(Math.min(end, col + width) - Math.max(start, col));
126
+ }
127
+ col += width;
128
}
77
- offset = runEnd;
78
- if (offset >= end) break;
129
+ pushRun(out, { pen: run.pen, text });
130
}
131
return out;
132
}
@@ -91,40 +142,58 @@ function pushRun(runs: Array<Run>, run: Run): void {
142
}
143
144
/** Mutates only lines owned by this feed. */
94
-function trimLineStart(runs: Array<Run>, count: number): void {
145
+function trimLineStart(runs: Array<Run>, count: number): number {
146
+ const width = lineWidth(runs);
147
+ const length = lineLength(runs);
148
+ let removedWidth = 0;
149
+ let removedUnits = 0;
150
let dropped = 0;
151
let remaining = count;
97
- while (remaining > 0 && dropped < runs.length) {
152
+ while (dropped < runs.length) {
153
const run = runs[dropped]!;
99
- if (run.text.length <= remaining) {
100
- remaining -= run.text.length;
101
- dropped += 1;
102
- } else {
103
- runs[dropped] = { pen: run.pen, text: run.text.slice(remaining) };
104
- remaining = 0;
154
+ let cut = Math.min(remaining, run.text.length);
155
+ // A retained tail must start at a complete character, with no detached marks.
156
+ while (cut < run.text.length) {
157
+ const code = run.text.charCodeAt(cut);
158
+ const character = String.fromCodePoint(run.text.codePointAt(cut)!);
159
+ if (!(code >= 0xdc00 && code <= 0xdfff) && characterWidth(character) !== 0) break;
160
+ cut += character.length;
161
+ }
162
+ removedWidth += textWidth(run.text.slice(0, cut));
163
+ removedUnits += cut;
164
+ remaining = Math.max(0, remaining - cut);
165
+ if (cut < run.text.length) {
166
+ runs[dropped] = { pen: run.pen, text: run.text.slice(cut) };
167
+ break;
168
}
169
+ dropped += 1;
170
}
171
if (dropped > 0) runs.splice(0, dropped);
172
+ lineLengths.set(runs, length - removedUnits);
173
+ lineWidths.set(runs, width - removedWidth);
174
+ return removedWidth;
175
}
176
177
function writeLine(line: Line, col: number, text: string, pen: string): Line {
111
- const length = lineLength(line);
178
+ const length = lineWidth(line);
179
if (col === length) {
180
const runs = line.slice();
181
pushRun(runs, { pen, text });
182
+ lineWidths.set(runs, length + textWidth(text));
183
+ lineLengths.set(runs, lineLength(line) + text.length);
184
return runs;
185
}
186
const runs: Array<Run> = [];
187
for (const run of sliceLine(line, 0, Math.min(col, length))) pushRun(runs, run);
188
if (col > length) pushRun(runs, { pen: "", text: " ".repeat(col - length) });
189
pushRun(runs, { pen, text });
121
- for (const run of sliceLine(line, col + text.length, length)) pushRun(runs, run);
190
+ for (const run of sliceLine(line, col + textWidth(text), length)) pushRun(runs, run);
191
return runs;
192
}
193
194
/** Erase in line: 0 cursor to end, 1 start to cursor, 2 whole line. */
195
function eraseLine(line: Line, col: number, mode: number): Line {
127
- const length = lineLength(line);
196
+ const length = lineWidth(line);
197
if (mode === 0) return sliceLine(line, 0, col);
198
if (mode === 1) {
199
const runs: Array<Run> = [];
@@ -358,15 +427,18 @@ class Terminal {
427
428
write(text: string): void {
429
let line = this.lines[this.row]!;
361
- const length = lineLength(line);
430
+ const length = lineWidth(line);
431
+ const width = textWidth(text);
432
if (this.col === length && this.owned.has(line)) {
433
+ const units = lineLength(line);
434
pushRun(line as Array<Run>, { pen: this.pen, text });
364
- lineLengths.set(line, length + text.length);
435
+ lineWidths.set(line, length + width);
436
+ lineLengths.set(line, units + text.length);
437
} else {
438
line = writeLine(line, this.col, text, this.pen);
439
this.owned.add(line);
440
}
369
- this.col += text.length;
441
+ this.col += width;
442
if (line.length > MAX_LINE_RUNS) {
443
// Flatten old runs so later trimming stays cheap.
444
const keep = line.length - MAX_LINE_RUNS / 8;
@@ -374,32 +446,23 @@ class Terminal {
446
.slice(0, keep)
447
.map((run) => run.text)
448
.join("");
449
+ const columns = lineWidth(line);
450
+ const units = lineLength(line);
451
line = [{ pen: "", text: plain }, ...line.slice(keep)];
452
+ lineWidths.set(line, columns);
453
+ lineLengths.set(line, units);
454
this.owned.add(line);
455
}
380
- // Trim each write to maxChars columns so results do not depend on chunk boundaries.
381
- let current = lineLength(line);
456
+ // The output budget is UTF-16 units; cursor positions are terminal columns.
457
+ const current = lineLength(line);
458
if (current > this.maxChars) {
383
- let removed = current - this.maxChars;
384
- if (!this.owned.has(line)) {
385
- line = line.slice();
386
- this.owned.add(line);
387
- }
388
- trimLineStart(line as Array<Run>, removed);
389
- // Never start on a lone low surrogate.
390
- const first = line[0]?.text.charCodeAt(0) ?? 0;
391
- if (first >= 0xdc00 && first <= 0xdfff) {
392
- trimLineStart(line as Array<Run>, 1);
393
- removed += 1;
394
- }
395
- current -= removed;
459
+ const removed = trimLineStart(line as Array<Run>, current - this.maxChars);
460
this.dropped = true;
461
this.col = Math.max(0, this.col - removed);
462
if (this.saved !== null && this.saved.row === this.row) {
463
this.saved = { ...this.saved, col: Math.max(0, this.saved.col - removed) };
464
}
465
}
402
- lineLengths.set(line, current);
466
this.lines[this.row] = line;
467
}
468
@@ -432,7 +495,7 @@ class Terminal {
495
moveTo(row: number, col: number): void {
496
this.row = Math.max(0, Math.min(this.lines.length - 1, row));
497
// A huge column (ESC[999999999C) would otherwise pad a line past any string limit.
435
- this.col = Math.max(0, Math.min(TERMINAL_OUTPUT_TAIL_CHARS, col));
498
+ this.col = Math.max(0, Math.min(2 * TERMINAL_OUTPUT_TAIL_CHARS, col));
499
}
500
501
csi(params: string, final: string): void {
@@ -547,6 +610,11 @@ class Terminal {
610
printable = index + 1;
611
}
612
}
613
+ const last = input.charCodeAt(input.length - 1);
614
+ if (last >= 0xd800 && last <= 0xdbff) {
615
+ flush(input.length - 1);
616
+ return input.slice(-1);
617
+ }
618
flush(input.length);
619
return "";
620
}
@@ -639,15 +707,17 @@ export function appendTerminalOutput(
707
const keep = Math.max(0, Math.min(length, budget));
708
if (keep < length) {
709
truncated = true;
642
- terminal.lines[row] = sliceLine(line, length - keep, length);
710
+ const runs = line.slice();
711
+ const removed = trimLineStart(runs, length - keep);
712
+ terminal.lines[row] = runs;
713
if (terminal.saved !== null && terminal.saved.row === row) {
714
terminal.saved = {
715
...terminal.saved,
646
- col: Math.max(0, terminal.saved.col - (length - keep)),
716
+ col: Math.max(0, terminal.saved.col - removed),
717
};
718
}
719
}
650
- budget -= keep;
720
+ budget -= lineLength(terminal.lines[row]!);
721
}
722
}
723
if (terminal.dropped) truncated = true;
@@ -693,7 +763,7 @@ export function terminalOutputResumeText(state: TerminalOutputState): string {
763
if (
764
screen.saved !== null ||
765
screen.row < lastRow ||
696
- screen.col !== lineLength(screen.lines[screen.row]!)
766
+ screen.col !== lineWidth(screen.lines[screen.row]!)
767
) {
768
suffix += goTo(screen.row, screen.col);
769
}
pnpm-lock.yaml
+3
@@ -2100,6 +2100,9 @@ importers:
2100
ffi-rs:
2101
specifier: 1.3.2
2102
version: 1.3.2
2103
+ get-east-asian-width:
2104
+ specifier: ^1.6.0
2105
+ version: 1.6.0
2106
mdast-util-directive:
2107
specifier: 'catalog:'
2108
version: 3.1.0(supports-color@8.1.1)