-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
226 lines (183 loc) · 5.25 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
'use strict';
/*!
* This is almost entirely based on:
* https://www.npmjs.com/package/inquirer-autocomplete-prompt
* Copyright (c) 2015, Martin Hansen <martin@martinhansen.no>
*/
var util = require('util');
var log = require('log-utils');
var figures = require('figures');
var Paginator = require('terminal-paginator');
var BasePrompt = require('prompt-base');
var Choices = require('prompt-choices');
var utils = require('readline-utils');
/**
* Create a new autocomplete `Prompt`
*/
function Prompt() {
BasePrompt.apply(this, arguments);
if (typeof this.question.source !== 'function') {
throw new TypeError('expected source to be defined');
}
this.currentChoices = [];
this.firstRender = true;
this.selected = 0;
// Make sure no default is set (so it won't be printed)
this.question.default = undefined;
this.paginator = new Paginator();
}
/**
* Inherit `BasePrompt`
*/
util.inherits(Prompt, BasePrompt);
/**
* Start the prompt session
* @param {Function} `cb` Callback when prompt is finished
* @return {Object} Returns the instance for chaining
*/
Prompt.prototype.ask = function(cb) {
this.callback = cb;
if (Array.isArray(this.rl.history)) {
this.rl.history = [];
}
this.only('line', this.onSubmit.bind(this));
this.only('down', this.createChoices.bind(this, ''));
this.only('keypress', this.onKeypress.bind(this));
// initialize search
this.search();
return this;
};
/**
* Render the prompt to screen
* @return {Prompt} self
*/
Prompt.prototype.render = function() {
var message = this.message;
var bottomContent = '';
if (this.firstRender) {
this.firstRender = false;
message += log.dim('(Use arrow keys or type to search)');
}
// Render choices or answer depending on the state
if (this.status === 'answered') {
message += log.cyan(this.answer);
} else if (this.searching) {
message += this.rl.line;
bottomContent += ' ' + log.dim('Searching...');
} else if (this.currentChoices.length) {
var choicesStr = listRender(this.currentChoices, this.selected);
message += this.rl.line;
bottomContent += this.paginator.paginate(choicesStr, this.selected);
} else {
message += this.rl.line;
bottomContent += ' ' + log.yellow('No results...');
}
this.ui.render(message, bottomContent);
};
/**
* When user press `enter` key
*/
Prompt.prototype.createChoices = function(line) {
if (this.currentChoices.length <= this.selected) {
this.rl.write(line);
this.search(line);
return true;
}
};
Prompt.prototype.onSubmit = function(line) {
if (this.createChoices(line)) return;
var choice = this.currentChoices.getChoice(this.selected);
this.only();
this.submitAnswer(choice.value);
};
Prompt.prototype.search = function(searchTerm) {
this.selected = 0;
var self = this;
// only render searching state after first time
if (this.searchedOnce) {
this.searching = true;
this.currentChoices = new Choices([]);
this.render(); // now render current searching state
} else {
this.searchedOnce = true;
}
this.lastSearchTerm = searchTerm;
var thisPromise = this.question.source(this.answers, searchTerm);
// store this promise for check in the callback
this.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();
});
};
/**
* Coerce selection to fit within the min-max accepted range
*/
Prompt.prototype.coerceToRange = function() {
var selectedIndex = Math.min(this.selected, this.currentChoices.length);
this.selected = Math.max(selectedIndex, 0);
};
/**
* When user types
*/
Prompt.prototype.onKeypress = function(keypress) {
var len = this.currentChoices.length;
// Let onSubmit handle return key press
if (keypress === 'return') {
return;
}
if (keypress === 'down') {
this.selected = (this.selected < len - 1) ? this.selected + 1 : 0;
this.coerceToRange();
this.render();
utils.up(this.rl, 2);
return;
}
if (keypress === 'up') {
this.selected = (this.selected > 0) ? this.selected - 1 : len - 1;
this.coerceToRange();
this.render();
return;
}
// render input
this.render();
// Only search if input has actually changed
if (this.lastSearchTerm !== this.rl.line) {
// trigger new search
this.search(this.rl.line);
}
};
/**
* Function for rendering list choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function listRender(choices, pointer) {
var separatorOffset = 0;
var output = '';
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 = log.cyan(line);
}
output += line + ' \n';
});
return output.replace(/\n$/, '');
}
/**
* Module exports
*/
module.exports = Prompt;