prompts.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. const $ = exports;
  3. const el = require('./elements');
  4. const noop = v => v;
  5. function toPrompt(type, args, opts = {}) {
  6. return new Promise((res, rej) => {
  7. const p = new el[type](args);
  8. const onAbort = opts.onAbort || noop;
  9. const onSubmit = opts.onSubmit || noop;
  10. p.on('state', args.onState || noop);
  11. p.on('submit', x => res(onSubmit(x)));
  12. p.on('abort', x => rej(onAbort(x)));
  13. });
  14. }
  15. /**
  16. * Text prompt
  17. * @param {string} args.message Prompt message to display
  18. * @param {string} [args.initial] Default string value
  19. * @param {string} [args.style="default"] Render style ('default', 'password', 'invisible')
  20. * @param {function} [args.onState] On state change callback
  21. * @returns {Promise} Promise with user input
  22. */
  23. $.text = args => toPrompt('TextPrompt', args);
  24. /**
  25. * Password prompt with masked input
  26. * @param {string} args.message Prompt message to display
  27. * @param {string} [args.initial] Default string value
  28. * @param {function} [args.onState] On state change callback
  29. * @returns {Promise} Promise with user input
  30. *
  31. */
  32. $.password = args => {
  33. args.style = 'password';
  34. return $.text(args);
  35. };
  36. /**
  37. * Prompt where input is invisible, like sudo
  38. * @param {string} args.message Prompt message to display
  39. * @param {string} [args.initial] Default string value
  40. * @param {function} [args.onState] On state change callback
  41. * @returns {Promise} Promise with user input
  42. */
  43. $.invisible = args => {
  44. args.style = 'invisible';
  45. return $.text(args);
  46. };
  47. /**
  48. * Number prompt
  49. * @param {string} args.message Prompt message to display
  50. * @param {number} args.initial Default number value
  51. * @param {function} [args.onState] On state change callback
  52. * @param {number} [args.max] Max value
  53. * @param {number} [args.min] Min value
  54. * @param {string} [args.style="default"] Render style ('default', 'password', 'invisible')
  55. * @param {Boolean} [opts.float=false] Parse input as floats
  56. * @param {Number} [opts.round=2] Round floats to x decimals
  57. * @param {Number} [opts.increment=1] Number to increment by when using arrow-keys
  58. * @returns {Promise} Promise with user input
  59. */
  60. $.number = args => toPrompt('NumberPrompt', args);
  61. /**
  62. * Classic yes/no prompt
  63. * @param {string} args.message Prompt message to display
  64. * @param {boolean} [args.initial=false] Default value
  65. * @param {function} [args.onState] On state change callback
  66. * @returns {Promise} Promise with user input
  67. */
  68. $.confirm = args => toPrompt('ConfirmPrompt', args);
  69. /**
  70. * List prompt, split intput string by `seperator`
  71. * @param {string} args.message Prompt message to display
  72. * @param {string} [args.initial] Default string value
  73. * @param {string} [args.style="default"] Render style ('default', 'password', 'invisible')
  74. * @param {string} [args.separator] String separator
  75. * @param {function} [args.onState] On state change callback
  76. * @returns {Promise} Promise with user input, in form of an `Array`
  77. */
  78. $.list = args => {
  79. const sep = args.separator || ',';
  80. return toPrompt('TextPrompt', args, {
  81. onSubmit: str => str.split(sep).map(s => s.trim())
  82. });
  83. };
  84. /**
  85. * Toggle/switch prompt
  86. * @param {string} args.message Prompt message to display
  87. * @param {boolean} [args.initial=false] Default value
  88. * @param {string} [args.active="on"] Text for `active` state
  89. * @param {string} [args.inactive="off"] Text for `inactive` state
  90. * @param {function} [args.onState] On state change callback
  91. * @returns {Promise} Promise with user input
  92. */
  93. $.toggle = args => toPrompt('TogglePrompt', args);
  94. /**
  95. * Interactive select prompt
  96. * @param {string} args.message Prompt message to display
  97. * @param {Array} args.choices Array of choices objects `[{ title, value }, ...]`
  98. * @param {number} [args.initial] Index of default value
  99. * @param {String} [args.hint] Hint to display
  100. * @param {function} [args.onState] On state change callback
  101. * @returns {Promise} Promise with user input
  102. */
  103. $.select = args => toPrompt('SelectPrompt', args);
  104. /**
  105. * Interactive multi-select prompt
  106. * @param {string} args.message Prompt message to display
  107. * @param {Array} args.choices Array of choices objects `[{ title, value, [selected] }, ...]`
  108. * @param {number} [args.max] Max select
  109. * @param {string} [args.hint] Hint to display user
  110. * @param {Number} [args.cursor=0] Cursor start position
  111. * @param {function} [args.onState] On state change callback
  112. * @returns {Promise} Promise with user input
  113. */
  114. $.multiselect = args => {
  115. args.choices = [].concat(args.choices || []);
  116. const toSelected = items => items.filter(item => item.selected).map(item => item.value);
  117. return toPrompt('MultiselectPrompt', args, {
  118. onAbort: toSelected,
  119. onSubmit: toSelected
  120. });
  121. };
  122. const byTitle = (input, choices) => Promise.resolve(choices.filter(item => item.title.slice(0, input.length).toLowerCase() === input.toLowerCase()));
  123. /**
  124. * Interactive auto-complete prompt
  125. * @param {string} args.message Prompt message to display
  126. * @param {Array} args.choices Array of auto-complete choices objects `[{ title, value }, ...]`
  127. * @param {Function} [args.suggest] Function to filter results based on user input. Defaults to sort by `title`
  128. * @param {number} [args.limit=10] Max number of results to show
  129. * @param {string} [args.style="default"] Render style ('default', 'password', 'invisible')
  130. * @param {String} [args.initial] Index of the default value
  131. * @param {String} [args.fallback] Fallback message - defaults to initial value
  132. * @param {function} [args.onState] On state change callback
  133. * @returns {Promise} Promise with user input
  134. */
  135. $.autocomplete = args => {
  136. args.suggest = args.suggest || byTitle;
  137. args.choices = [].concat(args.choices || []);
  138. return toPrompt('AutocompletePrompt', args);
  139. };