Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions apps/web/src/terminal/ghostty/renderer.browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { describe, expect, it } from "vite-plus/test";

import type { GhosttyCell, GhosttySnapshot } from "./core";
import { renderGhosttySnapshot } from "./renderer";

const background = { r: 255, g: 255, b: 255 };

const blank: GhosttyCell = {
text: "",
wide: 0,
foreground: { r: 0, g: 0, b: 0 },
background,
bold: false,
italic: false,
invisible: false,
strikethrough: false,
overline: false,
underline: false,
selected: false,
};

/** Counts device pixels that are not the terminal background. */
function pixelsOffBackground(context: CanvasRenderingContext2D): number {
const { data } = context.getImageData(0, 0, context.canvas.width, context.canvas.height);
let count = 0;
for (let index = 0; index < data.length; index += 4) {
if (data[index] !== 255 || data[index + 1] !== 255 || data[index + 2] !== 255) count += 1;
}
return count;
}

/** A 40 by 60 CSS pixel canvas drawn at the given device pixel ratio. */
function scaledContext(scale: number): CanvasRenderingContext2D {
const canvas = document.createElement("canvas");
canvas.width = Math.round(40 * scale);
canvas.height = Math.round(60 * scale);
const context = canvas.getContext("2d");
if (!context) throw new Error("2D canvas unavailable");
context.setTransform(scale, 0, 0, scale, 0, 0);
return context;
}

const layout = {
metrics: { width: 7.8, height: 17, baseline: 13 },
fontSize: 13,
fontFamily: "monospace",
padding: 4,
originY: 4,
};

