Options for creating a pseudo-terminal (PTY).
constructor
Terminal.constructor
Referenced types
interface TerminalOptions
- data?: (terminal: Terminal, data: Uint8Array<ArrayBuffer>) => void
Callback invoked when data is received from the terminal.
- 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.
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`
- 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.
Async dispose for use with
await using.Close the terminal.
Reference the terminal to keep the event loop alive.
- @param cols
New number of columns
@param rowsNew number of rows
- enabled: boolean): void;
Set raw mode on the terminal. In raw mode, input is passed directly without processing.
@param enabledWhether to enable raw mode
Unreference the terminal to allow the event loop to exit.
- @param data
The data to write (string or BufferSource)
@returnsThe number of bytes written