Node.js v16.20.0 文档


TTY#

稳定性: 2 - 稳定的

¥Stability: 2 - Stable

源代码: 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 是否在终端上下文中运行的首选方法是检查 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

代表终端的可读端。在正常情况下,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。默认为 false

¥A boolean that is true if the TTY is currently configured to operate as a raw device. Defaults to false.

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> If true, configures the tty.ReadStream to operate as a raw device. If false, configures the tty.ReadStream to operate in its default mode. The readStream.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

代表终端的可写端。在正常情况下,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.

事件:'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: 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; otherwise true.

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; otherwise true.

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; otherwise true.

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_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 对应的终端的尺寸。该数组的类型为 [numColumns, numRows],其中 numColumnsnumRows 表示相应终端中的列数和行数。

¥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; otherwise true.

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.

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