Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autocomplete.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. 'use strict';
  2. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  3. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  4. const color = require('kleur');
  5. const Prompt = require('./prompt');
  6. const _require = require('sisteransi'),
  7. erase = _require.erase,
  8. cursor = _require.cursor;
  9. const _require2 = require('../util'),
  10. style = _require2.style,
  11. clear = _require2.clear,
  12. figures = _require2.figures,
  13. wrap = _require2.wrap,
  14. entriesToDisplay = _require2.entriesToDisplay;
  15. const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);
  16. const getTitle = (arr, i) => arr[i] && (arr[i].title || arr[i].value || arr[i]);
  17. const getIndex = (arr, valOrTitle) => {
  18. const index = arr.findIndex(el => el.value === valOrTitle || el.title === valOrTitle);
  19. return index > -1 ? index : undefined;
  20. };
  21. /**
  22. * TextPrompt Base Element
  23. * @param {Object} opts Options
  24. * @param {String} opts.message Message
  25. * @param {Array} opts.choices Array of auto-complete choices objects
  26. * @param {Function} [opts.suggest] Filter function. Defaults to sort by title
  27. * @param {Number} [opts.limit=10] Max number of results to show
  28. * @param {Number} [opts.cursor=0] Cursor start position
  29. * @param {String} [opts.style='default'] Render style
  30. * @param {String} [opts.fallback] Fallback message - initial to default value
  31. * @param {String} [opts.initial] Index of the default value
  32. * @param {Boolean} [opts.clearFirst] The first ESCAPE keypress will clear the input
  33. * @param {Stream} [opts.stdin] The Readable stream to listen to
  34. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  35. * @param {String} [opts.noMatches] The no matches found label
  36. */
  37. class AutocompletePrompt extends Prompt {
  38. constructor(opts = {}) {
  39. super(opts);
  40. this.msg = opts.message;
  41. this.suggest = opts.suggest;
  42. this.choices = opts.choices;
  43. this.initial = typeof opts.initial === 'number' ? opts.initial : getIndex(opts.choices, opts.initial);
  44. this.select = this.initial || opts.cursor || 0;
  45. this.i18n = {
  46. noMatches: opts.noMatches || 'no matches found'
  47. };
  48. this.fallback = opts.fallback || this.initial;
  49. this.clearFirst = opts.clearFirst || false;
  50. this.suggestions = [];
  51. this.input = '';
  52. this.limit = opts.limit || 10;
  53. this.cursor = 0;
  54. this.transform = style.render(opts.style);
  55. this.scale = this.transform.scale;
  56. this.render = this.render.bind(this);
  57. this.complete = this.complete.bind(this);
  58. this.clear = clear('', this.out.columns);
  59. this.complete(this.render);
  60. this.render();
  61. }
  62. set fallback(fb) {
  63. this._fb = Number.isSafeInteger(parseInt(fb)) ? parseInt(fb) : fb;
  64. }
  65. get fallback() {
  66. let choice;
  67. if (typeof this._fb === 'number') choice = this.choices[this._fb];else if (typeof this._fb === 'string') choice = {
  68. title: this._fb
  69. };
  70. return choice || this._fb || {
  71. title: this.i18n.noMatches
  72. };
  73. }
  74. moveSelect(i) {
  75. this.select = i;
  76. if (this.suggestions.length > 0) this.value = getVal(this.suggestions, i);else this.value = this.fallback.value;
  77. this.fire();
  78. }
  79. complete(cb) {
  80. var _this = this;
  81. return _asyncToGenerator(function* () {
  82. const p = _this.completing = _this.suggest(_this.input, _this.choices);
  83. const suggestions = yield p;
  84. if (_this.completing !== p) return;
  85. _this.suggestions = suggestions.map((s, i, arr) => ({
  86. title: getTitle(arr, i),
  87. value: getVal(arr, i),
  88. description: s.description
  89. }));
  90. _this.completing = false;
  91. const l = Math.max(suggestions.length - 1, 0);
  92. _this.moveSelect(Math.min(l, _this.select));
  93. cb && cb();
  94. })();
  95. }
  96. reset() {
  97. this.input = '';
  98. this.complete(() => {
  99. this.moveSelect(this.initial !== void 0 ? this.initial : 0);
  100. this.render();
  101. });
  102. this.render();
  103. }
  104. exit() {
  105. if (this.clearFirst && this.input.length > 0) {
  106. this.reset();
  107. } else {
  108. this.done = this.exited = true;
  109. this.aborted = false;
  110. this.fire();
  111. this.render();
  112. this.out.write('\n');
  113. this.close();
  114. }
  115. }
  116. abort() {
  117. this.done = this.aborted = true;
  118. this.exited = false;
  119. this.fire();
  120. this.render();
  121. this.out.write('\n');
  122. this.close();
  123. }
  124. submit() {
  125. this.done = true;
  126. this.aborted = this.exited = false;
  127. this.fire();
  128. this.render();
  129. this.out.write('\n');
  130. this.close();
  131. }
  132. _(c, key) {
  133. let s1 = this.input.slice(0, this.cursor);
  134. let s2 = this.input.slice(this.cursor);
  135. this.input = `${s1}${c}${s2}`;
  136. this.cursor = s1.length + 1;
  137. this.complete(this.render);
  138. this.render();
  139. }
  140. delete() {
  141. if (this.cursor === 0) return this.bell();
  142. let s1 = this.input.slice(0, this.cursor - 1);
  143. let s2 = this.input.slice(this.cursor);
  144. this.input = `${s1}${s2}`;
  145. this.complete(this.render);
  146. this.cursor = this.cursor - 1;
  147. this.render();
  148. }
  149. deleteForward() {
  150. if (this.cursor * this.scale >= this.rendered.length) return this.bell();
  151. let s1 = this.input.slice(0, this.cursor);
  152. let s2 = this.input.slice(this.cursor + 1);
  153. this.input = `${s1}${s2}`;
  154. this.complete(this.render);
  155. this.render();
  156. }
  157. first() {
  158. this.moveSelect(0);
  159. this.render();
  160. }
  161. last() {
  162. this.moveSelect(this.suggestions.length - 1);
  163. this.render();
  164. }
  165. up() {
  166. if (this.select === 0) {
  167. this.moveSelect(this.suggestions.length - 1);
  168. } else {
  169. this.moveSelect(this.select - 1);
  170. }
  171. this.render();
  172. }
  173. down() {
  174. if (this.select === this.suggestions.length - 1) {
  175. this.moveSelect(0);
  176. } else {
  177. this.moveSelect(this.select + 1);
  178. }
  179. this.render();
  180. }
  181. next() {
  182. if (this.select === this.suggestions.length - 1) {
  183. this.moveSelect(0);
  184. } else this.moveSelect(this.select + 1);
  185. this.render();
  186. }
  187. nextPage() {
  188. this.moveSelect(Math.min(this.select + this.limit, this.suggestions.length - 1));
  189. this.render();
  190. }
  191. prevPage() {
  192. this.moveSelect(Math.max(this.select - this.limit, 0));
  193. this.render();
  194. }
  195. left() {
  196. if (this.cursor <= 0) return this.bell();
  197. this.cursor = this.cursor - 1;
  198. this.render();
  199. }
  200. right() {
  201. if (this.cursor * this.scale >= this.rendered.length) return this.bell();
  202. this.cursor = this.cursor + 1;
  203. this.render();
  204. }
  205. renderOption(v, hovered, isStart, isEnd) {
  206. let desc;
  207. let prefix = isStart ? figures.arrowUp : isEnd ? figures.arrowDown : ' ';
  208. let title = hovered ? color.cyan().underline(v.title) : v.title;
  209. prefix = (hovered ? color.cyan(figures.pointer) + ' ' : ' ') + prefix;
  210. if (v.description) {
  211. desc = ` - ${v.description}`;
  212. if (prefix.length + title.length + desc.length >= this.out.columns || v.description.split(/\r?\n/).length > 1) {
  213. desc = '\n' + wrap(v.description, {
  214. margin: 3,
  215. width: this.out.columns
  216. });
  217. }
  218. }
  219. return prefix + ' ' + title + color.gray(desc || '');
  220. }
  221. render() {
  222. if (this.closed) return;
  223. if (this.firstRender) this.out.write(cursor.hide);else this.out.write(clear(this.outputText, this.out.columns));
  224. super.render();
  225. let _entriesToDisplay = entriesToDisplay(this.select, this.choices.length, this.limit),
  226. startIndex = _entriesToDisplay.startIndex,
  227. endIndex = _entriesToDisplay.endIndex;
  228. this.outputText = [style.symbol(this.done, this.aborted, this.exited), color.bold(this.msg), style.delimiter(this.completing), this.done && this.suggestions[this.select] ? this.suggestions[this.select].title : this.rendered = this.transform.render(this.input)].join(' ');
  229. if (!this.done) {
  230. const suggestions = this.suggestions.slice(startIndex, endIndex).map((item, i) => this.renderOption(item, this.select === i + startIndex, i === 0 && startIndex > 0, i + startIndex === endIndex - 1 && endIndex < this.choices.length)).join('\n');
  231. this.outputText += `\n` + (suggestions || color.gray(this.fallback.title));
  232. }
  233. this.out.write(erase.line + cursor.to(0) + this.outputText);
  234. }
  235. }
  236. module.exports = AutocompletePrompt;