Node.js v24.19.0 documentation
- Node.js v24.19.0
-
Table of contents
- TTY
- Class:
tty.ReadStream - Class:
tty.WriteStreamnew tty.ReadStream(fd[, options])new tty.WriteStream(fd)- Event:
'resize' writeStream.clearLine(dir[, callback])writeStream.clearScreenDown([callback])writeStream.columnswriteStream.cursorTo(x[, y][, callback])writeStream.getColorDepth([env])writeStream.getWindowSize()writeStream.hasColors([count][, env])writeStream.isTTYwriteStream.moveCursor(dx, dy[, callback])writeStream.rows
tty.isatty(fd)
- Class:
- TTY
-
Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Environment Variables
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
TTY#
Source Code: lib/tty.js
The node:tty module provides the tty.ReadStream and tty.WriteStream
classes. In most cases, it will not be necessary or possible to use this module
directly. 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 of
tty.ReadStream and both process.stdout and process.stderr will, by
default, be instances of tty.WriteStream. The preferred method of determining
whether Node.js is being run within a TTY context is to check that the value of
the process.stdout.isTTY property is true:
$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false
In most cases, there should be little to no reason for an application to
manually create instances of the tty.ReadStream and tty.WriteStream
classes.
Class: tty.ReadStream#
- Extends: <net.Socket>
Represents the readable side of a TTY. In normal circumstances
process.stdin will be the only tty.ReadStream instance in a Node.js
process and there should be no reason to create additional instances.
readStream.isRaw#
A boolean that is true if the TTY is currently configured to operate as a
raw device.
This flag is always false when a process starts, even if the terminal is
operating in raw mode. Its value will change with subsequent calls to
setRawMode.
readStream.isTTY#
A boolean that is always true for tty.ReadStream instances.
readStream.setRawMode(mode)#
mode<boolean> Iftrue, configures thetty.ReadStreamto operate as a raw device. Iffalse, configures thetty.ReadStreamto operate in its default mode. ThereadStream.isRawproperty will be set to the resulting mode.- Returns: <this> The read stream instance.
Allows configuration of tty.ReadStream so that it operates as a raw device.
When in raw mode, input is always available character-by-character, not
including modifiers. Additionally, all special processing of input characters
by the terminal is disabled, including echoing input
characters. Ctrl+C will no longer cause a SIGINT when
in this mode. This mode does not affect terminal output processing, such as
newline translation on Unix terminals.
Class: tty.WriteStream#
- Extends: <net.Socket>
Represents the writable side of a TTY. In normal circumstances,
process.stdout and process.stderr will be the only
tty.WriteStream instances created for a Node.js process and there
should be no reason to create additional instances.
new tty.ReadStream(fd[, options])#
fd<number> A file descriptor associated with a TTY.options<Object> Options passed to parentnet.Socket, seeoptionsofnet.Socketconstructor.- Returns: <tty.ReadStream>
Creates a ReadStream for fd associated with a TTY.
new tty.WriteStream(fd)#
fd<number> A file descriptor associated with a TTY.- Returns: <tty.WriteStream>
Creates a WriteStream for fd associated with a TTY.
Event: 'resize'#
The 'resize' event is emitted whenever either of the writeStream.columns
or writeStream.rows properties have changed. No arguments are passed to the
listener 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])#
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 wait for the'drain'event to be emitted before continuing to write additional data; otherwisetrue.
writeStream.clearLine() clears the current line of this WriteStream in a
direction identified by dir.
writeStream.clearScreenDown([callback])#
callback<Function> Invoked once the operation completes.- Returns: <boolean>
falseif the stream wishes for the calling code to wait for the'drain'event to be emitted before continuing to write additional data; otherwisetrue.
writeStream.clearScreenDown() clears this WriteStream from the current
cursor down.
writeStream.columns#
A number specifying the number of columns the TTY currently has. This property
is updated whenever the 'resize' event is emitted.