-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Ctrl+C breaks my terminal with "nested" prompts #1493
Comments
I think process.stdin.resume(); |
@medallyon @SBoudrias I ended up writing this to handle exiting correctly (unfortunately it relies on a string comparison as the error.name returns /**
* Wrapper for prompts that correctly exit the application when the user cancels the prompt.
* @private
* @param type
* @param options
* @returns
*/
export async function handleRequestWithExit(
type: Function,
options: Record<string, any>,
): Promise<any> {
return type(options)
.then((value: any) => (typeof value === 'string' ? value.trim() : value))
.catch((error: Error) => {
if (error.message.includes('User force closed the prompt with 0 null')) {
process.emit('SIGINT', 'SIGINT');
return;
}
console.error(`CLI exited: ${error.message}`, { cause: error });
process.exit(1);
});
} Then in my main CLI file I have this at the top: process.on('SIGINT', () => {
console.log('🚪 User exited process');
// Perform any cleanup tasks here
process.exit(0);
}); Now when I use a inquirer function I wrap it like this: const name = await handleRequestWithExit(input, {
message: `Enter your name`,
required: true,
}); This was the only reliable way I could find to work with multiple prompts |
@SBoudrias I'm not sure where I'd implement this - I tried plastering it around my code but it seemingly has no effect. Thanks for this @tanepiper. I might still find use for your snippet but I think my use case is a little different as I'm looking to "move between menus" so to speak; the main menu being on an infinite loop until the user exits, and nested menus appearing when selecting something from the main menu. I'd like the user to be able to cancel the current prompt and return to the main menu/prompt, I was hoping to achieve this with Ctrl+C however I'm starting to see that listening to P.S. You can check the name of the constructor for the error class, that's how I did it: if (error.constructor.name === "ExitPromptError") |
@medallyon if you look at #1489 (comment) you'll see I do something similar too - I go into a loop for search by returning it, similarly you could return other menus such a sub-menu. I removed it for that example, but I use the same function there too and it wraps nicely |
Windows: Windows 10 Pro 20H2 v19042.1706
Windows Terminal: v1.20.11781.0
Using Cmd
I'm having trouble implementing Ctrl+C for "nested" prompts. I have a main
search
prompt on an indefinite loop. When selecting an option, it runs a module that may also createsearch
,input
, or other prompts. I think I have a good implementation for handling Ctrl+C in the main prompt loop, which captures theExitPromptError
and gracefully exits the program. However, I'm not seemingly able to achieve the same with a prompt that was spawned at a later point. The desired functionality is to be able to use Ctrl+C to abort a "nested" prompt and return to the main loop, however trying to do so breaks the CLI. It's difficult to explain in words, so here's a video:15-47-01.mp4
This is the relevant code:
index.ts
sample-register-action.ts
The text was updated successfully, but these errors were encountered: