readline.createInterface(options)
options<Object>input<stream.Readable> 要监听的可读流。 此选项是必需的。output<stream.Writable> 要将逐行读取的数据写入的可写流。completer<Function> 可选的用于制表符自动补全的函数。terminal<boolean> 如果input和output流应该被视为终端,并且写入了 ANSI/VT100 转义码,则为true。 默认值: 在实例化时检查output流上的isTTY。historySize<number> 保留的最大历史行数。 要禁用历史记录,则将此值设置为0。 仅当terminal由用户或内部的output检查设置为true时,此选项才有意义,否则历史缓存机制根本不会初始化。 默认值:30。prompt<string> 要使用的提示字符串。 默认值:'> '。crlfDelay<number> 如果\r和\n之间的延迟超过crlfDelay毫秒,则\r和\n都将被视为单独的行尾输入。crlfDelay将被强制为不小于100的数字。 它可以设置为Infinity,在这种情况下,\r后跟\n将始终被视为单个换行符(这对于具有\r\n行分隔符的文件读取可能是合理的)。 默认值:100。removeHistoryDuplicates<boolean> 如果为true,则当添加到历史列表的新输入行与旧输入行重复时,这将从列表中删除旧行。 默认值:false。escapeCodeTimeout<number>readline将等待字符的时长(当以毫秒为单位读取不明确的键序列时,既可以使用目前读取的输入形成完整的键序列,又可以采用额外的输入来完成更长的键序列)。 默认值:500。
readline.createInterface() 方法创建新的 readline.Interface 实例。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});一旦创建了 readline.Interface 实例,则最常见的场景就是监听 'line' 事件:
rl.on('line', (line) => {
console.log(`Received: ${line}`);
});如果此实例的 terminal 是 true,则如果它定义了 output.columns 属性,并且如果或当列发生变化时(process.stdout 会当其是终端时自动执行此操作)在 output 上触发 'resize' 事件,则 output 流将获得最佳的兼容性。
options<Object>input<stream.Readable> The Readable stream to listen to. This option is required.output<stream.Writable> The Writable stream to write readline data to.completer<Function> An optional function used for Tab autocompletion.terminal<boolean>trueif theinputandoutputstreams should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Default: checkingisTTYon theoutputstream upon instantiation.historySize<number> Maximum number of history lines retained. To disable the history set this value to0. This option makes sense only ifterminalis set totrueby the user or by an internaloutputcheck, otherwise the history caching mechanism is not initialized at all. Default:30.prompt<string> The prompt string to use. Default:'> '.crlfDelay<number> If the delay between\rand\nexceedscrlfDelaymilliseconds, both\rand\nwill be treated as separate end-of-line input.crlfDelaywill be coerced to a number no less than100. It can be set toInfinity, in which case\rfollowed by\nwill always be considered a single newline (which may be reasonable for reading files with\r\nline delimiter). Default:100.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.escapeCodeTimeout<number> The durationreadlinewill 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.
The readline.createInterface() method creates a new readline.Interface
instance.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});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}`);
});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).