autocomplete.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
  3. const util = require('../util');
  4. const color = require('kleur');
  5. const Prompt = require('./prompt');
  6. var _require = require('sisteransi');
  7. const cursor = _require.cursor;
  8. // Get value, with fallback to title
  9. const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);
  10. /**
  11. * TextPrompt Base Element
  12. * @param {Object} opts Options
  13. * @param {String} opts.message Message
  14. * @param {Array} opts.choices Array of auto-complete choices objects
  15. * @param {Function} [opts.suggest] Filter function. Defaults to sort by title
  16. * @param {Number} [opts.limit=10] Max number of results to show
  17. * @param {Number} [opts.cursor=0] Cursor start position
  18. * @param {String} [opts.style='default'] Render style
  19. * @param {String} [opts.fallback] Fallback message - initial to default value
  20. * @param {String} [opts.initial] Index of the default value
  21. */
  22. class AutocompletePrompt extends Prompt {
  23. constructor(opts = {}) {
  24. super(opts);
  25. this.msg = opts.message;
  26. this.suggest = opts.suggest;
  27. this.choices = opts.choices;
  28. this.initial = opts.initial;
  29. this.cursor = opts.initial || opts.cursor || 0;
  30. this.fallback = opts.fallback || opts.initial !== void 0 ? `${util.figures.pointerSmall} ${getVal(this.choices, this.initial)}` : `${util.figures.pointerSmall} no matches found`;
  31. this.suggestions = [];
  32. this.input = '';
  33. this.limit = opts.limit || 10;
  34. this.transform = util.style.render(opts.style);
  35. this.render = this.render.bind(this);
  36. this.complete = this.complete.bind(this);
  37. this.clear = util.clear('');
  38. this.complete(this.render);
  39. this.render(true);
  40. }
  41. moveCursor(i) {
  42. this.cursor = i;
  43. if (this.suggestions.length > 0) this.value = getVal(this.suggestions, i);else this.value = this.initial !== void 0 ? getVal(this.choices, this.initial) : null;
  44. this.fire();
  45. }
  46. complete(cb) {
  47. var _this = this;
  48. return _asyncToGenerator(function* () {
  49. const p = _this.completing = _this.suggest(_this.input, _this.choices);
  50. const suggestions = yield p;
  51. if (_this.completing !== p) return;
  52. _this.suggestions = suggestions.slice(0, _this.limit).map(function (s) {
  53. return util.strip(s);
  54. });
  55. _this.completing = false;
  56. const l = Math.max(suggestions.length - 1, 0);
  57. _this.moveCursor(Math.min(l, _this.cursor));
  58. cb && cb();
  59. })();
  60. }
  61. reset() {
  62. this.input = '';
  63. this.complete(() => {
  64. this.moveCursor(this.initial !== void 0 ? this.initial : 0);
  65. this.render();
  66. });
  67. this.render();
  68. }
  69. abort() {
  70. this.done = this.aborted = true;
  71. this.fire();
  72. this.render();
  73. this.out.write('\n');
  74. this.close();
  75. }
  76. submit() {
  77. this.done = true;
  78. this.aborted = false;
  79. this.fire();
  80. this.render();
  81. this.out.write('\n');
  82. this.close();
  83. }
  84. _(c, key) {
  85. this.input += c;
  86. this.complete(this.render);
  87. this.render();
  88. }
  89. delete() {
  90. if (this.input.length === 0) return this.bell();
  91. this.input = this.input.slice(0, -1);
  92. this.complete(this.render);
  93. this.render();
  94. }
  95. first() {
  96. this.moveCursor(0);
  97. this.render();
  98. }
  99. last() {
  100. this.moveCursor(this.suggestions.length - 1);
  101. this.render();
  102. }
  103. up() {
  104. if (this.cursor <= 0) return this.bell();
  105. this.moveCursor(this.cursor - 1);
  106. this.render();
  107. }
  108. down() {
  109. if (this.cursor >= this.suggestions.length - 1) return this.bell();
  110. this.moveCursor(this.cursor + 1);
  111. this.render();
  112. }
  113. next() {
  114. this.moveCursor((this.cursor + 1) % this.suggestions.length);
  115. this.render();
  116. }
  117. render(first) {
  118. if (first) this.out.write(cursor.hide);
  119. let prompt = [util.style.symbol(this.done, this.aborted), this.msg, util.style.delimiter(this.completing), this.done && this.suggestions[this.cursor] ? this.suggestions[this.cursor].title : this.transform.render(this.input)].join(' ');
  120. if (!this.done) {
  121. let suggestions = this.suggestions.map((item, i) => `\n${i === this.cursor ? color.cyan(item.title) : item.title}`);
  122. prompt += suggestions.length ? suggestions.reduce((acc, line) => acc + line, '') : `\n${color.gray(this.fallback)}`;
  123. }
  124. this.out.write(this.clear + prompt);
  125. this.clear = util.clear(prompt);
  126. }
  127. }
  128. module.exports = AutocompletePrompt;