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
39 changes: 39 additions & 0 deletions apps/web/src/terminal/ghostty/renderer.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,43 @@ describe.each([1.25, 2.2])("renderGhosttySnapshot at a %sx device pixel ratio",
expect(isDarkAt(context, x.outside, 12.5)).toBe(true);
expect(isDarkAt(context, x.flat, 12.5)).toBe(false);
});

// Cell 1 spans x 11.8 to 19.6 and row 0 spans y 4 to 21 in CSS pixels.
it.each([
["lower left", "\ue0b8", { filled: [12.3, 20.5], empty: [19.1, 4.5] }],
["lower right", "\ue0ba", { filled: [19.1, 20.5], empty: [12.3, 4.5] }],
["upper left", "\ue0bc", { filled: [12.3, 4.5], empty: [19.1, 20.5] }],
["upper right", "\ue0be", { filled: [19.1, 4.5], empty: [12.3, 20.5] }],
])("fills the %s slant into its cell corner", (_corner, text, point) => {
const context = scaledContext(scale);
renderGhosttySnapshot({
...layout,
context,
snapshot: {
cols: 3,
rows: 1,
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: [
{
cells: [blank, { ...blank, text }, blank],
text: "",
isWrapContinuation: false,
wrapsToNext: false,
},
],
},
forceFull: true,
cursorOn: false,
});
expect(isDarkAt(context, point.filled[0]!, point.filled[1]!)).toBe(true);
expect(isDarkAt(context, point.empty[0]!, point.empty[1]!)).toBe(false);
});
});
131 changes: 88 additions & 43 deletions apps/web/src/terminal/ghostty/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,33 @@ export interface GhosttyCellRange {
}

const DEFAULT_SELECTION_BACKGROUND = "rgba(72, 122, 191, 0.35)";
const POWERLINE_SOLID_RIGHT = "\ue0b0";
const POWERLINE_SOLID_LEFT = "\ue0b2";
const POWERLINE_ROUND_RIGHT = "\ue0b4";
const POWERLINE_ROUND_LEFT = "\ue0b6";
const POWERLINE_LINE_WIDTH = 1;

type CellPoint = readonly [x: number, y: number];
type PowerlineShape =
| { readonly kind: "fill"; readonly points: readonly CellPoint[] }
| { readonly kind: "stroke"; readonly points: readonly CellPoint[] }
| { readonly kind: "cap"; readonly flatEdge: "left" | "right"; readonly filled: boolean };

// prettier-ignore
const POWERLINE_SHAPES: ReadonlyMap<string, PowerlineShape> = new Map([
["\ue0b0", { kind: "fill", points: [[0, 0], [1, 0.5], [0, 1]] }],
["\ue0b1", { kind: "stroke", points: [[0, 0], [1, 0.5], [0, 1]] }],
["\ue0b2", { kind: "fill", points: [[1, 0], [0, 0.5], [1, 1]] }],
["\ue0b3", { kind: "stroke", points: [[1, 0], [0, 0.5], [1, 1]] }],
["\ue0b4", { kind: "cap", flatEdge: "left", filled: true }],
["\ue0b5", { kind: "cap", flatEdge: "left", filled: false }],
["\ue0b6", { kind: "cap", flatEdge: "right", filled: true }],
["\ue0b7", { kind: "cap", flatEdge: "right", filled: false }],
["\ue0b8", { kind: "fill", points: [[0, 0], [1, 1], [0, 1]] }],
["\ue0b9", { kind: "stroke", points: [[0, 0], [1, 1]] }],
["\ue0ba", { kind: "fill", points: [[1, 0], [1, 1], [0, 1]] }],
["\ue0bb", { kind: "stroke", points: [[1, 0], [0, 1]] }],
["\ue0bc", { kind: "fill", points: [[0, 0], [1, 0], [0, 1]] }],
["\ue0bd", { kind: "stroke", points: [[1, 0], [0, 1]] }],
["\ue0be", { kind: "fill", points: [[0, 0], [1, 0], [1, 1]] }],
["\ue0bf", { kind: "stroke", points: [[0, 0], [1, 1]] }],
]);

function cssColor(color: GhosttyColor): string {
return `rgb(${color.r}, ${color.g}, ${color.b})`;
Expand All @@ -37,48 +60,59 @@ function sameTextStyle(left: GhosttyCell, right: GhosttyCell): boolean {
);
}

