Bare Docs

bare-tty

Native TTY streams for JavaScript

Documented against v5.2.2
stable

bare-tty — Native TTY streams for JavaScript. It is a native addon and requires Bare >=1.16.0.

Mirrors the Node.js tty module.

npm i bare-tty

Usage

const tty = require('bare-tty')

const stdout = new tty.WriteStream(1)

stdout.write('Hello world!\n')

API

ReadStream

new ReadStream(fd: number, opts?: ReadStreamOptions)

Create a ReadStream for the TTY file descriptor fd.

Parameters

ParameterTypeDefaultDescription
fdnumber—The file descriptor of the TTY.
opts?ReadStreamOptions—Options; readBufferSize defaults to 65536 and allowHalfOpen to true.

ReadStream.fd: number

The stream's underlying file descriptor.

ReadStream.isTTY: true

Always true, identifying the stream as a TTY.

setMode(mode: number): this

Set the TTY mode directly, using one of the constants.mode values. Returns the stream.

Parameters

ParameterTypeDefaultDescription
modenumber—The TTY mode to set, one of the constants.mode values.

setRawMode(mode: boolean): this

Enable or disable raw mode. In raw mode, input is delivered byte-by-byte without line buffering or special key processing. Returns the stream.

As of v5.2.2, true sets constants.mode.RAW_VT rather than constants.mode.RAW. On Windows, RAW_VT has the console deliver virtual terminal sequences, so keys that libuv's own translation of console input leaves out reach the stream, such as Shift+Tab (\x1b[Z). On other platforms it behaves like RAW.

Parameters

ParameterTypeDefaultDescription
modeboolean—true to enable raw mode, false to return to normal mode.

WriteStream

new WriteStream(fd: number, opts?: WritableOptions)

Create a WriteStream for the TTY file descriptor fd.

Parameters

ParameterTypeDefaultDescription
fdnumber—The file descriptor of the TTY.
opts?WritableOptions——

columns: number

The current number of columns in the TTY.

WriteStream.fd: number

The stream's underlying file descriptor.

getWindowSize(): [width: number, height: number]

Return the current TTY dimensions as [width, height].

WriteStream.isTTY: true

Always true, identifying the stream as a TTY.

rows: number

The current number of rows in the TTY.

Functions

isTTY(fd: number): boolean

Return whether fd refers to a TTY.

Parameters

ParameterTypeDefaultDescription
fdnumber—The file descriptor to check.

Constants and variables

constants

constants: {
  mode: { NORMAL: number; RAW: number; RAW_VT: number; IO: number }
  state: { READING: number; CLOSING: number; CLOSED: number }
}

Native mode and internal state flags used by ReadStream/WriteStream, such as mode.NORMAL and mode.RAW.

Types

ReadStreamOptions

interface ReadStreamOptions {
  readBufferSize?: number
  encoding?: BufferEncoding
  read?(this: S, size: number): void
  eagerOpen?: boolean
  highWaterMark?: number
  signal?: AbortSignal
  open?(this: S, cb: StreamCallback): void
  predestroy?(this: S): void
  destroy?(this: S, err: Error | null, cb: StreamCallback): void
}

Classes

TTYError

class TTYError {
  code: string
}

bare-tty/constants

Constants and variables

constants.constants

constants: {
  mode: { NORMAL: number; RAW: number; RAW_VT: number; IO: number }
  state: { READING: number; CLOSING: number; CLOSED: number }
}

Native mode and internal state flags used by ReadStream/WriteStream, such as mode.NORMAL and mode.RAW.

bare-tty/errors

Classes

errors.TTYError

See also

  • Builds on bare-events, bare-signals, and bare-stream.
  • Bare modules — the full bare-* catalog.
  • Bare runtime API — the runtime these modules extend.

Last updated on

Was this helpful?

On this page