-
-
Notifications
You must be signed in to change notification settings - Fork 306
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
Changing confirm prompt so Ctrl+C exits the process #252
Comments
Thank you @tiagonapoli. Currently there are no native solutions to this. It should be an option in the next major version of prompts 👍 |
This is great to hear since I'm currently facing the same issue! Would you happen to have a roadmap or any estimates for the next version? |
sorry, I don't have any timeline at the moment. I'm quite busy working on other projects 👍 |
An alternate suggestion: if you have a lot of prompts, it might be better to make an onState that throws an error that can bubble up to the top, rather than immediately hard-exiting the program. class AbortedError extends Error { }
function onState(state) {
if (state.aborted) throw new AbortedError();
} Now you have a reusable onState you can attach to all of your prompts, and in the outermost catch of your application, you can choose to: main().catch(error => {
if (!(error instanceof AbortedError)) {
console.error(error.stack);
}
process.exitCode = 1;
}); This way the |
Update: I should have tested my suggestion first, it looks like throwing from inside onState() actually ends up aborting the process anyway because you are inside a readline callback. So my "graceful" claim is not true today. 🥩 |
My recommended solution: onState: (state) => {
if (state.aborted) {
process.nextTick(() => {
process.exit(0);
})
}
} |
@tiagonapoli Maybe there could be an API similar to this: import prompts from 'prompts'
prompts.setExitOnAbort(true) Or this: import prompts from 'prompts'
prompts.overrideAborting(false)
@terkelg Can we (me or @tiagonapoli or anyone) implement these changes and make a pull request? |
Please don't add a procedural interface that mutates the module instance. It's more error-prone and messy if you need to revert the behavior back. If anything you could add |
I have the same problem. Unfortunately, pressing |
Yes, it seems @MMK21Hub is right, is there a workaround? Maybe it is possible to remove Prompt's SIGINT (Ctrl-C?) event listener, or to attach a new event listener that catches SIGINT preferentially and exits? I tried to do this and failed. |
@terkelg why does prompts intercept any signals in the first place? It seems like a very strange design choice to me, but maybe there's something I'm not thinking about... |
Prompts does not directly intercept signals. It does set the input to raw mode (to listen for individual keypresses). In raw mode the default ctrl+c handling doesn't work. Here is the workaround I am using: // Handle Ctrl+C even when raw mode is used (for prompts)
process.stdin.on("keypress", function (_chunk, key) {
if (key && key.name === "c" && key.ctrl) {
process.exit(130);
}
}); Based on this stack overflow answer: https://stackoverflow.com/questions/20165605/detecting-ctrlc-in-node-js Bonus: Since prompts hides your cursor for some prompts, adding this (kinda hacky) to the listener makes it show the cursor on exit process.stdout.write("\x1B[?25h"); |
@calebeby oh, thanks, I didn't think about that. It would be good for prompts to at least provide an option to handle Ctrl-C by doing |
Is your feature request related to a problem?
In my use case when pressing CTRL+C on a prompt the program should immediately terminate. Using the default confirm prompt I noted that it would answer
yes
and continue the program, so I implemented the following solution - is there a native one?The text was updated successfully, but these errors were encountered: