Prompts
❯ Lightweight, beautiful and user-friendly interactive prompts
README
❯ Prompts
Lightweight, beautiful and user-friendly interactive prompts
>_ Easy to use CLI prompts to enquire users for information▌
❯ Install
- ```
- $ npm install --save prompts
- ```
This package supports Node 6 and above
❯ Usage
- ```js
- const prompts = require('prompts');
- (async () => {
- const response = await prompts({
- type: 'number',
- name: 'value',
- message: 'How old are you?',
- validate: value => value < 18 ? `Nightclub is 18+ only` : true
- });
- console.log(response); // => { value: 24 }
- })();
- ```
See [example.js](https://github.com/terkelg/prompts/blob/master/example.js) for more options.
❯ Examples
Single Prompt
- ```js
- const prompts = require('prompts');
- (async () => {
- const response = await prompts({
- type: 'text',
- name: 'meaning',
- message: 'What is the meaning of life?'
- });
- console.log(response.meaning);
- })();
- ```
Prompt Chain
- ```js
- const prompts = require('prompts');
- const questions = [
- {
- type: 'text',
- name: 'username',
- message: 'What is your GitHub username?'
- },
- {
- type: 'number',
- name: 'age',
- message: 'How old are you?'
- },
- {
- type: 'text',
- name: 'about',
- message: 'Tell something about yourself',
- initial: 'Why should I?'
- }
- ];
- (async () => {
- const response = await prompts(questions);
- // => response => { username, age, about }
- })();
- ```
Dynamic Prompts
- ```js
- const prompts = require('prompts');
- const questions = [
- {
- type: 'text',
- name: 'dish',
- message: 'Do you like pizza?'
- },
- {
- type: prev => prev == 'pizza' ? 'text' : null,
- name: 'topping',
- message: 'Name a topping'
- }
- ];
- (async () => {
- const response = await prompts(questions);
- })();
- ```
❯ API
prompts(prompts, options)
prompts
options.onSubmit
- ```js
- (async () => {
- const questions = [{ ... }];
- const onSubmit = (prompt, answer) => console.log(`Thanks I got ${answer} from ${prompt.name}`);
- const response = await prompts(questions, { onSubmit });
- })();
- ```
options.onCancel
- ```js
- (async () => {
- const questions = [{ ... }];
- const onCancel = prompt => {
- console.log('Never stop prompting!');
- return true;
- }
- const response = await prompts(questions, { onCancel });
- })();
- ```
override
- ```js
- const prompts = require('prompts');
- prompts.override(require('yargs').argv);
- (async () => {
- const response = await prompts([
- {
- type: 'text',
- name: 'twitter',
- message: `What's your twitter handle?`
- },
- {
- type: 'multiselect',
- name: 'color',
- message: 'Pick colors',
- choices: [
- { title: 'Red', value: '#ff0000' },
- { title: 'Green', value: '#00ff00' },
- { title: 'Blue', value: '#0000ff' }
- ],
- }
- ]);
- console.log(response);
- })();
- ```
inject(values)
values
- ```js
- const prompts = require('prompts');
- prompts.inject([ '@terkelg', ['#ff0000', '#0000ff'] ]);
- (async () => {
- const response = await prompts([
- {
- type: 'text',
- name: 'twitter',
- message: `What's your twitter handle?`
- },
- {
- type: 'multiselect',
- name: 'color',
- message: 'Pick colors',
- choices: [
- { title: 'Red', value: '#ff0000' },
- { title: 'Green', value: '#00ff00' },
- { title: 'Blue', value: '#0000ff' }
- ],
- }
- ]);
- // => { twitter: 'terkelg', color: [ '#ff0000', '#0000ff' ] }
- })();
- ```
❯ Prompt Objects
- ```js
- {
- type: String | Function,
- name: String | Function,
- message: String | Function,
- initial: String | Function | Async Function
- format: Function | Async Function,
- onRender: Function
- onState: Function
- stdin: Readable
- stdout: Writeable
- }
- ```
- ```js
- {
- type: prev => prev > 3 ? 'confirm' : null,
- name: 'confirm',
- message: (prev, values) => `Please confirm that you eat ${values.dish} times ${prev} a day?`
- }
- ```
type
- ```js
- {
- type: null,
- name: 'forgetme',
- message: `I'll never be shown anyway`,
- }
- ```
name
Make sure to give prompts unique names if you don't want to overwrite previous values.
message
initial
format
- ```js
- {
- type: 'number',
- name: 'price',
- message: 'Enter price',
- format: val => Intl.NumberFormat(undefined, { style: 'currency', currency: 'USD' }).format(val);
- }
- ```
onRender
- ```js
- {
- type: 'number',
- message: 'This message will be overridden',
- onRender(kleur) {
- this.msg = kleur.cyan('Enter a number');
- }
- }
- ```
onState
stdin and stdout
❯ Types
text(message, [initial], [style])
Text prompt for free text input.
Example
- ```js
- {
- type: 'text',
- name: 'value',
- message: `What's your twitter handle?`
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `string` | Default |
style | `string` | Render |
format | `function` | Receive |
validate | `function` | Receive |
onRender | `function` | On |
onState | `function` | On |
password(message, [initial])
Password prompt with masked input.
Example
- ```js
- {
- type: 'password',
- name: 'value',
- message: 'Tell me a secret'
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `string` | Default |
format | `function` | Receive |
validate | `function` | Receive |
onRender | `function` | On |
onState | `function` | On |
invisible(message, [initial])
Prompts user for invisible text input.
Example
- ```js
- {
- type: 'invisible',
- name: 'value',
- message: 'Enter password'
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `string` | Default |
format | `function` | Receive |
validate | `function` | Receive |
onRender | `function` | On |
onState | `function` | On |
number(message, initial, [max], [min], [style])
Prompts user for number input.
Example
- ```js
- {
- type: 'number',
- name: 'value',
- message: 'How old are you?',
- initial: 0,
- style: 'default',
- min: 2,
- max: 10
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `number` | Default |
format | `function` | Receive |
validate | `function` | Receive |
max | `number` | Max |
min | `number` | Min |
float | `boolean` | Allow |
round | `number` | Round |
increment | `number` | Increment |
style | `string` | Render |
onRender | `function` | On |
onState | `function` | On |
confirm(message, [initial])
Classic yes/no prompt.
Example
- ```js
- {
- type: 'confirm',
- name: 'value',
- message: 'Can you confirm?',
- initial: true
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `boolean` | Default |
format | `function` | Receive |
onRender | `function` | On |
onState | `function` | On |
list(message, [initial])
List prompt that return an array.
- ```js
- {
- type: 'list',
- name: 'value',
- message: 'Enter keywords',
- initial: '',
- separator: ','
- }
- ```
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `boolean` | Default |
format | `function` | Receive |
separator | `string` | String |
onRender | `function` | On |
onState | `function` | On |
toggle(message, [initial], [active], [inactive])
Interactive toggle/switch prompt.
Example
- ```js
- {
- type: 'toggle',
- name: 'value',
- message: 'Can you confirm?',
- initial: true,
- active: 'yes',
- inactive: 'no'
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `boolean` | Default |
format | `function` | Receive |
active | `string` | Text |
inactive | `string` | Text |
onRender | `function` | On |
onState | `function` | On |
select(message, choices, [initial], [hint], [warn])
Interactive select prompt.
Example
- ```js
- {
- type: 'select',
- name: 'value',
- message: 'Pick a color',
- choices: [
- { title: 'Red', description: 'This option has a description', value: '#ff0000' },
- { title: 'Green', value: '#00ff00', disabled: true },
- { title: 'Blue', value: '#0000ff' }
- ],
- initial: 1
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `number` | Index |
format | `function` | Receive |
hint | `string` | Hint |
warn | `string` | Message |
choices | `Array` | Array |
onRender | `function` | On |
onState | `function` | On |
multiselect(message, choices, [initial], [max], [hint], [warn])
autocompleteMultiselect(same)
Interactive multi-select prompt.
Autocomplete is a searchable multiselect prompt with the same options. Useful for long lists.
Example
- ```js
- {
- type: 'multiselect',
- name: 'value',
- message: 'Pick colors',
- choices: [
- { title: 'Red', value: '#ff0000' },
- { title: 'Green', value: '#00ff00', disabled: true },
- { title: 'Blue', value: '#0000ff', selected: true }
- ],
- max: 2,
- hint: '- Space to select. Return to submit'
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
format | `function` | Receive |
instructions | `string` | Prompt |
choices | `Array` | Array |
optionsPerPage | `number` | Number |
min | `number` | Min |
max | `number` | Max |
hint | `string` | Hint |
warn | `string` | Message |
onRender | `function` | On |
onState | `function` | On |
autocomplete(message, choices, [initial], [suggest], [limit], [style])
Interactive auto complete prompt.
Example
- ```js
- {
- type: 'autocomplete',
- name: 'value',
- message: 'Pick your favorite actor',
- choices: [
- { title: 'Cage' },
- { title: 'Clooney', value: 'silver-fox' },
- { title: 'Gyllenhaal' },
- { title: 'Gibson' },
- { title: 'Grant' }
- ]
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
format | `function` | Receive |
choices | `Array` | Array |
suggest | `function` | Filter |
limit | `number` | Max |
style | `string` | Render |
initial | `string | number` |
clearFirst | `boolean` | The |
fallback | `string` | Fallback |
onRender | `function` | On |
onState | `function` | On |
- ```js
- const suggestByTitle = (input, choices) =>
- Promise.resolve(choices.filter(i => i.title.slice(0, input.length) === input))
- ```
date(message, [initial], [warn])
Interactive date prompt.
Example
- ```js
- {
- type: 'date',
- name: 'value',
- message: 'Pick a date',
- initial: new Date(1997, 09, 12),
- validate: date => date > Date.now() ? 'Not in the future' : true
- }
- ```
Options
Param | Type | Description |
---|---|---|
----- | :--: | ----------- |
message | `string` | Prompt |
initial | `date` | Default |
locales | `object` | Use |
mask | `string` | The |
validate | `function` | Receive |
onRender | `function` | On |
onState | `function` | On |
- ```javascript
- {
- months: [
- 'January', 'February', 'March', 'April',
- 'May', 'June', 'July', 'August',
- 'September', 'October', 'November', 'December'
- ],
- monthsShort: [
- 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
- 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
- ],
- weekdays: [
- 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
- 'Thursday', 'Friday', 'Saturday'
- ],
- weekdaysShort: [
- 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
- ]
- }
- ```