Skip to content

Commit 01b815f

Browse files
fix(web): draw rounded powerline caps to fill the terminal cell (#34552)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 330b9f4 commit 01b815f

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

‎apps/web/src/terminal/ghostty/renderer.browser.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ function pixelsOffBackground(context: CanvasRenderingContext2D): number {
2929
return count;
3030
}
3131

32+
/** Whether the device pixel under a CSS pixel position is closer to black than white. */
33+
function isDarkAt(context: CanvasRenderingContext2D, x: number, y: number): boolean {
34+
const { a, d } = context.getTransform();
35+
const { data } = context.getImageData(Math.floor(x * a), Math.floor(y * d), 1, 1);
36+
return data[0]! < 128 && data[1]! < 128 && data[2]! < 128;
37+
}
38+
3239
/** A 40 by 60 CSS pixel canvas drawn at the given device pixel ratio. */
3340
function scaledContext(scale: number): CanvasRenderingContext2D {
3441
const canvas = document.createElement("canvas");
@@ -114,4 +121,51 @@ describe.each([1.25, 2.2])("renderGhosttySnapshot at a %sx device pixel ratio",
114121
renderGhosttySnapshot({ ...options, snapshot: firstRow(blank) });
115122
expect(pixelsOffBackground(context)).toBe(0);
116123
});
124+
125+
// Cell 1 spans x 11.8 to 19.6 and row 0 spans y 4 to 21 in CSS pixels.
126+
it.each([
127+
["right", "\ue0b4", { flat: 12.3, round: 19.1, neighbour: 0, outside: 11.3 }],
128+
["left", "\ue0b6", { flat: 19.1, round: 12.3, neighbour: 2, outside: 20.1 }],
129+
])("fills the rounded %s cap to the cell edges, under a block cursor too", (_side, text, x) => {
130+
const context = scaledContext(scale);
131+
const segment = { ...blank, background: { r: 0, g: 0, b: 0 } };
132+
const cells = [blank, { ...blank, text }, blank];
133+
cells[x.neighbour] = segment;
134+
const snapshot = (cursorVisible: boolean): GhosttySnapshot => ({
135+
cols: 3,
136+
rows: 1,
137+
foreground: { r: 0, g: 0, b: 0 },
138+
background,
139+
cursor: { r: 0, g: 0, b: 0 },
140+
cursorX: 1,
141+
cursorY: 0,
142+
cursorVisible,
143+
cursorBlinking: false,
144+
cursorStyle: 1,
145+
dirtyRows: new Set([0]),
146+
rowData: [{ cells, text: "", isWrapContinuation: false, wrapsToNext: false }],
147+
});
148+
149+
renderGhosttySnapshot({
150+
...layout,
151+
context,
152+
snapshot: snapshot(false),
153+
forceFull: true,
154+
cursorOn: false,
155+
});
156+
expect(isDarkAt(context, x.flat, 4.5)).toBe(true);
157+
expect(isDarkAt(context, x.flat, 20.5)).toBe(true);
158+
expect(isDarkAt(context, x.round, 12.5)).toBe(true);
159+
expect(isDarkAt(context, x.round, 4.5)).toBe(false);
160+
161+
renderGhosttySnapshot({
162+
...layout,
163+
context,
164+
snapshot: snapshot(true),
165+
forceFull: true,
166+
cursorOn: true,
167+
});
168+
expect(isDarkAt(context, x.outside, 12.5)).toBe(true);
169+
expect(isDarkAt(context, x.flat, 12.5)).toBe(false);
170+
});
117171
});

‎apps/web/src/terminal/ghostty/renderer.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export interface GhosttyCellRange {
2020
const DEFAULT_SELECTION_BACKGROUND = "rgba(72, 122, 191, 0.35)";
2121
const POWERLINE_SOLID_RIGHT = "\ue0b0";
2222
const POWERLINE_SOLID_LEFT = "\ue0b2";
23+
const POWERLINE_ROUND_RIGHT = "\ue0b4";
24+
const POWERLINE_ROUND_LEFT = "\ue0b6";
2325

2426
function cssColor(color: GhosttyColor): string {
2527
return `rgb(${color.r}, ${color.g}, ${color.b})`;
@@ -36,25 +38,44 @@ function sameTextStyle(left: GhosttyCell, right: GhosttyCell): boolean {
3638
}
3739

3840
function isSolidPowerlineSeparator(text: string): boolean {
39-
return text === POWERLINE_SOLID_RIGHT || text === POWERLINE_SOLID_LEFT;
41+
return (
42+
text === POWERLINE_SOLID_RIGHT ||
43+
text === POWERLINE_SOLID_LEFT ||
44+
text === POWERLINE_ROUND_RIGHT ||
45+
text === POWERLINE_ROUND_LEFT
46+
);
4047
}
4148

4249
// Fallback fonts inset or scale these private-use glyphs, leaving a gap where
43-
// the separator should meet the next cell. Fill the triangle from the cell's
44-
// own edges instead, as native terminals do.
50+
// the separator should meet the next cell. Fill the triangle or half-ellipse
51+
// from the cell's own edges instead, as native terminals do.
4552
function fillSolidPowerlineSeparator(
4653
context: CanvasRenderingContext2D,
4754
text: string,
4855
left: number,
4956
top: number,
5057
width: number,
5158
height: number,
59+
seamOverlap: number,
5260
): void {
5361
const right = left + width;
62+
const middle = top + height / 2;
63+
if (text === POWERLINE_ROUND_RIGHT || text === POWERLINE_ROUND_LEFT) {
64+
const [base, start, inward] =
65+
text === POWERLINE_ROUND_RIGHT ? [left, -Math.PI / 2, -1] : [right, Math.PI / 2, 1];
66+
context.beginPath();
67+
context.ellipse(base, middle, width, height / 2, 0, start, start + Math.PI);
68+
context.closePath();
69+
// Overlap the segment this cap closes so anti-aliasing at a fractional
70+
// cell edge does not leave a hairline seam.
71+
context.rect(Math.min(base, base + inward * seamOverlap), top, seamOverlap, height);
72+
context.fill();
73+
return;
74+
}
5475
const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left];
5576
context.beginPath();
5677
context.moveTo(base, top);
57-
context.lineTo(point, top + height / 2);
78+
context.lineTo(point, middle);
5879
context.lineTo(base, top + height);
5980
context.closePath();
6081
context.fill();
@@ -234,6 +255,7 @@ export function renderGhosttySnapshot(options: {
234255
top,
235256
metrics.width,
236257
height,
258+
1,
237259
);
238260
}
239261
runStart += 1;
@@ -311,7 +333,7 @@ export function renderGhosttySnapshot(options: {
311333
if (cell?.text) {
312334
context.fillStyle = cssColor(snapshot.background);
313335
if (isSolidPowerlineSeparator(cell.text)) {
314-
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height);
336+
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics.width, height, 0);
315337
} else {
316338
context.font = fontForCell(cell, fontSize, fontFamily);
317339
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);

0 commit comments

Comments
 (0)