rawlist.js 3.9 KB

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