rawlist.js 4.7 KB

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