Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

rawlist.js 4.7KB

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.keypress
  54. .pipe(takeUntil(validation.success))
  55. .forEach(this.onKeypress.bind(this));
  56. events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
  57. events.normalizedDownKey
  58. .pipe(takeUntil(events.line))
  59. .forEach(this.onDownKey.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 || index === '') {
  91. index = this.rawDefault;
  92. } else {
  93. index -= 1;
  94. }
  95. var choice = this.opt.choices.getChoice(index);
  96. return choice ? choice.value : null;
  97. }
  98. onEnd(state) {
  99. this.status = 'answered';
  100. this.answer = state.value;
  101. // Re-render prompt
  102. this.render();
  103. this.screen.done();
  104. this.done(state.value);
  105. }
  106. onError() {
  107. this.render('Please enter a valid index');
  108. }
  109. /**
  110. * When user press a key
  111. */
  112. onKeypress() {
  113. var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
  114. if (this.opt.choices.getChoice(index)) {
  115. this.selected = index;
  116. } else {
  117. this.selected = undefined;
  118. }
  119. this.render();
  120. }
  121. /**
  122. * When user press up key
  123. */
  124. onUpKey() {
  125. this.onArrowKey('up');
  126. }
  127. /**
  128. * When user press down key
  129. */
  130. onDownKey() {
  131. this.onArrowKey('down');
  132. }
  133. /**
  134. * When user press up or down key
  135. * @param {String} type Arrow type: up or down
  136. */
  137. onArrowKey(type) {
  138. var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
  139. if (type === 'up') index = index === 0 ? this.opt.choices.length - 1 : index - 1;
  140. else index = index === this.opt.choices.length - 1 ? 0 : index + 1;
  141. this.rl.line = String(index + 1);
  142. this.onKeypress();
  143. }
  144. }
  145. /**
  146. * Function for rendering list choices
  147. * @param {Number} pointer Position of the pointer
  148. * @return {String} Rendered content
  149. */
  150. function renderChoices(choices, pointer) {
  151. var output = '';
  152. var separatorOffset = 0;
  153. choices.forEach(function(choice, i) {
  154. output += '\n ';
  155. if (choice.type === 'separator') {
  156. separatorOffset++;
  157. output += ' ' + choice;
  158. return;
  159. }
  160. var index = i - separatorOffset;
  161. var display = index + 1 + ') ' + choice.name;
  162. if (index === pointer) {
  163. display = chalk.cyan(display);
  164. }
  165. output += display;
  166. });
  167. return output;
  168. }
  169. module.exports = RawListPrompt;