Node.js v24.13.0 文档


文字电话#>

【TTY】

源代码: lib/tty.js

node:tty 模块提供了 tty.ReadStreamtty.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.stdoutprocess.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.ReadStreamtty.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

表示 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

表示 TTY 的可写端。在正常情况下,process.stdoutprocess.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])#>

为与 TTY 关联的 fd 创建一个 ReadStream

【Creates a ReadStream for fd associated with a TTY.】

new tty.WriteStream(fd)#>

为与 TTY 关联的 fd 创建一个 WriteStream

【Creates a WriteStream for fd associated with a TTY.】

事件:'resize'#>

【Event: 'resize'

每当 writeStream.columnswriteStream.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])#>

  • env <Object> 一个包含要检查的环境变量的对象。这可以模拟特定终端的使用。默认值: process.env
  • 返回值:<number>

返回:

【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_COLORNODE_DISABLE_COLORS 环境变量来禁用颜色支持。

【Disabling color support is also possible by using the NO_COLOR and NODE_DISABLE_COLORS environment variables.】

writeStream.getWindowSize()#>

writeStream.getWindowSize() 返回与此 WriteStream 对应的 TTY 的大小。该数组的类型为 [numColumns, numRows],其中 numColumnsnumRows 分别表示对应 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#>

一个总是 trueboolean

【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.】

Node.js 中文网 - 粤ICP备13048890号