- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- 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 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
Node.js v24.13.0 文档
- Node.js v24.13.0
-
目录
- 文字电话
- 类:
tty.ReadStream - 类:
tty.WriteStreamnew tty.ReadStream(fd[, options])new tty.WriteStream(fd)- 事件:
'resize' writeStream.clearLine(dir[, callback])writeStream.clearScreenDown([callback])writeStream.columnswriteStream.cursorTo(x[, y][, callback])writeStream.getColorDepth([env])writeStream.getWindowSize()writeStream.hasColors([count][, env])writeStream.isTTYwriteStream.moveCursor(dx, dy[, callback])writeStream.rows
tty.isatty(fd)
- 类:
- 文字电话
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- 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 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- 其他版本
文字电话#>
【TTY】
源代码: 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 是否在 TTY 环境中运行的首选方法是检查 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>
表示 TTY 的可读端。在正常情况下,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类型,如果 TTY 当前配置为作为原始设备操作,则为 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属性将被设置为最终模式。- 返回值: <this> 读取流实例。
允许配置 tty.ReadStream,使其作为原始设备运行。
【Allows configuration of tty.ReadStream so that it operates as a raw device.】
在原始模式下,输入始终可以逐字符获得,不包括修饰符。此外,终端对字符的所有特殊处理都被禁用,包括回显输入字符。在此模式下,Ctrl+C 将不再引发 SIGINT。
类:tty.WriteStream#>
【Class: tty.WriteStream】
- 继承: <net.Socket>
表示 TTY 的可写端。在正常情况下,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 相关的文件描述符。options<Object> 传递给父net.Socket的选项, 参见net.Socket构造函数 的options。- 返回值: <tty.ReadStream>
为与 TTY 关联的 fd 创建一个 ReadStream。
【Creates a ReadStream for fd associated with a TTY.】
new tty.WriteStream(fd)#>
fd<number> 与 TTY 相关联的文件描述符。- 返回: <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:从光标向右0:整行
callback<Function> 在操作完成后调用。- 返回值:<boolean> 如果流希望调用代码等待
'drain'事件触发后再继续写入额外数据,则返回false;否则返回true。
writeStream.clearLine() 会根据 dir 指定的方向清除此 WriteStream 的当前行。
writeStream.clearScreenDown([callback])#>
callback<Function> 操作完成后调用一次。- 返回值: <boolean> 如果流希望调用代码在继续写入额外数据之前等待
'drain'事件触发,则返回false;否则返回true。
writeStream.clearScreenDown() 会从当前光标位置向下清除此 WriteStream 的内容。
writeStream.columns#>
一个 number,指定 TTY 当前的列数。每当 '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> 在操作完成后调用。- 返回值: <boolean> 如果流希望调用代码在继续写入额外数据之前等待
'drain'事件被触发,则返回false;否则返回true。
writeStream.cursorTo() 将此 WriteStream 的光标移动到指定位置。
writeStream.getColorDepth([env])#>
返回:
【Returns:】
1对应 2,4对应 16,8对应 256,24对应 16,777,216 种颜色支持。
使用这个来确定终端支持哪些颜色。由于终端中色彩的特性,可能会出现假阳性或假阴性。这取决于进程信息和可能对所使用终端有所掩饰的环境变量。可以传入一个 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(禁用颜色) - 16 种颜色:
FORCE_COLOR = 1 - 256 种颜色:
FORCE_COLOR = 2 - 1677 万种颜色:
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[]>
writeStream.getWindowSize() 返回与此 WriteStream 对应的 TTY 的大小。该数组的类型为 [numColumns, numRows],其中 numColumns 和 numRows 分别表示对应 TTY 的列数和行数。
writeStream.hasColors([count][, env])#>
count<integer> 请求的颜色数量(最少 2 种)。 默认值: 16。env<Object> 包含要检查的环境变量的对象。
这可以模拟特定终端的使用情况。默认值:
process.env。- 返回值:<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#>
一个总是 true 的 boolean。
【A boolean that is always true.】
writeStream.moveCursor(dx, dy[, callback])#>
dx<number>dy<number>callback<Function> 在操作完成后调用。- 返回值: <boolean> 如果流希望调用代码在继续写入额外数据之前等待
'drain'事件被触发,则返回false;否则返回true。
writeStream.moveCursor() 会将此 WriteStream 的光标相对于当前位置移动。
writeStream.rows#>
一个 number,指定 TTY 当前的行数。每当 '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)#>
tty.isatty() 方法如果给定的 fd 与一个 TTY 相关联则返回 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.】