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.

list.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. /**
  3. * `list` type prompt
  4. */
  5. var _ = require('lodash');
  6. var chalk = require('chalk');
  7. var figures = require('figures');
  8. var cliCursor = require('cli-cursor');
  9. var runAsync = require('run-async');
  10. var { flatMap, map, take, takeUntil } = require('rxjs/operators');
  11. var Base = require('./base');
  12. var observe = require('../utils/events');
  13. var Paginator = require('../utils/paginator');
  14. class ListPrompt extends Base {
  15. constructor(questions, rl, answers) {
  16. super(questions, rl, answers);
  17. if (!this.opt.choices) {
  18. this.throwParamError('choices');
  19. }
  20. this.firstRender = true;
  21. this.selected = 0;
  22. var def = this.opt.default;
  23. // If def is a Number, then use as index. Otherwise, check for value.
  24. if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
  25. this.selected = def;
  26. } else if (!_.isNumber(def) && def != null) {
  27. let index = _.findIndex(this.opt.choices.realChoices, ({ value }) => value === def);
  28. this.selected = Math.max(index, 0);
  29. }
  30. // Make sure no default is set (so it won't be printed)
  31. this.opt.default = null;
  32. this.paginator = new Paginator(this.screen);
  33. }
  34. /**
  35. * Start the Inquiry session
  36. * @param {Function} cb Callback when prompt is done
  37. * @return {this}
  38. */
  39. _run(cb) {
  40. this.done = cb;
  41. var self = this;
  42. var events = observe(this.rl);
  43. events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
  44. events.normalizedDownKey
  45. .pipe(takeUntil(events.line))
  46. .forEach(this.onDownKey.bind(this));
  47. events.numberKey.pipe(takeUntil(events.line)).forEach(this.onNumberKey.bind(this));
  48. events.line
  49. .pipe(
  50. take(1),
  51. map(this.getCurrentValue.bind(this)),
  52. flatMap(value => runAsync(self.opt.filter)(value).catch(err => err))
  53. )
  54. .forEach(this.onSubmit.bind(this));
  55. // Init the prompt
  56. cliCursor.hide();
  57. this.render();
  58. return this;
  59. }
  60. /**
  61. * Render the prompt to screen
  62. * @return {ListPrompt} self
  63. */
  64. render() {
  65. // Render question
  66. var message = this.getQuestion();
  67. if (this.firstRender) {
  68. message += chalk.dim('(Use arrow keys)');
  69. }
  70. // Render choices or answer depending on the state
  71. if (this.status === 'answered') {
  72. message += chalk.cyan(this.opt.choices.getChoice(this.selected).short);
  73. } else {
  74. var choicesStr = listRender(this.opt.choices, this.selected);
  75. var indexPosition = this.opt.choices.indexOf(
  76. this.opt.choices.getChoice(this.selected)
  77. );
  78. message +=
  79. '\n' + this.paginator.paginate(choicesStr, indexPosition, this.opt.pageSize);
  80. }
  81. this.firstRender = false;
  82. this.screen.render(message);
  83. }
  84. /**
  85. * When user press `enter` key
  86. */
  87. onSubmit(value) {
  88. this.status = 'answered';
  89. // Rerender prompt
  90. this.render();
  91. this.screen.done();
  92. cliCursor.show();
  93. this.done(value);
  94. }
  95. getCurrentValue() {
  96. return this.opt.choices.getChoice(this.selected).value;
  97. }
  98. /**
  99. * When user press a key
  100. */
  101. onUpKey() {
  102. var len = this.opt.choices.realLength;
  103. this.selected = this.selected > 0 ? this.selected - 1 : len - 1;
  104. this.render();
  105. }
  106. onDownKey() {
  107. var len = this.opt.choices.realLength;
  108. this.selected = this.selected < len - 1 ? this.selected + 1 : 0;
  109. this.render();
  110. }
  111. onNumberKey(input) {
  112. if (input <= this.opt.choices.realLength) {
  113. this.selected = input - 1;
  114. }
  115. this.render();
  116. }
  117. }
  118. /**
  119. * Function for rendering list choices
  120. * @param {Number} pointer Position of the pointer
  121. * @return {String} Rendered content
  122. */
  123. function listRender(choices, pointer) {
  124. var output = '';
  125. var separatorOffset = 0;
  126. choices.forEach((choice, i) => {
  127. if (choice.type === 'separator') {
  128. separatorOffset++;
  129. output += ' ' + choice + '\n';
  130. return;
  131. }
  132. if (choice.disabled) {
  133. separatorOffset++;
  134. output += ' - ' + choice.name;
  135. output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
  136. output += '\n';
  137. return;
  138. }
  139. var isSelected = i - separatorOffset === pointer;
  140. var line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
  141. if (isSelected) {
  142. line = chalk.cyan(line);
  143. }
  144. output += line + ' \n';
  145. });
  146. return output.replace(/\n$/, '');
  147. }
  148. module.exports = ListPrompt;