Skip to content

Changing confirm prompt so Ctrl+C exits the process  #252

@tiagonapoli

Description

@tiagonapoli

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?

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions