Coconstructor
Bun

constructor

Terminal.constructor

constructor Terminal(

Referenced types

interface TerminalOptions

Options for creating a pseudo-terminal (PTY).

  • cols?: number

    Number of columns for the terminal.

  • data?: (terminal: Terminal, data: Uint8Array<ArrayBuffer>) => void

    Callback invoked when data is received from the terminal.

  • drain?: (terminal: Terminal) => void

    Callback invoked when the terminal is ready to receive more data.

  • exit?: (terminal: Terminal, exitCode: number, signal: null | string) => void

    Callback invoked when the PTY stream closes (EOF or read error). Note: exitCode is a PTY lifecycle status (0=clean EOF, 1=error), NOT the subprocess exit code. Use Subprocess.exited or onExit callback for actual process exit information.

  • name?: string

    Terminal name (e.g., "xterm-256color").

  • rows?: number

    Number of rows for the terminal.

class Terminal

A pseudo-terminal (PTY) that can be used to spawn interactive terminal programs.

await using terminal = new Bun.Terminal({
  cols: 80,
  rows: 24,
  data(term, data) {
    console.log("Received:", new TextDecoder().decode(data));
  },
});

// Spawn a shell connected to the PTY
const proc = Bun.spawn(["bash"], { terminal });

// Write to the terminal
terminal.write("echo hello\n");

// Wait for process to exit
await proc.exited;

// Terminal is closed automatically by `await using`
  • readonly closed: boolean

    Whether the terminal is closed.

  • controlFlags: number

    Terminal control flags (c_cflag from termios). Controls hardware characteristics like CSIZE, PARENB, etc. Returns 0 if terminal is closed. Setting returns true on success, false on failure.

  • inputFlags: number

    Terminal input flags (c_iflag from termios). Controls input processing behavior like ICRNL, IXON, etc. Returns 0 if terminal is closed. Setting returns true on success, false on failure.

  • localFlags: number

    Terminal local flags (c_lflag from termios). Controls local processing like ICANON, ECHO, ISIG, etc. Returns 0 if terminal is closed. Setting returns true on success, false on failure.

  • outputFlags: number

    Terminal output flags (c_oflag from termios). Controls output processing behavior like OPOST, ONLCR, etc. Returns 0 if terminal is closed. Setting returns true on success, false on failure.

  • [Symbol.asyncDispose](): Promise<void>;

    Async dispose for use with await using.

  • close(): void;

    Close the terminal.

  • ref(): void;

    Reference the terminal to keep the event loop alive.

  • cols: number,
    rows: number
    ): void;

    Resize the terminal.

    @param cols

    New number of columns

    @param rows

    New number of rows

  • enabled: boolean
    ): void;

    Set raw mode on the terminal. In raw mode, input is passed directly without processing.

    @param enabled

    Whether to enable raw mode

  • unref(): void;

    Unreference the terminal to allow the event loop to exit.

  • data: string | BufferSource
    ): number;

    Write data to the terminal.

    @param data

    The data to write (string or BufferSource)

    @returns

    The number of bytes written