TTY#
Source Code:lib/tty.js
Thenode:tty module provides thetty.ReadStream andtty.WriteStreamclasses. In most cases, it will not be necessary or possible to use this moduledirectly. However, it can be accessed using:
const tty =require('node:tty');When Node.js detects that it is being run with a text terminal ("TTY")attached,process.stdin will, by default, be initialized as an instance oftty.ReadStream and bothprocess.stdout andprocess.stderr will, bydefault, be instances oftty.WriteStream. The preferred method of determiningwhether Node.js is being run within a TTY context is to check that the value oftheprocess.stdout.isTTY property istrue:
$node -p -e"Boolean(process.stdout.isTTY)"true$node -p -e"Boolean(process.stdout.isTTY)" |catfalseIn most cases, there should be little to no reason for an application tomanually create instances of thetty.ReadStream andtty.WriteStreamclasses.
Class:tty.ReadStream#
- Extends:<net.Socket>
Represents the readable side of a TTY. In normal circumstancesprocess.stdin will be the onlytty.ReadStream instance in a Node.jsprocess and there should be no reason to create additional instances.
readStream.isRaw#
Aboolean that istrue if the TTY is currently configured to operate as araw device.
This flag is alwaysfalse when a process starts, even if the terminal isoperating in raw mode. Its value will change with subsequent calls tosetRawMode.
readStream.setRawMode(mode)#
mode<boolean> Iftrue, configures thetty.ReadStreamto operate as araw device. Iffalse, configures thetty.ReadStreamto operate in itsdefault mode. ThereadStream.isRawproperty will be set to the resultingmode.- Returns:<this> The read stream instance.
Allows configuration oftty.ReadStream so that it operates as a raw device.
When in raw mode, input is always available character-by-character, notincluding modifiers. Additionally, all special processing of characters by theterminal is disabled, including echoing inputcharacters.Ctrl+C will no longer cause aSIGINT whenin this mode.
Class:tty.WriteStream#
- Extends:<net.Socket>
Represents the writable side of a TTY. In normal circumstances,process.stdout andprocess.stderr will be the onlytty.WriteStream instances created for a Node.js process and thereshould be no reason to create additional instances.
new tty.ReadStream(fd[, options])#
History
| Version | Changes |
|---|---|
| v0.9.4 | The |
| v0.5.8 | Added in: v0.5.8 |
fd<number> A file descriptor associated with a TTY.options<Object> Options passed to parentnet.Socket,seeoptionsofnet.Socketconstructor.- Returns:<tty.ReadStream>
Creates aReadStream forfd associated with a TTY.
new tty.WriteStream(fd)#
fd<number> A file descriptor associated with a TTY.- Returns:<tty.WriteStream>
Creates aWriteStream forfd associated with a TTY.
Event:'resize'#
The'resize' event is emitted whenever either of thewriteStream.columnsorwriteStream.rows properties have changed. No arguments are passed to thelistener callback when called.
process.stdout.on('resize',() => {console.log('screen size has changed!');console.log(`${process.stdout.columns}x${process.stdout.rows}`);});writeStream.clearLine(dir[, callback])#
History
| Version | Changes |
|---|---|
| v12.7.0 | The stream's write() callback and return value are exposed. |
| v0.7.7 | Added in: v0.7.7 |
dir<number>-1: to the left from cursor1: to the right from cursor0: the entire line
callback<Function> Invoked once the operation completes.- Returns:<boolean>
falseif the stream wishes for the calling code to waitfor the'drain'event to be emitted before continuing to write additionaldata; otherwisetrue.
writeStream.clearLine() clears the current line of thisWriteStream in adirection identified bydir.
writeStream.clearScreenDown([callback])#
History
| Version | Changes |
|---|---|
| v12.7.0 | The stream's write() callback and return value are exposed. |
| v0.7.7 | Added in: v0.7.7 |
callback<Function> Invoked once the operation completes.- Returns:<boolean>
falseif the stream wishes for the calling code to waitfor the'drain'event to be emitted before continuing to write additionaldata; otherwisetrue.
writeStream.clearScreenDown() clears thisWriteStream from the currentcursor down.
writeStream.columns#
Anumber specifying the number of columns the TTY currently has. This propertyis updated whenever the'resize' event is emitted.
writeStream.cursorTo(x[, y][, callback])#
History
| Version | Changes |
|---|---|
| v12.7.0 | The stream's write() callback and return value are exposed. |
| v0.7.7 | Added in: v0.7.7 |
x<number>y<number>callback<Function> Invoked once the operation completes.- Returns:<boolean>
falseif the stream wishes for the calling code to waitfor the'drain'event to be emitted before continuing to write additionaldata; otherwisetrue.
writeStream.cursorTo() moves thisWriteStream's cursor to the specifiedposition.
writeStream.getColorDepth([env])#
env<Object> An object containing the environment variables to check. Thisenables simulating the usage of a specific terminal.Default:process.env.- Returns:<number>
Returns:
1for 2,4for 16,8for 256,24for 16,777,216 colors supported.
Use this to determine what colors the terminal supports. Due to the nature ofcolors in terminals it is possible to either have false positives or falsenegatives. It depends on process information and the environment variables thatmay lie about what terminal is used.It is possible to pass in anenv object to simulate the usage of a specificterminal. This can be useful to check how specific environment settings behave.
To enforce a specific color support, use one of the below environment settings.
- 2 colors:
FORCE_COLOR = 0(Disables colors) - 16 colors:
FORCE_COLOR = 1 - 256 colors:
FORCE_COLOR = 2 - 16,777,216 colors:
FORCE_COLOR = 3
Disabling color support is also possible by using theNO_COLOR andNODE_DISABLE_COLORS environment variables.
writeStream.getWindowSize()#
- Returns:<number[]>
writeStream.getWindowSize() returns the size of the TTYcorresponding to thisWriteStream. The array is of the type[numColumns, numRows] wherenumColumns andnumRows represent the numberof columns and rows in the corresponding TTY.
writeStream.hasColors([count][, env])#
count<integer> The number of colors that are requested (minimum 2).Default: 16.env<Object> An object containing the environment variables to check. Thisenables simulating the usage of a specific terminal.Default:process.env.- Returns:<boolean>
Returnstrue if thewriteStream supports at least as many colors as providedincount. Minimum support is 2 (black and white).
This has the same false positives and negatives as described inwriteStream.getColorDepth().
process.stdout.hasColors();// Returns true or false depending on if `stdout` supports at least 16 colors.process.stdout.hasColors(256);// Returns true or false depending on if `stdout` supports at least 256 colors.process.stdout.hasColors({TMUX:'1' });// Returns true.process.stdout.hasColors(2 **24, {TMUX:'1' });// Returns false (the environment setting pretends to support 2 ** 8 colors).writeStream.moveCursor(dx, dy[, callback])#
History
| Version | Changes |
|---|---|
| v12.7.0 | The stream's write() callback and return value are exposed. |
| v0.7.7 | Added in: v0.7.7 |
dx<number>dy<number>callback<Function> Invoked once the operation completes.- Returns:<boolean>
falseif the stream wishes for the calling code to waitfor the'drain'event to be emitted before continuing to write additionaldata; otherwisetrue.
writeStream.moveCursor() moves thisWriteStream's cursorrelative to itscurrent position.
writeStream.rows#
Anumber specifying the number of rows the TTY currently has. This propertyis updated whenever the'resize' event is emitted.
tty.isatty(fd)#
Thetty.isatty() method returnstrue if the givenfd is associated witha TTY andfalse if it is not, including wheneverfd is not a non-negativeinteger.