.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / README.md
1 <h1 align="center">Enquirer</h1>
2
3 <p align="center">
4 <a href="https://npmjs.org/package/enquirer">
5 <img src="https://img.shields.io/npm/v/enquirer.svg" alt="version">
6 </a>
7 <a href="https://travis-ci.org/enquirer/enquirer">
8 <img src="https://img.shields.io/travis/enquirer/enquirer.svg" alt="travis">
9 </a>
10 <a href="https://npmjs.org/package/enquirer">
11 <img src="https://img.shields.io/npm/dm/enquirer.svg" alt="downloads">
12 </a>
13 </p>
14
15 <br>
16 <br>
17
18 <p align="center">
19 <b>Stylish CLI prompts that are user-friendly, intuitive and easy to create.</b><br>
20 <sub>>_ Prompts should be more like conversations than inquisitions▌</sub>
21 </p>
22
23 <br>
24
25 <p align="center">
26 <sub>(Example shows Enquirer's <a href="#survey-prompt">Survey Prompt</a>)</a></sub>
27 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/survey-prompt.gif" alt="Enquirer Survey Prompt" width="750"><br>
28 <sub>The terminal in all examples is <a href="https://hyper.is/">Hyper</a>, theme is <a href="https://github.com/jonschlinkert/hyper-monokai-extended">hyper-monokai-extended</a>.</sub><br><br>
29 <a href="#built-in-prompts"><strong>See more prompt examples</strong></a>
30 </p>
31
32 <br>
33 <br>
34
35 Created by [jonschlinkert](https://github.com/jonschlinkert) and [doowb](https://github.com/doowb), Enquirer is fast, easy to use, and lightweight enough for small projects, while also being powerful and customizable enough for the most advanced use cases.
36
37 * **Fast** - [Loads in ~4ms](#-performance) (that's about _3-4 times faster than a [single frame of a HD movie](http://www.endmemo.com/sconvert/framespersecondframespermillisecond.php) at 60fps_)
38 * **Lightweight** - Only one dependency, the excellent [ansi-colors](https://github.com/doowb/ansi-colors) by [Brian Woodward](https://github.com/doowb).
39 * **Easy to implement** - Uses promises and async/await and sensible defaults to make prompts easy to create and implement.
40 * **Easy to use** - Thrill your users with a better experience! Navigating around input and choices is a breeze. You can even create [quizzes](examples/fun/countdown.js), or [record](examples/fun/record.js) and [playback](examples/fun/play.js) key bindings to aid with tutorials and videos.
41 * **Intuitive** - Keypress combos are available to simplify usage.
42 * **Flexible** - All prompts can be used standalone or chained together.
43 * **Stylish** - Easily override semantic styles and symbols for any part of the prompt.
44 * **Extensible** - Easily create and use custom prompts by extending Enquirer's built-in [prompts](#-prompts).
45 * **Pluggable** - Add advanced features to Enquirer using plugins.
46 * **Validation** - Optionally validate user input with any prompt.
47 * **Well tested** - All prompts are well-tested, and tests are easy to create without having to use brittle, hacky solutions to spy on prompts or "inject" values.
48 * **Examples** - There are numerous [examples](examples) available to help you get started.
49
50 If you like Enquirer, please consider starring or tweeting about this project to show your support. Thanks!
51
52 <br>
53
54 <p align="center">
55 <b>>_ Ready to start making prompts your users will love? ▌</b><br>
56 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/heartbeat.gif" alt="Enquirer Select Prompt with heartbeat example" width="750">
57 </p>
58
59 <br>
60 <br>
61
62 ## ❯ Getting started
63
64 Get started with Enquirer, the most powerful and easy-to-use Node.js library for creating interactive CLI prompts.
65
66 * [Install](#-install)
67 * [Usage](#-usage)
68 * [Enquirer](#-enquirer)
69 * [Prompts](#-prompts)
70   - [Built-in Prompts](#-prompts)
71   - [Custom Prompts](#-custom-prompts)
72 * [Key Bindings](#-key-bindings)
73 * [Options](#-options)
74 * [Release History](#-release-history)
75 * [Performance](#-performance)
76 * [About](#-about)
77
78 <br>
79
80 ## ❯ Install
81
82 Install with [npm](https://www.npmjs.com/):
83
84 ```sh
85 $ npm install enquirer --save
86 ```
87 Install with [yarn](https://yarnpkg.com/en/):
88
89 ```sh
90 $ yarn add enquirer
91 ```
92
93 <p align="center">
94 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/npm-install.gif" alt="Install Enquirer with NPM" width="750">
95 </p>
96
97 _(Requires Node.js 8.6 or higher. Please let us know if you need support for an earlier version by creating an [issue](../../issues/new).)_
98
99 <br>
100
101 ## ❯ Usage
102
103 ### Single prompt
104
105 The easiest way to get started with enquirer is to pass a [question object](#prompt-options) to the `prompt` method.
106
107 ```js
108 const { prompt } = require('enquirer');
109
110 const response = await prompt({
111   type: 'input',
112   name: 'username',
113   message: 'What is your username?'
114 });
115
116 console.log(response); // { username: 'jonschlinkert' }
117 ```
118
119 _(Examples with `await` need to be run inside an `async` function)_
120
121 ### Multiple prompts
122
123 Pass an array of ["question" objects](#prompt-options) to run a series of prompts.
124
125 ```js
126 const response = await prompt([
127   {
128     type: 'input',
129     name: 'name',
130     message: 'What is your name?'
131   },
132   {
133     type: 'input',
134     name: 'username',
135     message: 'What is your username?'
136   }
137 ]);
138
139 console.log(response); // { name: 'Edward Chan', username: 'edwardmchan' }
140 ```
141
142 ### Different ways to run enquirer
143
144 #### 1. By importing the specific `built-in prompt`
145
146 ```js
147 const { Confirm } = require('enquirer');
148
149 const prompt = new Confirm({
150   name: 'question',
151   message: 'Did you like enquirer?'
152 });
153
154 prompt.run()
155   .then(answer => console.log('Answer:', answer));
156 ```
157
158 #### 2. By passing the options to `prompt`
159
160 ```js
161 const { prompt } = require('enquirer');
162
163 prompt({
164   type: 'confirm',
165   name: 'question',
166   message: 'Did you like enquirer?'
167 })
168   .then(answer => console.log('Answer:', answer));
169 ```
170
171 **Jump to**: [Getting Started](#-getting-started) · [Prompts](#-prompts) · [Options](#-options) · [Key Bindings](#-key-bindings)
172
173 <br>
174
175 ## ❯ Enquirer
176
177 **Enquirer is a prompt runner**
178
179 Add Enquirer to your JavaScript project with following line of code.
180
181 ```js
182 const Enquirer = require('enquirer');
183 ```
184
185 The main export of this library is the `Enquirer` class, which has methods and features designed to simplify running prompts.
186
187 ```js
188 const { prompt } = require('enquirer');
189 const question = [
190   {
191     type: 'input',
192     name: 'username',
193     message: 'What is your username?'
194   },
195   {
196     type: 'password',
197     name: 'password',
198     message: 'What is your password?'
199   }
200 ];
201
202 let answers = await prompt(question);
203 console.log(answers);
204 ```
205
206 **Prompts control how values are rendered and returned**
207
208 Each individual prompt is a class with special features and functionality for rendering the types of values you want to show users in the terminal, and subsequently returning the types of values you need to use in your application.
209
210 **How can I customize prompts?**
211
212 Below in this guide you will find information about creating [custom prompts](#-custom-prompts). For now, we'll focus on how to customize an existing prompt.
213
214 All of the individual [prompt classes](#built-in-prompts) in this library are exposed as static properties on Enquirer. This allows them to be used directly without using `enquirer.prompt()`.
215
216 Use this approach if you need to modify a prompt instance, or listen for events on the prompt.
217
218 **Example**
219
220 ```js
221 const { Input } = require('enquirer');
222 const prompt = new Input({
223   name: 'username',
224   message: 'What is your username?'
225 });
226
227 prompt.run()
228   .then(answer => console.log('Username:', answer))
229   .catch(console.error);
230 ```
231
232 ### [Enquirer](index.js#L20)
233
234 Create an instance of `Enquirer`.
235
236 **Params**
237
238 * `options` **{Object}**: (optional) Options to use with all prompts.
239 * `answers` **{Object}**: (optional) Answers object to initialize with.
240
241 **Example**
242
243 ```js
244 const Enquirer = require('enquirer');
245 const enquirer = new Enquirer();
246 ```
247
248 ### [register()](index.js#L42)
249
250 Register a custom prompt type.
251
252 **Params**
253
254 * `type` **{String}**
255 * `fn` **{Function|Prompt}**: `Prompt` class, or a function that returns a `Prompt` class.
256 * `returns` **{Object}**: Returns the Enquirer instance
257
258 **Example**
259
260 ```js
261 const Enquirer = require('enquirer');
262 const enquirer = new Enquirer();
263 enquirer.register('customType', require('./custom-prompt'));
264 ```
265
266 ### [prompt()](index.js#L78)
267
268 Prompt function that takes a "question" object or array of question objects, and returns an object with responses from the user.
269
270 **Params**
271
272 * `questions` **{Array|Object}**: Options objects for one or more prompts to run.
273 * `returns` **{Promise}**: Promise that returns an "answers" object with the user's responses.
274
275 **Example**
276
277 ```js
278 const Enquirer = require('enquirer');
279 const enquirer = new Enquirer();
280
281 const response = await enquirer.prompt({
282   type: 'input',
283   name: 'username',
284   message: 'What is your username?'
285 });
286 console.log(response);
287 ```
288
289 ### [use()](index.js#L160)
290
291 Use an enquirer plugin.
292
293 **Params**
294
295 * `plugin` **{Function}**: Plugin function that takes an instance of Enquirer.
296 * `returns` **{Object}**: Returns the Enquirer instance.
297
298 **Example**
299
300 ```js
301 const Enquirer = require('enquirer');
302 const enquirer = new Enquirer();
303 const plugin = enquirer => {
304   // do stuff to enquire instance
305 };
306 enquirer.use(plugin);
307 ```
308
309 ### [Enquirer#prompt](index.js#L210)
310
311 Prompt function that takes a "question" object or array of question objects, and returns an object with responses from the user.
312
313 **Params**
314
315 * `questions` **{Array|Object}**: Options objects for one or more prompts to run.
316 * `returns` **{Promise}**: Promise that returns an "answers" object with the user's responses.
317
318 **Example**
319
320 ```js
321 const { prompt } = require('enquirer');
322 const response = await prompt({
323   type: 'input',
324   name: 'username',
325   message: 'What is your username?'
326 });
327 console.log(response);
328 ```
329
330 <br>
331
332 ## ❯ Prompts
333
334 This section is about Enquirer's prompts: what they look like, how they work, how to run them, available options, and how to customize the prompts or create your own prompt concept.
335
336 **Getting started with Enquirer's prompts**
337
338 * [Prompt](#prompt) - The base `Prompt` class used by other prompts
339   - [Prompt Options](#prompt-options)
340 * [Built-in prompts](#built-in-prompts)
341 * [Prompt Types](#prompt-types) - The base `Prompt` class used by other prompts
342 * [Custom prompts](#%E2%9D%AF-custom-prompts) - Enquirer 2.0 introduced the concept of prompt "types", with the goal of making custom prompts easier than ever to create and use.
343
344 ### Prompt
345
346 The base `Prompt` class is used to create all other prompts.
347
348 ```js
349 const { Prompt } = require('enquirer');
350 class MyCustomPrompt extends Prompt {}
351 ```
352
353 See the documentation for [creating custom prompts](#-custom-prompts) to learn more about how this works.
354
355 #### Prompt Options
356
357 Each prompt takes an options object (aka "question" object), that implements the following interface:
358
359 ```js
360 {
361   // required
362   type: string | function,
363   name: string | function,
364   message: string | function | async function,
365
366   // optional
367   skip: boolean | function | async function,
368   initial: string | function | async function,
369   format: function | async function,
370   result: function | async function,
371   validate: function | async function,
372 }
373 ```
374 Each property of the options object is described below:
375
376 | **Property** | **Required?** | **Type**           | **Description**                                                                                                                                                                         |
377 | ------------ | ------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
378 | `type`       | yes           | `string\|function`  | Enquirer uses this value to determine the type of prompt to run, but it's optional when prompts are run directly.                                                                       |
379 | `name`       | yes           | `string\|function`  | Used as the key for the answer on the returned values (answers) object.                                                                                                                 |
380 | `message`    | yes           | `string\|function`  | The message to display when the prompt is rendered in the terminal.                                                                                                                     |
381 | `skip`       | no            | `boolean\|function` | If `true` it will not ask that prompt.                                                                                                                                                  |
382 | `initial`    | no            | `string\|function`  | The default value to return if the user does not supply a value.                                                                                                                        |
383 | `format`     | no            | `function`         | Function to format user input in the terminal.                                                                                                                                          |
384 | `result`     | no            | `function`         | Function to format the final submitted value before it's returned.                                                                                                                      |
385 | `validate`   | no            | `function`         | Function to validate the submitted value before it's returned. This function may return a boolean or a string. If a string is returned it will be used as the validation error message. |
386
387 **Example usage**
388
389 ```js
390 const { prompt } = require('enquirer');
391
392 const question = {
393   type: 'input',
394   name: 'username',
395   message: 'What is your username?'
396 };
397
398 prompt(question)
399   .then(answer => console.log('Answer:', answer))
400   .catch(console.error);
401 ```
402
403 <br>
404
405 ### Built-in prompts
406
407 * [AutoComplete Prompt](#autocomplete-prompt)
408 * [BasicAuth Prompt](#basicauth-prompt)
409 * [Confirm Prompt](#confirm-prompt)
410 * [Form Prompt](#form-prompt)
411 * [Input Prompt](#input-prompt)
412 * [Invisible Prompt](#invisible-prompt)
413 * [List Prompt](#list-prompt)
414 * [MultiSelect Prompt](#multiselect-prompt)
415 * [Numeral Prompt](#numeral-prompt)
416 * [Password Prompt](#password-prompt)
417 * [Quiz Prompt](#quiz-prompt)
418 * [Survey Prompt](#survey-prompt)
419 * [Scale Prompt](#scale-prompt)
420 * [Select Prompt](#select-prompt)
421 * [Sort Prompt](#sort-prompt)
422 * [Snippet Prompt](#snippet-prompt)
423 * [Toggle Prompt](#toggle-prompt)
424
425 ### AutoComplete Prompt
426
427 Prompt that auto-completes as the user types, and returns the selected value as a string.
428
429 <p align="center">
430 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/autocomplete-prompt.gif" alt="Enquirer AutoComplete Prompt" width="750">
431 </p>
432
433 **Example Usage**
434
435 ```js
436 const { AutoComplete } = require('enquirer');
437
438 const prompt = new AutoComplete({
439   name: 'flavor',
440   message: 'Pick your favorite flavor',
441   limit: 10,
442   initial: 2,
443   choices: [
444     'Almond',
445     'Apple',
446     'Banana',
447     'Blackberry',
448     'Blueberry',
449     'Cherry',
450     'Chocolate',
451     'Cinnamon',
452     'Coconut',
453     'Cranberry',
454     'Grape',
455     'Nougat',
456     'Orange',
457     'Pear',
458     'Pineapple',
459     'Raspberry',
460     'Strawberry',
461     'Vanilla',
462     'Watermelon',
463     'Wintergreen'
464   ]
465 });
466
467 prompt.run()
468   .then(answer => console.log('Answer:', answer))
469   .catch(console.error);
470 ```
471
472 **AutoComplete Options**
473
474 | Option      | Type       | Default                                                             | Description                                                                                                  |
475 | ----------- | ---------- | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
476 | `highlight` | `function` | `dim` version of primary style                                      | The color to use when "highlighting" characters in the list that match user input.                           |
477 | `multiple`  | `boolean`  | `false`                                                             | Allow multiple choices to be selected.                                                                       |
478 | `suggest`   | `function` | Greedy match, returns true if choice message contains input string. | Function that filters choices. Takes user input and a choices array, and returns a list of matching choices. |
479 | `initial`   | `number` | 0 | Preselected item in the list of choices. |
480 | `footer`   | `function` | None | Function that displays [footer text](https://github.com/enquirer/enquirer/blob/6c2819518a1e2ed284242a99a685655fbaabfa28/examples/autocomplete/option-footer.js#L10) |
481
482 **Related prompts**
483
484 * [Select](#select-prompt)
485 * [MultiSelect](#multiselect-prompt)
486 * [Survey](#survey-prompt)
487
488 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
489
490 ***
491
492 ### BasicAuth Prompt
493
494 Prompt that asks for username and password to authenticate the user. The default implementation of `authenticate` function in `BasicAuth` prompt is to compare the username and password with the values supplied while running the prompt. The implementer is expected to override the `authenticate` function with a custom logic such as making an API request to a server to authenticate the username and password entered and expect a token back.
495
496 <p align="center">
497 <img src="https://user-images.githubusercontent.com/13731210/61570485-7ffd9c00-aaaa-11e9-857a-d47dc7008284.gif" alt="Enquirer BasicAuth Prompt" width="750">
498 </p>
499
500 **Example Usage**
501
502 ```js
503 const { BasicAuth } = require('enquirer');
504
505  const prompt = new BasicAuth({
506   name: 'password',
507   message: 'Please enter your password',
508   username: 'rajat-sr',
509   password: '123',
510   showPassword: true
511 });
512
513  prompt
514   .run()
515   .then(answer => console.log('Answer:', answer))
516   .catch(console.error);
517 ```
518
519 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
520
521 ***
522
523 ### Confirm Prompt
524
525 Prompt that returns `true` or `false`.
526
527 <p align="center">
528 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/confirm-prompt.gif" alt="Enquirer Confirm Prompt" width="750">
529 </p>
530
531 **Example Usage**
532
533 ```js
534 const { Confirm } = require('enquirer');
535
536 const prompt = new Confirm({
537   name: 'question',
538   message: 'Want to answer?'
539 });
540
541 prompt.run()
542   .then(answer => console.log('Answer:', answer))
543   .catch(console.error);
544 ```
545
546 **Related prompts**
547
548 * [Input](#input-prompt)
549 * [Numeral](#numeral-prompt)
550 * [Password](#password-prompt)
551
552 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
553
554 ***
555
556 ### Form Prompt
557
558 Prompt that allows the user to enter and submit multiple values on a single terminal screen.
559
560 <p align="center">
561 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/form-prompt.gif" alt="Enquirer Form Prompt" width="750">
562 </p>
563
564 **Example Usage**
565
566 ```js
567 const { Form } = require('enquirer');
568
569 const prompt = new Form({
570   name: 'user',
571   message: 'Please provide the following information:',
572   choices: [
573     { name: 'firstname', message: 'First Name', initial: 'Jon' },
574     { name: 'lastname', message: 'Last Name', initial: 'Schlinkert' },
575     { name: 'username', message: 'GitHub username', initial: 'jonschlinkert' }
576   ]
577 });
578
579 prompt.run()
580   .then(value => console.log('Answer:', value))
581   .catch(console.error);
582 ```
583
584 **Related prompts**
585
586 * [Input](#input-prompt)
587 * [Survey](#survey-prompt)
588
589 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
590
591 ***
592
593 ### Input Prompt
594
595 Prompt that takes user input and returns a string.
596
597 <p align="center">
598 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/input-prompt.gif" alt="Enquirer Input Prompt" width="750">
599 </p>
600
601 **Example Usage**
602
603 ```js
604 const { Input } = require('enquirer');
605 const prompt = new Input({
606   message: 'What is your username?',
607   initial: 'jonschlinkert'
608 });
609
610 prompt.run()
611   .then(answer => console.log('Answer:', answer))
612   .catch(console.log);
613 ```
614
615 You can use [data-store](https://github.com/jonschlinkert/data-store) to store [input history](https://github.com/enquirer/enquirer/blob/master/examples/input/option-history.js) that the user can cycle through (see [source](https://github.com/enquirer/enquirer/blob/8407dc3579123df5e6e20215078e33bb605b0c37/lib/prompts/input.js)).
616
617 **Related prompts**
618
619 * [Confirm](#confirm-prompt)
620 * [Numeral](#numeral-prompt)
621 * [Password](#password-prompt)
622
623 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
624
625 ***
626
627 ### Invisible Prompt
628
629 Prompt that takes user input, hides it from the terminal, and returns a string.
630
631 <p align="center">
632 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/invisible-prompt.gif" alt="Enquirer Invisible Prompt" width="750">
633 </p>
634
635 **Example Usage**
636
637 ```js
638 const { Invisible } = require('enquirer');
639 const prompt = new Invisible({
640   name: 'secret',
641   message: 'What is your secret?'
642 });
643
644 prompt.run()
645   .then(answer => console.log('Answer:', { secret: answer }))
646   .catch(console.error);
647 ```
648
649 **Related prompts**
650
651 * [Password](#password-prompt)
652 * [Input](#input-prompt)
653
654 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
655
656 ***
657
658 ### List Prompt
659
660 Prompt that returns a list of values, created by splitting the user input. The default split character is `,` with optional trailing whitespace.
661
662 <p align="center">
663 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/list-prompt.gif" alt="Enquirer List Prompt" width="750">
664 </p>
665
666 **Example Usage**
667
668 ```js
669 const { List } = require('enquirer');
670 const prompt = new List({
671   name: 'keywords',
672   message: 'Type comma-separated keywords'
673 });
674
675 prompt.run()
676   .then(answer => console.log('Answer:', answer))
677   .catch(console.error);
678 ```
679
680 **Related prompts**
681
682 * [Sort](#sort-prompt)
683 * [Select](#select-prompt)
684
685 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
686
687 ***
688
689 ### MultiSelect Prompt
690
691 Prompt that allows the user to select multiple items from a list of options.
692
693 <p align="center">
694 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/multiselect-prompt.gif" alt="Enquirer MultiSelect Prompt" width="750">
695 </p>
696
697 **Example Usage**
698
699 ```js
700 const { MultiSelect } = require('enquirer');
701
702 const prompt = new MultiSelect({
703   name: 'value',
704   message: 'Pick your favorite colors',
705   limit: 7,
706   choices: [
707     { name: 'aqua', value: '#00ffff' },
708     { name: 'black', value: '#000000' },
709     { name: 'blue', value: '#0000ff' },
710     { name: 'fuchsia', value: '#ff00ff' },
711     { name: 'gray', value: '#808080' },
712     { name: 'green', value: '#008000' },
713     { name: 'lime', value: '#00ff00' },
714     { name: 'maroon', value: '#800000' },
715     { name: 'navy', value: '#000080' },
716     { name: 'olive', value: '#808000' },
717     { name: 'purple', value: '#800080' },
718     { name: 'red', value: '#ff0000' },
719     { name: 'silver', value: '#c0c0c0' },
720     { name: 'teal', value: '#008080' },
721     { name: 'white', value: '#ffffff' },
722     { name: 'yellow', value: '#ffff00' }
723   ]
724 });
725
726 prompt.run()
727   .then(answer => console.log('Answer:', answer))
728   .catch(console.error);
729
730 // Answer: ['aqua', 'blue', 'fuchsia']
731 ```
732
733 **Example key-value pairs**
734
735 Optionally, pass a `result` function and use the `.map` method to return an object of key-value pairs of the selected names and values: [example](./examples/multiselect/option-result.js)
736
737 ```js
738 const { MultiSelect } = require('enquirer');
739
740 const prompt = new MultiSelect({
741   name: 'value',
742   message: 'Pick your favorite colors',
743   limit: 7,
744   choices: [
745     { name: 'aqua', value: '#00ffff' },
746     { name: 'black', value: '#000000' },
747     { name: 'blue', value: '#0000ff' },
748     { name: 'fuchsia', value: '#ff00ff' },
749     { name: 'gray', value: '#808080' },
750     { name: 'green', value: '#008000' },
751     { name: 'lime', value: '#00ff00' },
752     { name: 'maroon', value: '#800000' },
753     { name: 'navy', value: '#000080' },
754     { name: 'olive', value: '#808000' },
755     { name: 'purple', value: '#800080' },
756     { name: 'red', value: '#ff0000' },
757     { name: 'silver', value: '#c0c0c0' },
758     { name: 'teal', value: '#008080' },
759     { name: 'white', value: '#ffffff' },
760     { name: 'yellow', value: '#ffff00' }
761   ],
762   result(names) {
763    return this.map(names);
764   }
765 });
766
767 prompt.run()
768   .then(answer => console.log('Answer:', answer))
769   .catch(console.error);
770
771 // Answer: { aqua: '#00ffff', blue: '#0000ff', fuchsia: '#ff00ff' }
772 ```
773
774 **Related prompts**
775
776 * [AutoComplete](#autocomplete-prompt)
777 * [Select](#select-prompt)
778 * [Survey](#survey-prompt)
779
780 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
781
782 ***
783
784 ### Numeral Prompt
785
786 Prompt that takes a number as input.
787
788 <p align="center">
789 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/numeral-prompt.gif" alt="Enquirer Numeral Prompt" width="750">
790 </p>
791
792 **Example Usage**
793
794 ```js
795 const { NumberPrompt } = require('enquirer');
796
797 const prompt = new NumberPrompt({
798   name: 'number',
799   message: 'Please enter a number'
800 });
801
802 prompt.run()
803   .then(answer => console.log('Answer:', answer))
804   .catch(console.error);
805 ```
806
807 **Related prompts**
808
809 * [Input](#input-prompt)
810 * [Confirm](#confirm-prompt)
811
812 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
813
814 ***
815
816 ### Password Prompt
817
818 Prompt that takes user input and masks it in the terminal. Also see the [invisible prompt](#invisible-prompt)
819
820 <p align="center">
821 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/password-prompt.gif" alt="Enquirer Password Prompt" width="750">
822 </p>
823
824 **Example Usage**
825
826 ```js
827 const { Password } = require('enquirer');
828
829 const prompt = new Password({
830   name: 'password',
831   message: 'What is your password?'
832 });
833
834 prompt.run()
835   .then(answer => console.log('Answer:', answer))
836   .catch(console.error);
837 ```
838
839 **Related prompts**
840
841 * [Input](#input-prompt)
842 * [Invisible](#invisible-prompt)
843
844 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
845
846 ***
847
848 ### Quiz Prompt
849
850 Prompt that allows the user to play multiple-choice quiz questions.
851
852 <p align="center">
853 <img src="https://user-images.githubusercontent.com/13731210/61567561-891d4780-aa6f-11e9-9b09-3d504abd24ed.gif" alt="Enquirer Quiz Prompt" width="750">
854 </p>
855
856 **Example Usage**
857
858 ```js
859 const { Quiz } = require('enquirer');
860
861  const prompt = new Quiz({
862   name: 'countries',
863   message: 'How many countries are there in the world?',
864   choices: ['165', '175', '185', '195', '205'],
865   correctChoice: 3
866 });
867
868  prompt
869   .run()
870   .then(answer => {
871     if (answer.correct) {
872       console.log('Correct!');
873     } else {
874       console.log(`Wrong! Correct answer is ${answer.correctAnswer}`);
875     }
876   })
877   .catch(console.error);
878 ```
879
880 **Quiz Options**
881
882 | Option         | Type        | Required    | Description                                                                                                  |
883 | -----------    | ----------  | ----------  | ------------------------------------------------------------------------------------------------------------ |
884 | `choices`      | `array`     | Yes         | The list of possible answers to the quiz question.                                                           |
885 | `correctChoice`| `number`    | Yes         | Index of the correct choice from the `choices` array.                                                        |
886
887 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
888
889 ***
890
891 ### Survey Prompt
892
893 Prompt that allows the user to provide feedback for a list of questions.
894
895 <p align="center">
896 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/survey-prompt.gif" alt="Enquirer Survey Prompt" width="750">
897 </p>
898
899 **Example Usage**
900
901 ```js
902 const { Survey } = require('enquirer');
903
904 const prompt = new Survey({
905   name: 'experience',
906   message: 'Please rate your experience',
907    scale: [
908     { name: '1', message: 'Strongly Disagree' },
909     { name: '2', message: 'Disagree' },
910     { name: '3', message: 'Neutral' },
911     { name: '4', message: 'Agree' },
912     { name: '5', message: 'Strongly Agree' }
913   ],
914   margin: [0, 0, 2, 1],
915   choices: [
916     {
917       name: 'interface',
918       message: 'The website has a friendly interface.'
919     },
920     {
921       name: 'navigation',
922       message: 'The website is easy to navigate.'
923     },
924     {
925       name: 'images',
926       message: 'The website usually has good images.'
927     },
928     {
929       name: 'upload',
930       message: 'The website makes it easy to upload images.'
931     },
932     {
933       name: 'colors',
934       message: 'The website has a pleasing color palette.'
935     }
936   ]
937 });
938
939 prompt.run()
940   .then(value => console.log('ANSWERS:', value))
941   .catch(console.error);
942 ```
943
944 **Related prompts**
945
946 * [Scale](#scale-prompt)
947 * [Snippet](#snippet-prompt)
948 * [Select](#select-prompt)
949
950 ***
951
952 ### Scale Prompt
953
954 A more compact version of the [Survey prompt](#survey-prompt), the Scale prompt allows the user to quickly provide feedback using a [Likert Scale](https://en.wikipedia.org/wiki/Likert_scale).
955
956 <p align="center">
957 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/scale-prompt.gif" alt="Enquirer Scale Prompt" width="750">
958 </p>
959
960 **Example Usage**
961
962 ```js
963 const { Scale } = require('enquirer');
964 const prompt = new Scale({
965   name: 'experience',
966   message: 'Please rate your experience',
967   scale: [
968     { name: '1', message: 'Strongly Disagree' },
969     { name: '2', message: 'Disagree' },
970     { name: '3', message: 'Neutral' },
971     { name: '4', message: 'Agree' },
972     { name: '5', message: 'Strongly Agree' }
973   ],
974   margin: [0, 0, 2, 1],
975   choices: [
976     {
977       name: 'interface',
978       message: 'The website has a friendly interface.',
979       initial: 2
980     },
981     {
982       name: 'navigation',
983       message: 'The website is easy to navigate.',
984       initial: 2
985     },
986     {
987       name: 'images',
988       message: 'The website usually has good images.',
989       initial: 2
990     },
991     {
992       name: 'upload',
993       message: 'The website makes it easy to upload images.',
994       initial: 2
995     },
996     {
997       name: 'colors',
998       message: 'The website has a pleasing color palette.',
999       initial: 2
1000     }
1001   ]
1002 });
1003
1004 prompt.run()
1005   .then(value => console.log('ANSWERS:', value))
1006   .catch(console.error);
1007 ```
1008
1009 **Related prompts**
1010
1011 * [AutoComplete](#autocomplete-prompt)
1012 * [Select](#select-prompt)
1013 * [Survey](#survey-prompt)
1014
1015 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
1016
1017 ***
1018
1019 ### Select Prompt
1020
1021 Prompt that allows the user to select from a list of options.
1022
1023 <p align="center">
1024 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/select-prompt.gif" alt="Enquirer Select Prompt" width="750">
1025 </p>
1026
1027 **Example Usage**
1028
1029 ```js
1030 const { Select } = require('enquirer');
1031
1032 const prompt = new Select({
1033   name: 'color',
1034   message: 'Pick a flavor',
1035   choices: ['apple', 'grape', 'watermelon', 'cherry', 'orange']
1036 });
1037
1038 prompt.run()
1039   .then(answer => console.log('Answer:', answer))
1040   .catch(console.error);
1041 ```
1042
1043 **Related prompts**
1044
1045 * [AutoComplete](#autocomplete-prompt)
1046 * [MultiSelect](#multiselect-prompt)
1047
1048 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
1049
1050 ***
1051
1052 ### Sort Prompt
1053
1054 Prompt that allows the user to sort items in a list.
1055
1056 **Example**
1057
1058 In this [example](https://github.com/enquirer/enquirer/raw/master/examples/sort/prompt.js), custom styling is applied to the returned values to make it easier to see what's happening.
1059
1060 <p align="center">
1061 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/sort-prompt.gif" alt="Enquirer Sort Prompt" width="750">
1062 </p>
1063
1064 **Example Usage**
1065
1066 ```js
1067 const colors = require('ansi-colors');
1068 const { Sort } = require('enquirer');
1069 const prompt = new Sort({
1070   name: 'colors',
1071   message: 'Sort the colors in order of preference',
1072   hint: 'Top is best, bottom is worst',
1073   numbered: true,
1074   choices: ['red', 'white', 'green', 'cyan', 'yellow'].map(n => ({
1075     name: n,
1076     message: colors[n](n)
1077   }))
1078 });
1079
1080 prompt.run()
1081   .then(function(answer = []) {
1082     console.log(answer);
1083     console.log('Your preferred order of colors is:');
1084     console.log(answer.map(key => colors[key](key)).join('\n'));
1085   })
1086   .catch(console.error);
1087 ```
1088
1089 **Related prompts**
1090
1091 * [List](#list-prompt)
1092 * [Select](#select-prompt)
1093
1094 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
1095
1096 ***
1097
1098 ### Snippet Prompt
1099
1100 Prompt that allows the user to replace placeholders in a snippet of code or text.
1101
1102 <p align="center">
1103 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/snippet-prompt.gif" alt="Prompts" width="750">
1104 </p>
1105
1106 **Example Usage**
1107
1108 ```js
1109 const semver = require('semver');
1110 const { Snippet } = require('enquirer');
1111 const prompt = new Snippet({
1112   name: 'username',
1113   message: 'Fill out the fields in package.json',
1114   required: true,
1115   fields: [
1116     {
1117       name: 'author_name',
1118       message: 'Author Name'
1119     },
1120     {
1121       name: 'version',
1122       validate(value, state, item, index) {
1123         if (item && item.name === 'version' && !semver.valid(value)) {
1124           return prompt.styles.danger('version should be a valid semver value');
1125         }
1126         return true;
1127       }
1128     }
1129   ],
1130   template: `{
1131   "name": "\${name}",
1132   "description": "\${description}",
1133   "version": "\${version}",
1134   "homepage": "https://github.com/\${username}/\${name}",
1135   "author": "\${author_name} (https://github.com/\${username})",
1136   "repository": "\${username}/\${name}",
1137   "license": "\${license:ISC}"
1138 }
1139 `
1140 });
1141
1142 prompt.run()
1143   .then(answer => console.log('Answer:', answer.result))
1144   .catch(console.error);
1145 ```
1146
1147 **Related prompts**
1148
1149 * [Survey](#survey-prompt)
1150 * [AutoComplete](#autocomplete-prompt)
1151
1152 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
1153
1154 ***
1155
1156 ### Toggle Prompt
1157
1158 Prompt that allows the user to toggle between two values then returns `true` or `false`.
1159
1160 <p align="center">
1161 <img src="https://raw.githubusercontent.com/enquirer/enquirer/master/media/toggle-prompt.gif" alt="Enquirer Toggle Prompt" width="750">
1162 </p>
1163
1164 **Example Usage**
1165
1166 ```js
1167 const { Toggle } = require('enquirer');
1168
1169 const prompt = new Toggle({
1170   message: 'Want to answer?',
1171   enabled: 'Yep',
1172   disabled: 'Nope'
1173 });
1174
1175 prompt.run()
1176   .then(answer => console.log('Answer:', answer))
1177   .catch(console.error);
1178 ```
1179
1180 **Related prompts**
1181
1182 * [Confirm](#confirm-prompt)
1183 * [Input](#input-prompt)
1184 * [Sort](#sort-prompt)
1185
1186 **↑ back to:** [Getting Started](#-getting-started) · [Prompts](#-prompts)
1187
1188 ***
1189
1190 ### Prompt Types
1191
1192 There are 5 (soon to be 6!) type classes:
1193
1194 * [ArrayPrompt](#arrayprompt)
1195   - [Options](#options)
1196   - [Properties](#properties)
1197   - [Methods](#methods)
1198   - [Choices](#choices)
1199   - [Defining choices](#defining-choices)
1200   - [Choice properties](#choice-properties)
1201   - [Related prompts](#related-prompts)
1202 * [AuthPrompt](#authprompt)
1203 * [BooleanPrompt](#booleanprompt)
1204 * DatePrompt (Coming Soon!)
1205 * [NumberPrompt](#numberprompt)
1206 * [StringPrompt](#stringprompt)
1207
1208 Each type is a low-level class that may be used as a starting point for creating higher level prompts. Continue reading to learn how.
1209
1210 ### ArrayPrompt
1211
1212 The `ArrayPrompt` class is used for creating prompts that display a list of choices in the terminal. For example, Enquirer uses this class as the basis for the [Select](#select) and [Survey](#survey) prompts.
1213
1214 #### Options
1215
1216 In addition to the [options](#options) available to all prompts, Array prompts also support the following options.
1217
1218 | **Option**  | **Required?** | **Type**        | **Description**                                                                                                         |
1219 | ----------- | ------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------- |
1220 | `autofocus` | `no`          | `string\|number` | The index or name of the choice that should have focus when the prompt loads. Only one choice may have focus at a time. |  |
1221 | `stdin`     | `no`          | `stream`        | The input stream to use for emitting keypress events. Defaults to `process.stdin`.                                      |
1222 | `stdout`    | `no`          | `stream`        | The output stream to use for writing the prompt to the terminal. Defaults to `process.stdout`.                          |
1223 |             |
1224
1225 #### Properties
1226
1227 Array prompts have the following instance properties and getters.
1228
1229 | **Property name** | **Type**                                                                          | **Description**                                                                                                                                                                                                                                                                                                                                    |
1230 | ----------------- | --------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1231 | `choices`         | `array`                                                                           | Array of choices that have been normalized from choices passed on the prompt options.                                                                                                                                                                                                                                                              |
1232 | `cursor`          | `number`                                                                          | Position of the cursor relative to the _user input (string)_.                                                                                                                                                                                                                                                                                      |
1233 | `enabled`         | `array`                                                                           | Returns an array of enabled choices.                                                                                                                                                                                                                                                                                                               |
1234 | `focused`         | `array`                                                                           | Returns the currently selected choice in the visible list of choices. This is similar to the concept of focus in HTML and CSS. Focused choices are always visible (on-screen). When a list of choices is longer than the list of visible choices, and an off-screen choice is _focused_, the list will scroll to the focused choice and re-render. |
1235 | `focused`         | Gets the currently selected choice. Equivalent to `prompt.choices[prompt.index]`. |
1236 | `index`           | `number`                                                                          | Position of the pointer in the _visible list (array) of choices_.                                                                                                                                                                                                                                                                                  |
1237 | `limit`           | `number`                                                                          | The number of choices to display on-screen.                                                                                                                                                                                                                                                                                                        |
1238 | `selected`        | `array`                                                                           | Either a list of enabled choices (when `options.multiple` is true) or the currently focused choice.                                                                                                                                                                                                                                                |
1239 | `visible`         | `string`                                                                          |                                                                                                                                                                                                                                                                                                                                                    |
1240
1241 #### Methods
1242
1243 | **Method**    | **Description**                                                                                                                                                                                |
1244 | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1245 | `pointer()`   | Returns the visual symbol to use to identify the choice that currently has focus. The `❯` symbol is often used for this. The pointer is not always visible, as with the `autocomplete` prompt. |
1246 | `indicator()` | Returns the visual symbol that indicates whether or not a choice is checked/enabled.                                                                                                           |
1247 | `focus()`     | Sets focus on a choice, if it can be focused.                                                                                                                                                  |
1248
1249 #### Choices
1250
1251 Array prompts support the `choices` option, which is the array of choices users will be able to select from when rendered in the terminal.
1252
1253 **Type**: `string|object`
1254
1255 **Example**
1256
1257 ```js
1258 const { prompt } = require('enquirer');
1259
1260 const questions = [{
1261   type: 'select',
1262   name: 'color',
1263   message: 'Favorite color?',
1264   initial: 1,
1265   choices: [
1266     { name: 'red',   message: 'Red',   value: '#ff0000' }, //<= choice object
1267     { name: 'green', message: 'Green', value: '#00ff00' }, //<= choice object
1268     { name: 'blue',  message: 'Blue',  value: '#0000ff' }  //<= choice object
1269   ]
1270 }];
1271
1272 let answers = await prompt(questions);
1273 console.log('Answer:', answers.color);
1274 ```
1275
1276 #### Defining choices
1277
1278 Whether defined as a string or object, choices are normalized to the following interface:
1279
1280 ```js
1281 {
1282   name: string;
1283   message: string | undefined;
1284   value: string | undefined;
1285   hint: string | undefined;
1286   disabled: boolean | string | undefined;
1287 }
1288 ```
1289
1290 **Example**
1291
1292 ```js
1293 const question = {
1294   name: 'fruit',
1295   message: 'Favorite fruit?',
1296   choices: ['Apple', 'Orange', 'Raspberry']
1297 };
1298 ```
1299
1300 Normalizes to the following when the prompt is run:
1301
1302 ```js
1303 const question = {
1304   name: 'fruit',
1305   message: 'Favorite fruit?',
1306   choices: [
1307     { name: 'Apple', message: 'Apple', value: 'Apple' },
1308     { name: 'Orange', message: 'Orange', value: 'Orange' },
1309     { name: 'Raspberry', message: 'Raspberry', value: 'Raspberry' }
1310   ]
1311 };
1312 ```
1313
1314 #### Choice properties
1315
1316 The following properties are supported on `choice` objects.
1317
1318 | **Option**  | **Type**          | **Description**                                                                                                                                                                                     |
1319 | ----------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1320 | `name`      | `string`          | The unique key to identify a choice                                                                                                                                                                 |
1321 | `message`   | `string`          | The message to display in the terminal. `name` is used when this is undefined.                                                                                                                      |
1322 | `value`     | `string`          | Value to associate with the choice. Useful for creating key-value pairs from user choices. `name` is used when this is undefined.                                                                   |
1323 | `choices`   | `array`           | Array of "child" choices.                                                                                                                                                                           |
1324 | `hint`      | `string`          | Help message to display next to a choice.                                                                                                                                                           |
1325 | `role`      | `string`          | Determines how the choice will be displayed. Currently the only role supported is `separator`. Additional roles may be added in the future (like `heading`, etc). Please create a [feature request] |
1326 | `enabled`   | `boolean`         | Enabled a choice by default. This is only supported when `options.multiple` is true or on prompts that support multiple choices, like [MultiSelect](#-multiselect).                                 |
1327 | `disabled`  | `boolean\|string`  | Disable a choice so that it cannot be selected. This value may either be `true`, `false`, or a message to display.                                                                                  |
1328 | `indicator` | `string\|function` | Custom indicator to render for a choice (like a check or radio button).                                                                                                                             |
1329
1330 #### Related prompts
1331
1332 * [AutoComplete](#autocomplete-prompt)
1333 * [Form](#form-prompt)
1334 * [MultiSelect](#multiselect-prompt)
1335 * [Select](#select-prompt)
1336 * [Survey](#survey-prompt)
1337
1338 ***
1339
1340 ### AuthPrompt
1341
1342 The `AuthPrompt` is used to create prompts to log in user using any authentication method. For example, Enquirer uses this class as the basis for the [BasicAuth Prompt](#basicauth-prompt). You can also find prompt examples in `examples/auth/` folder that utilizes `AuthPrompt` to create OAuth based authentication prompt or a prompt that authenticates using time-based OTP, among others.
1343
1344 `AuthPrompt` has a factory function that creates an instance of `AuthPrompt` class and it expects an `authenticate` function, as an argument, which overrides the `authenticate` function of the `AuthPrompt` class.
1345
1346 #### Methods
1347
1348 | **Method**         | **Description**                                                                                                                                                                                          |
1349 | -------------      | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1350 | `authenticate()`   | Contain all the authentication logic. This function should be overridden to implement custom authentication logic. The default `authenticate` function throws an error if no other function is provided. |
1351
1352 #### Choices
1353
1354 Auth prompt supports the `choices` option, which is the similar to the choices used in [Form Prompt](#form-prompt).
1355
1356 **Example**
1357
1358 ```js
1359 const { AuthPrompt } = require('enquirer');
1360
1361 function authenticate(value, state) {
1362   if (value.username === this.options.username && value.password === this.options.password) {
1363     return true;
1364   }
1365   return false;
1366 }
1367
1368 const CustomAuthPrompt = AuthPrompt.create(authenticate);
1369
1370 const prompt = new CustomAuthPrompt({
1371   name: 'password',
1372   message: 'Please enter your password',
1373   username: 'rajat-sr',
1374   password: '1234567',
1375   choices: [
1376     { name: 'username', message: 'username' },
1377     { name: 'password', message: 'password' }
1378   ]
1379 });
1380
1381 prompt
1382   .run()
1383   .then(answer => console.log('Authenticated?', answer))
1384   .catch(console.error);
1385 ```
1386
1387 #### Related prompts
1388
1389 * [BasicAuth Prompt](#basicauth-prompt)
1390
1391 ***
1392
1393 ### BooleanPrompt
1394
1395 The `BooleanPrompt` class is used for creating prompts that display and return a boolean value.
1396
1397 ```js
1398 const { BooleanPrompt } = require('enquirer');
1399
1400 const  prompt = new  BooleanPrompt({
1401   header:  '========================',
1402   message:  'Do you love enquirer?',
1403   footer:  '========================',
1404 });
1405
1406 prompt.run()
1407   .then(answer  =>  console.log('Selected:', answer))
1408   .catch(console.error);
1409 ```
1410
1411 **Returns**: `boolean`
1412
1413 ***
1414
1415 ### NumberPrompt
1416
1417 The `NumberPrompt` class is used for creating prompts that display and return a numerical value.
1418
1419 ```js
1420 const { NumberPrompt } = require('enquirer');
1421
1422 const  prompt = new  NumberPrompt({
1423   header:  '************************',
1424   message:  'Input the Numbers:',
1425   footer:  '************************',
1426 });
1427
1428 prompt.run()
1429   .then(answer  =>  console.log('Numbers are:', answer))
1430   .catch(console.error);
1431 ```
1432
1433 **Returns**: `string|number` (number, or number formatted as a string)
1434
1435 ***
1436
1437 ### StringPrompt
1438
1439 The `StringPrompt` class is used for creating prompts that display and return a string value.
1440
1441 ```js
1442 const { StringPrompt } = require('enquirer');
1443
1444 const prompt = new StringPrompt({
1445   header: '************************',
1446   message: 'Input the String:',
1447   footer: '************************'
1448 });
1449
1450 prompt.run()
1451   .then(answer => console.log('String is:', answer))
1452   .catch(console.error);
1453 ```
1454
1455 **Returns**: `string`
1456
1457 <br>
1458
1459 ## ❯ Custom prompts
1460
1461 With Enquirer 2.0, custom prompts are easier than ever to create and use.
1462
1463 **How do I create a custom prompt?**
1464
1465 Custom prompts are created by extending either:
1466
1467 * Enquirer's `Prompt` class
1468 * one of the built-in [prompts](#-prompts), or
1469 * low-level [types](#-types).
1470
1471 <!-- Example: HaiKarate Custom Prompt -->
1472
1473 ```js
1474 const { Prompt } = require('enquirer');
1475
1476 class HaiKarate extends Prompt {
1477   constructor(options = {}) {
1478     super(options);
1479     this.value = options.initial || 0;
1480     this.cursorHide();
1481   }
1482   up() {
1483     this.value++;
1484     this.render();
1485   }
1486   down() {
1487     this.value--;
1488     this.render();
1489   }
1490   render() {
1491     this.clear(); // clear previously rendered prompt from the terminal
1492     this.write(`${this.state.message}: ${this.value}`);
1493   }
1494 }
1495
1496 // Use the prompt by creating an instance of your custom prompt class.
1497 const prompt = new HaiKarate({
1498   message: 'How many sprays do you want?',
1499   initial: 10
1500 });
1501
1502 prompt.run()
1503   .then(answer => console.log('Sprays:', answer))
1504   .catch(console.error);
1505 ```
1506
1507 If you want to be able to specify your prompt by `type` so that it may be used alongside other prompts, you will need to first create an instance of `Enquirer`.
1508
1509 ```js
1510 const Enquirer = require('enquirer');
1511 const enquirer = new Enquirer();
1512 ```
1513
1514 Then use the `.register()` method to add your custom prompt.
1515
1516 ```js
1517 enquirer.register('haikarate', HaiKarate);
1518 ```
1519
1520 Now you can do the following when defining "questions".
1521
1522 ```js
1523 let spritzer = require('cologne-drone');
1524 let answers = await enquirer.prompt([
1525   {
1526     type: 'haikarate',
1527     name: 'cologne',
1528     message: 'How many sprays do you need?',
1529     initial: 10,
1530     async onSubmit(name, value) {
1531       await spritzer.activate(value); //<= activate drone
1532       return value;
1533     }
1534   }
1535 ]);
1536 ```
1537
1538 <br>
1539
1540 ## ❯ Key Bindings
1541
1542 ### All prompts
1543
1544 These key combinations may be used with all prompts.
1545
1546 | **command**                      | **description**                        |
1547 | -------------------------------- | -------------------------------------- |
1548 | <kbd>ctrl</kbd>  +  <kbd>c</kbd> | Cancel the prompt.                     |
1549 | <kbd>ctrl</kbd> + <kbd>g</kbd>   | Reset the prompt to its initial state. |
1550
1551 <br>
1552
1553 ### Move cursor
1554
1555 These combinations may be used on prompts that support user input (eg. [input prompt](#input-prompt), [password prompt](#password-prompt), and [invisible prompt](#invisible-prompt)).
1556
1557 | **command**                    | **description**                          |
1558 | ------------------------------ | ---------------------------------------- |
1559 | <kbd>left</kbd>                | Move the cursor back one character.      |
1560 | <kbd>right</kbd>               | Move the cursor forward one character.   |
1561 | <kbd>ctrl</kbd> + <kbd>a</kbd> | Move cursor to the start of the line     |
1562 | <kbd>ctrl</kbd> + <kbd>e</kbd> | Move cursor to the end of the line       |
1563 | <kbd>ctrl</kbd> + <kbd>b</kbd> | Move cursor back one character           |
1564 | <kbd>ctrl</kbd> + <kbd>f</kbd> | Move cursor forward one character        |
1565 | <kbd>ctrl</kbd> + <kbd>x</kbd> | Toggle between first and cursor position |
1566
1567 <br>
1568
1569 ### Edit Input
1570
1571 These key combinations may be used on prompts that support user input (eg. [input prompt](#input-prompt), [password prompt](#password-prompt), and [invisible prompt](#invisible-prompt)).
1572
1573 | **command**                    | **description**                          |
1574 | ------------------------------ | ---------------------------------------- |
1575 | <kbd>ctrl</kbd> + <kbd>a</kbd> | Move cursor to the start of the line     |
1576 | <kbd>ctrl</kbd> + <kbd>e</kbd> | Move cursor to the end of the line       |
1577 | <kbd>ctrl</kbd> + <kbd>b</kbd> | Move cursor back one character           |
1578 | <kbd>ctrl</kbd> + <kbd>f</kbd> | Move cursor forward one character        |
1579 | <kbd>ctrl</kbd> + <kbd>x</kbd> | Toggle between first and cursor position |
1580
1581 <br>
1582
1583 | **command (Mac)**                   | **command (Windows)**            | **description**                                                                                                                           |
1584 | ----------------------------------- | -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
1585 | <kbd>delete</kbd>                   | <kbd>backspace</kbd>             | Delete one character to the left.                                                                                                         |
1586 | <kbd>fn</kbd> + <kbd>delete</kbd>   | <kbd>delete</kbd>                | Delete one character to the right.                                                                                                        |
1587 | <kbd>option</kbd> + <kbd>up</kbd>   | <kbd>alt</kbd> + <kbd>up</kbd>   | Scroll to the previous item in history ([Input prompt](#input-prompt) only, when [history is enabled](examples/input/option-history.js)). |
1588 | <kbd>option</kbd> + <kbd>down</kbd> | <kbd>alt</kbd> + <kbd>down</kbd> | Scroll to the next item in history ([Input prompt](#input-prompt) only, when [history is enabled](examples/input/option-history.js)).     |
1589
1590 ### Select choices
1591
1592 These key combinations may be used on prompts that support _multiple_ choices, such as the [multiselect prompt](#multiselect-prompt), or the [select prompt](#select-prompt) when the `multiple` options is true.
1593
1594 | **command**       | **description**                                                                                                      |
1595 | ----------------- | -------------------------------------------------------------------------------------------------------------------- |
1596 | <kbd>space</kbd>  | Toggle the currently selected choice when `options.multiple` is true.                                                |
1597 | <kbd>number</kbd> | Move the pointer to the choice at the given index. Also toggles the selected choice when `options.multiple` is true. |
1598 | <kbd>a</kbd>      | Toggle all choices to be enabled or disabled.                                                                        |
1599 | <kbd>i</kbd>      | Invert the current selection of choices.                                                                             |
1600 | <kbd>g</kbd>      | Toggle the current choice group.                                                                                     |
1601
1602 <br>
1603
1604 ### Hide/show choices
1605
1606 | **command**                     | **description**                                |
1607 | ------------------------------- | ---------------------------------------------- |
1608 | <kbd>fn</kbd> + <kbd>up</kbd>   | Decrease the number of visible choices by one. |
1609 | <kbd>fn</kbd> + <kbd>down</kbd> | Increase the number of visible choices by one. |
1610
1611 <br>
1612
1613 ### Move/lock Pointer
1614
1615 | **command**                        | **description**                                                                                                      |
1616 | ---------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
1617 | <kbd>number</kbd>                  | Move the pointer to the choice at the given index. Also toggles the selected choice when `options.multiple` is true. |
1618 | <kbd>up</kbd>                      | Move the pointer up.                                                                                                 |
1619 | <kbd>down</kbd>                    | Move the pointer down.                                                                                               |
1620 | <kbd>ctrl</kbd> + <kbd>a</kbd>     | Move the pointer to the first _visible_ choice.                                                                      |
1621 | <kbd>ctrl</kbd> + <kbd>e</kbd>     | Move the pointer to the last _visible_ choice.                                                                       |
1622 | <kbd>shift</kbd> + <kbd>up</kbd>   | Scroll up one choice without changing pointer position (locks the pointer while scrolling).                          |
1623 | <kbd>shift</kbd> + <kbd>down</kbd> | Scroll down one choice without changing pointer position (locks the pointer while scrolling).                        |
1624
1625 <br>
1626
1627 | **command (Mac)**                | **command (Windows)** | **description**                                            |
1628 | -------------------------------- | --------------------- | ---------------------------------------------------------- |
1629 | <kbd>fn</kbd> + <kbd>left</kbd>  | <kbd>home</kbd>       | Move the pointer to the first choice in the choices array. |
1630 | <kbd>fn</kbd> + <kbd>right</kbd> | <kbd>end</kbd>        | Move the pointer to the last choice in the choices array.  |
1631
1632 <br>
1633
1634 ## ❯ Release History
1635
1636 Please see [CHANGELOG.md](CHANGELOG.md).
1637
1638 ## ❯ Performance
1639
1640 ### System specs
1641
1642 MacBook Pro, Intel Core i7, 2.5 GHz, 16 GB.
1643
1644 ### Load time
1645
1646 Time it takes for the module to load the first time (average of 3 runs):
1647
1648 ```
1649 enquirer: 4.013ms
1650 inquirer: 286.717ms
1651 ```
1652
1653 <br>
1654
1655 ## ❯ About
1656
1657 <details>
1658 <summary><strong>Contributing</strong></summary>
1659
1660 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
1661
1662 ### Todo
1663
1664 We're currently working on documentation for the following items. Please star and watch the repository for updates!
1665
1666 * [ ] Customizing symbols
1667 * [ ] Customizing styles (palette)
1668 * [ ] Customizing rendered input
1669 * [ ] Customizing returned values
1670 * [ ] Customizing key bindings
1671 * [ ] Question validation
1672 * [ ] Choice validation
1673 * [ ] Skipping questions
1674 * [ ] Async choices
1675 * [ ] Async timers: loaders, spinners and other animations
1676 * [ ] Links to examples
1677 </details>
1678
1679 <details>
1680 <summary><strong>Running Tests</strong></summary>
1681
1682 Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1683
1684 ```sh
1685 $ npm install && npm test
1686 ```
1687 ```sh
1688 $ yarn && yarn test
1689 ```
1690
1691 </details>
1692
1693 <details>
1694 <summary><strong>Building docs</strong></summary>
1695
1696 _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
1697
1698 To generate the readme, run the following command:
1699
1700 ```sh
1701 $ npm install -g verbose/verb#dev verb-generate-readme && verb
1702 ```
1703
1704 </details>
1705
1706 #### Contributors
1707
1708 | **Commits** | **Contributor** |  
1709 | --- | --- |  
1710 | 283 | [jonschlinkert](https://github.com/jonschlinkert) |  
1711 | 82  | [doowb](https://github.com/doowb) |  
1712 | 32  | [rajat-sr](https://github.com/rajat-sr) |  
1713 | 20  | [318097](https://github.com/318097) |  
1714 | 15  | [g-plane](https://github.com/g-plane) |  
1715 | 12  | [pixelass](https://github.com/pixelass) |  
1716 | 5   | [adityavyas611](https://github.com/adityavyas611) |  
1717 | 5   | [satotake](https://github.com/satotake) |  
1718 | 3   | [tunnckoCore](https://github.com/tunnckoCore) |  
1719 | 3   | [Ovyerus](https://github.com/Ovyerus) |  
1720 | 3   | [sw-yx](https://github.com/sw-yx) |  
1721 | 2   | [DanielRuf](https://github.com/DanielRuf) |  
1722 | 2   | [GabeL7r](https://github.com/GabeL7r) |  
1723 | 1   | [AlCalzone](https://github.com/AlCalzone) |  
1724 | 1   | [hipstersmoothie](https://github.com/hipstersmoothie) |  
1725 | 1   | [danieldelcore](https://github.com/danieldelcore) |  
1726 | 1   | [ImgBotApp](https://github.com/ImgBotApp) |  
1727 | 1   | [jsonkao](https://github.com/jsonkao) |  
1728 | 1   | [knpwrs](https://github.com/knpwrs) |  
1729 | 1   | [yeskunall](https://github.com/yeskunall) |  
1730 | 1   | [mischah](https://github.com/mischah) |  
1731 | 1   | [renarsvilnis](https://github.com/renarsvilnis) |  
1732 | 1   | [sbugert](https://github.com/sbugert) |  
1733 | 1   | [stephencweiss](https://github.com/stephencweiss) |  
1734 | 1   | [skellock](https://github.com/skellock) |  
1735 | 1   | [whxaxes](https://github.com/whxaxes) |  
1736
1737 #### Author
1738
1739 **Jon Schlinkert**
1740
1741 * [GitHub Profile](https://github.com/jonschlinkert)
1742 * [Twitter Profile](https://twitter.com/jonschlinkert)
1743 * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
1744
1745 #### Credit
1746
1747 Thanks to [derhuerst](https://github.com/derhuerst), creator of prompt libraries such as [prompt-skeleton](https://github.com/derhuerst/prompt-skeleton), which influenced some of the concepts we used in our prompts.
1748
1749 #### License
1750
1751 Copyright © 2018-present, [Jon Schlinkert](https://github.com/jonschlinkert).
1752 Released under the [MIT License](LICENSE).