readlinePromises.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
(如果input
和output
流应被视为 TTY,并且写入了 ANSI/VT100 转义码)。默认值:在实例化时检查output
流上的isTTY
。¥
terminal
<boolean>true
if theinput
andoutput
streams should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Default: checkingisTTY
on theoutput
stream upon instantiation. -
history
<string[]> 历史行的初始列表。仅当terminal
由用户或内部的output
检查设置为true
时,此选项才有意义,否则历史缓存机制根本不会初始化。默认值:[]
。¥
history
<string[]> Initial list of history lines. This option makes sense only ifterminal
is set totrue
by the user or by an internaloutput
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 to0
. This option makes sense only ifterminal
is set totrue
by the user or by an internaloutput
check, otherwise the history caching mechanism is not initialized at all. Default:30
. -
removeHistoryDuplicates
<boolean> 如果为true
,则当添加到历史列表的新输入行与旧输入行重复时,这将从列表中删除旧行。默认值:false
。¥
removeHistoryDuplicates
<boolean> Iftrue
, 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
exceedscrlfDelay
milliseconds, both\r
and\n
will be treated as separate end-of-line input.crlfDelay
will be coerced to a number no less than100
. It can be set toInfinity
, 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>readlinePromises
将等待字符的时长(当以毫秒为单位读取不明确的键序列时,既可以使用目前读取的输入形成完整的键序列,又可以采用额外的输入来完成更长的键序列)。默认值:500
。¥
escapeCodeTimeout
<number> The durationreadlinePromises
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
.
-
-
返回:<readlinePromises.Interface>
¥Returns: <readlinePromises.Interface>
readlinePromises.createInterface()
方法创建新的 readlinePromises.Interface
实例。
¥The readlinePromises.createInterface()
method creates a new readlinePromises.Interface
instance.
import { createInterface } from 'node:readline/promises';
import { stdin, stdout } from 'node:process';
const rl = createInterface({
input: stdin,
output: stdout,
});
const { createInterface } = require('node:readline/promises');
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
一旦创建了 readlinePromises.Interface
实例,则最常见的场景就是监听 'line'
事件:
¥Once the readlinePromises.Interface
instance is created, the most common case
is to listen for the 'line'
event:
rl.on('line', (line) => {
console.log(`Received: ${line}`);
});
如果此实例的 terminal
是 true
,则如果它定义了 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).