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, 23:30 UTC
fix(web): the terminal cursor no longer leaves faint lines when it blinks off (#21641)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
tiliakoos committed
Nov 11, 2026 at 12:14 UTC
4c68fdcaa9983595b4fe1556842a371b54a6a57b
3 files changed
+145
-17
apps/web/src/terminal/ghostty/renderer.browser.test.ts
new
+117
@@ -0,0 +1,117 @@
1
+import { describe, expect, it } from "vite-plus/test";
2
+
3
+import type { GhosttyCell, GhosttySnapshot } from "./core";
4
+import { renderGhosttySnapshot } from "./renderer";
5
+
6
+const background = { r: 255, g: 255, b: 255 };
7
+
8
+const blank: GhosttyCell = {
9
+ text: "",
10
+ wide: 0,
11
+ foreground: { r: 0, g: 0, b: 0 },
12
+ background,
13
+ bold: false,
14
+ italic: false,
15
+ invisible: false,
16
+ strikethrough: false,
17
+ overline: false,
18
+ underline: false,
19
+ selected: false,
20
+};
21
+
22
+/** Counts device pixels that are not the terminal background. */
23
+function pixelsOffBackground(context: CanvasRenderingContext2D): number {
24
+ const { data } = context.getImageData(0, 0, context.canvas.width, context.canvas.height);
25
+ let count = 0;
26
+ for (let index = 0; index < data.length; index += 4) {
27
+ if (data[index] !== 255 || data[index + 1] !== 255 || data[index + 2] !== 255) count += 1;
28
+ }
29
+ return count;
30
+}
31
+
32
+/** A 40 by 60 CSS pixel canvas drawn at the given device pixel ratio. */
33
+function scaledContext(scale: number): CanvasRenderingContext2D {
34
+ const canvas = document.createElement("canvas");
35
+ canvas.width = Math.round(40 * scale);
36
+ canvas.height = Math.round(60 * scale);
37
+ const context = canvas.getContext("2d");
38
+ if (!context) throw new Error("2D canvas unavailable");
39
+ context.setTransform(scale, 0, 0, scale, 0, 0);
40
+ return context;
41
+}
42
+
43
+const layout = {
44
+ metrics: { width: 7.8, height: 17, baseline: 13 },
45
+ fontSize: 13,
46
+ fontFamily: "monospace",
47
+ padding: 4,
48
+ originY: 4,
49
+};
50
+
51
+describe.each([1.25, 2.2])("renderGhosttySnapshot at a %sx device pixel ratio", (scale) => {
52
+ it.each([
53
+ ["bar", 0],
54
+ ["block", 1],
55
+ ["underline", 2],
56
+ ["hollow block", 3],
57
+ ])("erases a %s cursor completely when it blinks off", (_name, cursorStyle) => {
58
+ const context = scaledContext(scale);
59
+ const snapshot: GhosttySnapshot = {
60
+ cols: 3,
61
+ rows: 3,
62
+ foreground: { r: 0, g: 0, b: 0 },
63
+ background,
64
+ cursor: { r: 0, g: 0, b: 0 },
65
+ cursorX: 2,
66
+ cursorY: 1,
67
+ cursorVisible: true,
68
+ cursorBlinking: true,
69
+ cursorStyle,
70
+ dirtyRows: new Set(),
71
+ rowData: [0, 1, 2].map(() => ({
72
+ cells: [blank, blank, blank],
73
+ text: "",
74
+ isWrapContinuation: false,
75
+ wrapsToNext: false,
76
+ })),
77
+ };
78
+ const options = { ...layout, context, snapshot };
79
+
80
+ renderGhosttySnapshot({ ...options, forceFull: true, cursorOn: true });
81
+ expect(pixelsOffBackground(context)).toBeGreaterThan(0);
82
+
83
+ renderGhosttySnapshot({ ...options, forceFull: false, cursorOn: false, previousCursorY: 1 });
84
+ expect(pixelsOffBackground(context)).toBe(0);
85
+ });
86
+
87
+ it("erases a Powerline separator completely when only its row repaints", () => {
88
+ const context = scaledContext(scale);
89
+ const firstRow = (cell: GhosttyCell): GhosttySnapshot => ({
90
+ cols: 3,
91
+ rows: 3,
92
+ foreground: { r: 0, g: 0, b: 0 },
93
+ background,
94
+ cursor: { r: 0, g: 0, b: 0 },
95
+ cursorX: -1,
96
+ cursorY: -1,
97
+ cursorVisible: false,
98
+ cursorBlinking: false,
99
+ cursorStyle: 1,
100
+ dirtyRows: new Set([0]),
101
+ rowData: [cell, blank, blank].map((middle) => ({
102
+ cells: [blank, middle, blank],
103
+ text: "",
104
+ isWrapContinuation: false,
105
+ wrapsToNext: false,
106
+ })),
107
+ });
108
+ const options = { ...layout, context, forceFull: false, cursorOn: false };
109
+
110
+ renderGhosttySnapshot({ ...options, snapshot: firstRow(blank), forceFull: true });
111
+ renderGhosttySnapshot({ ...options, snapshot: firstRow({ ...blank, text: "\ue0b0" }) });
112
+ expect(pixelsOffBackground(context)).toBeGreaterThan(0);
113
+
114
+ renderGhosttySnapshot({ ...options, snapshot: firstRow(blank) });
115
+ expect(pixelsOffBackground(context)).toBe(0);
116
+ });
117
+});
apps/web/src/terminal/ghostty/renderer.ts
+27
-17
@@ -47,14 +47,15 @@ function fillSolidPowerlineSeparator(
47
text: string,
48
left: number,
49
top: number,
50
- metrics: GhosttyCellMetrics,
50
+ width: number,
51
+ height: number,
52
): void {
52
- const right = left + metrics.width;
53
+ const right = left + width;
54
const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left];
55
context.beginPath();
56
context.moveTo(base, top);
56
- context.lineTo(point, top + metrics.height / 2);
57
- context.lineTo(base, top + metrics.height);
57
+ context.lineTo(point, top + height / 2);
58
+ context.lineTo(base, top + height);
59
context.closePath();
60
context.fill();
61
}
@@ -145,6 +146,12 @@ export function renderGhosttySnapshot(options: {
146
const selectionBackground = options.selectionBackground ?? DEFAULT_SELECTION_BACKGROUND;
147
const hoveredLinkRange = options.hoveredLinkRange ?? null;
148
const originY = options.originY ?? padding;
149
+ // Rows start on whole device pixels and clear the full canvas width, so repainting
150
+ // a row replaces every pixel the last frame drew in it, including a cursor's edges.
151
+ const transform = context.getTransform();
152
+ const rowTop = (row: number) =>
153
+ Math.round((originY + row * metrics.height) * transform.d) / transform.d;
154
+ const rowWidth = context.canvas.width / transform.a;
155
const rowsToDraw = forceFull
156
? Array.from({ length: snapshot.rows }, (_, index) => index)
157
: [...snapshot.dirtyRows];
@@ -172,10 +179,11 @@ export function renderGhosttySnapshot(options: {
179
for (const rowIndex of rowsToDraw) {
180
const row = snapshot.rowData[rowIndex];
181
if (!row) continue;
175
- const top = originY + rowIndex * metrics.height;
182
+ const top = rowTop(rowIndex);
183
+ const height = rowTop(rowIndex + 1) - top;
184
185
context.fillStyle = cssColor(snapshot.background);
178
- context.fillRect(padding, top, snapshot.cols * metrics.width, metrics.height);
186
+ context.fillRect(0, top, rowWidth, height);
187
188
let backgroundStart = 0;
189
while (backgroundStart < row.cells.length) {
@@ -198,11 +206,11 @@ export function renderGhosttySnapshot(options: {
206
const width = (backgroundEnd - backgroundStart) * metrics.width;
207
if (!ghosttyColorsEqual(first.background, snapshot.background)) {
208
context.fillStyle = cssColor(first.background);
201
- context.fillRect(left, top, width, metrics.height);
209
+ context.fillRect(left, top, width, height);
210
}
211
if (first.selected) {
212
context.fillStyle = selectionBackground;
205
- context.fillRect(left, top, width, metrics.height);
213
+ context.fillRect(left, top, width, height);
214
}
215
}
216
backgroundStart = backgroundEnd;
@@ -224,7 +232,8 @@ export function renderGhosttySnapshot(options: {
232
first.text,
233
padding + runStart * metrics.width,
234
top,
227
- metrics,
235
+ metrics.width,
236
+ height,
237
);
238
}
239
runStart += 1;
@@ -242,7 +251,7 @@ export function renderGhosttySnapshot(options: {
251
padding + runStart * metrics.width,
252
top,
253
(runEnd - runStart) * metrics.width,
245
- metrics.height,
254
+ height,
255
);
256
context.clip();
257
context.font = fontForCell(first, fontSize, fontFamily);
@@ -283,25 +292,26 @@ export function renderGhosttySnapshot(options: {
292
293
if (cursorOn && snapshot.cursorVisible && snapshot.cursorX >= 0 && snapshot.cursorY >= 0) {
294
const left = padding + snapshot.cursorX * metrics.width;
286
- const top = originY + snapshot.cursorY * metrics.height;
295
+ const top = rowTop(snapshot.cursorY);
296
+ const height = rowTop(snapshot.cursorY + 1) - top;
297
context.fillStyle = cssColor(snapshot.cursor);
298
if (!focused) {
299
context.strokeStyle = cssColor(snapshot.cursor);
290
- context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
300
+ context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, height - 1);
301
} else if (snapshot.cursorStyle === 0) {
292
- context.fillRect(left, top, 2, metrics.height);
302
+ context.fillRect(left, top, 2, height);
303
} else if (snapshot.cursorStyle === 2) {
294
- context.fillRect(left, top + metrics.height - 2, metrics.width, 2);
304
+ context.fillRect(left, top + height - 2, metrics.width, 2);
305
} else if (snapshot.cursorStyle === 3) {
306
context.strokeStyle = cssColor(snapshot.cursor);
297
- context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
307
+ context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, height - 1);
308
} else {
299
- context.fillRect(left, top, metrics.width, metrics.height);
309
+ context.fillRect(left, top, metrics.width, height);
310
const cell = snapshot.rowData[snapshot.cursorY]?.cells[snapshot.cursorX];
311
if (cell?.text) {
312
context.fillStyle = cssColor(snapshot.background);
313
if (isSolidPowerlineSeparator(cell.text)) {
304
- fillSolidPowerlineSeparator(context, cell.text, left, top, metrics);
314
+ fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height);
315
} else {
316
context.font = fontForCell(cell, fontSize, fontFamily);
317
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);
apps/web/src/terminal/ghostty/surface.test.ts
+1
@@ -127,6 +127,7 @@ describe("GhosttyTerminalSurface visibility", () => {
127
canvas,
128
beginPath() {},
129
clip() {},
130
+ getTransform: () => ({ a: 1, d: 1 }),
131
rect() {},
132
resetTransform() {},
133
restore() {},