autocomplete.js 4.1 KB

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