Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt repeats itself on each keypress with multi-line template literals. #25

Open
davidrbright opened this issue Nov 30, 2016 · 9 comments

Comments

@davidrbright
Copy link

Used prompt with a multi-line template literal, and the stdout would repeat itself on a newline with each keypress. User input worked fine though.

The problem was fixed by going to es5 syntax.

@emilgoldsmith
Copy link

emilgoldsmith commented Jul 8, 2017

+1

@heapwolf
Copy link
Owner

heapwolf commented Jul 8, 2017

👍

PR welcome

@soroushjp
Copy link

On Node v12.14.1 , this is happening for me for all multi-line strings, even without template literals.

@soroushjp
Copy link

One easy workaround for this -- print your multi-line prompt using console.log and only print the very last line of the prompt using prompt-sync. Problem solved.

@CalculonPrime
Copy link

Why was this issue closed when it's still a problem as sorousjp reported? Why has a serious bug like this been left unfixed for 6 months? Should I look for a replacement package that's maintained?

@perilstar
Copy link

This is still an issue.

@axelKingsley
Copy link

It was really hard to find this issue since I didn't know why my prompts were being repeated, so I wonder if there are more folks having this issue who haven't realized it's due to multi-line literals. Taking @soroushjp suggestion, I've put together this script which helps demonstrate the problem, and solves the prompt issue by wrapping up the console logging and prompting automatically:

const prompt = require('prompt-sync')({sigint: true});                                    
                                                                                          
const multiLinePrompt = ask => {                                                          
    const lines = ask.split(/\r?\n/);                                                     
    const promptLine = lines.pop();                                                       
    console.log(lines.join('\n'));                                                        
    return prompt(promptLine);                                                            
};                                                                                        
                                                                                          
let response = ''                                                                         
                                                                                          
// Test 1: Basic Prompt on One Line                                                       
response = prompt(`This prompt uses a single line literal and WON'T have print errors: `);
console.log(`The prompt captured: ${response}`);                                          
                                                                                          
// Test 2: Reproduction of issue with multi-line literal prompts                          
response = prompt(`This prompt uses a multi line literal and                              
WILL                                                                                      
have print errors: `);                                                                    
console.log(`The prompt captured: ${response}`);                                          
                                                                                          
// Test 3: Demonstration of helper function to mitigate the issue                         
response = multiLinePrompt(`This prompt uses the above multi-line helper and              
WON'T                                                                                     
have print errors: `);                                                                    
console.log(`The prompt captured: ${response}`);                                          

axelKingsley added a commit to axelKingsley/prompt-sync that referenced this issue Mar 14, 2021
heapwolf#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 splits the provided ask by newline, prints
all but the last line only once, and assigns the final line
to the ask variable used for the remainder of the module.
axelKingsley added a commit to axelKingsley/prompt-sync that referenced this issue Mar 14, 2021
heapwolf#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 splits the provided ask by newline, prints
all but the last line only once, and assigns the final line
to the ask variable used for the remainder of the module.
axelKingsley added a commit to axelKingsley/prompt-sync that referenced this issue Mar 14, 2021
heapwolf#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.
@axelKingsley
Copy link

axelKingsley commented Mar 14, 2021

Whoops, sorry for the notification spam on this issue, I was polishing the PR commit in my fork.

My PR Should solve this issue. It just sets ask to be only the final line of the user's supplied input during redraw. Pinging @heapwolf for attention to the PR.

Thanks for this module, Paolo! Let me know if there's something I missed with regards to the fix.

#45

@Vpet95
Copy link

Vpet95 commented Dec 11, 2022

This repo seems to be inactive at this time. I wrote a modern port of it called prompt-sync-plus intended to be mostly a drop-in replacement for this (with some exceptions); multi-line prompts, and multi-line input in general was one of the issues addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants