Skip to content

Commit 4c68fdc

Browse files
fix(web): the terminal cursor no longer leaves faint lines when it blinks off (#21641)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 0629735 commit 4c68fdc

3 files changed

Lines changed: 145 additions & 17 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ function fillSolidPowerlineSeparator(
4747
text: string,
4848
left: number,
4949
top: number,
50-
metrics: GhosttyCellMetrics,
50+
width: number,
51+
height: number,
5152
): void {
52-
const right = left + metrics.width;
53+
const right = left + width;
5354
const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left];
5455
context.beginPath();
5556
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);
5859
context.closePath();
5960
context.fill();
6061
}
@@ -145,6 +146,12 @@ export function renderGhosttySnapshot(options: {
145146
const selectionBackground = options.selectionBackground ?? DEFAULT_SELECTION_BACKGROUND;
146147
const hoveredLinkRange = options.hoveredLinkRange ?? null;
147148
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;
148155
const rowsToDraw = forceFull
149156
? Array.from({ length: snapshot.rows }, (_, index) => index)
150157
: [...snapshot.dirtyRows];
@@ -172,10 +179,11 @@ export function renderGhosttySnapshot(options: {
172179
for (const rowIndex of rowsToDraw) {
173180
const row = snapshot.rowData[rowIndex];
174181
if (!row) continue;
175-
const top = originY + rowIndex * metrics.height;
182+
const top = rowTop(rowIndex);
183+
const height = rowTop(rowIndex + 1) - top;
176184

177185
context.fillStyle = cssColor(snapshot.background);
178-
context.fillRect(padding, top, snapshot.cols * metrics.width, metrics.height);
186+
context.fillRect(0, top, rowWidth, height);
179187

180188
let backgroundStart = 0;
181189
while (backgroundStart < row.cells.length) {
@@ -198,11 +206,11 @@ export function renderGhosttySnapshot(options: {
198206
const width = (backgroundEnd - backgroundStart) * metrics.width;
199207
if (!ghosttyColorsEqual(first.background, snapshot.background)) {
200208
context.fillStyle = cssColor(first.background);
201-
context.fillRect(left, top, width, metrics.height);
209+
context.fillRect(left, top, width, height);
202210
}
203211
if (first.selected) {
204212
context.fillStyle = selectionBackground;
205-
context.fillRect(left, top, width, metrics.height);
213+
context.fillRect(left, top, width, height);
206214
}
207215
}
208216
backgroundStart = backgroundEnd;
@@ -224,7 +232,8 @@ export function renderGhosttySnapshot(options: {
224232
first.text,
225233
padding + runStart * metrics.width,
226234
top,
227-
metrics,
235+
metrics.width,
236+
height,
228237
);
229238
}
230239
runStart += 1;
@@ -242,7 +251,7 @@ export function renderGhosttySnapshot(options: {
242251
padding + runStart * metrics.width,
243252
top,
244253
(runEnd - runStart) * metrics.width,
245-
metrics.height,
254+
height,
246255
);
247256
context.clip();
248257
context.font = fontForCell(first, fontSize, fontFamily);
@@ -283,25 +292,26 @@ export function renderGhosttySnapshot(options: {
283292

284293
if (cursorOn && snapshot.cursorVisible && snapshot.cursorX >= 0 && snapshot.cursorY >= 0) {
285294
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;
287297
context.fillStyle = cssColor(snapshot.cursor);
288298
if (!focused) {
289299
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);
291301
} else if (snapshot.cursorStyle === 0) {
292-
context.fillRect(left, top, 2, metrics.height);
302+
context.fillRect(left, top, 2, height);
293303
} 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);
295305
} else if (snapshot.cursorStyle === 3) {
296306
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);
298308
} else {
299-
context.fillRect(left, top, metrics.width, metrics.height);
309+
context.fillRect(left, top, metrics.width, height);
300310
const cell = snapshot.rowData[snapshot.cursorY]?.cells[snapshot.cursorX];
301311
if (cell?.text) {
302312
context.fillStyle = cssColor(snapshot.background);
303313
if (isSolidPowerlineSeparator(cell.text)) {
304-
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics);
314+
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height);
305315
} else {
306316
context.font = fontForCell(cell, fontSize, fontFamily);
307317
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);

‎apps/web/src/terminal/ghostty/surface.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ describe("GhosttyTerminalSurface visibility", () => {
127127
canvas,
128128
beginPath() {},
129129
clip() {},
130+
getTransform: () => ({ a: 1, d: 1 }),
130131
rect() {},
131132
resetTransform() {},
132133
restore() {},

0 commit comments

Comments
 (0)