heapwolf / prompt-sync

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

Fix Issue #25: Inappropriate Redrawn Multi-Line Asks #45

Open axelKingsley opened 3 years ago

axelKingsley commented 3 years ago

Commit Message

https://github.com/heapwolf/prompt-sync/issues/25

Prompts were causing bad user experience in situations where the prompt contains multiple lines. This is because prompt-sync will redraw the prompt as you type, and assumes it only takes up one line.

The fix provided prints the whole ask string, before reassigning ask to be only the final line of the prompt. This way, redraws don't affect above lines.

Testing

Wrote and executed a simple script that captures multi-line and single-line prompts.

const prompt = require('./prompt-sync')({sigint: true});        

let response = prompt(`Multi-Line                               
Prompts                                                         
Are                                                             
Cool: `);                                                       

console.log(`You Said: ${response}`);                           

response = prompt(`Single-Line Prompts are Fine Too I Guess: `);

console.log(`You Said: ${response}`);                           

And Executed:

[axel@arch prompt-sync]$ node test.js 
Multi-Line
Prompts
Are
Cool: yes they are
You Said: yes they are
Single-Line Prompts are Fine Too I Guess: yep
You Said: yep

Potential Side-Effects

I looked over the index.js and didn't see anything concerning, but if there's an expectation that the ask variable is the full string provided by the user, that expectation would be broken beyond line 79.

axelKingsley commented 3 years ago

One deficiency I noticed after using this for a while: If the users input should cause a line-wrap in their terminal, the original problem (reprint on every keystroke) starts back up. I would suspect this means that the printing itself should be reconsidered, but this commit at least improves the situation, if there are no side-effects.

mitchell-merry commented 1 year ago

would very much appreciate a merge here!