readline.createInterface(options)


  • options <Object>

    • input <stream.Readable> 要收听的 可读 流。此选项是必需的。

      ¥input <stream.Readable> The Readable stream to listen to. This option is required.

    • output <stream.Writable> 要写入 readline 数据的 可写 流。

      ¥output <stream.Writable> The Writable stream to write readline data to.

    • completer <Function> 可选的用于制表符自动补全的函数。

      ¥completer <Function> An optional function used for Tab autocompletion.

    • terminal <boolean> true(如果 inputoutput 流应被视为 TTY,并且写入了 ANSI/VT100 转义码)。默认值:在实例化时检查 output 流上的 isTTY

      ¥terminal <boolean> true if the input and output streams should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Default: checking isTTY on the output stream upon instantiation.

    • history <string[]> 历史行的初始列表。仅当 terminal 由用户或内部的 output 检查设置为 true 时,此选项才有意义,否则历史缓存机制根本不会初始化。默认值:[]

      ¥history <string[]> Initial list of history lines. This option makes sense only if terminal is set to true by the user or by an internal output check, otherwise the history caching mechanism is not initialized at all. Default: [].

    • historySize <number> 保留的最大历史行数。要禁用历史记录,则将此值设置为 0。仅当 terminal 由用户或内部的 output 检查设置为 true 时,此选项才有意义,否则历史缓存机制根本不会初始化。默认值:30

      ¥historySize <number> Maximum number of history lines retained. To disable the history set this value to 0. This option makes sense only if terminal is set to true by the user or by an internal output check, otherwise the history caching mechanism is not initialized at all. Default: 30.

    • removeHistoryDuplicates <boolean> 如果为 true,则当添加到历史列表的新输入行与旧输入行重复时,这将从列表中删除旧行。默认值:false

      ¥removeHistoryDuplicates <boolean> If true, when a new input line added to the history list duplicates an older one, this removes the older line from the list. Default: false.

    • prompt <string> 要使用的提示字符串。默认值:'> '

      ¥prompt <string> The prompt string to use. Default: '> '.

    • crlfDelay <number> 如果 \r\n 之间的延迟超过 crlfDelay 毫秒,则 \r\n 都将被视为单独的行尾输入。crlfDelay 将被强制为不小于 100 的数字。它可以设置为 Infinity,在这种情况下,\r 后跟 \n 将始终被视为单个换行符(这对于带有 \r\n 行分隔符的 读取文件 可能是合理的)。默认值:100

      ¥crlfDelay <number> If the delay between \r and \n exceeds crlfDelay milliseconds, both \r and \n will be treated as separate end-of-line input. crlfDelay will be coerced to a number no less than 100. It can be set to Infinity, in which case \r followed by \n will always be considered a single newline (which may be reasonable for reading files with \r\n line delimiter). Default: 100.

    • escapeCodeTimeout <number> readline 将等待字符的时长(当以毫秒为单位读取不明确的键序列时,既可以使用目前读取的输入形成完整的键序列,又可以采用额外的输入来完成更长的键序列)。默认值:500

      ¥escapeCodeTimeout <number> The duration readline will wait for a character (when reading an ambiguous key sequence in milliseconds one that can both form a complete key sequence using the input read so far and can take additional input to complete a longer key sequence). Default: 500.

    • tabSize <integer> 一个制表符等于的空格数(最小为 1)。默认值:8

      ¥tabSize <integer> The number of spaces a tab is equal to (minimum 1). Default: 8.

    • signal <AbortSignal> 允许使用中止信号关闭接口。中止信号将在内部调用接口上的 close

      ¥signal <AbortSignal> Allows closing the interface using an AbortSignal. Aborting the signal will internally call close on the interface.

  • 返回:<readline.Interface>

    ¥Returns: <readline.Interface>

readline.createInterface() 方法创建新的 readline.Interface 实例。

¥The readline.createInterface() method creates a new readline.Interface instance.

import { createInterface } from 'node:readline';
import { stdin, stdout } from 'node:process';
const rl = createInterface({
  input: stdin,
  output: stdout,
});const { createInterface } = require('node:readline');
const rl = createInterface({
  input: process.stdin,
  output: process.stdout,
});

一旦创建了 readline.Interface 实例,则最常见的场景就是监听 'line' 事件:

¥Once the readline.Interface instance is created, the most common case is to listen for the 'line' event:

rl.on('line', (line) => {
  console.log(`Received: ${line}`);
}); 

如果此实例的 terminaltrue,则如果它定义了 output.columns 属性,并且如果或当列发生变化时(process.stdout 会当其是终端时自动执行此操作)在 output 上触发 'resize' 事件,则 output 流将获得最佳的兼容性。

¥If terminal is true for this instance then the output stream will get the best compatibility if it defines an output.columns property and emits a 'resize' event on the output if or when the columns ever change (process.stdout does this automatically when it is a TTY).

当使用 stdin 作为输入创建 readline.Interface 时,程序将不会终止,直到它收到 EOF 字符。要在不等待用户输入的情况下退出,则调用 process.stdin.unref()

¥When creating a readline.Interface using stdin as input, the program will not terminate until it receives an EOF character. To exit without waiting for user input, call process.stdin.unref().