Skip to content

Commit 3982f20

Browse files
SadriG91juliusmarmingeclaude
authored
fix(web): render powerline separators at cell edges (pingdotgg#5607)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 7400bc5 commit 3982f20

2 files changed

Lines changed: 119 additions & 3 deletions

File tree

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,78 @@ describe("ghosttyTextRunEnd", () => {
7171
});
7272

7373
describe("renderGhosttySnapshot", () => {
74+
it("draws solid Powerline separators to their cell edges instead of as text", () => {
75+
const paths: number[][][] = [];
76+
let path: number[][] = [];
77+
const fillTextCalls: unknown[][] = [];
78+
const context = {
79+
canvas: { width: 200, height: 40 },
80+
beginPath: () => {
81+
path = [];
82+
},
83+
clip: () => {},
84+
closePath: () => {},
85+
fill: () => paths.push(path),
86+
fillRect: () => {},
87+
fillText: (...args: unknown[]) => fillTextCalls.push(args),
88+
lineTo: (x: number, y: number) => path.push([x, y]),
89+
moveTo: (x: number, y: number) => path.push([x, y]),
90+
rect: () => {},
91+
resetTransform: () => {},
92+
restore: () => {},
93+
save: () => {},
94+
set fillStyle(_value: string) {},
95+
set font(_value: string) {},
96+
set textBaseline(_value: string) {},
97+
} as unknown as CanvasRenderingContext2D;
98+
const snapshot: GhosttySnapshot = {
99+
cols: 3,
100+
rows: 1,
101+
foreground: { r: 255, g: 255, b: 255 },
102+
background: { r: 0, g: 0, b: 0 },
103+
cursor: { r: 255, g: 255, b: 255 },
104+
cursorX: -1,
105+
cursorY: -1,
106+
cursorVisible: false,
107+
cursorBlinking: false,
108+
cursorStyle: 1,
109+
dirtyRows: new Set([0]),
110+
rowData: [
111+
{
112+
cells: [cell("a"), cell("\ue0b0"), cell("\ue0b2")],
113+
text: "a\ue0b0\ue0b2",
114+
isWrapContinuation: false,
115+
wrapsToNext: false,
116+
},
117+
],
118+
};
119+
120+
renderGhosttySnapshot({
121+
context,
122+
snapshot,
123+
metrics: { width: 10, height: 20, baseline: 15 },
124+
fontSize: 12,
125+
fontFamily: "monospace",
126+
padding: 4,
127+
forceFull: false,
128+
cursorOn: false,
129+
});
130+
131+
expect(fillTextCalls).toEqual([["a", 4, 19, 10]]);
132+
expect(paths).toEqual([
133+
[
134+
[14, 4],
135+
[24, 14],
136+
[14, 24],
137+
],
138+
[
139+
[34, 4],
140+
[24, 14],
141+
[34, 24],
142+
],
143+
]);
144+
});
145+
74146
it("underlines every cell in a hovered wrapped link", () => {
75147
const fillRectCalls: number[][] = [];
76148
const context = {

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface GhosttyCellRange {
1818
}
1919

2020
const DEFAULT_SELECTION_BACKGROUND = "rgba(72, 122, 191, 0.35)";
21+
const POWERLINE_SOLID_RIGHT = "\ue0b0";
22+
const POWERLINE_SOLID_LEFT = "\ue0b2";
2123

2224
function cssColor(color: GhosttyColor): string {
2325
return `rgb(${color.r}, ${color.g}, ${color.b})`;
@@ -35,6 +37,30 @@ function sameTextStyle(left: GhosttyCell, right: GhosttyCell): boolean {
3537
);
3638
}
3739

40+
function isSolidPowerlineSeparator(text: string): boolean {
41+
return text === POWERLINE_SOLID_RIGHT || text === POWERLINE_SOLID_LEFT;
42+
}
43+
44+
// Fallback fonts inset or scale these private-use glyphs, leaving a gap where
45+
// the separator should meet the next cell. Fill the triangle from the cell's
46+
// own edges instead, as native terminals do.
47+
function fillSolidPowerlineSeparator(
48+
context: CanvasRenderingContext2D,
49+
text: string,
50+
left: number,
51+
top: number,
52+
metrics: GhosttyCellMetrics,
53+
): void {
54+
const right = left + metrics.width;
55+
const [base, point] = text === POWERLINE_SOLID_RIGHT ? [left, right] : [right, left];
56+
context.beginPath();
57+
context.moveTo(base, top);
58+
context.lineTo(point, top + metrics.height / 2);
59+
context.lineTo(base, top + metrics.height);
60+
context.closePath();
61+
context.fill();
62+
}
63+
3864
export function ghosttyTextRunEnd(
3965
cells: readonly GhosttyCell[],
4066
start: number,
@@ -48,7 +74,7 @@ export function ghosttyTextRunEnd(
4874
end += 1;
4975
continue;
5076
}
51-
if (next.text.length === 0 || !sameStyle(next)) break;
77+
if (next.text.length === 0 || isSolidPowerlineSeparator(next.text) || !sameStyle(next)) break;
5278
end += 1;
5379
}
5480
return end;
@@ -192,6 +218,20 @@ export function renderGhosttySnapshot(options: {
192218
runStart += 1;
193219
continue;
194220
}
221+
if (isSolidPowerlineSeparator(first.text)) {
222+
if (!first.invisible) {
223+
context.fillStyle = cssColor(first.foreground);
224+
fillSolidPowerlineSeparator(
225+
context,
226+
first.text,
227+
padding + runStart * metrics.width,
228+
top,
229+
metrics,
230+
);
231+
}
232+
runStart += 1;
233+
continue;
234+
}
195235
const runEnd = ghosttyTextRunEnd(row.cells, runStart, (cell) => sameTextStyle(cell, first));
196236
const text = row.cells
197237
.slice(runStart, runEnd)
@@ -261,9 +301,13 @@ export function renderGhosttySnapshot(options: {
261301
context.fillRect(left, top, metrics.width, metrics.height);
262302
const cell = snapshot.rowData[snapshot.cursorY]?.cells[snapshot.cursorX];
263303
if (cell?.text) {
264-
context.font = fontForCell(cell, fontSize, fontFamily);
265304
context.fillStyle = cssColor(snapshot.background);
266-
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);
305+
if (isSolidPowerlineSeparator(cell.text)) {
306+
fillSolidPowerlineSeparator(context, cell.text, left, top, metrics);
307+
} else {
308+
context.font = fontForCell(cell, fontSize, fontFamily);
309+
context.fillText(cell.text, left, top + metrics.baseline, metrics.width);
310+
}
267311
}
268312
}
269313
}

0 commit comments

Comments
 (0)