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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. this.firstRender = true;
  28. // Make sure no default is set (so it won't be printed)
  29. this.opt.default = null;
  30. this.paginator = new Paginator(this.screen);
  31. }
  32. /**
  33. * Start the Inquiry session
  34. * @param {Function} cb Callback when prompt is done
  35. * @return {this}
  36. */
  37. _run(cb) {
  38. this.done = cb;
  39. var events = observe(this.rl);
  40. var validation = this.handleSubmitEvents(
  41. events.line.pipe(map(this.getCurrentValue.bind(this)))
  42. );
  43. validation.success.forEach(this.onEnd.bind(this));
  44. validation.error.forEach(this.onError.bind(this));
  45. events.normalizedUpKey
  46. .pipe(takeUntil(validation.success))
  47. .forEach(this.onUpKey.bind(this));
  48. events.normalizedDownKey
  49. .pipe(takeUntil(validation.success))
  50. .forEach(this.onDownKey.bind(this));
  51. events.numberKey
  52. .pipe(takeUntil(validation.success))
  53. .forEach(this.onNumberKey.bind(this));
  54. events.spaceKey
  55. .pipe(takeUntil(validation.success))
  56. .forEach(this.onSpaceKey.bind(this));
  57. events.aKey.pipe(takeUntil(validation.success)).forEach(this.onAllKey.bind(this));
  58. events.iKey.pipe(takeUntil(validation.success)).forEach(this.onInverseKey.bind(this));
  59. // Init the prompt
  60. cliCursor.hide();
  61. this.render();
  62. this.firstRender = false;
  63. return this;
  64. }
  65. /**
  66. * Render the prompt to screen
  67. * @return {CheckboxPrompt} self
  68. */
  69. render(error) {
  70. // Render question
  71. var message = this.getQuestion();
  72. var bottomContent = '';
  73. if (this.firstRender) {
  74. message +=
  75. '(Press ' +
  76. chalk.cyan.bold('<space>') +
  77. ' to select, ' +
  78. chalk.cyan.bold('<a>') +
  79. ' to toggle all, ' +
  80. chalk.cyan.bold('<i>') +
  81. ' to invert selection)';
  82. }
  83. // Render choices or answer depending on the state
  84. if (this.status === 'answered') {
  85. message += chalk.cyan(this.selection.join(', '));
  86. } else {
  87. var choicesStr = renderChoices(this.opt.choices, this.pointer);
  88. var indexPosition = this.opt.choices.indexOf(
  89. this.opt.choices.getChoice(this.pointer)
  90. );
  91. message +=
  92. '\n' + this.paginator.paginate(choicesStr, indexPosition, this.opt.pageSize);
  93. }
  94. if (error) {
  95. bottomContent = chalk.red('>> ') + error;
  96. }
  97. this.screen.render(message, bottomContent);
  98. }
  99. /**
  100. * When user press `enter` key
  101. */
  102. onEnd(state) {
  103. this.status = 'answered';
  104. // Rerender prompt (and clean subline error)
  105. this.render();
  106. this.screen.done();
  107. cliCursor.show();
  108. this.done(state.value);
  109. }
  110. onError(state) {
  111. this.render(state.isValid);
  112. }
  113. getCurrentValue() {
  114. var choices = this.opt.choices.filter(function(choice) {
  115. return Boolean(choice.checked) && !choice.disabled;
  116. });
  117. this.selection = _.map(choices, 'short');
  118. return _.map(choices, 'value');
  119. }
  120. onUpKey() {
  121. var len = this.opt.choices.realLength;
  122. this.pointer = this.pointer > 0 ? this.pointer - 1 : len - 1;
  123. this.render();
  124. }
  125. onDownKey() {
  126. var len = this.opt.choices.realLength;
  127. this.pointer = this.pointer < len - 1 ? this.pointer + 1 : 0;
  128. this.render();
  129. }
  130. onNumberKey(input) {
  131. if (input <= this.opt.choices.realLength) {
  132. this.pointer = input - 1;
  133. this.toggleChoice(this.pointer);
  134. }
  135. this.render();
  136. }
  137. onSpaceKey() {
  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;