質問

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?

役に立ちましたか?

解決

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);
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top