rawlist.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. 'use strict';
  2. /**
  3. * `rawlist` type prompt
  4. */
  5. var _ = {
  6. extend: require('lodash/extend'),
  7. isNumber: require('lodash/isNumber'),
  8. findIndex: require('lodash/findIndex'),
  9. };
  10. var chalk = require('chalk');
  11. var { map, takeUntil } = require('rxjs/operators');
  12. var Base = require('./base');
  13. var Separator = require('../objects/separator');
  14. var observe = require('../utils/events');
  15. var Paginator = require('../utils/paginator');
  16. var incrementListIndex = require('../utils/incrementListIndex');
  17. class RawListPrompt extends Base {
  18. constructor(questions, rl, answers) {
  19. super(questions, rl, answers);
  20. if (!this.opt.choices) {
  21. this.throwParamError('choices');
  22. }
  23. this.opt.validChoices = this.opt.choices.filter(Separator.exclude);
  24. this.selected = 0;
  25. this.rawDefault = 0;
  26. _.extend(this.opt, {
  27. validate: function (val) {
  28. return val != null;
  29. },
  30. });
  31. var def = this.opt.default;
  32. if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
  33. this.selected = def;
  34. this.rawDefault = def;
  35. } else if (!_.isNumber(def) && def != null) {
  36. let index = _.findIndex(this.opt.choices.realChoices, ({ value }) => value === def);
  37. let safeIndex = Math.max(index, 0);
  38. this.selected = safeIndex;
  39. this.rawDefault = safeIndex;
  40. }
  41. // Make sure no default is set (so it won't be printed)
  42. this.opt.default = null;
  43. const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
  44. this.paginator = new Paginator(undefined, { isInfinite: shouldLoop });
  45. }
  46. /**
  47. * Start the Inquiry session
  48. * @param {Function} cb Callback when prompt is done
  49. * @return {this}
  50. */
  51. _run(cb) {
  52. this.done = cb;
  53. // Once user confirm (enter key)
  54. var events = observe(this.rl);
  55. var submit = events.line.pipe(map(this.getCurrentValue.bind(this)));
  56. var validation = this.handleSubmitEvents(submit);
  57. validation.success.forEach(this.onEnd.bind(this));
  58. validation.error.forEach(this.onError.bind(this));
  59. events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
  60. events.normalizedDownKey
  61. .pipe(takeUntil(events.line))
  62. .forEach(this.onDownKey.bind(this));
  63. events.keypress
  64. .pipe(takeUntil(validation.success))
  65. .forEach(this.onKeypress.bind(this));
  66. // Init the prompt
  67. this.render();
  68. return this;
  69. }
  70. /**
  71. * Render the prompt to screen
  72. * @return {RawListPrompt} self
  73. */
  74. render(error) {
  75. // Render question
  76. var message = this.getQuestion();
  77. var bottomContent = '';
  78. if (this.status === 'answered') {
  79. message += chalk.cyan(this.answer);
  80. } else {
  81. var choicesStr = renderChoices(this.opt.choices, this.selected);
  82. message +=
  83. '\n' + this.paginator.paginate(choicesStr, this.selected, this.opt.pageSize);
  84. message += '\n Answer: ';
  85. }
  86. message += this.rl.line;
  87. if (error) {
  88. bottomContent = '\n' + chalk.red('>> ') + error;
  89. }
  90. this.screen.render(message, bottomContent);
  91. }
  92. /**
  93. * When user press `enter` key
  94. */
  95. getCurrentValue(index) {
  96. if (index == null) {
  97. index = this.rawDefault;
  98. } else if (index === '') {
  99. index = this.selected;
  100. } else {
  101. index -= 1;
  102. }
  103. var choice = this.opt.choices.getChoice(index);
  104. return choice ? choice.value : null;
  105. }
  106. onEnd(state) {
  107. this.status = 'answered';
  108. this.answer = state.value;
  109. // Re-render prompt
  110. this.render();
  111. this.screen.done();
  112. this.done(state.value);
  113. }
  114. onError() {
  115. this.render('Please enter a valid index');
  116. }
  117. /**
  118. * When user press a key
  119. */
  120. onKeypress() {
  121. var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
  122. if (this.opt.choices.getChoice(index)) {
  123. this.selected = index;
  124. } else {
  125. this.selected = undefined;
  126. }
  127. this.render();
  128. }
  129. /**
  130. * When user press up key
  131. */
  132. onUpKey() {
  133. this.onArrowKey('up');
  134. }
  135. /**
  136. * When user press down key
  137. */
  138. onDownKey() {
  139. this.onArrowKey('down');
  140. }
  141. /**
  142. * When user press up or down key
  143. * @param {String} type Arrow type: up or down
  144. */
  145. onArrowKey(type) {
  146. this.selected = incrementListIndex(this.selected, type, this.opt);
  147. this.rl.line = String(this.selected + 1);
  148. }
  149. }
  150. /**
  151. * Function for rendering list choices
  152. * @param {Number} pointer Position of the pointer
  153. * @return {String} Rendered content
  154. */
  155. function renderChoices(choices, pointer) {
  156. var output = '';
  157. var separatorOffset = 0;
  158. choices.forEach(function (choice, i) {
  159. output += '\n ';
  160. if (choice.type === 'separator') {
  161. separatorOffset++;
  162. output += ' ' + choice;
  163. return;
  164. }
  165. var index = i - separatorOffset;
  166. var display = index + 1 + ') ' + choice.name;
  167. if (index === pointer) {
  168. display = chalk.cyan(display);
  169. }
  170. output += display;
  171. });
  172. return output;
  173. }
  174. module.exports = RawListPrompt;