Skip to content

Commit 99d9307

Browse files
committed
fix(web): draw rounded powerline caps to fill the terminal cell
The Ghostty canvas renderer already fills the solid triangle separators (U+E0B0, U+E0B2) from the cell edges, but the rounded caps (U+E0B4, U+E0B6) used by Powerlevel10k and Starship pill styles were still drawn from the font. Fonts size those glyphs to their own em box, which is shorter and narrower than the terminal cell, so pill segments render as boxes with small rounded nubs. Draw them as half-ellipses spanning the full cell, overlapping the closed segment by 1px to avoid an anti-aliasing seam at fractional cell edges.
1 parent 836b86a commit 99d9307

2 files changed

Lines changed: 140 additions & 4 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { GHOSTTY_CELL_WIDE, type GhosttyCell, type GhosttySnapshot } from "./core";
4+
import { renderGhosttySnapshot } from "./renderer";
5+
6+
const BLACK = { r: 0, g: 0, b: 0 };
7+
const WHITE = { r: 255, g: 255, b: 255 };
8+
const METRICS = { width: 8, height: 20, baseline: 15 };
9+
10+
function cell(text: string): GhosttyCell {
11+
return {
12+
text,
13+
wide: GHOSTTY_CELL_WIDE.narrow,
14+
foreground: WHITE,
15+
background: BLACK,
16+
bold: false,
17+
italic: false,
18+
invisible: false,
19+
strikethrough: false,
20+
overline: false,
21+
underline: false,
22+
selected: false,
23+
};
24+
}
25+
26+
function render(text: string) {
27+
const calls: Array<{ readonly op: string; readonly args: readonly unknown[] }> = [];
28+
const record =
29+
(op: string) =>
30+
(...args: unknown[]) => {
31+
calls.push({ op, args });
32+
};
33+
const context = {
34+
canvas: { width: 100, height: 100 },
35+
fillStyle: "",
36+
strokeStyle: "",
37+
font: "",
38+
textBaseline: "",
39+
save: record("save"),
40+
restore: record("restore"),
41+
resetTransform: record("resetTransform"),
42+
getTransform: () => ({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 }),
43+
fillRect: record("fillRect"),
44+
strokeRect: record("strokeRect"),
45+
beginPath: record("beginPath"),
46+
moveTo: record("moveTo"),
47+
lineTo: record("lineTo"),
48+
ellipse: record("ellipse"),
49+
closePath: record("closePath"),
50+
fill: record("fill"),
51+
rect: record("rect"),
52+
clip: record("clip"),
53+
fillText: record("fillText"),
54+
} as unknown as CanvasRenderingContext2D;
55+
const snapshot: GhosttySnapshot = {
56+
cols: 1,
57+
rows: 1,
58+
foreground: WHITE,
59+
background: BLACK,
60+
cursor: WHITE,
61+
cursorX: -1,
62+
cursorY: -1,
63+
cursorVisible: false,
64+
cursorBlinking: false,
65+
cursorStyle: 1,
66+
dirtyRows: new Set([0]),
67+
rowData: [{ cells: [cell(text)], text, isWrapContinuation: false, wrapsToNext: false }],
68+
};
69+
renderGhosttySnapshot({
70+
context,
71+
snapshot,
72+
metrics: METRICS,
73+
fontSize: 14,
74+
fontFamily: "monospace",
75+
padding: 0,
76+
forceFull: false,
77+
cursorOn: false,
78+
});
79+
return calls;
80+
}
81+
82+
describe("renderGhosttySnapshot powerline separators", () => {
83+
it("fills the right half-circle cap from the cell's left edge across its full height", () => {
84+
const calls = render("");
85+
expect(calls.some((call) => call.op === "fillText")).toBe(false);
86+
expect(calls.find((call) => call.op === "ellipse")?.args).toEqual([
87+
0,
88+
10,
89+
8,
90+
10,
91+
0,
92+
-Math.PI / 2,
93+
Math.PI / 2,
94+
]);
95+
});
96+
97+
it("fills the left half-circle cap from the cell's right edge across its full height", () => {
98+
const calls = render("");
99+
expect(calls.some((call) => call.op === "fillText")).toBe(false);
100+
expect(calls.find((call) => call.op === "ellipse")?.args).toEqual([
101+
8,
102+
10,
103+
8,
104+
10,
105+
0,
106+
Math.PI / 2,
107+
(3 * Math.PI) / 2,
108+
]);
109+
});
110+
111+
it("still draws other private-use glyphs from the font", () => {
112+
const calls = render("");
113+
expect(calls.some((call) => call.op === "ellipse")).toBe(false);
114+
expect(calls.some((call) => call.op === "fillText")).toBe(true);
115+
});
116+
});

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

Lines changed: 24 additions & 4 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,12 +38,17 @@ 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,
@@ -51,10 +58,23 @@ function fillSolidPowerlineSeparator(
5158
height: number,
5259
): void {
5360
const right = left + width;
61+
const middle = top + height / 2;
62+
if (text === POWERLINE_ROUND_RIGHT || text === POWERLINE_ROUND_LEFT) {
63+
const [base, start, inward] =
64+
text === POWERLINE_ROUND_RIGHT ? [left, -Math.PI / 2, -1] : [right, Math.PI / 2, 1];
65+
context.beginPath();
66+
context.ellipse(base, middle, width, height / 2, 0, start, start + Math.PI);
67+
context.closePath();
68+
// Overlap the segment this cap closes by a pixel so anti-aliasing at a
69+
// fractional cell edge does not leave a hairline seam.
70+
context.rect(Math.min(base, base + inward), top, 1, height);
71+
context.fill();
72+
return;
73+
}
5474
const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left];
5575
context.beginPath();
5676
context.moveTo(base, top);
57-
context.lineTo(point, top + height / 2);
77+
context.lineTo(point, middle);
5878
context.lineTo(base, top + height);
5979
context.closePath();
6080
context.fill();

0 commit comments

Comments
 (0)