Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node.js TTY


May 10, 2021 Node.js


Table of contents


Tty

稳定性: 2 - 不稳定

Node.js tty tty.ReadStream and tty.WriteStream class, in most cases, you don't have to use the module directly, and the methods for accessing the module are as follows:

const tty = require('tty');

When node detects that it is running in the TTY context, process.stdin will be tty.ReadStream instance, and process.stdout be tty.WriteStream instance. A good way to detect whether node is running in the TTY context process.stdout.isTTY

$ node -p -e "Boolean(process.stdout.isTTY)"
true
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
false

tty.isatty(fd)

If fd with the terminal to return true otherwise false is false

tty.setRawMode(mode)

had been abandoned. U se tty.ReadStream#setRawMode() process.stdin.setRawMode() replacement.

Class: ReadStream

net.Socket sub-class of Socket that represents the readable part of ttty. T ypically, in any node program (only if isatty(0) is true), process.stdin is tty.ReadStream

rs.isRaw

Boolean value, which defaults to false I t represents the tty.ReadStream state of the ReadStream instance.

rs.setRawMode(mode)

mode needs to true or false I t sets tty.ReadStream property is the original device or default. isRaw be set to result mode.

Class: WriteStream

net.Socket sub-class of Socket that represents the writeable part of ttty. T ypically, process.stdout is tty.WriteStream unique instance (only if isatty(1) is true).

ws.columns

The number of columns currently owned by TTY. This value is updated when the "resize" event is triggered.

ws.rows

The number of rows currently owned by TTY. This value is updated when the "resize" event is triggered.

Event: 'resize'

function () {}

The refreshSize() event is triggered when a row or column changes.

process.stdout.on('resize', function() {
  console.log('screen size has changed!');
  console.log(process.stdout.columns + 'x' + process.stdout.rows);
});