source: frontend/node_modules/prompts/lib/elements/autocomplete.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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