Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. <p align="center">
  2. <img src="https://github.com/terkelg/prompts/raw/master/prompts.png" alt="Prompts" width="500" />
  3. </p>
  4. <h1 align="center">❯ Prompts</h1>
  5. <p align="center">
  6. <a href="https://npmjs.org/package/prompts">
  7. <img src="https://img.shields.io/npm/v/prompts.svg" alt="version" />
  8. </a>
  9. <a href="https://travis-ci.org/terkelg/prompts">
  10. <img src="https://img.shields.io/travis/terkelg/prompts.svg" alt="travis" />
  11. </a>
  12. <a href="https://npmjs.org/package/prompts">
  13. <img src="https://img.shields.io/npm/dm/prompts.svg" alt="downloads" />
  14. </a>
  15. <!---
  16. <a href="https://packagephobia.now.sh/result?p=prompts">
  17. <img src="https://packagephobia.now.sh/badge?p=prompts" alt="install size" />
  18. </a>
  19. --->
  20. </p>
  21. <p align="center">
  22. <b>Lightweight, beautiful and user-friendly interactive prompts</b><br />
  23. <sub>>_ Easy to use CLI prompts to enquire users for information▌</sub>
  24. </p>
  25. <br />
  26. * **Simple**: prompts has [no big dependencies](http://npm.anvaka.com/#/view/2d/prompts) nor is it broken into a [dozen](http://npm.anvaka.com/#/view/2d/inquirer) tiny modules that only work well together.
  27. * **User friendly**: prompt uses layout and colors to create beautiful cli interfaces.
  28. * **Promised**: uses promises and `async`/`await`. No callback hell.
  29. * **Flexible**: all prompts are independent and can be used on their own.
  30. * **Testable**: provides a way to submit answers programmatically.
  31. * **Unified**: consistent experience across all [prompts](#-types).
  32. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  33. ## ❯ Install
  34. ```
  35. $ npm install --save prompts
  36. ```
  37. > This package supports Node 6 and above
  38. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  39. ## ❯ Usage
  40. <img src="https://github.com/terkelg/prompts/raw/master/media/example.gif" alt="example prompt" width="499" height="103" />
  41. ```js
  42. const prompts = require('prompts');
  43. (async () => {
  44. const response = await prompts({
  45. type: 'number',
  46. name: 'value',
  47. message: 'How old are you?',
  48. validate: value => value < 18 ? `Nightclub is 18+ only` : true
  49. });
  50. console.log(response); // => { value: 24 }
  51. })();
  52. ```
  53. > See [`example.js`](https://github.com/terkelg/prompts/blob/master/example.js) for more options.
  54. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  55. ## ❯ Examples
  56. ### Single Prompt
  57. Prompt with a single prompt object. Returns an object with the response.
  58. ```js
  59. const prompts = require('prompts');
  60. (async () => {
  61. const response = await prompts({
  62. type: 'text',
  63. name: 'meaning',
  64. message: 'What is the meaning of life?'
  65. });
  66. console.log(response.meaning);
  67. })();
  68. ```
  69. ### Prompt Chain
  70. Prompt with a list of prompt objects. Returns an object with the responses.
  71. Make sure to give each prompt a unique `name` property to prevent overwriting values.
  72. ```js
  73. const prompts = require('prompts');
  74. const questions = [
  75. {
  76. type: 'text',
  77. name: 'username',
  78. message: 'What is your GitHub username?'
  79. },
  80. {
  81. type: 'number',
  82. name: 'age',
  83. message: 'How old are you?'
  84. },
  85. {
  86. type: 'text',
  87. name: 'about',
  88. message: 'Tell something about yourself',
  89. initial: 'Why should I?'
  90. }
  91. ];
  92. (async () => {
  93. const response = await prompts(questions);
  94. // => response => { username, age, about }
  95. })();
  96. ```
  97. ### Dynamic Prompts
  98. Prompt properties can be functions too.
  99. Prompt Objects with `type` set to `falsy` values are skipped.
  100. ```js
  101. const prompts = require('prompts');
  102. const questions = [
  103. {
  104. type: 'text',
  105. name: 'dish',
  106. message: 'Do you like pizza?'
  107. },
  108. {
  109. type: prev => prev == 'pizza' ? 'text' : null,
  110. name: 'topping',
  111. message: 'Name a topping'
  112. }
  113. ];
  114. (async () => {
  115. const response = await prompts(questions);
  116. })();
  117. ```
  118. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  119. ## ❯ API
  120. ### prompts(prompts, options)
  121. Type: `Function`<br>
  122. Returns: `Object`
  123. Prompter function which takes your [prompt objects](#-prompt-objects) and returns an object with responses.
  124. #### prompts
  125. Type: `Array|Object`<br>
  126. Array of [prompt objects](#-prompt-objects).
  127. These are the questions the user will be prompted. You can see the list of supported [prompt types here](#-types).
  128. Prompts can be submitted (<kbd>return</kbd>, <kbd>enter</kbd>) or canceled (<kbd>esc</kbd>, <kbd>abort</kbd>, <kbd>ctrl</kbd>+<kbd>c</kbd>, <kbd>ctrl</kbd>+<kbd>d</kbd>). No property is being defined on the returned response object when a prompt is canceled.
  129. #### options.onSubmit
  130. Type: `Function`<br>
  131. Default: `() => {}`
  132. Callback that's invoked after each prompt submission.
  133. Its signature is `(prompt, answer, answers)` where `prompt` is the current prompt object, `answer` the user answer to the current question and `answers` the user answers so far. Async functions are supported.
  134. Return `true` to quit the prompt chain and return all collected responses so far, otherwise continue to iterate prompt objects.
  135. **Example:**
  136. ```js
  137. (async () => {
  138. const questions = [{ ... }];
  139. const onSubmit = (prompt, answer) => console.log(`Thanks I got ${answer} from ${prompt.name}`);
  140. const response = await prompts(questions, { onSubmit });
  141. })();
  142. ```
  143. #### options.onCancel
  144. Type: `Function`<br>
  145. Default: `() => {}`
  146. Callback that's invoked when the user cancels/exits the prompt.
  147. Its signature is `(prompt, answers)` where `prompt` is the current prompt object and `answers` the user answers so far. Async functions are supported.
  148. Return `true` to continue and prevent the prompt loop from aborting.
  149. On cancel responses collected so far are returned.
  150. **Example:**
  151. ```js
  152. (async () => {
  153. const questions = [{ ... }];
  154. const onCancel = prompt => {
  155. console.log('Never stop prompting!');
  156. return true;
  157. }
  158. const response = await prompts(questions, { onCancel });
  159. })();
  160. ```
  161. ### override
  162. Type: `Function`
  163. Preanswer questions by passing an object with answers to `prompts.override`.
  164. Powerful when combined with arguments of process.
  165. **Example**
  166. ```js
  167. const prompts = require('prompts');
  168. prompts.override(require('yargs').argv);
  169. (async () => {
  170. const response = await prompts([
  171. {
  172. type: 'text',
  173. name: 'twitter',
  174. message: `What's your twitter handle?`
  175. },
  176. {
  177. type: 'multiselect',
  178. name: 'color',
  179. message: 'Pick colors',
  180. choices: [
  181. { title: 'Red', value: '#ff0000' },
  182. { title: 'Green', value: '#00ff00' },
  183. { title: 'Blue', value: '#0000ff' }
  184. ],
  185. }
  186. ]);
  187. console.log(response);
  188. })();
  189. ```
  190. ### inject(values)
  191. Type: `Function`<br>
  192. Programmatically inject responses. This enables you to prepare the responses ahead of time.
  193. If any injected value is found the prompt is immediately resolved with the injected value.
  194. This feature is intended for testing only.
  195. #### values
  196. Type: `Array`
  197. Array with values to inject. Resolved values are removed from the internal inject array.
  198. Each value can be an array of values in order to provide answers for a question asked multiple times.
  199. If a value is an instance of `Error` it will simulate the user cancelling/exiting the prompt.
  200. **Example:**
  201. ```js
  202. const prompts = require('prompts');
  203. prompts.inject([ '@terkelg', ['#ff0000', '#0000ff'] ]);
  204. (async () => {
  205. const response = await prompts([
  206. {
  207. type: 'text',
  208. name: 'twitter',
  209. message: `What's your twitter handle?`
  210. },
  211. {
  212. type: 'multiselect',
  213. name: 'color',
  214. message: 'Pick colors',
  215. choices: [
  216. { title: 'Red', value: '#ff0000' },
  217. { title: 'Green', value: '#00ff00' },
  218. { title: 'Blue', value: '#0000ff' }
  219. ],
  220. }
  221. ]);
  222. // => { twitter: 'terkelg', color: [ '#ff0000', '#0000ff' ] }
  223. })();
  224. ```
  225. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  226. ## ❯ Prompt Objects
  227. Prompts Objects are JavaScript objects that define the "questions" and the [type of prompt](#-types).
  228. Almost all prompt objects have the following properties:
  229. ```js
  230. {
  231. type: String | Function,
  232. name: String | Function,
  233. message: String | Function,
  234. initial: String | Function | Async Function
  235. format: Function | Async Function,
  236. onRender: Function
  237. onState: Function
  238. stdin: Readable
  239. stdout: Writeable
  240. }
  241. ```
  242. Each property be of type `function` and will be invoked right before prompting the user.
  243. The function signature is `(prev, values, prompt)`, where `prev` is the value from the previous prompt,
  244. `values` is the response object with all values collected so far and `prompt` is the previous prompt object.
  245. **Function example:**
  246. ```js
  247. {
  248. type: prev => prev > 3 ? 'confirm' : null,
  249. name: 'confirm',
  250. message: (prev, values) => `Please confirm that you eat ${values.dish} times ${prev} a day?`
  251. }
  252. ```
  253. The above prompt will be skipped if the value of the previous prompt is less than 3.
  254. ### type
  255. Type: `String|Function`
  256. Defines the type of prompt to display. See the list of [prompt types](#-types) for valid values.
  257. If `type` is a falsy value the prompter will skip that question.
  258. ```js
  259. {
  260. type: null,
  261. name: 'forgetme',
  262. message: `I'll never be shown anyway`,
  263. }
  264. ```
  265. ### name
  266. Type: `String|Function`
  267. The response will be saved under this key/property in the returned response object.
  268. In case you have multiple prompts with the same name only the latest response will be stored.
  269. > Make sure to give prompts unique names if you don't want to overwrite previous values.
  270. ### message
  271. Type: `String|Function`
  272. The message to be displayed to the user.
  273. ### initial
  274. Type: `String|Function`
  275. Optional default prompt value. Async functions are supported too.
  276. ### format
  277. Type: `Function`
  278. Receive the user input and return the formatted value to be used inside the program.
  279. The value returned will be added to the response object.
  280. The function signature is `(val, values)`, where `val` is the value from the current prompt and
  281. `values` is the current response object in case you need to format based on previous responses.
  282. **Example:**
  283. ```js
  284. {
  285. type: 'number',
  286. name: 'price',
  287. message: 'Enter price',
  288. format: val => Intl.NumberFormat(undefined, { style: 'currency', currency: 'USD' }).format(val);
  289. }
  290. ```
  291. ### onRender
  292. Type: `Function`
  293. Callback for when the prompt is rendered.
  294. The function receives [kleur](https://github.com/lukeed/kleur) as its first argument and `this` refers to the current prompt.
  295. **Example:**
  296. ```js
  297. {
  298. type: 'number',
  299. message: 'This message will be overridden',
  300. onRender(kleur) {
  301. this.msg = kleur.cyan('Enter a number');
  302. }
  303. }
  304. ```
  305. ### onState
  306. Type: `Function`
  307. Callback for when the state of the current prompt changes.
  308. The function signature is `(state)` where `state` is an object with a snapshot of the current state.
  309. The state object has two properties `value` and `aborted`. E.g `{ value: 'This is ', aborted: false }`
  310. ### stdin and stdout
  311. Type: `Stream`
  312. By default, prompts uses `process.stdin` for receiving input and `process.stdout` for writing output.
  313. If you need to use different streams, for instance `process.stderr`, you can set these with the `stdin` and `stdout` properties.
  314. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  315. ## ❯ Types
  316. * [text](#textmessage-initial-style)
  317. * [password](#passwordmessage-initial)
  318. * [invisible](#invisiblemessage-initial)
  319. * [number](#numbermessage-initial-max-min-style)
  320. * [confirm](#confirmmessage-initial)
  321. * [list](#listmessage-initial)
  322. * [toggle](#togglemessage-initial-active-inactive)
  323. * [select](#selectmessage-choices-initial-hint-warn)
  324. * [multiselect](#multiselectmessage-choices-initial-max-hint-warn)
  325. * [autocompleteMultiselect](#multiselectmessage-choices-initial-max-hint-warn)
  326. * [autocomplete](#autocompletemessage-choices-initial-suggest-limit-style)
  327. * [date](#datemessage-initial-warn)
  328. ***
  329. ### text(message, [initial], [style])
  330. > Text prompt for free text input.
  331. Hit <kbd>tab</kbd> to autocomplete to `initial` value when provided.
  332. #### Example
  333. <img src="https://github.com/terkelg/prompts/raw/master/media/text.gif" alt="text prompt" width="499" height="103" />
  334. ```js
  335. {
  336. type: 'text',
  337. name: 'value',
  338. message: `What's your twitter handle?`
  339. }
  340. ```
  341. #### Options
  342. | Param | Type | Description |
  343. | ----- | :--: | ----------- |
  344. | message | `string` | Prompt message to display |
  345. | initial | `string` | Default string value |
  346. | style | `string` | Render style (`default`, `password`, `invisible`, `emoji`). Defaults to `default` |
  347. | format | `function` | Receive user input. The returned value will be added to the response object |
  348. | validate | `function` | Receive user input. Should return `true` if the value is valid, and an error message `String` otherwise. If `false` is returned, a default error message is shown |
  349. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  350. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  351. **↑ back to:** [Prompt types](#-types)
  352. ***
  353. ### password(message, [initial])
  354. > Password prompt with masked input.
  355. This prompt is a similar to a prompt of type `'text'` with `style` set to `'password'`.
  356. #### Example
  357. <img src="https://github.com/terkelg/prompts/raw/master/media/password.gif" alt="password prompt" width="499" height="103" />
  358. ```js
  359. {
  360. type: 'password',
  361. name: 'value',
  362. message: 'Tell me a secret'
  363. }
  364. ```
  365. #### Options
  366. | Param | Type | Description |
  367. | ----- | :--: | ----------- |
  368. | message | `string` | Prompt message to display |
  369. | initial | `string` | Default string value |
  370. | format | `function` | Receive user input. The returned value will be added to the response object |
  371. | validate | `function` | Receive user input. Should return `true` if the value is valid, and an error message `String` otherwise. If `false` is returned, a default error message is shown |
  372. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  373. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  374. **↑ back to:** [Prompt types](#-types)
  375. ***
  376. ### invisible(message, [initial])
  377. > Prompts user for invisible text input.
  378. This prompt is working like `sudo` where the input is invisible.
  379. This prompt is a similar to a prompt of type `'text'` with style set to `'invisible'`.
  380. #### Example
  381. <img src="https://github.com/terkelg/prompts/raw/master/media/invisible.gif" alt="invisible prompt" width="499" height="103" />
  382. ```js
  383. {
  384. type: 'invisible',
  385. name: 'value',
  386. message: 'Enter password'
  387. }
  388. ```
  389. #### Options
  390. | Param | Type | Description |
  391. | ----- | :--: | ----------- |
  392. | message | `string` | Prompt message to display |
  393. | initial | `string` | Default string value |
  394. | format | `function` | Receive user input. The returned value will be added to the response object |
  395. | validate | `function` | Receive user input. Should return `true` if the value is valid, and an error message `String` otherwise. If `false` is returned, a default error message is shown |
  396. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  397. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  398. **↑ back to:** [Prompt types](#-types)
  399. ***
  400. ### number(message, initial, [max], [min], [style])
  401. > Prompts user for number input.
  402. You can type in numbers and use <kbd>up</kbd>/<kbd>down</kbd> to increase/decrease the value. Only numbers are allowed as input. Hit <kbd>tab</kbd> to autocomplete to `initial` value when provided.
  403. #### Example
  404. <img src="https://github.com/terkelg/prompts/raw/master/media/number.gif" alt="number prompt" width="499" height="103" />
  405. ```js
  406. {
  407. type: 'number',
  408. name: 'value',
  409. message: 'How old are you?',
  410. initial: 0,
  411. style: 'default',
  412. min: 2,
  413. max: 10
  414. }
  415. ```
  416. #### Options
  417. | Param | Type | Description |
  418. | ----- | :--: | ----------- |
  419. | message | `string` | Prompt message to display |
  420. | initial | `number` | Default number value |
  421. | format | `function` | Receive user input. The returned value will be added to the response object |
  422. | validate | `function` | Receive user input. Should return `true` if the value is valid, and an error message `String` otherwise. If `false` is returned, a default error message is shown |
  423. | max | `number` | Max value. Defaults to `Infinity` |
  424. | min | `number` | Min value. Defaults to `-infinity` |
  425. | float | `boolean` | Allow floating point inputs. Defaults to `false` |
  426. | round | `number` | Round `float` values to x decimals. Defaults to `2` |
  427. | increment | `number` | Increment step when using <kbd>arrow</kbd> keys. Defaults to `1` |
  428. | style | `string` | Render style (`default`, `password`, `invisible`, `emoji`). Defaults to `default` |
  429. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  430. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  431. **↑ back to:** [Prompt types](#-types)
  432. ***
  433. ### confirm(message, [initial])
  434. > Classic yes/no prompt.
  435. Hit <kbd>y</kbd> or <kbd>n</kbd> to confirm/reject.
  436. #### Example
  437. <img src="https://github.com/terkelg/prompts/raw/master/media/confirm.gif" alt="confirm prompt" width="499" height="103" />
  438. ```js
  439. {
  440. type: 'confirm',
  441. name: 'value',
  442. message: 'Can you confirm?',
  443. initial: true
  444. }
  445. ```
  446. #### Options
  447. | Param | Type | Description |
  448. | ----- | :--: | ----------- |
  449. | message | `string` | Prompt message to display |
  450. | initial | `boolean` | Default value. Default is `false` |
  451. | format | `function` | Receive user input. The returned value will be added to the response object |
  452. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  453. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  454. **↑ back to:** [Prompt types](#-types)
  455. ***
  456. ### list(message, [initial])
  457. > List prompt that return an array.
  458. Similar to the `text` prompt, but the output is an `Array` containing the
  459. string separated by `separator`.
  460. ```js
  461. {
  462. type: 'list',
  463. name: 'value',
  464. message: 'Enter keywords',
  465. initial: '',
  466. separator: ','
  467. }
  468. ```
  469. <img src="https://github.com/terkelg/prompts/raw/master/media/list.gif" alt="list prompt" width="499" height="103" />
  470. | Param | Type | Description |
  471. | ----- | :--: | ----------- |
  472. | message | `string` | Prompt message to display |
  473. | initial | `boolean` | Default value |
  474. | format | `function` | Receive user input. The returned value will be added to the response object |
  475. | separator | `string` | String separator. Will trim all white-spaces from start and end of string. Defaults to `','` |
  476. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  477. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  478. **↑ back to:** [Prompt types](#-types)
  479. ***
  480. ### toggle(message, [initial], [active], [inactive])
  481. > Interactive toggle/switch prompt.
  482. Use tab or <kbd>arrow keys</kbd>/<kbd>tab</kbd>/<kbd>space</kbd> to switch between options.
  483. #### Example
  484. <img src="https://github.com/terkelg/prompts/raw/master/media/toggle.gif" alt="toggle prompt" width="499" height="103" />
  485. ```js
  486. {
  487. type: 'toggle',
  488. name: 'value',
  489. message: 'Can you confirm?',
  490. initial: true,
  491. active: 'yes',
  492. inactive: 'no'
  493. }
  494. ```
  495. #### Options
  496. | Param | Type | Description |
  497. | ----- | :--: | ----------- |
  498. | message | `string` | Prompt message to display |
  499. | initial | `boolean` | Default value. Defaults to `false` |
  500. | format | `function` | Receive user input. The returned value will be added to the response object |
  501. | active | `string` | Text for `active` state. Defaults to `'on'` |
  502. | inactive | `string` | Text for `inactive` state. Defaults to `'off'` |
  503. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  504. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  505. **↑ back to:** [Prompt types](#-types)
  506. ***
  507. ### select(message, choices, [initial], [hint], [warn])
  508. > Interactive select prompt.
  509. Use <kbd>up</kbd>/<kbd>down</kbd> to navigate. Use <kbd>tab</kbd> to cycle the list.
  510. #### Example
  511. <img src="https://github.com/terkelg/prompts/raw/master/media/select.gif" alt="select prompt" width="499" height="130" />
  512. ```js
  513. {
  514. type: 'select',
  515. name: 'value',
  516. message: 'Pick a color',
  517. choices: [
  518. { title: 'Red', description: 'This option has a description', value: '#ff0000' },
  519. { title: 'Green', value: '#00ff00', disabled: true },
  520. { title: 'Blue', value: '#0000ff' }
  521. ],
  522. initial: 1
  523. }
  524. ```
  525. #### Options
  526. | Param | Type | Description |
  527. | ----- | :--: | ----------- |
  528. | message | `string` | Prompt message to display |
  529. | initial | `number` | Index of default value |
  530. | format | `function` | Receive user input. The returned value will be added to the response object |
  531. | hint | `string` | Hint to display to the user |
  532. | warn | `string` | Message to display when selecting a disabled option |
  533. | choices | `Array` | Array of strings or choices objects `[{ title, description, value, disabled }, ...]`. The choice's index in the array will be used as its value if it is not specified. |
  534. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  535. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  536. **↑ back to:** [Prompt types](#-types)
  537. ***
  538. ### multiselect(message, choices, [initial], [max], [hint], [warn])
  539. ### autocompleteMultiselect(same)
  540. > Interactive multi-select prompt.
  541. > Autocomplete is a searchable multiselect prompt with the same options. Useful for long lists.
  542. Use <kbd>space</kbd> to toggle select/unselect and <kbd>up</kbd>/<kbd>down</kbd> to navigate. Use <kbd>tab</kbd> to cycle the list. You can also use <kbd>right</kbd> to select and <kbd>left</kbd> to deselect.
  543. By default this prompt returns an `array` containing the **values** of the selected items - not their display title.
  544. #### Example
  545. <img src="https://github.com/terkelg/prompts/raw/master/media/multiselect.gif" alt="multiselect prompt" width="499" height="130" />
  546. ```js
  547. {
  548. type: 'multiselect',
  549. name: 'value',
  550. message: 'Pick colors',
  551. choices: [
  552. { title: 'Red', value: '#ff0000' },
  553. { title: 'Green', value: '#00ff00', disabled: true },
  554. { title: 'Blue', value: '#0000ff', selected: true }
  555. ],
  556. max: 2,
  557. hint: '- Space to select. Return to submit'
  558. }
  559. ```
  560. #### Options
  561. | Param | Type | Description |
  562. | ----- | :--: | ----------- |
  563. | message | `string` | Prompt message to display |
  564. | format | `function` | Receive user input. The returned value will be added to the response object |
  565. | instructions | `string` or `boolean` | Prompt instructions to display |
  566. | choices | `Array` | Array of strings or choices objects `[{ title, value, disabled }, ...]`. The choice's index in the array will be used as its value if it is not specified. |
  567. | optionsPerPage | `number` | Number of options displayed per page (default: 10) |
  568. | min | `number` | Min select - will display error |
  569. | max | `number` | Max select |
  570. | hint | `string` | Hint to display to the user |
  571. | warn | `string` | Message to display when selecting a disabled option |
  572. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  573. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  574. This is one of the few prompts that don't take a initial value.
  575. If you want to predefine selected values, give the choice object an `selected` property of `true`.
  576. **↑ back to:** [Prompt types](#-types)
  577. ***
  578. ### autocomplete(message, choices, [initial], [suggest], [limit], [style])
  579. > Interactive auto complete prompt.
  580. The prompt will list options based on user input. Type to filter the list.
  581. Use <kbd>⇧</kbd>/<kbd>⇩</kbd> to navigate. Use <kbd>tab</kbd> to cycle the result. Use <kbd>Page Up</kbd>/<kbd>Page Down</kbd> (on Mac: <kbd>fn</kbd> + <kbd>⇧</kbd> / <kbd>⇩</kbd>) to change page. Hit <kbd>enter</kbd> to select the highlighted item below the prompt.
  582. The default suggests function is sorting based on the `title` property of the choices.
  583. You can overwrite how choices are being filtered by passing your own suggest function.
  584. #### Example
  585. <img src="https://github.com/terkelg/prompts/raw/master/media/autocomplete.gif" alt="auto complete prompt" width="499" height="163" />
  586. ```js
  587. {
  588. type: 'autocomplete',
  589. name: 'value',
  590. message: 'Pick your favorite actor',
  591. choices: [
  592. { title: 'Cage' },
  593. { title: 'Clooney', value: 'silver-fox' },
  594. { title: 'Gyllenhaal' },
  595. { title: 'Gibson' },
  596. { title: 'Grant' }
  597. ]
  598. }
  599. ```
  600. #### Options
  601. | Param | Type | Description |
  602. | ----- | :--: | ----------- |
  603. | message | `string` | Prompt message to display |
  604. | format | `function` | Receive user input. The returned value will be added to the response object |
  605. | choices | `Array` | Array of auto-complete choices objects `[{ title, value }, ...]` |
  606. | suggest | `function` | Filter function. Defaults to sort by `title` property. `suggest` should always return a promise. Filters using `title` by default |
  607. | limit | `number` | Max number of results to show. Defaults to `10` |
  608. | style | `string` | Render style (`default`, `password`, `invisible`, `emoji`). Defaults to `'default'` |
  609. | initial | `string \| number` | Default initial value |
  610. | clearFirst | `boolean` | The first ESCAPE keypress will clear the input |
  611. | fallback | `string` | Fallback message when no match is found. Defaults to `initial` value if provided |
  612. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  613. | onState | `function` | On state change callback. Function signature is an `object` with three properties: `value`, `aborted` and `exited` |
  614. Example on what a `suggest` function might look like:
  615. ```js
  616. const suggestByTitle = (input, choices) =>
  617. Promise.resolve(choices.filter(i => i.title.slice(0, input.length) === input))
  618. ```
  619. **↑ back to:** [Prompt types](#-types)
  620. ***
  621. ### date(message, [initial], [warn])
  622. > Interactive date prompt.
  623. Use <kbd>left</kbd>/<kbd>right</kbd>/<kbd>tab</kbd> to navigate. Use <kbd>up</kbd>/<kbd>down</kbd> to change date.
  624. #### Example
  625. <img src="https://github.com/terkelg/prompts/raw/master/media/date.gif" alt="date prompt" width="499" height="103" />
  626. ```js
  627. {
  628. type: 'date',
  629. name: 'value',
  630. message: 'Pick a date',
  631. initial: new Date(1997, 09, 12),
  632. validate: date => date > Date.now() ? 'Not in the future' : true
  633. }
  634. ```
  635. #### Options
  636. | Param | Type | Description |
  637. | ----- | :--: | ----------- |
  638. | message | `string` | Prompt message to display |
  639. | initial | `date` | Default date |
  640. | locales | `object` | Use to define custom locales. See below for an example. |
  641. | mask | `string` | The format mask of the date. See below for more information.<br />Default: `YYYY-MM-DD HH:mm:ss` |
  642. | validate | `function` | Receive user input. Should return `true` if the value is valid, and an error message `String` otherwise. If `false` is returned, a default error message is shown |
  643. | onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
  644. | onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
  645. Default locales:
  646. ```javascript
  647. {
  648. months: [
  649. 'January', 'February', 'March', 'April',
  650. 'May', 'June', 'July', 'August',
  651. 'September', 'October', 'November', 'December'
  652. ],
  653. monthsShort: [
  654. 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  655. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  656. ],
  657. weekdays: [
  658. 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
  659. 'Thursday', 'Friday', 'Saturday'
  660. ],
  661. weekdaysShort: [
  662. 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  663. ]
  664. }
  665. ```
  666. >**Formatting**: See full list of formatting options in the [wiki](https://github.com/terkelg/prompts/wiki/Date-Time-Formatting)
  667. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  668. **↑ back to:** [Prompt types](#-types)
  669. ***
  670. ## ❯ Credit
  671. Many of the prompts are based on the work of [derhuerst](https://github.com/derhuerst).
  672. ## ❯ License
  673. MIT © [Terkel Gjervig](https://terkel.com)