Pregunta

I have some code to read in some user input, nothing fancy:

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Please enter some text:\n', function(answer){
    rl.pause();
    console.log(answer);
});

Unfortunately, when the user is prompted for the input, if they make a typo and attempt to use BACKSPACE to delete the text, the prompt is shifted down a line. Is there a way to stop this from happening?

¿Fue útil?

Solución

After looking into this further, it appears it only occurs if the rl.question() prompt contains a \n. If the prompt doesn't contain a newline character, the text can be backspaced safely.

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Please enter some text: ', function(answer){
    rl.pause();
    console.log(answer);
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top