@@ -36,6 +36,7 @@ import { OSC8LinkProvider } from './providers/osc8-link-provider';
3636import { UrlRegexProvider } from './providers/url-regex-provider' ;
3737import { CanvasRenderer } from './renderer' ;
3838import { SelectionManager } from './selection-manager' ;
39+ import type { GhosttyTerminalConfig } from './types' ;
3940import type { ILink , ILinkProvider } from './types' ;
4041
4142// ============================================================================
@@ -174,6 +175,82 @@ export class Terminal implements ITerminalCore {
174175 this . buffer = new BufferNamespace ( this ) ;
175176 }
176177
178+ // ==========================================================================
179+ // Theme to WASM Config Conversion
180+ // ==========================================================================
181+
182+ /**
183+ * Parse a CSS color string to 0xRRGGBB format.
184+ * Returns 0 if the color is undefined or invalid.
185+ */
186+ private parseColorToHex ( color ?: string ) : number {
187+ if ( ! color ) return 0 ;
188+
189+ // Handle hex colors (#RGB, #RRGGBB)
190+ if ( color . startsWith ( '#' ) ) {
191+ let hex = color . slice ( 1 ) ;
192+ if ( hex . length === 3 ) {
193+ hex = hex [ 0 ] + hex [ 0 ] + hex [ 1 ] + hex [ 1 ] + hex [ 2 ] + hex [ 2 ] ;
194+ }
195+ const value = Number . parseInt ( hex , 16 ) ;
196+ return Number . isNaN ( value ) ? 0 : value ;
197+ }
198+
199+ // Handle rgb(r, g, b) format
200+ const match = color . match ( / r g b \( ( \d + ) , \s * ( \d + ) , \s * ( \d + ) \) / ) ;
201+ if ( match ) {
202+ const r = Number . parseInt ( match [ 1 ] , 10 ) ;
203+ const g = Number . parseInt ( match [ 2 ] , 10 ) ;
204+ const b = Number . parseInt ( match [ 3 ] , 10 ) ;
205+ return ( r << 16 ) | ( g << 8 ) | b ;
206+ }
207+
208+ return 0 ;
209+ }
210+
211+ /**
212+ * Convert terminal options to WASM terminal config.
213+ */
214+ private buildWasmConfig ( ) : GhosttyTerminalConfig | undefined {
215+ const theme = this . options . theme ;
216+ const scrollback = this . options . scrollback ;
217+
218+ // If no theme and default scrollback, use defaults
219+ if ( ! theme && scrollback === 1000 ) {
220+ return undefined ;
221+ }
222+
223+ // Build palette array from theme colors
224+ // Order: black, red, green, yellow, blue, magenta, cyan, white,
225+ // brightBlack, brightRed, brightGreen, brightYellow, brightBlue, brightMagenta, brightCyan, brightWhite
226+ const palette : number [ ] = [
227+ this . parseColorToHex ( theme ?. black ) ,
228+ this . parseColorToHex ( theme ?. red ) ,
229+ this . parseColorToHex ( theme ?. green ) ,
230+ this . parseColorToHex ( theme ?. yellow ) ,
231+ this . parseColorToHex ( theme ?. blue ) ,
232+ this . parseColorToHex ( theme ?. magenta ) ,
233+ this . parseColorToHex ( theme ?. cyan ) ,
234+ this . parseColorToHex ( theme ?. white ) ,
235+ this . parseColorToHex ( theme ?. brightBlack ) ,
236+ this . parseColorToHex ( theme ?. brightRed ) ,
237+ this . parseColorToHex ( theme ?. brightGreen ) ,
238+ this . parseColorToHex ( theme ?. brightYellow ) ,
239+ this . parseColorToHex ( theme ?. brightBlue ) ,
240+ this . parseColorToHex ( theme ?. brightMagenta ) ,
241+ this . parseColorToHex ( theme ?. brightCyan ) ,
242+ this . parseColorToHex ( theme ?. brightWhite ) ,
243+ ] ;
244+
245+ return {
246+ scrollbackLimit : scrollback ,
247+ fgColor : this . parseColorToHex ( theme ?. foreground ) ,
248+ bgColor : this . parseColorToHex ( theme ?. background ) ,
249+ cursorColor : this . parseColorToHex ( theme ?. cursor ) ,
250+ palette,
251+ } ;
252+ }
253+
177254 // ==========================================================================
178255 // Option Change Handling (for mutable options)
179256 // ==========================================================================
@@ -248,8 +325,9 @@ export class Terminal implements ITerminalCore {
248325 parent . setAttribute ( 'tabindex' , '0' ) ;
249326 }
250327
251- // Create WASM terminal with current dimensions
252- this . wasmTerm = this . ghostty ! . createTerminal ( this . cols , this . rows ) ;
328+ // Create WASM terminal with current dimensions and theme config
329+ const wasmConfig = this . buildWasmConfig ( ) ;
330+ this . wasmTerm = this . ghostty ! . createTerminal ( this . cols , this . rows , wasmConfig ) ;
253331
254332 // Create canvas element
255333 this . canvas = document . createElement ( 'canvas' ) ;
@@ -549,7 +627,8 @@ export class Terminal implements ITerminalCore {
549627 if ( this . wasmTerm ) {
550628 this . wasmTerm . free ( ) ;
551629 }
552- this . wasmTerm = this . ghostty ! . createTerminal ( this . cols , this . rows ) ;
630+ const wasmConfig = this . buildWasmConfig ( ) ;
631+ this . wasmTerm = this . ghostty ! . createTerminal ( this . cols , this . rows , wasmConfig ) ;
553632
554633 // Clear renderer
555634 this . renderer ! . clear ( ) ;
0 commit comments