@@ -18,8 +18,33 @@ export interface GhosttyCellRange {
1818}
1919
2020const DEFAULT_SELECTION_BACKGROUND = "rgba(72, 122, 191, 0.35)" ;
21- const POWERLINE_SOLID_RIGHT = "\ue0b0" ;
22- const POWERLINE_SOLID_LEFT = "\ue0b2" ;
21+ const POWERLINE_LINE_WIDTH = 1 ;
22+
23+ type CellPoint = readonly [ x : number , y : number ] ;
24+ type PowerlineShape =
25+ | { readonly kind : "fill" ; readonly points : readonly CellPoint [ ] }
26+ | { readonly kind : "stroke" ; readonly points : readonly CellPoint [ ] }
27+ | { readonly kind : "cap" ; readonly flatEdge : "left" | "right" ; readonly filled : boolean } ;
28+
29+ // prettier-ignore
30+ const POWERLINE_SHAPES : ReadonlyMap < string , PowerlineShape > = new Map ( [
31+ [ "\ue0b0" , { kind : "fill" , points : [ [ 0 , 0 ] , [ 1 , 0.5 ] , [ 0 , 1 ] ] } ] ,
32+ [ "\ue0b1" , { kind : "stroke" , points : [ [ 0 , 0 ] , [ 1 , 0.5 ] , [ 0 , 1 ] ] } ] ,
33+ [ "\ue0b2" , { kind : "fill" , points : [ [ 1 , 0 ] , [ 0 , 0.5 ] , [ 1 , 1 ] ] } ] ,
34+ [ "\ue0b3" , { kind : "stroke" , points : [ [ 1 , 0 ] , [ 0 , 0.5 ] , [ 1 , 1 ] ] } ] ,
35+ [ "\ue0b4" , { kind : "cap" , flatEdge : "left" , filled : true } ] ,
36+ [ "\ue0b5" , { kind : "cap" , flatEdge : "left" , filled : false } ] ,
37+ [ "\ue0b6" , { kind : "cap" , flatEdge : "right" , filled : true } ] ,
38+ [ "\ue0b7" , { kind : "cap" , flatEdge : "right" , filled : false } ] ,
39+ [ "\ue0b8" , { kind : "fill" , points : [ [ 0 , 0 ] , [ 1 , 1 ] , [ 0 , 1 ] ] } ] ,
40+ [ "\ue0b9" , { kind : "stroke" , points : [ [ 0 , 0 ] , [ 1 , 1 ] ] } ] ,
41+ [ "\ue0ba" , { kind : "fill" , points : [ [ 1 , 0 ] , [ 1 , 1 ] , [ 0 , 1 ] ] } ] ,
42+ [ "\ue0bb" , { kind : "stroke" , points : [ [ 1 , 0 ] , [ 0 , 1 ] ] } ] ,
43+ [ "\ue0bc" , { kind : "fill" , points : [ [ 0 , 0 ] , [ 1 , 0 ] , [ 0 , 1 ] ] } ] ,
44+ [ "\ue0bd" , { kind : "stroke" , points : [ [ 1 , 0 ] , [ 0 , 1 ] ] } ] ,
45+ [ "\ue0be" , { kind : "fill" , points : [ [ 0 , 0 ] , [ 1 , 0 ] , [ 1 , 1 ] ] } ] ,
46+ [ "\ue0bf" , { kind : "stroke" , points : [ [ 0 , 0 ] , [ 1 , 1 ] ] } ] ,
47+ ] ) ;
2348
2449function cssColor ( color : GhosttyColor ) : string {
2550 return `rgb(${ color . r } , ${ color . g } , ${ color . b } )` ;
@@ -35,28 +60,49 @@ function sameTextStyle(left: GhosttyCell, right: GhosttyCell): boolean {
3560 ) ;
3661}
3762
38- function isSolidPowerlineSeparator ( text : string ) : boolean {
39- return text === POWERLINE_SOLID_RIGHT || text === POWERLINE_SOLID_LEFT ;
40- }
41-
42- // 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.
45- function fillSolidPowerlineSeparator (
63+ // Fonts inset or scale these private-use glyphs, leaving gaps where they should
64+ // meet the neighboring cells. Draw them from the cell's own edges, as Ghostty does.
65+ function drawPowerlineShape (
4666 context : CanvasRenderingContext2D ,
47- text : string ,
67+ shape : PowerlineShape ,
68+ color : string ,
4869 left : number ,
4970 top : number ,
5071 metrics : GhosttyCellMetrics ,
5172) : void {
52- const right = left + metrics . width ;
53- const [ base , point ] = text === POWERLINE_SOLID_RIGHT ? [ left , right ] : [ right , left ] ;
73+ const { width, height } = metrics ;
74+ const filled = shape . kind === "fill" || ( shape . kind === "cap" && shape . filled ) ;
75+ context . save ( ) ;
76+ if ( ! filled ) {
77+ context . beginPath ( ) ;
78+ context . rect ( left , top , width , height ) ;
79+ context . clip ( ) ;
80+ }
5481 context . beginPath ( ) ;
55- context . moveTo ( base , top ) ;
56- context . lineTo ( point , top + metrics . height / 2 ) ;
57- context . lineTo ( base , top + metrics . height ) ;
58- context . closePath ( ) ;
59- context . fill ( ) ;
82+ if ( shape . kind === "cap" ) {
83+ const inset = filled ? 0 : POWERLINE_LINE_WIDTH / 2 ;
84+ const capTop = top + inset ;
85+ const capBottom = top + height - inset ;
86+ const radius = Math . min ( width - inset , ( capBottom - capTop ) / 2 ) ;
87+ const flatX = shape . flatEdge === "left" ? left : left + width ;
88+ const bulgeX = shape . flatEdge === "left" ? flatX + radius : flatX - radius ;
89+ context . moveTo ( flatX , capTop ) ;
90+ context . arcTo ( bulgeX , capTop , bulgeX , capTop + radius , radius ) ;
91+ context . lineTo ( bulgeX , capBottom - radius ) ;
92+ context . arcTo ( bulgeX , capBottom , flatX , capBottom , radius ) ;
93+ } else {
94+ for ( const [ x , y ] of shape . points ) context . lineTo ( left + x * width , top + y * height ) ;
95+ }
96+ if ( filled ) {
97+ context . fillStyle = color ;
98+ context . fill ( ) ;
99+ } else {
100+ context . strokeStyle = color ;
101+ context . lineWidth = POWERLINE_LINE_WIDTH ;
102+ context . lineCap = "butt" ;
103+ context . stroke ( ) ;
104+ }
105+ context . restore ( ) ;
60106}
61107
62108function ghosttyTextRunEnd (
@@ -72,7 +118,7 @@ function ghosttyTextRunEnd(
72118 end += 1 ;
73119 continue ;
74120 }
75- if ( next . text . length === 0 || isSolidPowerlineSeparator ( next . text ) || ! sameStyle ( next ) ) break ;
121+ if ( next . text . length === 0 || POWERLINE_SHAPES . has ( next . text ) || ! sameStyle ( next ) ) break ;
76122 end += 1 ;
77123 }
78124 return end ;
@@ -216,12 +262,13 @@ export function renderGhosttySnapshot(options: {
216262 runStart += 1 ;
217263 continue ;
218264 }
219- if ( isSolidPowerlineSeparator ( first . text ) ) {
265+ const powerlineShape = POWERLINE_SHAPES . get ( first . text ) ;
266+ if ( powerlineShape ) {
220267 if ( ! first . invisible ) {
221- context . fillStyle = cssColor ( first . foreground ) ;
222- fillSolidPowerlineSeparator (
268+ drawPowerlineShape (
223269 context ,
224- first . text ,
270+ powerlineShape ,
271+ cssColor ( first . foreground ) ,
225272 padding + runStart * metrics . width ,
226273 top ,
227274 metrics ,
@@ -299,10 +346,18 @@ export function renderGhosttySnapshot(options: {
299346 context . fillRect ( left , top , metrics . width , metrics . height ) ;
300347 const cell = snapshot . rowData [ snapshot . cursorY ] ?. cells [ snapshot . cursorX ] ;
301348 if ( cell ?. text ) {
302- context . fillStyle = cssColor ( snapshot . background ) ;
303- if ( isSolidPowerlineSeparator ( cell . text ) ) {
304- fillSolidPowerlineSeparator ( context , cell . text , left , top , metrics ) ;
349+ const powerlineShape = POWERLINE_SHAPES . get ( cell . text ) ;
350+ if ( powerlineShape ) {
351+ drawPowerlineShape (
352+ context ,
353+ powerlineShape ,
354+ cssColor ( snapshot . background ) ,
355+ left ,
356+ top ,
357+ metrics ,
358+ ) ;
305359 } else {
360+ context . fillStyle = cssColor ( snapshot . background ) ;
306361 context . font = fontForCell ( cell , fontSize , fontFamily ) ;
307362 context . fillText ( cell . text , left , top + metrics . baseline , metrics . width ) ;
308363 }
0 commit comments