DuckDB Terminal - v0.2.0
    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

    • get cols(): number

      The number of columns (characters per line) in the terminal.

      Returns number

      The current column count, or 80 if not initialized

    • get rows(): number

      The number of rows (lines) in the terminal.

      Returns number

      The current row count, or 24 if not initialized

    Methods

    • Clears the terminal screen and moves cursor to top-left.

      Returns void

      adapter.clear();
      adapter.writeln('Fresh start!');
    • Internal

      Copies text to clipboard using synchronous methods that work in Safari.

      Parameters

      • text: string

        The text to copy to clipboard

      Returns void

    • Internal

      Creates or recreates the terminal instance.

      Returns void

    • 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();
    • Gives keyboard focus to the terminal. On mobile devices, focuses the hidden input to trigger the virtual keyboard.

      Returns void

      adapter.focus();
      
    • 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,
      });
    • Internal

      Checks if the current device supports touch input.

      Returns boolean

    • 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 mobile-specific functionality: touch scrolling and keyboard input.

      Returns void

    • Internal

      Creates a hidden textarea for mobile keyboard input. Appended to document.body to avoid interfering with terminal touch events.

      Returns void

    • 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

    • Internal

      Sets up touch-based scrolling for the terminal. Translates touch gestures into scroll commands.

      Returns void

    • Internal

      Converts a Theme object to Ghostty's theme format.

      Parameters

      • theme: Theme

        The theme to convert

      Returns Record<string, string>

      The theme in Ghostty's expected format

    • Writes text to the terminal without a trailing newline.

      Parameters

      • text: string

        The text to write

      Returns void

      adapter.write('Hello');
      adapter.write(' World');
    • 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');