I have a setInterval who logs the time every second.
I want to use STDIN to execute commands in my script, but the STDOUT is moving the cursor while I’m typing and puts itself in the prompt.
I don’t have much experience with prompts, just started diving into this.
Script:
setInterval(function(){
console.log(new Date().toUTCString());
},1000)
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Hi there, how are you?", function(answer) {
});
Output:
Hi there, how are you?Mon, 17 Dec 2012 16:20:34 GMT
fine
Mon, 17 Dec 2012 16:20:35 GMT
Mon, 17 Dec 2012 16:20:36 GMT
Mon, 17 Dec 2012 16:20:37 GMT
Mon, 17 Dec 2012 16:20:38 GMT
How would you solve something like this? Cache all the STDOUT, clear the screen, write all the STDOUT and prompt again every time console.log() is logging?
Yes I do want to create some kind of chat based system/command line interface where the output stays above the input.
Thanks!
Basically what you are asking is how to make a complex terminal application. The most popular library for this is called
ncursesand it has node bindings here. I don’t have personal experience using it unfortunately.Your other option would be to do as your said, and manually re-render the terminal using your own internal buffers. You can get the size of the output terminal using Node’s
ttymodule, docs here, and then use ANSI escape codes to clear the terminal, position the cursor where you want, and then useprocess.stdout.writeto print what you want.You can see an example of using escape codes in my other question over here