function isSolidPowerlineSeparator(text: string): boolean {
return (
text === POWERLINE_SOLID_RIGHT ||
text === POWERLINE_SOLID_LEFT ||
text === POWERLINE_ROUND_RIGHT ||
text === POWERLINE_ROUND_LEFT
);
}

// Fallback fonts inset or scale these private-use glyphs, leaving a gap where
// the separator should meet the next cell. Fill the triangle or half-ellipse
// from the cell's own edges instead, as native terminals do.
function fillSolidPowerlineSeparator(
// Fonts inset or scale these private-use glyphs, leaving gaps where they should
// meet the neighboring cells. Draw them from the cell's own edges, as Ghostty does.
function drawPowerlineShape(
context: CanvasRenderingContext2D,
text: string,
shape: PowerlineShape,
color: string,
left: number,
top: number,
width: number,
height: number,
seamOverlap: number,
): void {
const right = left + width;
const middle = top + height / 2;
if (text === POWERLINE_ROUND_RIGHT || text === POWERLINE_ROUND_LEFT) {
const [base, start, inward] =
text === POWERLINE_ROUND_RIGHT ? [left, -Math.PI / 2, -1] : [right, Math.PI / 2, 1];
const filled = shape.kind === "fill" || (shape.kind === "cap" && shape.filled);
context.save();
if (!filled) {
context.beginPath();
context.ellipse(base, middle, width, height / 2, 0, start, start + Math.PI);
context.closePath();
// Overlap the segment this cap closes so anti-aliasing at a fractional
// cell edge does not leave a hairline seam.
context.rect(Math.min(base, base + inward * seamOverlap), top, seamOverlap, height);
context.fill();
return;
context.rect(left, top, width, height);
context.clip();
}
const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left];
context.beginPath();
context.moveTo(base, top);
context.lineTo(point, middle);
context.lineTo(base, top + height);
context.closePath();
context.fill();
if (shape.kind === "cap") {
const inset = filled ? 0 : POWERLINE_LINE_WIDTH / 2;
const [base, start] =
shape.flatEdge === "left" ? [left, -Math.PI / 2] : [left + width, Math.PI / 2];
context.ellipse(
base,
top + height / 2,
width - inset,
height / 2 - inset,
0,
start,
start + Math.PI,
);
if (filled) {
context.closePath();
// Overlap the segment this cap closes so anti-aliasing at a fractional
// cell edge does not leave a hairline seam.
const inward = shape.flatEdge === "left" ? -1 : 1;
context.rect(Math.min(base, base + inward * seamOverlap), top, seamOverlap, height);
}
} else {
for (const [x, y] of shape.points) context.lineTo(left + x * width, top + y * height);
}
if (filled) {
context.fillStyle = color;
context.fill();
} else {
context.strokeStyle = color;
context.lineWidth = POWERLINE_LINE_WIDTH;
context.lineCap = "butt";
context.stroke();
}
context.restore();
}

function ghosttyTextRunEnd(
Expand All @@ -94,7 +128,7 @@ function ghosttyTextRunEnd(
end += 1;
continue;
}
if (next.text.length === 0 || isSolidPowerlineSeparator(next.text) || !sameStyle(next)) break;
if (next.text.length === 0 || POWERLINE_SHAPES.has(next.text) || !sameStyle(next)) break;
end += 1;
}
return end;
Expand Down Expand Up @@ -311,12 +345,13 @@ export function renderGhosttySnapshot(options: {
runStart += 1;
continue;
}
if (isSolidPowerlineSeparator(first.text)) {
const powerlineShape = POWERLINE_SHAPES.get(first.text);
if (powerlineShape) {
if (!first.invisible) {
context.fillStyle = cssColor(first.foreground);
fillSolidPowerlineSeparator(
drawPowerlineShape(
context,
first.text,
powerlineShape,
cssColor(first.foreground),
padding + runStart * metrics.width,
top,
metrics.width,
Expand Down Expand Up @@ -403,8 +438,18 @@ export function renderGhosttySnapshot(options: {
context.fillRect(left, top, width, height);
context.fillStyle = cssColor(snapshot.background);
if (cell?.text && !cell.invisible) {
if (isSolidPowerlineSeparator(cell.text)) {
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height, 0);
const powerlineShape = POWERLINE_SHAPES.get(cell.text);
if (powerlineShape) {
drawPowerlineShape(
context,
powerlineShape,
cssColor(snapshot.background),
left,
top,
metrics.width,
height,
0,
);
} else {
context.font = fontForCell(cell, fontSize, fontFamily);
context.textAlign = wide ? "center" : "start";
Expand Down
Loading