UI for Zipcoin Blue

list.js 4.2KB

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