@@ -47,6 +47,19 @@ const CELL_DATA = {
4747 selected : 7 ,
4848} as const ;
4949
50+ // wasm32 arrays for the official row_cells_get_multi API. Optional colors
51+ // come last because an unset color stops the query at that field.
52+ const CELL_QUERY = {
53+ keys : 0 ,
54+ values : 20 ,
55+ graphemesLength : 40 ,
56+ selected : 44 ,
57+ background : 48 ,
58+ foreground : 52 ,
59+ written : 56 ,
60+ size : 60 ,
61+ } as const ;
62+
5063const RAW_CELL_DATA = {
5164 wide : 3 ,
5265} as const ;
@@ -210,6 +223,7 @@ export class GhosttyTerminalCore {
210223 private graphemes = 0 ;
211224 private graphemeCapacity = 0 ;
212225 private style = 0 ;
226+ private cellQuery = 0 ;
213227 private scrollbar = 0 ;
214228 private rows : GhosttyRow [ ] = [ ] ;
215229 private disposed = false ;
@@ -310,6 +324,19 @@ export class GhosttyTerminalCore {
310324 const styleSize = this . runtime . layout ( "GhosttyStyle" ) . size ;
311325 this . style = this . runtime . alloc ( styleSize ) ;
312326 this . runtime . setField ( this . style , "GhosttyStyle" , "size" , styleSize ) ;
327+ this . cellQuery = this . runtime . alloc ( CELL_QUERY . size ) ;
328+ const queryView = this . runtime . memoryDataView ( ) ;
329+ const cellFields = [
330+ [ CELL_DATA . style , this . style ] ,
331+ [ CELL_DATA . graphemesLength , this . cellQuery + CELL_QUERY . graphemesLength ] ,
332+ [ CELL_DATA . selected , this . cellQuery + CELL_QUERY . selected ] ,
333+ [ CELL_DATA . background , this . cellQuery + CELL_QUERY . background ] ,
334+ [ CELL_DATA . foreground , this . cellQuery + CELL_QUERY . foreground ] ,
335+ ] as const ;
336+ for ( const [ index , [ key , pointer ] ] of cellFields . entries ( ) ) {
337+ queryView . setUint32 ( this . cellQuery + CELL_QUERY . keys + index * 4 , key , true ) ;
338+ queryView . setUint32 ( this . cellQuery + CELL_QUERY . values + index * 4 , pointer , true ) ;
339+ }
313340 this . scrollbar = this . runtime . alloc ( this . runtime . layout ( "GhosttyTerminalScrollbar" ) . size ) ;
314341 this . setTheme ( theme ) ;
315342 this . resize ( cols , rows , cellWidth , cellHeight ) ;
@@ -658,17 +685,26 @@ export class GhosttyTerminalCore {
658685 }
659686 }
660687
661- selectAll ( ) : void {
688+ selectAll ( ) : GhosttySelectionRange [ "screen" ] | null {
662689 this . ensureActive ( ) ;
663690 const layout = this . runtime . layout ( "GhosttySelection" ) ;
664691 const selection = this . runtime . alloc ( layout . size ) ;
665- this . runtime . setField ( selection , "GhosttySelection" , "size" , layout . size ) ;
666- if (
667- this . runtime . call ( "ghostty_terminal_select_all" , this . terminal , selection ) === GHOSTTY_SUCCESS
668- ) {
692+ try {
693+ this . runtime . setField ( selection , "GhosttySelection" , "size" , layout . size ) ;
694+ if (
695+ this . runtime . call ( "ghostty_terminal_select_all" , this . terminal , selection ) !==
696+ GHOSTTY_SUCCESS
697+ ) {
698+ return null ;
699+ }
700+ const start = this . pointFromGridRef ( selection + layout . fields . start ! . offset , 2 ) ;
701+ const end = this . pointFromGridRef ( selection + layout . fields . end ! . offset , 2 ) ;
702+ if ( ! start || ! end ) return null ;
669703 this . runtime . call ( "ghostty_terminal_set" , this . terminal , 21 , selection ) ;
704+ return { start, end } ;
705+ } finally {
706+ this . runtime . free ( selection , layout . size ) ;
670707 }
671- this . runtime . free ( selection , layout . size ) ;
672708 }
673709
674710 selectWord ( col : number , row : number ) : GhosttySelectionRange | null {
@@ -877,6 +913,7 @@ export class GhosttyTerminalCore {
877913 this . runtime . call ( "ghostty_terminal_free" , this . terminal ) ;
878914 }
879915 if ( this . style ) this . runtime . free ( this . style , this . runtime . layout ( "GhosttyStyle" ) . size ) ;
916+ if ( this . cellQuery ) this . runtime . free ( this . cellQuery , CELL_QUERY . size ) ;
880917 if ( this . scrollbar ) {
881918 this . runtime . free ( this . scrollbar , this . runtime . layout ( "GhosttyTerminalScrollbar" ) . size ) ;
882919 }
@@ -954,17 +991,29 @@ export class GhosttyTerminalCore {
954991 cells . length < cols &&
955992 this . runtime . call ( "ghostty_render_state_row_cells_next" , cellsIterator ) !== 0
956993 ) {
957- let foreground = this . getCellColor ( cellsIterator , CELL_DATA . foreground , defaultForeground ) ;
958- let background = this . getCellColor ( cellsIterator , CELL_DATA . background , defaultBackground ) ;
959994 this . runtime . bytes ( this . style , styleSize ) . fill ( 0 ) ;
960995 this . runtime . setField ( this . style , "GhosttyStyle" , "size" , styleSize ) ;
961- this . runtime . call (
962- "ghostty_render_state_row_cells_get " ,
996+ const result = this . runtime . call (
997+ "ghostty_render_state_row_cells_get_multi " ,
963998 cellsIterator ,
964- CELL_DATA . style ,
965- this . style ,
999+ 5 ,
1000+ this . cellQuery + CELL_QUERY . keys ,
1001+ this . cellQuery + CELL_QUERY . values ,
1002+ this . cellQuery + CELL_QUERY . written ,
9661003 ) ;
967- const graphemeLength = this . getCellU32 ( cellsIterator , CELL_DATA . graphemesLength ) ;
1004+ const queryView = this . runtime . memoryDataView ( ) ;
1005+ const written = queryView . getUint32 ( this . cellQuery + CELL_QUERY . written , true ) ;
1006+ if ( written < 3 ) this . assertSuccess ( "ghostty_render_state_row_cells_get_multi" , result ) ;
1007+ const graphemeLength = queryView . getUint32 ( this . cellQuery + CELL_QUERY . graphemesLength , true ) ;
1008+ const selected = queryView . getUint8 ( this . cellQuery + CELL_QUERY . selected ) !== 0 ;
1009+ let background =
1010+ written >= 4 ? this . readColor ( this . cellQuery + CELL_QUERY . background ) : defaultBackground ;
1011+ let foreground =
1012+ written === 5
1013+ ? this . readColor ( this . cellQuery + CELL_QUERY . foreground )
1014+ : written === 3
1015+ ? this . getCellColor ( cellsIterator , CELL_DATA . foreground , defaultForeground )
1016+ : defaultForeground ;
9681017 let text = "" ;
9691018 if ( graphemeLength > 0 ) {
9701019 const bufferSize = graphemeLength * 4 ;
@@ -1008,26 +1057,25 @@ export class GhosttyTerminalCore {
10081057 ) ;
10091058 wide = this . runtime . view ( this . scratch + 8 , 4 ) . getUint32 ( 0 , true ) ;
10101059 }
1011- const selected = this . getCellBool ( cellsIterator , CELL_DATA . selected ) ;
10121060 // Read the style after allocation and ABI calls, which can grow WASM memory.
1013- const styleView = this . runtime . view ( this . style , styleSize ) ;
1014- if ( styleView . getUint8 ( styleFields . inverse ! . offset ) !== 0 ) {
1061+ const styleView = this . runtime . memoryDataView ( ) ;
1062+ if ( styleView . getUint8 ( this . style + styleFields . inverse ! . offset ) !== 0 ) {
10151063 [ foreground , background ] = [ background , foreground ] ;
10161064 }
1017- if ( styleView . getUint8 ( styleFields . faint ! . offset ) !== 0 ) {
1065+ if ( styleView . getUint8 ( this . style + styleFields . faint ! . offset ) !== 0 ) {
10181066 foreground = blend ( foreground , background ) ;
10191067 }
10201068 cells . push ( {
10211069 text,
10221070 wide,
10231071 foreground,
10241072 background,
1025- bold : styleView . getUint8 ( styleFields . bold ! . offset ) !== 0 ,
1026- italic : styleView . getUint8 ( styleFields . italic ! . offset ) !== 0 ,
1027- invisible : styleView . getUint8 ( styleFields . invisible ! . offset ) !== 0 ,
1028- strikethrough : styleView . getUint8 ( styleFields . strikethrough ! . offset ) !== 0 ,
1029- overline : styleView . getUint8 ( styleFields . overline ! . offset ) !== 0 ,
1030- underline : styleView . getInt32 ( styleFields . underline ! . offset , true ) !== 0 ,
1073+ bold : styleView . getUint8 ( this . style + styleFields . bold ! . offset ) !== 0 ,
1074+ italic : styleView . getUint8 ( this . style + styleFields . italic ! . offset ) !== 0 ,
1075+ invisible : styleView . getUint8 ( this . style + styleFields . invisible ! . offset ) !== 0 ,
1076+ strikethrough : styleView . getUint8 ( this . style + styleFields . strikethrough ! . offset ) !== 0 ,
1077+ overline : styleView . getUint8 ( this . style + styleFields . overline ! . offset ) !== 0 ,
1078+ underline : styleView . getInt32 ( this . style + styleFields . underline ! . offset , true ) !== 0 ,
10311079 selected,
10321080 } ) ;
10331081 }
@@ -1177,25 +1225,6 @@ export class GhosttyTerminalCore {
11771225 ) ;
11781226 }
11791227
1180- private getCellU32 ( iterator : number , data : number ) : number {
1181- this . runtime . bytes ( this . scratch , 4 ) . fill ( 0 ) ;
1182- const result = this . runtime . call (
1183- "ghostty_render_state_row_cells_get" ,
1184- iterator ,
1185- data ,
1186- this . scratch ,
1187- ) ;
1188- return result === GHOSTTY_SUCCESS ? this . runtime . view ( this . scratch , 4 ) . getUint32 ( 0 , true ) : 0 ;
1189- }
1190-
1191- private getCellBool ( iterator : number , data : number ) : boolean {
1192- this . runtime . bytes ( this . scratch , 1 ) [ 0 ] = 0 ;
1193- return (
1194- this . runtime . call ( "ghostty_render_state_row_cells_get" , iterator , data , this . scratch ) ===
1195- GHOSTTY_SUCCESS && this . runtime . bytes ( this . scratch , 1 ) [ 0 ] !== 0
1196- ) ;
1197- }
1198-
11991228 private getCellColor ( iterator : number , data : number , fallback : GhosttyColor ) : GhosttyColor {
12001229 this . runtime . bytes ( this . scratch , 3 ) . fill ( 0 ) ;
12011230 const result = this . runtime . call (
@@ -1208,8 +1237,12 @@ export class GhosttyTerminalCore {
12081237 }
12091238
12101239 private readColor ( pointer : number ) : GhosttyColor {
1211- const bytes = this . runtime . bytes ( pointer , 3 ) ;
1212- return { r : bytes [ 0 ] ?? 0 , g : bytes [ 1 ] ?? 0 , b : bytes [ 2 ] ?? 0 } ;
1240+ const view = this . runtime . memoryDataView ( ) ;
1241+ return {
1242+ r : view . getUint8 ( pointer ) ,
1243+ g : view . getUint8 ( pointer + 1 ) ,
1244+ b : view . getUint8 ( pointer + 2 ) ,
1245+ } ;
12131246 }
12141247
12151248 private emptyCell ( foreground : GhosttyColor , background : GhosttyColor ) : GhosttyCell {
0 commit comments