Skip to content

Commit 5d2fe07

Browse files
committed
fix: restore terminal config support and fix formatting
- Add GhosttyTerminalConfig struct and ghostty_terminal_new_with_config to WASM API - Update GhosttyTerminal constructor to accept optional config parameter - Update Ghostty.createTerminal() to pass config through - Fix double semicolon formatting issue in terminal.ts Restored config options: - scrollbackLimit: Max scrollback lines (0 = unlimited) - fgColor: Default foreground color (0xRRGGBB) - bgColor: Default background color (0xRRGGBB) - cursorColor: Cursor color (0xRRGGBB) - palette: 16-color ANSI palette
1 parent 96da9d4 commit 5d2fe07

4 files changed

Lines changed: 292 additions & 130 deletions

File tree

lib/ghostty.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
CellFlags,
1111
type Cursor,
1212
DirtyState,
13+
GHOSTTY_CONFIG_SIZE,
1314
type GhosttyCell,
15+
type GhosttyTerminalConfig,
1416
type GhosttyWasmExports,
1517
KeyEncoderOption,
1618
type KeyEvent,
@@ -49,8 +51,12 @@ export class Ghostty {
4951
return new KeyEncoder(this.exports);
5052
}
5153

52-
createTerminal(cols: number = 80, rows: number = 24): GhosttyTerminal {
53-
return new GhosttyTerminal(this.exports, this.memory, cols, rows);
54+
createTerminal(
55+
cols: number = 80,
56+
rows: number = 24,
57+
config?: GhosttyTerminalConfig
58+
): GhosttyTerminal {
59+
return new GhosttyTerminal(this.exports, this.memory, cols, rows, config);
5460
}
5561

5662
static async load(wasmPath?: string): Promise<Ghostty> {
@@ -261,13 +267,61 @@ export class GhosttyTerminal {
261267
/** Cell pool for zero-allocation rendering */
262268
private cellPool: GhosttyCell[] = [];
263269

264-
constructor(exports: GhosttyWasmExports, memory: WebAssembly.Memory, cols: number, rows: number) {
270+
constructor(
271+
exports: GhosttyWasmExports,
272+
memory: WebAssembly.Memory,
273+
cols: number,
274+
rows: number,
275+
config?: GhosttyTerminalConfig
276+
) {
265277
this.exports = exports;
266278
this.memory = memory;
267279
this._cols = cols;
268280
this._rows = rows;
269281

270-
this.handle = this.exports.ghostty_terminal_new(cols, rows);
282+
if (config) {
283+
// Allocate config struct in WASM memory
284+
const configPtr = this.exports.ghostty_wasm_alloc_u8_array(GHOSTTY_CONFIG_SIZE);
285+
if (configPtr === 0) {
286+
throw new Error('Failed to allocate config (out of memory)');
287+
}
288+
289+
try {
290+
// Write config to WASM memory
291+
const view = new DataView(this.memory.buffer);
292+
let offset = configPtr;
293+
294+
// scrollback_limit (u32)
295+
view.setUint32(offset, config.scrollbackLimit ?? 10000, true);
296+
offset += 4;
297+
298+
// fg_color (u32)
299+
view.setUint32(offset, config.fgColor ?? 0, true);
300+
offset += 4;
301+
302+
// bg_color (u32)
303+
view.setUint32(offset, config.bgColor ?? 0, true);
304+
offset += 4;
305+
306+
// cursor_color (u32)
307+
view.setUint32(offset, config.cursorColor ?? 0, true);
308+
offset += 4;
309+
310+
// palette[16] (u32 * 16)
311+
for (let i = 0; i < 16; i++) {
312+
view.setUint32(offset, config.palette?.[i] ?? 0, true);
313+
offset += 4;
314+
}
315+
316+
this.handle = this.exports.ghostty_terminal_new_with_config(cols, rows, configPtr);
317+
} finally {
318+
// Free the config memory
319+
this.exports.ghostty_wasm_free_u8_array(configPtr, GHOSTTY_CONFIG_SIZE);
320+
}
321+
} else {
322+
this.handle = this.exports.ghostty_terminal_new(cols, rows);
323+
}
324+
271325
if (!this.handle) throw new Error('Failed to create terminal');
272326

273327
this.initCellPool();

lib/terminal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export class Terminal implements ITerminalCore {
260260
parent.setAttribute('aria-multiline', 'true');
261261

262262
// Create WASM terminal with current dimensions
263-
this.wasmTerm = this.ghostty!.createTerminal(this.cols, this.rows);;
263+
this.wasmTerm = this.ghostty!.createTerminal(this.cols, this.rows);
264264

265265
// Create canvas element
266266
this.canvas = document.createElement('canvas');

lib/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ export interface GhosttyWasmExports extends WebAssembly.Exports {
344344

345345
// Terminal lifecycle
346346
ghostty_terminal_new(cols: number, rows: number): TerminalHandle;
347+
ghostty_terminal_new_with_config(cols: number, rows: number, configPtr: number): TerminalHandle;
347348
ghostty_terminal_free(terminal: TerminalHandle): void;
348349
ghostty_terminal_resize(terminal: TerminalHandle, cols: number, rows: number): void;
349350
ghostty_terminal_write(terminal: TerminalHandle, dataPtr: number, dataLen: number): void;
@@ -427,14 +428,23 @@ export const CURSOR_STRUCT_SIZE = 8;
427428
*/
428429
export const COLORS_STRUCT_SIZE = 12;
429430

430-
// Legacy - kept for compatibility but not used with new API
431+
/**
432+
* Terminal configuration (passed to ghostty_terminal_new_with_config)
433+
* All color values use 0xRRGGBB format. A value of 0 means "use default".
434+
*/
431435
export interface GhosttyTerminalConfig {
432436
scrollbackLimit?: number;
433437
fgColor?: number;
434438
bgColor?: number;
435439
cursorColor?: number;
436440
palette?: number[];
437441
}
442+
443+
/**
444+
* Size of GhosttyTerminalConfig struct in WASM memory (bytes).
445+
* Layout: scrollback_limit(u32) + fg_color(u32) + bg_color(u32) + cursor_color(u32) + palette[16](u32*16)
446+
* Total: 4 + 4 + 4 + 4 + 64 = 80 bytes
447+
*/
438448
export const GHOSTTY_CONFIG_SIZE = 80;
439449

440450
/**

0 commit comments

Comments
 (0)