describe.each([1.25, 2.2])("renderGhosttySnapshot at a %sx device pixel ratio", (scale) => {
it.each([
["bar", 0],
["block", 1],
["underline", 2],
["hollow block", 3],
])("erases a %s cursor completely when it blinks off", (_name, cursorStyle) => {
const context = scaledContext(scale);
const snapshot: GhosttySnapshot = {
cols: 3,
rows: 3,
foreground: { r: 0, g: 0, b: 0 },
background,
cursor: { r: 0, g: 0, b: 0 },
cursorX: 2,
cursorY: 1,
cursorVisible: true,
cursorBlinking: true,
cursorStyle,
dirtyRows: new Set(),
rowData: [0, 1, 2].map(() => ({
cells: [blank, blank, blank],
text: "",
isWrapContinuation: false,
wrapsToNext: false,
})),
};
const options = { ...layout, context, snapshot };

renderGhosttySnapshot({ ...options, forceFull: true, cursorOn: true });
expect(pixelsOffBackground(context)).toBeGreaterThan(0);

renderGhosttySnapshot({ ...options, forceFull: false, cursorOn: false, previousCursorY: 1 });
expect(pixelsOffBackground(context)).toBe(0);
});

it("erases a Powerline separator completely when only its row repaints", () => {
const context = scaledContext(scale);
const firstRow = (cell: GhosttyCell): GhosttySnapshot => ({
cols: 3,
rows: 3,
foreground: { r: 0, g: 0, b: 0 },
background,
cursor: { r: 0, g: 0, b: 0 },
cursorX: -1,
cursorY: -1,
cursorVisible: false,
cursorBlinking: false,
cursorStyle: 1,
dirtyRows: new Set([0]),
rowData: [cell, blank, blank].map((middle) => ({
cells: [blank, middle, blank],
text: "",
isWrapContinuation: false,
wrapsToNext: false,
})),
});
const options = { ...layout, context, forceFull: false, cursorOn: false };

renderGhosttySnapshot({ ...options, snapshot: firstRow(blank), forceFull: true });
renderGhosttySnapshot({ ...options, snapshot: firstRow({ ...blank, text: "\ue0b0" }) });
expect(pixelsOffBackground(context)).toBeGreaterThan(0);

renderGhosttySnapshot({ ...options, snapshot: firstRow(blank) });
expect(pixelsOffBackground(context)).toBe(0);
});
});
44 changes: 27 additions & 17 deletions apps/web/src/terminal/ghostty/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ function fillSolidPowerlineSeparator(
text: string,
left: number,
top: number,
metrics: GhosttyCellMetrics,
width: number,
height: number,
): void {
const right = left + metrics.width;
const right = left + width;
const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left];
context.beginPath();
context.moveTo(base, top);
context.lineTo(point, top + metrics.height / 2);
context.lineTo(base, top + metrics.height);
context.lineTo(point, top + height / 2);
context.lineTo(base, top + height);
context.closePath();
context.fill();
}
Expand Down Expand Up @@ -145,6 +146,12 @@ export function renderGhosttySnapshot(options: {
const selectionBackground = options.selectionBackground ?? DEFAULT_SELECTION_BACKGROUND;
const hoveredLinkRange = options.hoveredLinkRange ?? null;
const originY = options.originY ?? padding;
// Rows start on whole device pixels and clear the full canvas width, so repainting
// a row replaces every pixel the last frame drew in it, including a cursor's edges.
const transform = context.getTransform();
const rowTop = (row: number) =>
Math.round((originY + row * metrics.height) * transform.d) / transform.d;
const rowWidth = context.canvas.width / transform.a;
const rowsToDraw = forceFull
? Array.from({ length: snapshot.rows }, (_, index) => index)
: [...snapshot.dirtyRows];
Expand Down Expand Up @@ -172,10 +179,11 @@ export function renderGhosttySnapshot(options: {
for (const rowIndex of rowsToDraw) {
const row = snapshot.rowData[rowIndex];
if (!row) continue;
const top = originY + rowIndex * metrics.height;
const top = rowTop(rowIndex);
const height = rowTop(rowIndex + 1) - top;

context.fillStyle = cssColor(snapshot.background);
context.fillRect(padding, top, snapshot.cols * metrics.width, metrics.height);
context.fillRect(0, top, rowWidth, height);

let backgroundStart = 0;
while (backgroundStart < row.cells.length) {
Expand All @@ -198,11 +206,11 @@ export function renderGhosttySnapshot(options: {
const width = (backgroundEnd - backgroundStart) * metrics.width;
if (!ghosttyColorsEqual(first.background, snapshot.background)) {
context.fillStyle = cssColor(first.background);
context.fillRect(left, top, width, metrics.height);
context.fillRect(left, top, width, height);
}
if (first.selected) {
context.fillStyle = selectionBackground;
context.fillRect(left, top, width, metrics.height);
context.fillRect(left, top, width, height);
}
}
backgroundStart = backgroundEnd;
Expand All @@ -224,7 +232,8 @@ export function renderGhosttySnapshot(options: {
first.text,
padding + runStart * metrics.width,
top,
metrics,
metrics.width,
height,
);
}
runStart += 1;
Expand All @@ -242,7 +251,7 @@ export function renderGhosttySnapshot(options: {
padding + runStart * metrics.width,
top,
(runEnd - runStart) * metrics.width,
metrics.height,
height,
);
context.clip();
context.font = fontForCell(first, fontSize, fontFamily);
Expand Down Expand Up @@ -283,25 +292,26 @@ export function renderGhosttySnapshot(options: {

if (cursorOn && snapshot.cursorVisible && snapshot.cursorX >= 0 && snapshot.cursorY >= 0) {
const left = padding + snapshot.cursorX * metrics.width;
const top = originY + snapshot.cursorY * metrics.height;
const top = rowTop(snapshot.cursorY);
const height = rowTop(snapshot.cursorY + 1) - top;
context.fillStyle = cssColor(snapshot.cursor);
if (!focused) {
context.strokeStyle = cssColor(snapshot.cursor);
context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, height - 1);
} else if (snapshot.cursorStyle === 0) {
context.fillRect(left, top, 2, metrics.height);
context.fillRect(left, top, 2, height);
} else if (snapshot.cursorStyle === 2) {
context.fillRect(left, top + metrics.height - 2, metrics.width, 2);
context.fillRect(left, top + height - 2, metrics.width, 2);
} else if (snapshot.cursorStyle === 3) {
context.strokeStyle = cssColor(snapshot.cursor);
context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, metrics.height - 1);
context.strokeRect(left + 0.5, top + 0.5, metrics.width - 1, height - 1);
} else {
context.fillRect(left, top, metrics.width, metrics.height);
context.fillRect(left, top, metrics.width, height);
const cell = snapshot.rowData[snapshot.cursorY]?.cells[snapshot.cursorX];
if (cell?.text) {
context.fillStyle = cssColor(snapshot.background);
if (isSolidPowerlineSeparator(cell.text)) {
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics);
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height);
} else {
context.font = fontForCell(cell, fontSize, fontFamily);
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/terminal/ghostty/surface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe("GhosttyTerminalSurface visibility", () => {
canvas,
beginPath() {},
clip() {},
getTransform: () => ({ a: 1, d: 1 }),
rect() {},
resetTransform() {},
restore() {},
Expand Down
Loading