tty 终端
源代码: lib/tty.js
node:tty
模块提供了 tty.ReadStream
和 tty.WriteStream
类。
在大多数情况下,没有必要或不可能直接使用此模块。
但是,可以使用以下方式访问它:
const tty = require('node:tty');
当 Node.js 检测到其运行时绑定了文本终端,则默认情况下,process.stdin
将被初始化为 tty.ReadStream
的实例,而 process.stdout
和 process.stderr
都将默认为 tty.WriteStream
的实例。
确定 Node.js 是否在终端上下文中运行的首选方法是检查 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
类的实例。
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.