tty(终端)
tty
模块提供 tty.ReadStream
和 tty.WriteStream
类。
在大多数情况下,没有必要或可能直接使用此模块。
可以使用以下方法访问它:
const tty = require('tty');
当 Node.js 检测到它附加了文本终端(TTY)时,默认情况下,process.stdin
将被初始化为 tty.ReadStream
的一个实例,process.stdout
和 process.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.ReadStream
和 tty.WriteStream
类的实例。
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.