select.js 2.6 KB

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