Javascript 节点读取行接口以乘法方式重复每个字符
我已经用创建了一个节点readline接口Javascript 节点读取行接口以乘法方式重复每个字符,javascript,node.js,typescript,Javascript,Node.js,Typescript,我已经用创建了一个节点readline接口 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.toU
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];
}
});
当第一次在控制台中键入时,它会正常显示,如>gonorth
。但是,在我获取此输入后,在代码的其余部分使用它,并再次使用readline提示用户,它看起来像>ggoo nnoorrtthh
,如果我第三次提示,它看起来像>ggooo nnnoorrttthh
。我在创建界面时尝试过使用terminal:false
,但它没有改变任何东西。是否有任何东西使我的readline重复我以乘法方式键入的字符
编辑:我还应该补充一点,当我将用户的输入解析到我的代码中时,我得到了正确的输出(它总是返回“go north”),但是它实际上输入用户输入两次,然后再输入三次,依此类推
处理输入的代码:
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();
}
“应该”永远是真的。如果用户键入“退出”,io将关闭。尝试将最后一行替换为
return hits.length?hits:commands
,原始版本似乎每次都会追加每个字符。不幸的是,这并没有改变它,我想每次调用readline时,我都会越来越多地打开它,但我不知道会发生什么情况。我认为您缺少这两行this.io.close()
和process.stdin.destroy()代码>在您的完成符中(我可能在这里出错)。来源:当用户键入“退出”时,我关闭我的readline,这是它唯一一次被关闭。否则,当他们键入另一个命令时,我的代码将对其进行处理,然后以递归模式使用此.io.prompt()重新编译用户。我应该在任何其他地方关闭读线吗?如果我过早地关闭读线,它将不会重新提示用户输入,并且会终止IO,我认为这更多的是我递归地调用提示符,以某种方式将我的输入同时解释为两个输入