-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.js
268 lines (225 loc) · 7.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// @flow
/**
* `autocomplete` type prompt
*/
var ansiEscapes = require('ansi-escapes');
var chalk = require('chalk');
var figures = require('figures');
var Base = require('inquirer/lib/prompts/base');
var Choices = require('inquirer/lib/objects/choices');
var observe = require('inquirer/lib/utils/events');
var utils = require('inquirer/lib/utils/readline');
var Paginator = require('inquirer/lib/utils/paginator');
var runAsync = require('run-async');
var { takeWhile } = require('rxjs/operators');
class AutocompletePrompt extends Base {
constructor(
questions /*: Array<any> */,
rl /*: readline$Interface */,
answers /*: Array<any> */
) {
super(questions, rl, answers);
if (!this.opt.source) {
this.throwParamError('source');
}
this.currentChoices = [];
this.firstRender = true;
this.selected = 0;
// Make sure no default is set (so it won't be printed)
this.opt.default = null;
this.paginator = new Paginator();
}
/**
* Start the Inquiry session
* @param {Function} cb Callback when prompt is done
* @return {this}
*/
_run(cb /*: Function */) /*: this*/ {
this.done = cb;
if (this.rl.history instanceof Array) {
this.rl.history = [];
}
var events = observe(this.rl);
const dontHaveAnswer = () => this.answer === undefined;
events.line
.pipe(takeWhile(dontHaveAnswer))
.forEach(this.onSubmit.bind(this));
events.keypress
.pipe(takeWhile(dontHaveAnswer))
.forEach(this.onKeypress.bind(this));
// Call once at init
this.search(undefined);
return this;
}
/**
* Render the prompt to screen
* @return {undefined}
*/
render(error /*: ?string */) {
// Render question
var content = this.getQuestion();
var bottomContent = '';
if (this.firstRender) {
var suggestText = this.opt.suggestOnly ? ', tab to autocomplete' : '';
content += chalk.dim(
'(Use arrow keys or type to search' + suggestText + ')'
);
}
// Render choices or answer depending on the state
if (this.status === 'answered') {
content += chalk.cyan(this.shortAnswer || this.answerName || this.answer);
} else if (this.searching) {
content += this.rl.line;
bottomContent += ' ' + chalk.dim('Searching...');
} else if (this.currentChoices.length) {
var choicesStr = listRender(this.currentChoices, this.selected);
content += this.rl.line;
bottomContent += this.paginator.paginate(
choicesStr,
this.selected,
this.opt.pageSize
);
} else {
content += this.rl.line;
bottomContent += ' ' + chalk.yellow('No results...');
}
if (error) {
bottomContent += '\n' + chalk.red('>> ') + error;
}
this.firstRender = false;
this.screen.render(content, bottomContent);
}
/**
* When user press `enter` key
*/
onSubmit(line /* : string */) {
if (typeof this.opt.validate === 'function' && this.opt.suggestOnly) {
var validationResult = this.opt.validate(line);
if (validationResult !== true) {
this.render(
validationResult || 'Enter something, tab to autocomplete!'
);
return;
}
}
var choice = {};
if (this.currentChoices.length <= this.selected && !this.opt.suggestOnly) {
this.rl.write(line);
this.search(line);
return;
}
if (this.opt.suggestOnly) {
choice.value = line || this.rl.line;
this.answer = line || this.rl.line;
this.answerName = line || this.rl.line;
this.shortAnswer = line || this.rl.line;
this.rl.line = '';
} else {
choice = this.currentChoices.getChoice(this.selected);
this.answer = choice.value;
this.answerName = choice.name;
this.shortAnswer = choice.short;
}
runAsync(this.opt.filter, (err, value) => {
choice.value = value;
this.answer = value;
if (this.opt.suggestOnly) {
this.shortAnswer = value;
}
this.status = 'answered';
// Rerender prompt
this.render();
this.screen.done();
this.done(choice.value);
})(choice.value);
}
search(searchTerm /* : ?string */) {
var self = this;
self.selected = 0;
// Only render searching state after first time
if (self.searchedOnce) {
self.searching = true;
self.currentChoices = new Choices([]);
self.render(); // Now render current searching state
} else {
self.searchedOnce = true;
}
self.lastSearchTerm = searchTerm;
var thisPromise = self.opt.source(self.answers, searchTerm);
// Store this promise for check in the callback
self.lastPromise = thisPromise;
return thisPromise.then(function inner(choices) {
// If another search is triggered before the current search finishes, don't set results
if (thisPromise !== self.lastPromise) return;
choices = new Choices(
choices.filter(function(choice) {
return choice.type !== 'separator';
})
);
self.currentChoices = choices;
self.searching = false;
self.render();
});
}
ensureSelectedInRange() {
var selectedIndex = Math.min(this.selected, this.currentChoices.length); // Not above currentChoices length - 1
this.selected = Math.max(selectedIndex, 0); // Not below 0
}
/**
* When user type
*/
onKeypress(e /* : {key: { name: string }, value: string} */) {
var len;
var keyName = (e.key && e.key.name) || undefined;
if (keyName === 'tab' && this.opt.suggestOnly) {
if (this.currentChoices.getChoice(this.selected)) {
this.rl.write(ansiEscapes.cursorLeft);
var autoCompleted = this.currentChoices.getChoice(this.selected).value;
this.rl.write(ansiEscapes.cursorForward(autoCompleted.length));
this.rl.line = autoCompleted;
this.render();
}
} else if (keyName === 'down') {
len = this.currentChoices.length;
this.selected = this.selected < len - 1 ? this.selected + 1 : 0;
this.ensureSelectedInRange();
this.render();
utils.up(this.rl, 2);
} else if (keyName === 'up') {
len = this.currentChoices.length;
this.selected = this.selected > 0 ? this.selected - 1 : len - 1;
this.ensureSelectedInRange();
this.render();
} else {
this.render(); // Render input automatically
// Only search if input have actually changed, not because of other keypresses
if (this.lastSearchTerm !== this.rl.line) {
this.search(this.rl.line); // Trigger new search
}
}
}
}
/**
* Function for rendering list choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function listRender(choices, pointer /*: string */) /*: string */ {
var output = '';
var separatorOffset = 0;
choices.forEach(function(choice, i) {
if (choice.type === 'separator') {
separatorOffset++;
output += ' ' + choice + '\n';
return;
}
var isSelected = i - separatorOffset === pointer;
var line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
if (isSelected) {
line = chalk.cyan(line);
}
output += line + ' \n';
});
return output.replace(/\n$/, '');
}
module.exports = AutocompletePrompt;