const re = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
re.on("line", (order) => {
if (!this.checkIsCorrectOrder(order)) {
console.log("Wrong");
return;
}
someFun(order)
}).on("close", () => {
process.exit();
});
... ohter Asynchronous
let count = 0
setInterval(()=>{
console.log(count++)
},1000)
I want to type while avoiding overlapping readline and console.log
how can i do that?
Is there any other way besides readline??? thanks!!
I am assuming that you mean you want to output to the terminal at the same time as you are writing commands into it, without it being spoiled?
Not quite done playing around with it, but so far this works for me:
var stdin = process.stdin;
var stdout = process.stdout;
var prompt = ">";
var current = "";
stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdout.write(prompt);
stdin.on( 'data', function( key ){
switch (key){
case '\u001B\u005B\u0041'://up
case '\u001B\u005B\u0043'://right
case '\u001B\u005B\u0042'://down
case '\u001B\u005B\u0044'://left
break;
case '\u0003':
process.exit();
break;
case '\u000d':
//RunCommands(current)
current = "";
console.log("\b");
stdout.write(prompt);
break;
case '\u007f':
stdout.write("\r\x1b[K") ;
current = current.slice(0, -1);
stdout.write(prompt + current);
break;
default:
stdout.write(key);
current += key;
break;
}
});
function print(str){
let totalCurrentLength = current.length + prompt.length;
let lines = Math.ceil(totalCurrentLength / stdout.columns);
for(i = 0; i < lines; i++){
stdout.clearLine();
stdout.write('\u001B\u005B\u0041');
}
stdout.write('\u001B\u005B\u0042');
stdout.cursorTo(0)
console.log(str);
stdout.write(prompt + current);
}
var count = 0;
setInterval(() => {
print("Test interference: " + count++)
}, 500);
Basically, I am keeping track of user inputs per keystroke and storing them in a string until the user presses return. Then I sent the "current" string to the place where I sort out and process all the possible command combinations.
I've been facing the same problem you have, I think, output disrupting input. So long as you use print() (which almost works like console.log, except you have to pass one argument, it does handle chalk and stuff tho) for the messages it should work ok.
Note, I disabled the arrow keys at the top, so that pressing the arrows doesn't move the cursor around and mess it all up.
'current' should be the string of commands you write in your terminal for the app, I made a function to deal with all the possible comand line functions.