- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v20.18.0 文档
- Node.js v20.18.0
-
目录
- TTY
- 类:
tty.ReadStream
- 类:
tty.WriteStream
new tty.ReadStream(fd[, options])
new tty.WriteStream(fd)
- 事件:
'resize'
writeStream.clearLine(dir[, callback])
writeStream.clearScreenDown([callback])
writeStream.columns
writeStream.cursorTo(x[, y][, callback])
writeStream.getColorDepth([env])
writeStream.getWindowSize()
writeStream.hasColors([count][, env])
writeStream.isTTY
writeStream.moveCursor(dx, dy[, callback])
writeStream.rows
tty.isatty(fd)
- 类:
- TTY
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
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.
类:tty.ReadStream
#
¥Class: tty.ReadStream
-
继承:<net.Socket>
¥Extends: <net.Socket>
代表终端的可读端。在正常情况下,process.stdin
将是 Node.js 进程中唯一的 tty.ReadStream
实例,应该没有理由创建额外的实例。
¥Represents the readable side of a TTY. In normal circumstances
process.stdin
will be the only tty.ReadStream
instance in a Node.js
process and there should be no reason to create additional instances.
readStream.isRaw
#
boolean
值,如果终端当前配置为作为原始设备运行,则为 true
。
¥A boolean
that is true
if the TTY is currently configured to operate as a
raw device.
当进程启动时,此标志始终为 false
,即使终端在原始模式下运行也是如此。它的值将随着对 setRawMode
的后续调用而改变。
¥This flag is always false
when a process starts, even if the terminal is
operating in raw mode. Its value will change with subsequent calls to
setRawMode
.
readStream.isTTY
#
boolean
值,对于 tty.ReadStream
实例,始终为 true
。
¥A boolean
that is always true
for tty.ReadStream
instances.
readStream.setRawMode(mode)
#
-
mode
<boolean> 如果为true
,则将tty.ReadStream
配置为作为原始设备运行。如果为false
,则将tty.ReadStream
配置为在其默认模式下运行。readStream.isRaw
属性将设置为结果模式。¥
mode
<boolean> Iftrue
, configures thetty.ReadStream
to operate as a raw device. Iffalse
, configures thetty.ReadStream
to operate in its default mode. ThereadStream.isRaw
property will be set to the resulting mode. -
返回:<this> 读取流实例。
¥Returns: <this> The read stream instance.
允许配置 tty.ReadStream
,使其作为原始设备运行。
¥Allows configuration of tty.ReadStream
so that it operates as a raw device.
当在原始模式下时,输入总是逐个字符可用,不包括修饰符。此外,终端对字符的所有特殊处理都被禁用,包括回显输入字符。在此模式下,Ctrl+C 将不再导致 SIGINT
。
¥When in raw mode, input is always available character-by-character, not
including modifiers. Additionally, all special processing of characters by the
terminal is disabled, including echoing input
characters. Ctrl+C will no longer cause a SIGINT
when
in this mode.
类:tty.WriteStream
#
¥Class: tty.WriteStream
-
继承:<net.Socket>
¥Extends: <net.Socket>
代表终端的可写端。在正常情况下,process.stdout
和 process.stderr
将是为 Node.js 进程创建的唯一的 tty.WriteStream
实例,应该没有理由创建额外的实例。
¥Represents the writable side of a TTY. In normal circumstances,
process.stdout
and process.stderr
will be the only
tty.WriteStream
instances created for a Node.js process and there
should be no reason to create additional instances.
new tty.ReadStream(fd[, options])
#
-
fd
<number> 与 TTY 关联的文件描述符。¥
fd
<number> A file descriptor associated with a TTY. -
options
<Object> 传递给父net.Socket
的选项,请参阅net.Socket
构造函数 中的options
。¥
options
<Object> Options passed to parentnet.Socket
, seeoptions
ofnet.Socket
constructor. -
¥Returns <tty.ReadStream>
为与 TTY 关联的 fd
创建 ReadStream
。
¥Creates a ReadStream
for fd
associated with a TTY.
new tty.WriteStream(fd)
#
-
fd
<number> 与 TTY 关联的文件描述符。¥
fd
<number> A file descriptor associated with a TTY. -
¥Returns <tty.WriteStream>
为与 TTY 关联的 fd
创建 WriteStream
。
¥Creates a WriteStream
for fd
associated with a TTY.
事件:'resize'
#
¥Event: 'resize'
每当 writeStream.columns
或 writeStream.rows
属性发生更改时,则会触发 'resize'
事件。当调用时,没有参数传给监听器回调。
¥The 'resize'
event is emitted whenever either of the writeStream.columns
or writeStream.rows
properties have changed. No arguments are passed to the
listener callback when called.
process.stdout.on('resize', () => {
console.log('screen size has changed!');
console.log(`${process.stdout.columns}x${process.stdout.rows}`);
});
writeStream.clearLine(dir[, callback])
#
-
dir
<number>-
-1
:从光标向左¥
-1
: to the left from cursor -
1
:从光标向右¥
1
: to the right from cursor -
0
:整行¥
0
: the entire line
-
-
callback
<Function> 操作完成后调用。¥
callback
<Function> Invoked once the operation completes. -
返回:<boolean>
false
如果流希望调用代码在继续写入附加数据之前等待'drain'
事件触发;否则true
。¥Returns: <boolean>
false
if the stream wishes for the calling code to wait for the'drain'
event to be emitted before continuing to write additional data; otherwisetrue
.
writeStream.clearLine()
在 dir
标识的方向上清除此 WriteStream
的当前行。
¥writeStream.clearLine()
clears the current line of this WriteStream
in a
direction identified by dir
.
writeStream.clearScreenDown([callback])
#
-
callback
<Function> 操作完成后调用。¥
callback
<Function> Invoked once the operation completes. -
返回:<boolean>
false
如果流希望调用代码在继续写入附加数据之前等待'drain'
事件触发;否则true
。¥Returns: <boolean>
false
if the stream wishes for the calling code to wait for the'drain'
event to be emitted before continuing to write additional data; otherwisetrue
.
writeStream.clearScreenDown()
从当前光标向下清除此 WriteStream
。
¥writeStream.clearScreenDown()
clears this WriteStream
from the current
cursor down.
writeStream.columns
#
number
指定终端当前具有的列数。每当触发 'resize'
事件时,则会更新此属性。
¥A number
specifying the number of columns the TTY currently has. This property
is updated whenever the 'resize'
event is emitted.
writeStream.cursorTo(x[, y][, callback])
#
-
x
<number> -
y
<number> -
callback
<Function> 操作完成后调用。¥
callback
<Function> Invoked once the operation completes. -
返回:<boolean>
false
如果流希望调用代码在继续写入附加数据之前等待'drain'
事件触发;否则true
。¥Returns: <boolean>
false
if the stream wishes for the calling code to wait for the'drain'
event to be emitted before continuing to write additional data; otherwisetrue
.
writeStream.cursorTo()
将此 WriteStream
的光标移动到指定位置。
¥writeStream.cursorTo()
moves this WriteStream
's cursor to the specified
position.
writeStream.getColorDepth([env])
#
-
env
<Object> 包含要检查的环境变量的对象。这使得模拟特定终端的使用成为可能。默认值:process.env
。¥
env
<Object> An object containing the environment variables to check. This enables simulating the usage of a specific terminal. Default:process.env
. -
返回:<number>
¥Returns: <number>
返回:
¥Returns:
-
1
表示支持 2 种颜色,¥
1
for 2, -
4
表示支持 16 种颜色,¥
4
for 16, -
8
表示支持 256 种颜色,¥
8
for 256, -
24
表示支持 16,777,216 种颜色。¥
24
for 16,777,216 colors supported.
使用此来确定终端支持的颜色。由于终端颜色的性质,可能出现误报或漏报。这取决于进程信息和环境变量,这些可能与使用的终端有关。可以传入 env
对象来模拟特定终端的使用。这对于检查特定环境设置的行为方式很有用。
¥Use this to determine what colors the terminal supports. Due to the nature of
colors in terminals it is possible to either have false positives or false
negatives. It depends on process information and the environment variables that
may lie about what terminal is used.
It is possible to pass in an env
object to simulate the usage of a specific
terminal. This can be useful to check how specific environment settings behave.
要强制特定的颜色支持,则使用以下环境设置之一。
¥To enforce a specific color support, use one of the below environment settings.
-
2 种颜色:
FORCE_COLOR = 0
(禁用颜色)¥2 colors:
FORCE_COLOR = 0
(Disables colors) -
16 种颜色:
FORCE_COLOR = 1
¥16 colors:
FORCE_COLOR = 1
-
256 种颜色:
FORCE_COLOR = 2
¥256 colors:
FORCE_COLOR = 2
-
16,777,216 种颜色:
FORCE_COLOR = 3
¥16,777,216 colors:
FORCE_COLOR = 3
使用 NO_COLOR
和 NODE_DISABLE_COLORS
环境变量也可以禁用颜色支持。
¥Disabling color support is also possible by using the NO_COLOR
and
NODE_DISABLE_COLORS
environment variables.
writeStream.getWindowSize()
#
-
返回:<number[]>
¥Returns: <number[]>
writeStream.getWindowSize()
返回此 WriteStream
对应的终端的尺寸。该数组的类型为 [numColumns, numRows]
,其中 numColumns
和 numRows
表示相应终端中的列数和行数。
¥writeStream.getWindowSize()
returns the size of the TTY
corresponding to this WriteStream
. The array is of the type
[numColumns, numRows]
where numColumns
and numRows
represent the number
of columns and rows in the corresponding TTY.
writeStream.hasColors([count][, env])
#
-
count
<integer> 请求的颜色数量(最小为 2)。默认值:16.¥
count
<integer> The number of colors that are requested (minimum 2). Default: 16. -
env
<Object> 包含要检查的环境变量的对象。这使得模拟特定终端的使用成为可能。默认值:process.env
。¥
env
<Object> An object containing the environment variables to check. This enables simulating the usage of a specific terminal. Default:process.env
. -
返回:<boolean>
¥Returns: <boolean>
如果 writeStream
支持的颜色至少与 count
中提供的颜色一样多,则返回 true
。最小支持为 2(黑色和白色)。
¥Returns true
if the writeStream
supports at least as many colors as provided
in count
. Minimum support is 2 (black and white).
这具有与 writeStream.getColorDepth()
中描述的相同的误报和漏报。
¥This has the same false positives and negatives as described in
writeStream.getColorDepth()
.
process.stdout.hasColors();
// Returns true or false depending on if `stdout` supports at least 16 colors.
process.stdout.hasColors(256);
// Returns true or false depending on if `stdout` supports at least 256 colors.
process.stdout.hasColors({ TMUX: '1' });
// Returns true.
process.stdout.hasColors(2 ** 24, { TMUX: '1' });
// Returns false (the environment setting pretends to support 2 ** 8 colors).
writeStream.isTTY
#
boolean
值,始终为 true
。
¥A boolean
that is always true
.
writeStream.moveCursor(dx, dy[, callback])
#
-
dx
<number> -
dy
<number> -
callback
<Function> 操作完成后调用。¥
callback
<Function> Invoked once the operation completes. -
返回:<boolean>
false
如果流希望调用代码在继续写入附加数据之前等待'drain'
事件触发;否则true
。¥Returns: <boolean>
false
if the stream wishes for the calling code to wait for the'drain'
event to be emitted before continuing to write additional data; otherwisetrue
.
writeStream.moveCursor()
将此 WriteStream
的光标相对于其当前位置移动。
¥writeStream.moveCursor()
moves this WriteStream
's cursor relative to its
current position.
writeStream.rows
#
number
指定终端当前具有的行数。每当触发 'resize'
事件时,则会更新此属性。
¥A number
specifying the number of rows the TTY currently has. This property
is updated whenever the 'resize'
event is emitted.
tty.isatty(fd)
#
如果给定的 fd
与终端关联,则 tty.isatty()
方法返回 true
,否则返回 false
,包括每当 fd
不是非负整数时。
¥The tty.isatty()
method returns true
if the given fd
is associated with
a TTY and false
if it is not, including whenever fd
is not a non-negative
integer.