@@ -22,6 +22,64 @@ const cell = (text: string, wide = 0): GhosttyCell => ({
2222 selected : false ,
2323} ) ;
2424
25+ const WHITE = "rgb(255, 255, 255)" ;
26+ const BLACK = "rgb(0, 0, 0)" ;
27+
28+ /** Canvas stub that records fillRect calls per fill style and every fillText call. */
29+ function recordingContext ( ) {
30+ const rects = new Map < string , number [ ] [ ] > ( ) ;
31+ const fillTextCalls : unknown [ ] [ ] = [ ] ;
32+ let fillStyle = "" ;
33+ const context = {
34+ canvas : { width : 200 , height : 80 } ,
35+ beginPath : ( ) => { } ,
36+ clip : ( ) => { } ,
37+ fillRect : ( ...args : number [ ] ) => {
38+ rects . set ( fillStyle , [ ...( rects . get ( fillStyle ) ?? [ ] ) , args ] ) ;
39+ } ,
40+ fillText : ( ...args : unknown [ ] ) => fillTextCalls . push ( args ) ,
41+ rect : ( ) => { } ,
42+ resetTransform : ( ) => { } ,
43+ restore : ( ) => { } ,
44+ save : ( ) => { } ,
45+ set fillStyle ( value : string ) {
46+ fillStyle = value ;
47+ } ,
48+ set font ( _value : string ) { } ,
49+ set textBaseline ( _value : string ) { } ,
50+ } as unknown as CanvasRenderingContext2D ;
51+ return {
52+ context,
53+ fillTextCalls,
54+ rectsFilledWith : ( style : string ) => rects . get ( style ) ?? [ ] ,
55+ } ;
56+ }
57+
58+ /** White-on-black snapshot of the given rows, every row dirty, one cell per character. */
59+ function textSnapshot ( rows : readonly string [ ] , cursor ?: { x : number ; y : number } ) : GhosttySnapshot {
60+ return {
61+ cols : Math . max ( ...rows . map ( ( row ) => row . length ) ) ,
62+ rows : rows . length ,
63+ foreground : { r : 255 , g : 255 , b : 255 } ,
64+ background : { r : 0 , g : 0 , b : 0 } ,
65+ cursor : { r : 255 , g : 255 , b : 255 } ,
66+ cursorX : cursor ?. x ?? - 1 ,
67+ cursorY : cursor ?. y ?? - 1 ,
68+ cursorVisible : cursor !== undefined ,
69+ cursorBlinking : false ,
70+ cursorStyle : 1 ,
71+ dirtyRows : new Set ( rows . map ( ( _ , index ) => index ) ) ,
72+ rowData : rows . map ( ( text ) => ( {
73+ cells : Array . from ( text , ( char ) => cell ( char ) ) ,
74+ text,
75+ isWrapContinuation : false ,
76+ wrapsToNext : false ,
77+ } ) ) ,
78+ } ;
79+ }
80+
81+ const RENDER_OPTIONS = { fontSize : 12 , fontFamily : "monospace" , padding : 4 , forceFull : false } ;
82+
2583describe ( "terminalGridSize" , ( ) => {
2684 it ( "matches the mobile renderer's cell-and-padding sizing model" , ( ) => {
2785 expect ( terminalGridSize ( 808 , 408 , { width : 10 , height : 20 , baseline : 15 } , 4 ) ) . toEqual ( {
@@ -76,62 +134,19 @@ describe("ghosttyTextRunEnd", () => {
76134
77135describe ( "renderGhosttySnapshot" , ( ) => {
78136 it ( "fills block elements edge to edge so stacked rows leave no stripe" , ( ) => {
79- const foregroundRects : number [ ] [ ] = [ ] ;
80- const fillTextCalls : unknown [ ] [ ] = [ ] ;
81- let fillStyle = "" ;
82- const context = {
83- canvas : { width : 200 , height : 80 } ,
84- beginPath : ( ) => { } ,
85- clip : ( ) => { } ,
86- fillRect : ( ...args : number [ ] ) => {
87- if ( fillStyle === "rgb(255, 255, 255)" ) foregroundRects . push ( args ) ;
88- } ,
89- fillText : ( ...args : unknown [ ] ) => fillTextCalls . push ( args ) ,
90- rect : ( ) => { } ,
91- resetTransform : ( ) => { } ,
92- restore : ( ) => { } ,
93- save : ( ) => { } ,
94- set fillStyle ( value : string ) {
95- fillStyle = value ;
96- } ,
97- set font ( _value : string ) { } ,
98- set textBaseline ( _value : string ) { } ,
99- } as unknown as CanvasRenderingContext2D ;
100- const row = ( text : string ) => ( {
101- cells : Array . from ( text , ( char ) => cell ( char ) ) ,
102- text,
103- isWrapContinuation : false ,
104- wrapsToNext : false ,
105- } ) ;
106- const snapshot : GhosttySnapshot = {
107- cols : 3 ,
108- rows : 2 ,
109- foreground : { r : 255 , g : 255 , b : 255 } ,
110- background : { r : 0 , g : 0 , b : 0 } ,
111- cursor : { r : 255 , g : 255 , b : 255 } ,
112- cursorX : - 1 ,
113- cursorY : - 1 ,
114- cursorVisible : false ,
115- cursorBlinking : false ,
116- cursorStyle : 1 ,
117- dirtyRows : new Set ( [ 0 , 1 ] ) ,
118- // The half blocks qrcode-terminal emits for Expo QR codes, plus a
119- // quadrant so the merged-rectangle table is exercised.
120- rowData : [ row ( "\u2580\u2584\u2588" ) , row ( "\u2588 \u2599" ) ] ,
121- } ;
137+ const { context, fillTextCalls, rectsFilledWith } = recordingContext ( ) ;
122138
123139 renderGhosttySnapshot ( {
140+ ...RENDER_OPTIONS ,
124141 context,
125- snapshot,
142+ // The half blocks qrcode-terminal emits for Expo QR codes, plus a
143+ // quadrant so the merged-rectangle table is exercised.
144+ snapshot : textSnapshot ( [ "\u2580\u2584\u2588" , "\u2588 \u2599" ] ) ,
126145 metrics : { width : 10 , height : 20 , baseline : 15 } ,
127- fontSize : 12 ,
128- fontFamily : "monospace" ,
129- padding : 4 ,
130- forceFull : false ,
131146 cursorOn : false ,
132147 } ) ;
133148
134- expect ( foregroundRects ) . toEqual ( [
149+ expect ( rectsFilledWith ( WHITE ) ) . toEqual ( [
135150 [ 4 , 4 , 10 , 10 ] ,
136151 [ 14 , 14 , 10 , 10 ] ,
137152 [ 24 , 4 , 10 , 20 ] ,
@@ -143,116 +158,34 @@ describe("renderGhosttySnapshot", () => {
143158 } ) ;
144159
145160 it ( "keeps a sub-pixel eighth block inside its own cell" , ( ) => {
146- const foregroundRects : number [ ] [ ] = [ ] ;
147- let fillStyle = "" ;
148- const context = {
149- canvas : { width : 200 , height : 40 } ,
150- beginPath : ( ) => { } ,
151- clip : ( ) => { } ,
152- fillRect : ( ...args : number [ ] ) => {
153- if ( fillStyle === "rgb(255, 255, 255)" ) foregroundRects . push ( args ) ;
154- } ,
155- fillText : ( ) => { } ,
156- rect : ( ) => { } ,
157- resetTransform : ( ) => { } ,
158- restore : ( ) => { } ,
159- save : ( ) => { } ,
160- set fillStyle ( value : string ) {
161- fillStyle = value ;
162- } ,
163- set font ( _value : string ) { } ,
164- set textBaseline ( _value : string ) { } ,
165- } as unknown as CanvasRenderingContext2D ;
166- const snapshot : GhosttySnapshot = {
167- cols : 2 ,
168- rows : 1 ,
169- foreground : { r : 255 , g : 255 , b : 255 } ,
170- background : { r : 0 , g : 0 , b : 0 } ,
171- cursor : { r : 255 , g : 255 , b : 255 } ,
172- cursorX : - 1 ,
173- cursorY : - 1 ,
174- cursorVisible : false ,
175- cursorBlinking : false ,
176- cursorStyle : 1 ,
177- dirtyRows : new Set ( [ 0 ] ) ,
178- rowData : [
179- {
180- cells : [ cell ( " " ) , cell ( "\u2595" ) ] ,
181- text : " \u2595" ,
182- isWrapContinuation : false ,
183- wrapsToNext : false ,
184- } ,
185- ] ,
186- } ;
161+ const { context, rectsFilledWith } = recordingContext ( ) ;
187162
188163 renderGhosttySnapshot ( {
164+ ...RENDER_OPTIONS ,
189165 context,
190- snapshot,
166+ snapshot : textSnapshot ( [ " \u2595" ] ) ,
191167 // The right eighth of the second cell spans 17.5..18.4, which rounds to
192168 // nothing; it must land on 17..18 rather than spill into column three.
193169 metrics : { width : 7.2 , height : 16 , baseline : 11 } ,
194- fontSize : 12 ,
195- fontFamily : "monospace" ,
196- padding : 4 ,
197- forceFull : false ,
198170 cursorOn : false ,
199171 } ) ;
200172
201- expect ( foregroundRects ) . toEqual ( [ [ 17 , 4 , 1 , 16 ] ] ) ;
173+ expect ( rectsFilledWith ( WHITE ) ) . toEqual ( [ [ 17 , 4 , 1 , 16 ] ] ) ;
202174 } ) ;
203175
204176 it ( "keeps block geometry when a block cursor inverts the cell" , ( ) => {
205- const backgroundRects : number [ ] [ ] = [ ] ;
206- const fillTextCalls : unknown [ ] [ ] = [ ] ;
207- let fillStyle = "" ;
208- const context = {
209- canvas : { width : 200 , height : 40 } ,
210- beginPath : ( ) => { } ,
211- clip : ( ) => { } ,
212- fillRect : ( ...args : number [ ] ) => {
213- if ( fillStyle === "rgb(0, 0, 0)" ) backgroundRects . push ( args ) ;
214- } ,
215- fillText : ( ...args : unknown [ ] ) => fillTextCalls . push ( args ) ,
216- rect : ( ) => { } ,
217- resetTransform : ( ) => { } ,
218- restore : ( ) => { } ,
219- save : ( ) => { } ,
220- set fillStyle ( value : string ) {
221- fillStyle = value ;
222- } ,
223- set font ( _value : string ) { } ,
224- set textBaseline ( _value : string ) { } ,
225- } as unknown as CanvasRenderingContext2D ;
226- const snapshot : GhosttySnapshot = {
227- cols : 1 ,
228- rows : 1 ,
229- foreground : { r : 255 , g : 255 , b : 255 } ,
230- background : { r : 0 , g : 0 , b : 0 } ,
231- cursor : { r : 255 , g : 255 , b : 255 } ,
232- cursorX : 0 ,
233- cursorY : 0 ,
234- cursorVisible : true ,
235- cursorBlinking : false ,
236- cursorStyle : 1 ,
237- dirtyRows : new Set ( ) ,
238- rowData : [
239- { cells : [ cell ( "\u2584" ) ] , text : "\u2584" , isWrapContinuation : false , wrapsToNext : false } ,
240- ] ,
241- } ;
177+ const { context, fillTextCalls, rectsFilledWith } = recordingContext ( ) ;
242178
243179 renderGhosttySnapshot ( {
180+ ...RENDER_OPTIONS ,
244181 context,
245- snapshot,
182+ snapshot : textSnapshot ( [ "\u2584" ] , { x : 0 , y : 0 } ) ,
246183 metrics : { width : 10 , height : 20 , baseline : 15 } ,
247- fontSize : 12 ,
248- fontFamily : "monospace" ,
249- padding : 4 ,
250- forceFull : false ,
251184 cursorOn : true ,
252185 } ) ;
253186
254187 // The row clear comes first; the inverted lower half block follows it.
255- expect ( backgroundRects ) . toEqual ( [
188+ expect ( rectsFilledWith ( BLACK ) ) . toEqual ( [
256189 [ 4 , 4 , 10 , 20 ] ,
257190 [ 4 , 14 , 10 , 10 ] ,
258191 ] ) ;
0 commit comments