tty(终端)


稳定性: 2 - 稳定

源代码: lib/tty.js

tty 模块提供了 tty.ReadStreamtty.WriteStream 类。 在大多数情况下,不需要也不可能直接地使用此模块。 但是,可以使用以下方法访问它:

const tty = require('tty');

当 Node.js 检测到它被运行时附加了一个文本终端(TTY),则默认情况下,process.stdin 会被初始化为 tty.ReadStream 的实例,process.stdoutprocess.stderr 会被初始化为 tty.WriteStream 的实例。 判断 Node.js 是否被运行在一个 TTY 上下文中的首选方法是检查 process.stdout.isTTY 属性的值是否为 true

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

在大多数情况下,应用程序几乎没有理由手动地创建 tty.ReadStreamtty.WriteStream 类的实例。

Stability: 2 - Stable

Source Code: lib/tty.js

The 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('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.