TTY
¥Stability: 2 - Stable
源代码: lib/tty.js
node:tty
模块提供了 tty.ReadStream
和 tty.WriteStream
类。在大多数情况下,没有必要或不可能直接使用此模块。但是,可以使用以下方式访问它:
¥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');
当 Node.js 检测到它正在与附加的文本终端 ("TTY") 一起运行时,默认情况下,process.stdin
将被初始化为 tty.ReadStream
的实例,而 process.stdout
和 process.stderr
将默认为 tty.WriteStream
的实例。确定 Node.js 是否在终端上下文中运行的首选方法是检查 process.stdout.isTTY
属性的值是否为 true
:
¥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
在大多数情况下,应用几乎没有理由手动创建 tty.ReadStream
和 tty.WriteStream
类的实例。
¥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.