expand.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. 'use strict';
  2. /**
  3. * `rawlist` type prompt
  4. */
  5. var _ = {
  6. uniq: require('lodash/uniq'),
  7. isString: require('lodash/isString'),
  8. isNumber: require('lodash/isNumber'),
  9. findIndex: require('lodash/findIndex'),
  10. };
  11. var chalk = require('chalk');
  12. var { map, takeUntil } = require('rxjs/operators');
  13. var Base = require('./base');
  14. var Separator = require('../objects/separator');
  15. var observe = require('../utils/events');
  16. var Paginator = require('../utils/paginator');
  17. class ExpandPrompt extends Base {
  18. constructor(questions, rl, answers) {
  19. super(questions, rl, answers);
  20. if (!this.opt.choices) {
  21. this.throwParamError('choices');
  22. }
  23. this.validateChoices(this.opt.choices);
  24. // Add the default `help` (/expand) option
  25. this.opt.choices.push({
  26. key: 'h',
  27. name: 'Help, list all options',
  28. value: 'help',
  29. });
  30. this.opt.validate = (choice) => {
  31. if (choice == null) {
  32. return 'Please enter a valid command';
  33. }
  34. return choice !== 'help';
  35. };
  36. // Setup the default string (capitalize the default key)
  37. this.opt.default = this.generateChoicesString(this.opt.choices, this.opt.default);
  38. this.paginator = new Paginator(this.screen);
  39. }
  40. /**
  41. * Start the Inquiry session
  42. * @param {Function} cb Callback when prompt is done
  43. * @return {this}
  44. */
  45. _run(cb) {
  46. this.done = cb;
  47. // Save user answer and update prompt to show selected option.
  48. var events = observe(this.rl);
  49. var validation = this.handleSubmitEvents(
  50. events.line.pipe(map(this.getCurrentValue.bind(this)))
  51. );
  52. validation.success.forEach(this.onSubmit.bind(this));
  53. validation.error.forEach(this.onError.bind(this));
  54. this.keypressObs = events.keypress
  55. .pipe(takeUntil(validation.success))
  56. .forEach(this.onKeypress.bind(this));
  57. // Init the prompt
  58. this.render();
  59. return this;
  60. }
  61. /**
  62. * Render the prompt to screen
  63. * @return {ExpandPrompt} self
  64. */
  65. render(error, hint) {
  66. var message = this.getQuestion();
  67. var bottomContent = '';
  68. if (this.status === 'answered') {
  69. message += chalk.cyan(this.answer);
  70. } else if (this.status === 'expanded') {
  71. var choicesStr = renderChoices(this.opt.choices, this.selectedKey);
  72. message += this.paginator.paginate(choicesStr, this.selectedKey, this.opt.pageSize);
  73. message += '\n Answer: ';
  74. }
  75. message += this.rl.line;
  76. if (error) {
  77. bottomContent = chalk.red('>> ') + error;
  78. }
  79. if (hint) {
  80. bottomContent = chalk.cyan('>> ') + hint;
  81. }
  82. this.screen.render(message, bottomContent);
  83. }
  84. getCurrentValue(input) {
  85. if (!input) {
  86. input = this.rawDefault;
  87. }
  88. var selected = this.opt.choices.where({ key: input.toLowerCase().trim() })[0];
  89. if (!selected) {
  90. return null;
  91. }
  92. return selected.value;
  93. }
  94. /**
  95. * Generate the prompt choices string
  96. * @return {String} Choices string
  97. */
  98. getChoices() {
  99. var output = '';
  100. this.opt.choices.forEach((choice) => {
  101. output += '\n ';
  102. if (choice.type === 'separator') {
  103. output += ' ' + choice;
  104. return;
  105. }
  106. var choiceStr = choice.key + ') ' + choice.name;
  107. if (this.selectedKey === choice.key) {
  108. choiceStr = chalk.cyan(choiceStr);
  109. }
  110. output += choiceStr;
  111. });
  112. return output;
  113. }
  114. onError(state) {
  115. if (state.value === 'help') {
  116. this.selectedKey = '';
  117. this.status = 'expanded';
  118. this.render();
  119. return;
  120. }
  121. this.render(state.isValid);
  122. }
  123. /**
  124. * When user press `enter` key
  125. */
  126. onSubmit(state) {
  127. this.status = 'answered';
  128. var choice = this.opt.choices.where({ value: state.value })[0];
  129. this.answer = choice.short || choice.name;
  130. // Re-render prompt
  131. this.render();
  132. this.screen.done();
  133. this.done(state.value);
  134. }
  135. /**
  136. * When user press a key
  137. */
  138. onKeypress() {
  139. this.selectedKey = this.rl.line.toLowerCase();
  140. var selected = this.opt.choices.where({ key: this.selectedKey })[0];
  141. if (this.status === 'expanded') {
  142. this.render();
  143. } else {
  144. this.render(null, selected ? selected.name : null);
  145. }
  146. }
  147. /**
  148. * Validate the choices
  149. * @param {Array} choices
  150. */
  151. validateChoices(choices) {
  152. var formatError;
  153. var errors = [];
  154. var keymap = {};
  155. choices.filter(Separator.exclude).forEach((choice) => {
  156. if (!choice.key || choice.key.length !== 1) {
  157. formatError = true;
  158. }
  159. if (keymap[choice.key]) {
  160. errors.push(choice.key);
  161. }
  162. keymap[choice.key] = true;
  163. choice.key = String(choice.key).toLowerCase();
  164. });
  165. if (formatError) {
  166. throw new Error(
  167. 'Format error: `key` param must be a single letter and is required.'
  168. );
  169. }
  170. if (keymap.h) {
  171. throw new Error(
  172. 'Reserved key error: `key` param cannot be `h` - this value is reserved.'
  173. );
  174. }
  175. if (errors.length) {
  176. throw new Error(
  177. 'Duplicate key error: `key` param must be unique. Duplicates: ' +
  178. _.uniq(errors).join(', ')
  179. );
  180. }
  181. }
  182. /**
  183. * Generate a string out of the choices keys
  184. * @param {Array} choices
  185. * @param {Number|String} default - the choice index or name to capitalize
  186. * @return {String} The rendered choices key string
  187. */
  188. generateChoicesString(choices, defaultChoice) {
  189. var defIndex = choices.realLength - 1;
  190. if (_.isNumber(defaultChoice) && this.opt.choices.getChoice(defaultChoice)) {
  191. defIndex = defaultChoice;
  192. } else if (_.isString(defaultChoice)) {
  193. let index = _.findIndex(
  194. choices.realChoices,
  195. ({ value }) => value === defaultChoice
  196. );
  197. defIndex = index === -1 ? defIndex : index;
  198. }
  199. var defStr = this.opt.choices.pluck('key');
  200. this.rawDefault = defStr[defIndex];
  201. defStr[defIndex] = String(defStr[defIndex]).toUpperCase();
  202. return defStr.join('');
  203. }
  204. }
  205. /**
  206. * Function for rendering checkbox choices
  207. * @param {String} pointer Selected key
  208. * @return {String} Rendered content
  209. */
  210. function renderChoices(choices, pointer) {
  211. var output = '';
  212. choices.forEach((choice) => {
  213. output += '\n ';
  214. if (choice.type === 'separator') {
  215. output += ' ' + choice;
  216. return;
  217. }
  218. var choiceStr = choice.key + ') ' + choice.name;
  219. if (pointer === choice.key) {
  220. choiceStr = chalk.cyan(choiceStr);
  221. }
  222. output += choiceStr;
  223. });
  224. return output;
  225. }
  226. module.exports = ExpandPrompt;