list.js 4.2 KB

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