我已经创建了一个 Node readline 接口(interface)
this.io = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer:(line:string) => { //adapted from Node docs
const hits = commands.filter((c) => c.startsWith(line.toUpperCase()));
return [hits.length ? hits : commands, line];
}
});
第一次在控制台输入时,正常显示,如> go north
。然而,在我接受这个输入后,在我的其余代码中使用它,并再次用 readline 提示用户,它看起来像 > ggoo nnoorrtthh
,如果我第三次提示它看起来像> gggooo nnnooorrrttthhh
。我在创建界面时尝试使用 terminal:false
但它没有改变任何东西。有没有什么东西让我的 readline 重复我以乘法方式键入的字符?
编辑:我还应该补充一点,当我将用户的输入解析到我的代码中时,我得到了正确的输出(它总是只返回“向北走”),但是它实际上输入了两次用户输入,然后输入了三次次等。
处理输入的代码:
let arg = line.substr(firstSpace+1);
let shouldProceed = this.handler(cmd, arg); //call handler function!
if(shouldProceed){
this.io.prompt();
} else {
this.io.close();
}
}
else {
console.log('Invalid command. Commands are:', commands.join(', '));
this.io.prompt();
}
shouldProceed 始终为真。如果用户键入“退出”,则 io 将关闭。
最佳答案
我也有这个问题。现在刚修好。如果您的应用程序中有多个 readline.createInterface,就会发生这种情况。或者至少您必须确保在使用之前删除其他创建的界面。
重复的字符数取决于你创建了多少个界面
关于javascript - Node readline 接口(interface)以乘法方式重复每个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494624/