select.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. const color = require('kleur');
  3. const Prompt = require('./prompt');
  4. const { style, clear, figures } = require('../util');
  5. const { erase, 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. */
  14. class SelectPrompt extends Prompt {
  15. constructor(opts={}) {
  16. super(opts);
  17. this.msg = opts.message;
  18. this.hint = opts.hint || '- Use arrow-keys. Return to submit.';
  19. this.cursor = opts.initial || 0;
  20. this.values = opts.choices || [];
  21. this.value = opts.choices[this.cursor].value;
  22. this.clear = clear('');
  23. this.render(true);
  24. }
  25. moveCursor(n) {
  26. this.cursor = n;
  27. this.value = this.values[n].value;
  28. this.fire();
  29. }
  30. reset() {
  31. this.moveCursor(0);
  32. this.fire();
  33. this.render();
  34. }
  35. abort() {
  36. this.done = this.aborted = true;
  37. this.fire();
  38. this.render();
  39. this.out.write('\n');
  40. this.close();
  41. }
  42. submit() {
  43. this.done = true;
  44. this.aborted = false;
  45. this.fire();
  46. this.render();
  47. this.out.write('\n');
  48. this.close();
  49. }
  50. first() {
  51. this.moveCursor(0);
  52. this.render();
  53. }
  54. last() {
  55. this.moveCursor(this.values.length - 1);
  56. this.render();
  57. }
  58. up() {
  59. if (this.cursor === 0) return this.bell();
  60. this.moveCursor(this.cursor - 1);
  61. this.render();
  62. }
  63. down() {
  64. if (this.cursor === this.values.length - 1) return this.bell();
  65. this.moveCursor(this.cursor + 1);
  66. this.render();
  67. }
  68. next() {
  69. this.moveCursor((this.cursor + 1) % this.values.length);
  70. this.render();
  71. }
  72. _(c, key) {
  73. if (c === ' ') return this.submit();
  74. }
  75. render(first) {
  76. if (first) this.out.write(cursor.hide);
  77. else this.out.write(erase.lines(this.values.length + 1));
  78. // Print prompt
  79. this.out.write(
  80. [
  81. style.symbol(this.done, this.aborted),
  82. color.bold(this.msg),
  83. style.delimiter(false),
  84. this.done ? this.values[this.cursor].title : color.gray(this.hint)
  85. ].join(' ')
  86. );
  87. // Print choices
  88. if (!this.done) {
  89. this.out.write(
  90. '\n' +
  91. this.values
  92. .map((v, i) => {
  93. let title = this.cursor === i ? color.cyan.underline(v.title) : v.title;
  94. let prefix = this.cursor === i ? color.cyan(figures.pointer) + ' ' : ' ';
  95. return `${prefix} ${title}`;
  96. })
  97. .join('\n')
  98. );
  99. }
  100. }
  101. }
  102. module.exports = SelectPrompt;