UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * `rawlist` type prompt
  3. */
  4. var _ = require('lodash');
  5. var util = require('util');
  6. var chalk = require('chalk');
  7. var Base = require('./base');
  8. var Separator = require('../objects/separator');
  9. var observe = require('../utils/events');
  10. var Paginator = require('../utils/paginator');
  11. /**
  12. * Module exports
  13. */
  14. module.exports = Prompt;
  15. /**
  16. * Constructor
  17. */
  18. function Prompt() {
  19. Base.apply(this, arguments);
  20. if (!this.opt.choices) {
  21. this.throwParamError('choices');
  22. }
  23. this.opt.validChoices = this.opt.choices.filter(Separator.exclude);
  24. this.selected = 0;
  25. this.rawDefault = 0;
  26. _.extend(this.opt, {
  27. validate: function (val) {
  28. return val != null;
  29. }
  30. });
  31. var def = this.opt.default;
  32. if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
  33. this.selected = this.rawDefault = def;
  34. }
  35. // Make sure no default is set (so it won't be printed)
  36. this.opt.default = null;
  37. this.paginator = new Paginator();
  38. }
  39. util.inherits(Prompt, Base);
  40. /**
  41. * Start the Inquiry session
  42. * @param {Function} cb Callback when prompt is done
  43. * @return {this}
  44. */
  45. Prompt.prototype._run = function (cb) {
  46. this.done = cb;
  47. // Once user confirm (enter key)
  48. var events = observe(this.rl);
  49. var submit = events.line.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.takeUntil(validation.success).forEach(this.onKeypress.bind(this));
  54. // Init the prompt
  55. this.render();
  56. return this;
  57. };
  58. /**
  59. * Render the prompt to screen
  60. * @return {Prompt} self
  61. */
  62. Prompt.prototype.render = function (error) {
  63. // Render question
  64. var message = this.getQuestion();
  65. var bottomContent = '';
  66. if (this.status === 'answered') {
  67. message += chalk.cyan(this.answer);
  68. } else {
  69. var choicesStr = renderChoices(this.opt.choices, this.selected);
  70. message += this.paginator.paginate(choicesStr, this.selected, this.opt.pageSize);
  71. message += '\n Answer: ';
  72. }
  73. message += this.rl.line;
  74. if (error) {
  75. bottomContent = '\n' + chalk.red('>> ') + error;
  76. }
  77. this.screen.render(message, bottomContent);
  78. };
  79. /**
  80. * When user press `enter` key
  81. */
  82. Prompt.prototype.getCurrentValue = function (index) {
  83. if (index == null || index === '') {
  84. index = this.rawDefault;
  85. } else {
  86. index -= 1;
  87. }
  88. var choice = this.opt.choices.getChoice(index);
  89. return choice ? choice.value : null;
  90. };
  91. Prompt.prototype.onEnd = function (state) {
  92. this.status = 'answered';
  93. this.answer = state.value;
  94. // Re-render prompt
  95. this.render();
  96. this.screen.done();
  97. this.done(state.value);
  98. };
  99. Prompt.prototype.onError = function () {
  100. this.render('Please enter a valid index');
  101. };
  102. /**
  103. * When user press a key
  104. */
  105. Prompt.prototype.onKeypress = function () {
  106. var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
  107. if (this.opt.choices.getChoice(index)) {
  108. this.selected = index;
  109. } else {
  110. this.selected = undefined;
  111. }
  112. this.render();
  113. };
  114. /**
  115. * Function for rendering list choices
  116. * @param {Number} pointer Position of the pointer
  117. * @return {String} Rendered content
  118. */
  119. function renderChoices(choices, pointer) {
  120. var output = '';
  121. var separatorOffset = 0;
  122. choices.forEach(function (choice, i) {
  123. output += '\n ';
  124. if (choice.type === 'separator') {
  125. separatorOffset++;
  126. output += ' ' + choice;
  127. return;
  128. }
  129. var index = i - separatorOffset;
  130. var display = (index + 1) + ') ' + choice.name;
  131. if (index === pointer) {
  132. display = chalk.cyan(display);
  133. }
  134. output += display;
  135. });
  136. return output;
  137. }