select.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. const color = require('kleur');
  3. const Prompt = require('./prompt');
  4. const { style, clear, figures, wrap, entriesToDisplay } = require('../util');
  5. const { cursor } = require('sisteransi');
  6. /**
  7. * SelectPrompt Base Element
  8. * @param {Object} opts Options
  9. * @param {String} opts.message Message
  10. * @param {Array} opts.choices Array of choice objects
  11. * @param {String} [opts.hint] Hint to display
  12. * @param {Number} [opts.initial] Index of default value
  13. * @param {Stream} [opts.stdin] The Readable stream to listen to
  14. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  15. * @param {Number} [opts.optionsPerPage=10] Max options to display at once
  16. */
  17. class SelectPrompt extends Prompt {
  18. constructor(opts={}) {
  19. super(opts);
  20. this.msg = opts.message;
  21. this.hint = opts.hint || '- Use arrow-keys. Return to submit.';
  22. this.warn = opts.warn || '- This option is disabled';
  23. this.cursor = opts.initial || 0;
  24. this.choices = opts.choices.map((ch, idx) => {
  25. if (typeof ch === 'string')
  26. ch = {title: ch, value: idx};
  27. return {
  28. title: ch && (ch.title || ch.value || ch),
  29. value: ch && (ch.value === undefined ? idx : ch.value),
  30. description: ch && ch.description,
  31. selected: ch && ch.selected,
  32. disabled: ch && ch.disabled
  33. };
  34. });
  35. this.optionsPerPage = opts.optionsPerPage || 10;
  36. this.value = (this.choices[this.cursor] || {}).value;
  37. this.clear = clear('', this.out.columns);
  38. this.render();
  39. }
  40. moveCursor(n) {
  41. this.cursor = n;
  42. this.value = this.choices[n].value;
  43. this.fire();
  44. }
  45. reset() {
  46. this.moveCursor(0);
  47. this.fire();
  48. this.render();
  49. }
  50. exit() {
  51. this.abort();
  52. }
  53. abort() {
  54. this.done = this.aborted = true;
  55. this.fire();
  56. this.render();
  57. this.out.write('\n');
  58. this.close();
  59. }
  60. submit() {
  61. if (!this.selection.disabled) {
  62. this.done = true;
  63. this.aborted = false;
  64. this.fire();
  65. this.render();
  66. this.out.write('\n');
  67. this.close();
  68. } else
  69. this.bell();
  70. }
  71. first() {
  72. this.moveCursor(0);
  73. this.render();
  74. }
  75. last() {
  76. this.moveCursor(this.choices.length - 1);
  77. this.render();
  78. }
  79. up() {
  80. if (this.cursor === 0) {
  81. this.moveCursor(this.choices.length - 1);
  82. } else {
  83. this.moveCursor(this.cursor - 1);
  84. }
  85. this.render();
  86. }
  87. down() {
  88. if (this.cursor === this.choices.length - 1) {
  89. this.moveCursor(0);
  90. } else {
  91. this.moveCursor(this.cursor + 1);
  92. }
  93. this.render();
  94. }
  95. next() {
  96. this.moveCursor((this.cursor + 1) % this.choices.length);
  97. this.render();
  98. }
  99. _(c, key) {
  100. if (c === ' ') return this.submit();
  101. }
  102. get selection() {
  103. return this.choices[this.cursor];
  104. }
  105. render() {
  106. if (this.closed) return;
  107. if (this.firstRender) this.out.write(cursor.hide);
  108. else this.out.write(clear(this.outputText, this.out.columns));
  109. super.render();
  110. let { startIndex, endIndex } = entriesToDisplay(this.cursor, this.choices.length, this.optionsPerPage);
  111. // Print prompt
  112. this.outputText = [
  113. style.symbol(this.done, this.aborted),
  114. color.bold(this.msg),
  115. style.delimiter(false),
  116. this.done ? this.selection.title : this.selection.disabled
  117. ? color.yellow(this.warn) : color.gray(this.hint)
  118. ].join(' ');
  119. // Print choices
  120. if (!this.done) {
  121. this.outputText += '\n';
  122. for (let i = startIndex; i < endIndex; i++) {
  123. let title, prefix, desc = '', v = this.choices[i];
  124. // Determine whether to display "more choices" indicators
  125. if (i === startIndex && startIndex > 0) {
  126. prefix = figures.arrowUp;
  127. } else if (i === endIndex - 1 && endIndex < this.choices.length) {
  128. prefix = figures.arrowDown;
  129. } else {
  130. prefix = ' ';
  131. }
  132. if (v.disabled) {
  133. title = this.cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
  134. prefix = (this.cursor === i ? color.bold().gray(figures.pointer) + ' ' : ' ') + prefix;
  135. } else {
  136. title = this.cursor === i ? color.cyan().underline(v.title) : v.title;
  137. prefix = (this.cursor === i ? color.cyan(figures.pointer) + ' ' : ' ') + prefix;
  138. if (v.description && this.cursor === i) {
  139. desc = ` - ${v.description}`;
  140. if (prefix.length + title.length + desc.length >= this.out.columns
  141. || v.description.split(/\r?\n/).length > 1) {
  142. desc = '\n' + wrap(v.description, { margin: 3, width: this.out.columns });
  143. }
  144. }
  145. }
  146. this.outputText += `${prefix} ${title}${color.gray(desc)}\n`;
  147. }
  148. }
  149. this.out.write(this.outputText);
  150. }
  151. }
  152. module.exports = SelectPrompt;