heapwolf / prompt-sync

a synchronous prompt for node.js
MIT License
211 stars 42 forks source link

solution to repeated prompt on each keystroke #65

Closed trimination closed 8 months ago

trimination commented 1 year ago

Hacky fix for weird behaviour when calling prompt('some text\n');. I don't understand how the prompt() function works entirely, but if the ask variable includes a newline character, weird behaviour can occur, with each keystroke the prompt is printed again.

Reproduce:

const prompt = require('prompt-sync')();
const in = prompt('this is a test: \n');
console.log(in);

With this code, each keystroke will cause the prompt() text to be printed again, until a return keystroke is sent. This patch checks for \n within the ask variable inside the prompt() function prints it separately, then strips it from the string.

Before change:

Screenshot 2023-05-31 at 00 41 20

After change:

Screenshot 2023-05-31 at 00 41 59