list.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Base = require("./base");
  10. var observe = require("../utils/events");
  11. var utils = require("../utils/readline");
  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. // Default being a Number
  29. if ( _.isNumber(def) && def >= 0 && def < this.opt.choices.realLength ) {
  30. this.selected = def;
  31. }
  32. // Default being a String
  33. if ( _.isString(def) ) {
  34. this.selected = this.opt.choices.pluck("value").indexOf( def );
  35. }
  36. // Make sure no default is set (so it won't be printed)
  37. this.opt.default = null;
  38. this.paginator = new Paginator();
  39. }
  40. util.inherits( Prompt, Base );
  41. /**
  42. * Start the Inquiry session
  43. * @param {Function} cb Callback when prompt is done
  44. * @return {this}
  45. */
  46. Prompt.prototype._run = function( cb ) {
  47. this.done = cb;
  48. var events = observe(this.rl);
  49. events.normalizedUpKey.takeUntil( events.line ).forEach( this.onUpKey.bind(this) );
  50. events.normalizedDownKey.takeUntil( events.line ).forEach( this.onDownKey.bind(this) );
  51. events.numberKey.takeUntil( events.line ).forEach( this.onNumberKey.bind(this) );
  52. events.line.take(1).forEach( this.onSubmit.bind(this) );
  53. // Init the prompt
  54. cliCursor.hide();
  55. this.render();
  56. return this;
  57. };
  58. /**
  59. * Render the prompt to screen
  60. * @return {Prompt} self
  61. */
  62. Prompt.prototype.render = function() {
  63. // Render question
  64. var message = this.getQuestion();
  65. if ( this.firstRender ) {
  66. message += chalk.dim( "(Use arrow keys)" );
  67. }
  68. // Render choices or answer depending on the state
  69. if ( this.status === "answered" ) {
  70. message += chalk.cyan( this.opt.choices.getChoice(this.selected).name );
  71. } else {
  72. var choicesStr = listRender(this.opt.choices, this.selected );
  73. message += "\n" + this.paginator.paginate(choicesStr, this.selected);
  74. }
  75. this.firstRender = false;
  76. this.screen.render(message);
  77. };
  78. /**
  79. * When user press `enter` key
  80. */
  81. Prompt.prototype.onSubmit = function() {
  82. var choice = this.opt.choices.getChoice( this.selected );
  83. this.status = "answered";
  84. // Rerender prompt
  85. this.render();
  86. this.screen.done();
  87. cliCursor.show();
  88. this.done( choice.value );
  89. };
  90. /**
  91. * When user press a key
  92. */
  93. Prompt.prototype.onUpKey = function() {
  94. var len = this.opt.choices.realLength;
  95. this.selected = (this.selected > 0) ? this.selected - 1 : len - 1;
  96. this.render();
  97. };
  98. Prompt.prototype.onDownKey = function() {
  99. var len = this.opt.choices.realLength;
  100. this.selected = (this.selected < len - 1) ? this.selected + 1 : 0;
  101. this.render();
  102. };
  103. Prompt.prototype.onNumberKey = function( input ) {
  104. if ( input <= this.opt.choices.realLength ) {
  105. this.selected = input - 1;
  106. }
  107. this.render();
  108. };
  109. /**
  110. * Function for rendering list choices
  111. * @param {Number} pointer Position of the pointer
  112. * @return {String} Rendered content
  113. */
  114. function listRender(choices, pointer) {
  115. var output = '';
  116. var separatorOffset = 0;
  117. choices.forEach(function (choice, i) {
  118. if (choice.type === 'separator') {
  119. separatorOffset++;
  120. output += ' ' + choice + '\n';
  121. return;
  122. }
  123. var isSelected = (i - separatorOffset === pointer);
  124. var line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
  125. if (isSelected) {
  126. line = chalk.cyan(line);
  127. }
  128. output += line + ' \n';
  129. });
  130. return output.replace(/\n$/, '');
  131. }