1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var _ = require("lodash")
|
8 | , inquirer = require("inquirer")
|
9 | , EventEmitter = require("events").EventEmitter
|
10 | , VantageUtil = require("./util")
|
11 | ;
|
12 |
|
13 | var ui = {
|
14 |
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | _init: function() {
|
25 |
|
26 | var self = this;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | this.parent = void 0;
|
33 |
|
34 |
|
35 | this._activePrompt = void 0;
|
36 |
|
37 |
|
38 |
|
39 | this._midPrompt = false;
|
40 |
|
41 |
|
42 | this.inquirer = inquirer;
|
43 |
|
44 |
|
45 | this._cancelled = false;
|
46 |
|
47 |
|
48 | this._pipeFn = void 0;
|
49 |
|
50 |
|
51 | inquirer.prompt.prompts.input.prototype.onKeypress = function(e) {
|
52 | self.emit("client_keypress", e);
|
53 | return self._keypressHandler(e, this);
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 | (function(render){
|
59 | inquirer.prompt.prompts.input.prototype.render = function() {
|
60 | self._activePrompt = this;
|
61 | return render.call(this);
|
62 | };
|
63 | })(inquirer.prompt.prompts.input.prototype.render);
|
64 | },
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | prompt: function(options, cb) {
|
75 | var self = this;
|
76 | options = options || {};
|
77 | if (!this.parent) { return; }
|
78 | if (options.delimiter) {
|
79 | this.setDelimiter(options.delimiter);
|
80 | }
|
81 | if (options.message) {
|
82 | this.setDelimiter(options.message);
|
83 | }
|
84 | if (self._midPrompt) {
|
85 | console.log("Prompt called when mid prompt...");
|
86 | throw new Error("UI Prompt called when already mid prompt.");
|
87 | }
|
88 | self._midPrompt = true;
|
89 | try {
|
90 | inquirer.prompt(options, function(result) {
|
91 | self._midPrompt = false;
|
92 | cb(result);
|
93 | });
|
94 | } catch(e) {
|
95 | console.log("Vantage Prompt error:", e);
|
96 | }
|
97 | },
|
98 |
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | midPrompt: function() {
|
108 | return (
|
109 | (this._midPrompt && this.parent) ? true : false
|
110 | );
|
111 | },
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | setDelimiter: function(str) {
|
124 | if (!this.parent) { return; }
|
125 | str = String(str).trim() + " ";
|
126 | this._lastDelimiter = str;
|
127 | inquirer.prompt.prompts.password.prototype.prefix = function(){
|
128 | return str;
|
129 | };
|
130 | inquirer.prompt.prompts.input.prototype.prefix = function(){
|
131 | return str;
|
132 | };
|
133 | },
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | _keypressHandler: function(e, prompt) {
|
145 | if (!this.parent) { return; }
|
146 | this._activePrompt = prompt;
|
147 | var key = (e.key || {}).name;
|
148 | var value = (prompt) ? String(prompt.rl.line).trim() : void 0;
|
149 | this.emit("vantage_ui_keypress", { key: key, value: value });
|
150 | },
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | refresh: function() {
|
160 | if (!this.parent) { return false; }
|
161 | if (!this._activePrompt) { return false; }
|
162 | if (!this._midPrompt) { return false; }
|
163 | this._activePrompt.clean();
|
164 | this._midPrompt = false;
|
165 | this._cancelled = true;
|
166 | if (this._activePrompt.status !== "answered") {
|
167 | this._activePrompt.status = "answered";
|
168 | this._activePrompt.done();
|
169 | }
|
170 | this._cancelled = false;
|
171 | this.parent._prompt();
|
172 | return this;
|
173 | },
|
174 |
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | pause: function() {
|
184 | if (!this.parent) { return false; }
|
185 | if (!this._activePrompt) { return false; }
|
186 | if (!this._midPrompt) { return false; }
|
187 | var val = this._activePrompt.rl.line;
|
188 | this._midPrompt = false;
|
189 | this._cancelled = true;
|
190 | this._activePrompt.clean();
|
191 | this._activePrompt.status = "answered";
|
192 | this._activePrompt.done();
|
193 | return val;
|
194 | },
|
195 |
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | resume: function(val) {
|
207 | if (!this.parent) { return this; }
|
208 | val = val || "";
|
209 | if (!this._activePrompt) { return this; }
|
210 | if (this._midPrompt) { return this; }
|
211 | this.parent._prompt();
|
212 | this._activePrompt.rl.line = val;
|
213 | this._activePrompt.rl.cursor = val.length;
|
214 | this._activePrompt.cacheCursorPos();
|
215 | this._activePrompt.clean().render().write( this._activePrompt.rl.line );
|
216 | this._activePrompt.restoreCursorPos();
|
217 | return this;
|
218 | },
|
219 |
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | imprint: function(str) {
|
228 | if (!this.parent) { return this; }
|
229 | var val = this._activePrompt.rl.line;
|
230 | var delimiter = this._lastDelimiter || "";
|
231 | this.log(delimiter + val);
|
232 | return this;
|
233 | },
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | redraw: function(str) {
|
244 | if (!this.parent) { return this; }
|
245 | this._activePrompt.rl.line = str;
|
246 | this._activePrompt.rl.cursor = str.length;
|
247 | this._activePrompt.cacheCursorPos();
|
248 | this._activePrompt.clean().render().write( this._activePrompt.rl.line );
|
249 | this._activePrompt.restoreCursorPos();
|
250 | return this;
|
251 | },
|
252 |
|
253 | |
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | attach: function(vantage) {
|
262 | this.parent = vantage;
|
263 | this.refresh();
|
264 | this.parent._prompt();
|
265 | return this;
|
266 | },
|
267 |
|
268 | |
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | log: function() {
|
280 | var args = VantageUtil.fixArgsForApply(arguments);
|
281 | args = (_.isFunction(this._pipeFn))
|
282 | ? this._pipeFn.call(this, args)
|
283 | : args;
|
284 | if (args === "") {
|
285 | return this;
|
286 | }
|
287 | args = VantageUtil.fixArgsForApply(args);
|
288 | if (this.midPrompt()) {
|
289 | var data = this.pause();
|
290 | console.log.apply(console.log, args);
|
291 | if (typeof data !== "undefined" && data !== false) {
|
292 | this.resume(data);
|
293 | } else {
|
294 | console.log("Log got back 'false' as data. This shouldn't happen.", data);
|
295 | }
|
296 | } else {
|
297 | console.log.apply(console.log, args);
|
298 | }
|
299 | return this;
|
300 | },
|
301 |
|
302 | |
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | detach: function(vantage) {
|
311 | if (vantage === this.parent) {
|
312 | this.parent = void 0;
|
313 | }
|
314 | return this;
|
315 | }
|
316 | };
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | _.assign(ui, EventEmitter.prototype);
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | global.__vantage = global.__vantage || {};
|
343 | global.__vantage.ui = global.__vantage.ui || {
|
344 | exists: false,
|
345 | exports: void 0
|
346 | };
|
347 |
|
348 | if (!global.__vantage.ui.exists) {
|
349 | global.__vantage.ui.exists = true;
|
350 | global.__vantage.ui.exports = ui;
|
351 | module.exports = exports = ui;
|
352 | ui._init();
|
353 | } else {
|
354 | module.exports = global.__vantage.ui.exports;
|
355 | }
|