Skip to content

patik/inquirer-select-with-state

Repository files navigation

inquirer-select-with-state

Inquirer select prompt with a stateful banner

A fork of inquirer's built-in select command line prompt, but with the ability to add a stateful banner above the list of choices.

You provide a statefulBanner function. This function receives a setState function which can be called at will. The string sent to setState is shown above the select prompt. statefulBanner can also return a cleanup function.

Install

pnpm add inquirer-select-with-state
yarn add inquirer-select-with-state
npm add inquirer-select-with-state

Usage

import select from 'inquirer-select-with-state'

const answer = await select({
    message: 'Choose an option',
    choices: [
        { name: '1', value: '1' },
        { name: '2', value: '2' },
        { name: '3', value: '3' },
    ],
    statefulBanner: (setState: (s: string) => void) => {
        setState('Directory size: loading...')
        exec('du -sh', (err, stdout) => {
            if (!err) {
                setState(`Directory size: ${stdout}`)
            }
        })
    },
})

The prompt will initially look like this:

Directory size: loading...
? Choose an option
 1
  2
  3

A moment later, it will automatically update:

Directory size: 123M
? Choose an option
 1
  2
  3

Return value

If your banner has any side effects (e.g. timeouts), you can return a cleanup function which will be called when the prompt quits.

Example

See src/example.ts for a full example using async/await.

About

Inquirer select prompt with a stateful banner

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •