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.

checkbox.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. 'use strict';
  2. /**
  3. * `list` type prompt
  4. */
  5. var _ = require('lodash');
  6. var chalk = require('chalk');
  7. var cliCursor = require('cli-cursor');
  8. var figures = require('figures');
  9. var { map, takeUntil } = require('rxjs/operators');
  10. var Base = require('./base');
  11. var observe = require('../utils/events');
  12. var Paginator = require('../utils/paginator');
  13. class CheckboxPrompt extends Base {
  14. constructor(questions, rl, answers) {
  15. super(questions, rl, answers);
  16. if (!this.opt.choices) {
  17. this.throwParamError('choices');
  18. }
  19. if (_.isArray(this.opt.default)) {
  20. this.opt.choices.forEach(function(choice) {
  21. if (this.opt.default.indexOf(choice.value) >= 0) {
  22. choice.checked = true;
  23. }
  24. }, this);
  25. }
  26. this.pointer = 0;
  27. // Make sure no default is set (so it won't be printed)
  28. this.opt.default = null;
  29. this.paginator = new Paginator(this.screen);
  30. }
  31. /**
  32. * Start the Inquiry session
  33. * @param {Function} cb Callback when prompt is done
  34. * @return {this}
  35. */
  36. _run(cb) {
  37. this.done = cb;
  38. var events = observe(this.rl);
  39. var validation = this.handleSubmitEvents(
  40. events.line.pipe(map(this.getCurrentValue.bind(this)))
  41. );
  42. validation.success.forEach(this.onEnd.bind(this));
  43. validation.error.forEach(this.onError.bind(this));
  44. events.normalizedUpKey
  45. .pipe(takeUntil(validation.success))
  46. .forEach(this.onUpKey.bind(this));
  47. events.normalizedDownKey
  48. .pipe(takeUntil(validation.success))
  49. .forEach(this.onDownKey.bind(this));
  50. events.numberKey
  51. .pipe(takeUntil(validation.success))
  52. .forEach(this.onNumberKey.bind(this));
  53. events.spaceKey
  54. .pipe(takeUntil(validation.success))
  55. .forEach(this.onSpaceKey.bind(this));
  56. events.aKey.pipe(takeUntil(validation.success)).forEach(this.onAllKey.bind(this));
  57. events.iKey.pipe(takeUntil(validation.success)).forEach(this.onInverseKey.bind(this));
  58. // Init the prompt
  59. cliCursor.hide();
  60. this.render();
  61. this.firstRender = false;
  62. return this;
  63. }
  64. /**
  65. * Render the prompt to screen
  66. * @return {CheckboxPrompt} self
  67. */
  68. render(error) {
  69. // Render question
  70. var message = this.getQuestion();
  71. var bottomContent = '';
  72. if (!this.spaceKeyPressed) {
  73. message +=
  74. '(Press ' +
  75. chalk.cyan.bold('<space>') +
  76. ' to select, ' +
  77. chalk.cyan.bold('<a>') +
  78. ' to toggle all, ' +
  79. chalk.cyan.bold('<i>') +
  80. ' to invert selection)';
  81. }
  82. // Render choices or answer depending on the state
  83. if (this.status === 'answered') {
  84. message += chalk.cyan(this.selection.join(', '));
  85. } else {
  86. var choicesStr = renderChoices(this.opt.choices, this.pointer);
  87. var indexPosition = this.opt.choices.indexOf(
  88. this.opt.choices.getChoice(this.pointer)
  89. );
  90. message +=
  91. '\n' + this.paginator.paginate(choicesStr, indexPosition, this.opt.pageSize);
  92. }
  93. if (error) {
  94. bottomContent = chalk.red('>> ') + error;
  95. }
  96. this.screen.render(message, bottomContent);
  97. }
  98. /**
  99. * When user press `enter` key
  100. */
  101. onEnd(state) {
  102. this.status = 'answered';
  103. // Rerender prompt (and clean subline error)
  104. this.render();
  105. this.screen.done();
  106. cliCursor.show();
  107. this.done(state.value);
  108. }
  109. onError(state) {
  110. this.render(state.isValid);
  111. }
  112. getCurrentValue() {
  113. var choices = this.opt.choices.filter(function(choice) {
  114. return Boolean(choice.checked) && !choice.disabled;
  115. });
  116. this.selection = _.map(choices, 'short');
  117. return _.map(choices, 'value');
  118. }
  119. onUpKey() {
  120. var len = this.opt.choices.realLength;
  121. this.pointer = this.pointer > 0 ? this.pointer - 1 : len - 1;
  122. this.render();
  123. }
  124. onDownKey() {
  125. var len = this.opt.choices.realLength;
  126. this.pointer = this.pointer < len - 1 ? this.pointer + 1 : 0;
  127. this.render();
  128. }
  129. onNumberKey(input) {
  130. if (input <= this.opt.choices.realLength) {
  131. this.pointer = input - 1;
  132. this.toggleChoice(this.pointer);
  133. }
  134. this.render();
  135. }
  136. onSpaceKey() {
  137. this.spaceKeyPressed = true;
  138. this.toggleChoice(this.pointer);
  139. this.render();
  140. }
  141. onAllKey() {
  142. var shouldBeChecked = Boolean(
  143. this.opt.choices.find(function(choice) {
  144. return choice.type !== 'separator' && !choice.checked;
  145. })
  146. );
  147. this.opt.choices.forEach(function(choice) {
  148. if (choice.type !== 'separator') {
  149. choice.checked = shouldBeChecked;
  150. }
  151. });
  152. this.render();
  153. }
  154. onInverseKey() {
  155. this.opt.choices.forEach(function(choice) {
  156. if (choice.type !== 'separator') {
  157. choice.checked = !choice.checked;
  158. }
  159. });
  160. this.render();
  161. }
  162. toggleChoice(index) {
  163. var item = this.opt.choices.getChoice(index);
  164. if (item !== undefined) {
  165. this.opt.choices.getChoice(index).checked = !item.checked;
  166. }
  167. }
  168. }
  169. /**
  170. * Function for rendering checkbox choices
  171. * @param {Number} pointer Position of the pointer
  172. * @return {String} Rendered content
  173. */
  174. function renderChoices(choices, pointer) {
  175. var output = '';
  176. var separatorOffset = 0;
  177. choices.forEach(function(choice, i) {
  178. if (choice.type === 'separator') {
  179. separatorOffset++;
  180. output += ' ' + choice + '\n';
  181. return;
  182. }
  183. if (choice.disabled) {
  184. separatorOffset++;
  185. output += ' - ' + choice.name;
  186. output += ' (' + (_.isString(choice.disabled) ? choice.disabled : 'Disabled') + ')';
  187. } else {
  188. var line = getCheckbox(choice.checked) + ' ' + choice.name;
  189. if (i - separatorOffset === pointer) {
  190. output += chalk.cyan(figures.pointer + line);
  191. } else {
  192. output += ' ' + line;
  193. }
  194. }
  195. output += '\n';
  196. });
  197. return output.replace(/\n$/, '');
  198. }
  199. /**
  200. * Get the checkbox
  201. * @param {Boolean} checked - add a X or not to the checkbox
  202. * @return {String} Composited checkbox string
  203. */
  204. function getCheckbox(checked) {
  205. return checked ? chalk.green(figures.radioOn) : figures.radioOff;
  206. }
  207. module.exports = CheckboxPrompt;