DuckDB Terminal - v0.4.2
    Preparing search index...

    Class TerminalAdapter

    An adapter that wraps the Ghostty terminal emulator for web use.

    This class provides a simplified interface for:

    • Initializing the Ghostty WASM terminal
    • Writing text and handling input
    • Managing themes and dimensions
    • Auto-fitting to container size

    The adapter handles all the low-level details of working with the Ghostty terminal emulator, including WASM initialization, addon loading, and event handling.

    const adapter = new TerminalAdapter();
    await adapter.init(document.getElementById('container'), {
    fontSize: 16,
    theme: darkTheme,
    });

    adapter.write('Hello, World!');
    adapter.onData((input) => {
    console.log('User typed:', input);
    });
    const adapter = new TerminalAdapter();
    await adapter.init(container);

    adapter.setTheme(darkTheme);
    // Later...
    adapter.setTheme(lightTheme);
    Index

    Constructors

    Properties

    container: HTMLElement | null = null
    currentTheme: Theme | null = null
    dataHandler?: (data: string) => void
    fitAddon: FitAddon
    ghostty: Ghostty
    initialized: boolean = false
    mobileInput: HTMLTextAreaElement | null = null
    options: TerminalOptions = {}
    resizeHandler?: (cols: number, rows: number) => void
    resizeObserver: ResizeObserver | null = null
    terminal: Terminal

    Accessors

    Methods

    • Disposes of the terminal and cleans up resources.

      After calling this method, the adapter cannot be used again. Create a new instance if you need another terminal.

      Returns void

      adapter.dispose();
      // adapter is no longer usable
    • Fits the terminal to its container dimensions.

      This method should be called when the container size changes. It's automatically called on window resize, but you may need to call it manually after dynamic layout changes.

      Returns void

      // After changing container size
      container.style.height = '500px';
      adapter.fit();
    • Initializes the Ghostty terminal and mounts it to the container.

      This method:

      1. Initializes the Ghostty WASM module
      2. Creates the terminal with the specified options
      3. Loads the FitAddon for auto-resizing
      4. Mounts the terminal to the container
      5. Sets up resize observers

      Parameters

      • container: HTMLElement

        The HTML element to mount the terminal into

      • options: TerminalOptions = {}

        Configuration options for the terminal

      Returns Promise<void>

      A promise that resolves when initialization is complete

      const adapter = new TerminalAdapter();
      await adapter.init(document.getElementById('terminal'), {
      fontSize: 14,
      fontFamily: 'JetBrains Mono',
      theme: darkTheme,
      });
    • Registers a callback to receive user input data.

      The callback is invoked whenever the user types in the terminal. This includes regular characters, control sequences, and escape codes.

      Parameters

      • handler: (data: string) => void

        The callback function to handle input data

      Returns void

      adapter.onData((data) => {
      if (data === '\r') {
      console.log('User pressed Enter');
      } else {
      console.log('User typed:', data);
      }
      });
    • Registers a callback to receive terminal resize events.

      Parameters

      • handler: (cols: number, rows: number) => void

        The callback function receiving new dimensions

      Returns void

      adapter.onResize((cols, rows) => {
      console.log(`Terminal resized to ${cols}x${rows}`);
      });
    • Sets the terminal color theme.

      Since Ghostty-web doesn't fully support runtime theme changes, this method recreates the terminal with the new theme.

      Parameters

      • theme: Theme

        The theme to apply

      Returns void

      adapter.setTheme({
      name: 'dark',
      colors: {
      background: '#1e1e1e',
      foreground: '#d4d4d4',
      // ... other colors
      },
      });
    • Internal

      Sets up a workaround for Safari's clipboard restrictions.

      Safari requires clipboard operations to happen synchronously within a user gesture. The async Clipboard API loses the gesture context after an await, causing copy to fail. This workaround intercepts Cmd+C and uses the synchronous execCommand method.

      Returns void

    • Writes text to the terminal followed by a newline.

      Parameters

      • text: string

        The text to write

      Returns void

      adapter.writeln('First line');
      adapter.writeln('Second line');