wobsoriano/bun-promptx

How to exit out of a selection process?

Opened this issue · 3 comments

const responses = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((item) => {
  return createSelection(
    [
      {
        text: 'skip',
      },
      {
        text: 'higher',
      },
      {
        text: 'lower',
      },
    ],
    {
      headerText: `What should be done with number: ${item}?`,
    }
  )
})

console.log(responses)

If I enter the command [CTRL] + [C], it doesn't exit the running process as expected.
Instead, it continues throughout the list of items.

CM-IV commented

You're iterating through the array of numbers and printing the prompt for each number. You have 10 numbers in it, so hitting ctrl + c 10 times would exit out of the program.

Here's what it looks like with just 3 numbers in the array.

import { createSelection } from 'bun-promptx'

const responses = [1, 2, 3].map((item) => {
    return createSelection(
      [
        {
          text: 'skip',
        },
        {
          text: 'higher',
        },
        {
          text: 'lower',
        },
      ],
      {
        headerText: `What should be done with number: ${item}?`,
      }
    )
  })
  
  console.log(responses)

image

Thanks, is there a way to exit out of all of them though?

CM-IV commented

I have never seen the selection prompt used like this, but I suppose you could do the following:

import { createSelection } from 'bun-promptx'

[1, 2, 3, 4].map((item) => {

    const selection = createSelection(
        [
          {
            text: 'skip',
          },
          {
            text: 'higher',
          },
          {
            text: 'lower',
          },
          {
            text: 'exit',
          },
        ],
        {
          headerText: `What should be done with number: ${item}?`,
        }
    )

    if (selection.selectedIndex === 3) {
        console.log(selection)
        process.exit(0);
    }

    console.log(selection)
})

You don't ctrl + c to exit this, but instead pick the exit option.
I'm more partial to using createSelection() like this:

import { createSelection } from 'bun-promptx'

function showMenu() {
    const selection = createSelection(
        [
          {
            text: 'skip',
          },
          {
            text: 'higher',
          },
          {
            text: 'lower',
          },
          {
            text: 'exit',
          },
        ],
        {
          headerText: `What would you like to do?`,
        }
    )

    return selection.selectedIndex
}

function init() {
    while (true) {
        const choice = showMenu() as number

        switch (choice) {
            case 0:
                console.log("First Choice")
                break
            case 1:
                console.log("Second Choice")
                break
            case 2:
                console.log("Third Choice")
                break
            case 3:
                console.log("Goodbye!")
                return
        }
    }
}

init()