Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <p align="center">
  2. <img src="https://github.com/terkelg/prompts/raw/master/prompts.png" alt="Prompts" width="500" height="120" />
  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. <a href="https://packagephobia.now.sh/result?p=prompts">
  16. <img src="https://packagephobia.now.sh/badge?p=prompts" alt="install size" />
  17. </a>
  18. </p>
  19. <p align="center">
  20. <b>Lightweight, beautiful and user-friendly interactive prompts</b></br>
  21. <sub>>_ Easy to use CLI prompts to enquire users for information▌<sub>
  22. </p>
  23. <br />
  24. * **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.
  25. * **User friendly**: prompt uses layout and colors to create beautiful cli interfaces.
  26. * **Promised**: uses promises and `async`/`await`. No callback hell.
  27. * **Flexible**: all prompts are independent and can be used on their own.
  28. * **Testable**: provides a way to submit answers programmatically.
  29. * **Unified**: consistent experience across all prompts.
  30. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  31. ## ❯ Install
  32. ```
  33. $ npm install --save prompts
  34. ```
  35. > This package supports Node 6 and above
  36. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  37. ## ❯ Usage
  38. <img src="https://github.com/terkelg/prompts/raw/master/media/number.gif" alt="example prompt" width="499" height="103" />
  39. ```js
  40. const prompts = require('prompts');
  41. const response = await prompts({
  42. type: 'number',
  43. name: 'value',
  44. message: 'How old are you?'
  45. });
  46. console.log(response); // => { value: 23 }
  47. ```
  48. > Examples are meant to be illustrative. `await` calls need to be run within an async function. See [`example.js`](https://github.com/terkelg/prompts/blob/master/example.js).
  49. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  50. ## ❯ Examples
  51. ### Single Prompt
  52. Prompt with a single prompt object. Returns object with the response.
  53. ```js
  54. const prompts = require('prompts');
  55. let response = await prompts({
  56. type: 'text',
  57. name: 'meaning',
  58. message: 'What is the meaning of life?'
  59. });
  60. console.log(response.meaning);
  61. ```
  62. ### Prompt Chain
  63. Prompt with a list of prompt objects. Returns object with response.
  64. Make sure to give each prompt a unique `name` property to prevent overwriting values.
  65. ```js
  66. const prompts = require('prompts');
  67. let questions = [
  68. {
  69. type: 'text',
  70. name: 'username',
  71. message: 'What is your GitHub username?'
  72. },
  73. {
  74. type: 'number',
  75. name: 'age',
  76. message: 'How old are you?'
  77. },
  78. {
  79. type: 'text',
  80. name: 'about',
  81. message: 'Tell something about yourself',
  82. initial: 'Why should I?'
  83. }
  84. ];
  85. let response = await prompts(questions);
  86. // => response => { username, age, about }
  87. ```
  88. ### Dynamic Prompts
  89. Prompt properties can be functions too.
  90. Prompt Objects with `type` set to `falsy` values are skipped.
  91. ```js
  92. const prompts = require('prompts');
  93. let questions = [
  94. {
  95. type: 'text',
  96. name: 'dish',
  97. message: 'Do you like pizza?'
  98. },
  99. {
  100. type: prev => prev == 'pizza' ? 'text' : null,
  101. name: 'topping',
  102. message: 'Name a topping'
  103. }
  104. ];
  105. let response = await prompts(questions);
  106. ```
  107. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  108. ## ❯ API
  109. ### prompts(prompts, options)
  110. Type: `Function`<br>
  111. Returns: `Object`
  112. Prompter function which takes your [prompt objects](#-prompt-objects) and returns an object with responses.
  113. #### prompts
  114. Type: `Array|Object`<br>
  115. Array of [prompt objects](#-prompt-objects).
  116. These are the questions the user will be prompted. You can see the list of supported [prompt types here](#-types).
  117. 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.
  118. #### options.onSubmit
  119. Type: `Function`<br>
  120. Default: `() => {}`
  121. Callback that's invoked after each prompt submission.
  122. Its signature is `(prompt, response)` where `prompt` is the current prompt object.
  123. Return `true` to quit the prompt chain and return all collected responses so far, otherwise continue to iterate prompt objects.
  124. **Example:**
  125. ```js
  126. let questions = [{ ... }];
  127. let onSubmit = (prompt, response) => console.log(`Thanks I got ${response} from ${prompt.name}`);
  128. let response = await prompts(questions, { onSubmit });
  129. ```
  130. #### options.onCancel
  131. Type: `Function`<br>
  132. Default: `() => {}`
  133. Callback that's invoked when the user cancels/exits the prompt.
  134. Its signature is `(prompt)` where `prompt` is the current prompt object.
  135. Return `true` to continue and prevent the prompt loop from aborting.
  136. On cancel responses collected so far are returned.
  137. **Example:**
  138. ```js
  139. let questions = [{ ... }];
  140. let onCancel = prompt => {
  141. console.log('Never stop prompting!');
  142. return true;
  143. }
  144. let response = await prompts(questions, { onCancel });
  145. ```
  146. ### inject(values)
  147. Type: `Function`<br>
  148. Programmatically inject responses. This enables you to prepare the responses ahead of time.
  149. If any injected values are found the prompt is immediately resolved with the injected value.
  150. This feature is intended for testing only.
  151. #### values
  152. Type: `Object`
  153. Object with key/values to inject. Resolved values are deleted from the internal inject object.
  154. **Example:**
  155. ```js
  156. const prompts = require('prompts');
  157. prompts.inject({ q1: 'a1', q2: 'q2' });
  158. let response = await prompts({
  159. type: 'text',
  160. name: 'q1',
  161. message: 'Question 1'
  162. });
  163. // => { q1: 'a1' }
  164. ```
  165. > When `q1` resolves it's wiped. `q2` doesn't resolve and is left untouched.
  166. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  167. ## ❯ Prompt Objects
  168. Prompts Objects are JavaScript objects that define the "questions" and the [type of prompt](#-types).
  169. Almost all prompt objects have the following properties:
  170. ```js
  171. {
  172. type: String || Function,
  173. name: String || Function,
  174. message: String || Function,
  175. initial: String || Function || Async Function
  176. format: Function || Async Function,
  177. onState: Function
  178. }
  179. ```
  180. Each property be of type `function` and will be invoked right before prompting the user.
  181. The function signature is `(prev, values, prompt)`, where `prev` is the value from the previous prompt,
  182. `values` is the response object with all values collected so far and `prompt` is the previous prompt object.
  183. **Function example:**
  184. ```js
  185. {
  186. type: prev => prev > 3 ? 'confirm' : null,
  187. name: 'confirm',
  188. message: (prev, values) => `Please confirm that you eat ${values.dish} times ${prev} a day?`
  189. }
  190. ```
  191. The above prompt will be skipped if the value of the previous prompt is less than 3.
  192. ### type
  193. Type: `String|Function`
  194. Defines the type of prompt to display. See the list of [prompt types](#-types) for valid values.
  195. If `type` is a falsy value the prompter will skip that question.
  196. ```js
  197. {
  198. type: null,
  199. name: 'forgetme',
  200. message: `I'll never be shown anyway`,
  201. }
  202. ```
  203. ### name
  204. Type: `String|Function`
  205. The response will be saved under this key/property in the returned response object.
  206. In case you have multiple prompts with the same name only the latest response will be stored.
  207. > Make sure to give prompts unique names if you don't want to overwrite previous values.
  208. ### message
  209. Type: `String|Function`
  210. The message to be displayed to the user.
  211. ### initial
  212. Type: `String|Function`
  213. Optional default prompt value. Async functions are supported too.
  214. ### format
  215. Type: `Function`
  216. Receive the user input and return the formatted value to be used inside the program.
  217. The value returned will be added to the response object.
  218. The function signature is `(val, values)`, where `val` is the value from the current prompt and
  219. `values` is the current response object in case you need to format based on previous responses.
  220. **Example:**
  221. ```js
  222. {
  223. type: 'number',
  224. name: 'price',
  225. message: 'Enter price',
  226. format: val => Intl.NumberFormat(undefined, { style: 'currency', currency: 'USD' }).format(val);
  227. }
  228. ```
  229. ### onState
  230. Type: `Function`
  231. Callback for when the state of the current prompt changes.
  232. The function signature is `(state)` where `state` is an object with a snapshot of the current state.
  233. The state object have two properties `value` and `aborted`. E.g `{ value: 'This is ', aborted: false }`
  234. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  235. ## ❯ Types
  236. ### text(message, [initial], [style])
  237. > Text prompt for free text input.
  238. #### Example
  239. <img src="https://github.com/terkelg/prompts/raw/master/media/text.gif" alt="text prompt" width="499" height="103" />
  240. ```js
  241. {
  242. type: 'text',
  243. name: 'value',
  244. message: `What's your twitter handle?`,
  245. style: 'default',
  246. initial: ''
  247. }
  248. ```
  249. #### Options
  250. | Param | Type | Default | Description |
  251. | --- | --- | --- | --- |
  252. | message | <code>string</code> | | Prompt message to display |
  253. | initial | <code>string</code> | <code>''</code> | Default string value |
  254. | style | <code>string</code> | <code>'default'</code> | Render style (`default`, `password`, `invisible`) |
  255. | format | <code>function</code> | | Receive user input. The returned value will be added to the response object |
  256. | onState | <code>function</code> | | On state change callback |
  257. ### password(message, [initial])
  258. > Password prompt with masked input.
  259. This prompt is a similar to a prompt of type `'text'` with `style` set to `'password'`.
  260. #### Example
  261. <img src="https://github.com/terkelg/prompts/raw/master/media/password.gif" alt="password prompt" width="499" height="103" />
  262. ```js
  263. {
  264. type: 'password',
  265. name: 'value',
  266. message: 'Tell me a secret',
  267. initial '',
  268. }
  269. ```
  270. #### Options
  271. | Param | Type | Description |
  272. | --- | --- | --- |
  273. | message | <code>string</code> | Prompt message to display |
  274. | initial | <code>string</code> | Default string value |
  275. | format | <code>function</code> | Receive user input. The returned value will be added to the response object |
  276. | onState | <code>function</code> | On state change callback |
  277. ### invisible(message, [initial])
  278. > Prompts user for invisible text input.
  279. This prompt is working like `sudo` where the input is invisible.
  280. This prompt is a similar to a prompt of type `'text'` with style set to `'invisible'`.
  281. #### Example
  282. <img src="https://github.com/terkelg/prompts/raw/master/media/invisible.gif" alt="invisible prompt" width="499" height="103" />
  283. ```js
  284. {
  285. type: 'invisible',
  286. name: 'value',
  287. message: 'Enter password',
  288. initial: ''
  289. }
  290. ```
  291. #### Options
  292. | Param | Type | Description |
  293. | --- | --- | --- |
  294. | message | <code>string</code> | Prompt message to display |
  295. | initial | <code>string</code> | Default string value |
  296. | format | <code>function</code> | Receive user input. The returned value will be added to the response object |
  297. | onState | <code>function</code> | On state change callback |
  298. ### number(message, initial, [max], [min], [style])
  299. > Prompts user for number input.
  300. You can type in numbers and use <kbd>up</kbd>/<kbd>down</kbd> to increase/decrease the value. Only numbers are allowed as input.
  301. #### Example
  302. <img src="https://github.com/terkelg/prompts/raw/master/media/number.gif" alt="number prompt" width="499" height="103" />
  303. ```js
  304. {
  305. type: 'number',
  306. name: 'value',
  307. message: 'How old are you?',
  308. initial: 0,
  309. style: 'default',
  310. min: 2,
  311. max: 10
  312. }
  313. ```
  314. #### Options
  315. | Param | Type | Default | Description |
  316. | --- | --- | --- | --- |
  317. | message | <code>string</code> | | Prompt message to display |
  318. | initial | <code>number</code> | `null` | Default number value |
  319. | format | <code>function</code> | | Receive user input. The returned value will be added to the response object |
  320. | max | <code>number</code> | `Infinity` | Max value |
  321. | min | <code>number</code> | `-infinity` | Min value |
  322. | style | <code>string</code> | <code>'default'</code> | Render style (`default`, `password`, `invisible`) |
  323. | onState | <code>function</code> | | On state change callback |
  324. ### confirm(message, [initial])
  325. > Classic yes/no prompt.
  326. Hit <kbd>y</kbd> or <kbd>n</kbd> to confirm/reject.
  327. #### Example
  328. <img src="https://github.com/terkelg/prompts/raw/master/media/confirm.gif" alt="confirm prompt" width="499" height="103" />
  329. ```js
  330. {
  331. type: 'confirm',
  332. name: 'value',
  333. message: 'Can you confirm?',
  334. initial: true
  335. }
  336. ```
  337. #### Options
  338. | Param | Type | Default | Description |
  339. | --- | --- | --- | --- |
  340. | message | <code>string</code> | | Prompt message to display |
  341. | initial | <code>boolean</code> | <code>false</code> | Default value |
  342. | format | <code>function</code> | | Receive user input. The returned value will be added to the response object |
  343. | onState | <code>function</code> | | On state change callback |
  344. ### list(message, [initial])
  345. > List prompt that return an array.
  346. Similar to the `text` prompt, but the output is an `Array` containing the
  347. string separated by `separator`.
  348. ```js
  349. {
  350. type: 'list',
  351. name: 'value',
  352. message: 'Enter keywords',
  353. initial: '',
  354. separator: ','
  355. }
  356. ```
  357. <img src="https://github.com/terkelg/prompts/raw/master/media/list.gif" alt="list prompt" width="499" height="103" />
  358. | Param | Type | Default | Description |
  359. | --- | --- | --- | --- |
  360. | message | <code>string</code> | | Prompt message to display |
  361. | initial | <code>boolean</code> | <code>false</code> | Default value |
  362. | format | <code>function</code> | | Receive user input. The returned value will be added to the response object |
  363. | separator | <code>string</code> | <code>','</code> | String separator. Will trim all white-spaces from start and end of string |
  364. | onState | <code>function</code> | | On state change callback |
  365. ### toggle(message, [initial], [active], [inactive])
  366. > Interactive toggle/switch prompt.
  367. Use tab or <kbd>arrow keys</kbd>/<kbd>tab</kbd>/<kbd>space</kbd> to switch between options.
  368. #### Example
  369. <img src="https://github.com/terkelg/prompts/raw/master/media/toggle.gif" alt="toggle prompt" width="499" height="103" />
  370. ```js
  371. {
  372. type: 'toggle',
  373. name: 'value',
  374. message: 'Can you confirm?',
  375. initial: true,
  376. active: 'yes',
  377. inactive: 'no'
  378. }
  379. ```
  380. #### Options
  381. | Param | Type | Default | Description |
  382. | --- | --- | --- | --- |
  383. | message | <code>string</code> | | Prompt message to display |
  384. | initial | <code>boolean</code> | <code>false</code> | Default value |
  385. | format | <code>function</code> | | Receive user input. The returned value will be added to the response object |
  386. | active | <code>string</code> | <code>'on'</code> | Text for `active` state |
  387. | inactive | <code>string</code> | <code>'off'</code> | Text for `inactive` state |
  388. | onState | <code>function</code> | | On state change callback |
  389. ### select(message, choices, [initial])
  390. > Interactive select prompt.
  391. Use <kbd>up</kbd>/<kbd>down</kbd> to navigate. Use <kbd>tab</kbd> to cycle the list.
  392. #### Example
  393. <img src="https://github.com/terkelg/prompts/raw/master/media/select.gif" alt="select prompt" width="499" height="130" />
  394. ```js
  395. {
  396. type: 'select',
  397. name: 'value',
  398. message: 'Pick a color',
  399. choices: [
  400. { title: 'Red', value: '#ff0000' },
  401. { title: 'Green', value: '#00ff00' },
  402. { title: 'Blue', value: '#0000ff' }
  403. ],
  404. initial: 1
  405. }
  406. ```
  407. #### Options
  408. | Param | Type | Description |
  409. | --- | --- | --- |
  410. | message | <code>string</code> | Prompt message to display |
  411. | initial | <code>number</code> | Index of default value |
  412. | format | <code>function</code> | Receive user input. The returned value will be added to the response object |
  413. | choices | <code>Array</code> | Array of choices objects `[{ title, value }, ...]` |
  414. | onState | <code>function</code> | On state change callback |
  415. ### multiselect(message, choices, [initial], [max], [hint])
  416. > Interactive multi-select prompt.
  417. 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.
  418. By default this prompt returns an `array` containing the **values** of the selected items - not their display title.
  419. #### Example
  420. <img src="https://github.com/terkelg/prompts/raw/master/media/multiselect.gif" alt="multiselect prompt" width="499" height="130" />
  421. ```js
  422. {
  423. type: 'multiselect',
  424. name: 'value',
  425. message: 'Pick colors',
  426. choices: [
  427. { title: 'Red', value: '#ff0000' },
  428. { title: 'Green', value: '#00ff00' },
  429. { title: 'Blue', value: '#0000ff', selected: true }
  430. ],
  431. initial: 1,
  432. max: 2,
  433. hint: '- Space to select. Return to submit'
  434. }
  435. ```
  436. #### Options
  437. | Param | Type | Description |
  438. | --- | --- | --- |
  439. | message | <code>string</code> | Prompt message to display |
  440. | format | <code>function</code> | Receive user input. The returned value will be added to the response object |
  441. | choices | <code>Array</code> | Array of choices objects `[{ title, value, [selected] }, ...]` |
  442. | max | <code>number</code> | Max select |
  443. | hint | <code>string</code> | Hint to display user |
  444. | onState | <code>function</code> | On state change callback |
  445. This is one of the few prompts that don't take a initial value.
  446. If you want to predefine selected values, give the choice object an `selected` property of `true`.
  447. ### autocomplete(message, choices, [initial], [suggest], [limit], [style])
  448. > Interactive auto complete prompt.
  449. The prompt will list options based on user input. Type to filter the list.
  450. Use <kbd>up</kbd>/<kbd>down</kbd> to navigate. Use <kbd>tab</kbd> to cycle the result. Hit <kbd>enter</kbd> to select the highlighted item below the prompt.
  451. The default suggests function is sorting based on the `title` property of the choices.
  452. You can overwrite how choices are being filtered by passing your own suggest function.
  453. #### Example
  454. <img src="https://github.com/terkelg/prompts/raw/master/media/autocomplete.gif" alt="auto complete prompt" width="499" height="163" />
  455. ```js
  456. {
  457. type: 'autocomplete',
  458. name: 'value',
  459. message: 'Pick your favorite actor',
  460. choices: [
  461. { title: 'Cage' },
  462. { title: 'Clooney', value: 'silver-fox' },
  463. { title: 'Gyllenhaal' },
  464. { title: 'Gibson' },
  465. { title: 'Grant' },
  466. ]
  467. }
  468. ```
  469. #### Options
  470. | Param | Type | Default | Description |
  471. | --- | --- | --- | --- |
  472. | message | <code>string</code> | | Prompt message to display |
  473. | format | <code>function</code> | | Receive user input. The returned value will be added to the response object |
  474. | choices | <code>Array</code> | | Array of auto-complete choices objects `[{ title, value }, ...]` |
  475. | suggest | <code>function</code> | By `title` string | Filter function. Defaults to sort by `title` property. `suggest` should always return a promise |
  476. | limit | <code>number</code> | <code>10</code> | Max number of results to show |
  477. | style | <code>string</code> | `'default'` | Render style (`default`, `password`, `invisible`) |
  478. | onState | <code>function</code> | | On state change callback |
  479. Example on what a `suggest` function might look like:
  480. ```js
  481. const suggestByTitle = (input, choices) =>
  482. Promise.resolve(choices.filter(i => i.title.slice(0, input.length) === input))
  483. ```
  484. ![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
  485. ## ❯ Credit
  486. Many of the prompts are based on the work of [derhuerst](https://github.com/derhuerst).
  487. ## ❯ License
  488. MIT © [Terkel Gjervig](https://terkel.com)