#252·prompts

Changing confirm prompt so Ctrl+C exits the process

Author: tiagonapoliCreated Feb 4, 2020Updated Jul 4, 2026
Labelsenhancementquestion

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?

typescript
import prompts from 'prompts'

interface PromptState {
  aborted: boolean
}

const enableTerminalCursor = () => {
  process.stdout.write('\x1B[?25h')
}

const onState = (state: PromptState) => {
  if (state.aborted) {
    // If we don't re-enable the terminal cursor before exiting
    // the program, the cursor will remain hidden
    enableTerminalCursor()
    process.stdout.write('\n')
    process.exit(1)
  }
}

export const promptConfirm = async (message: string, initial = true): Promise<boolean> => {
  const { response } = await prompts([{ message, initial, onState, type: 'confirm', name: 'response' }])
  return response
}