UI for Zipcoin Blue

checkbox.js 5.8KB

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