Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rawlist.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Init the prompt
  57. this.render();
  58. return this;
  59. }
  60. /**
  61. * Render the prompt to screen
  62. * @return {RawListPrompt} self
  63. */
  64. render(error) {
  65. // Render question
  66. var message = this.getQuestion();
  67. var bottomContent = '';
  68. if (this.status === 'answered') {
  69. message += chalk.cyan(this.answer);
  70. } else {
  71. var choicesStr = renderChoices(this.opt.choices, this.selected);
  72. message += this.paginator.paginate(choicesStr, this.selected, this.opt.pageSize);
  73. message += '\n Answer: ';
  74. }
  75. message += this.rl.line;
  76. if (error) {
  77. bottomContent = '\n' + chalk.red('>> ') + error;
  78. }
  79. this.screen.render(message, bottomContent);
  80. }
  81. /**
  82. * When user press `enter` key
  83. */
  84. getCurrentValue(index) {
  85. if (index == null || index === '') {
  86. index = this.rawDefault;
  87. } else {
  88. index -= 1;
  89. }
  90. var choice = this.opt.choices.getChoice(index);
  91. return choice ? choice.value : null;
  92. }
  93. onEnd(state) {
  94. this.status = 'answered';
  95. this.answer = state.value;
  96. // Re-render prompt
  97. this.render();
  98. this.screen.done();
  99. this.done(state.value);
  100. }
  101. onError() {
  102. this.render('Please enter a valid index');
  103. }
  104. /**
  105. * When user press a key
  106. */
  107. onKeypress() {
  108. var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
  109. if (this.opt.choices.getChoice(index)) {
  110. this.selected = index;
  111. } else {
  112. this.selected = undefined;
  113. }
  114. this.render();
  115. }
  116. }
  117. /**
  118. * Function for rendering list choices
  119. * @param {Number} pointer Position of the pointer
  120. * @return {String} Rendered content
  121. */
  122. function renderChoices(choices, pointer) {
  123. var output = '';
  124. var separatorOffset = 0;
  125. choices.forEach(function(choice, i) {
  126. output += '\n ';
  127. if (choice.type === 'separator') {
  128. separatorOffset++;
  129. output += ' ' + choice;
  130. return;
  131. }
  132. var index = i - separatorOffset;
  133. var display = index + 1 + ') ' + choice.name;
  134. if (index === pointer) {
  135. display = chalk.cyan(display);
  136. }
  137. output += display;
  138. });
  139. return output;
  140. }
  141. module.exports = RawListPrompt;