rl.line


节点正在处理的当前输入数据。

【The current input data being processed by node.】

当从 TTY 流收集输入以在 line 事件触发之前获取当前已处理的值时,可以使用此方法。一旦 line 事件被触发,该属性将为空字符串。

【This can be used when collecting input from a TTY stream to retrieve the current value that has been processed thus far, prior to the line event being emitted. Once the line event has been emitted, this property will be an empty string.】

请注意,如果不同时控制 rl.cursor,在实例运行时修改该值可能会产生意想不到的后果。

【Be aware that modifying the value during the instance runtime may have unintended consequences if rl.cursor is not also controlled.】

如果不使用 TTY 流作为输入,请使用 'line' 事件。

一个可能的用例如下:

【One possible use case would be as follows:】

const values = ['lorem ipsum', 'dolor sit amet'];
const rl = readline.createInterface(process.stdin);
const showResults = debounce(() => {
  console.log(
    '\n',
    values.filter((val) => val.startsWith(rl.line)).join(' ')
  );
}, 300);
process.stdin.on('keypress', (c, k) => {
  showResults();
});