3 const Events = require('events');
4 const colors = require('ansi-colors');
5 const keypress = require('./keypress');
6 const timer = require('./timer');
7 const State = require('./state');
8 const theme = require('./theme');
9 const utils = require('./utils');
10 const ansi = require('./ansi');
13 * Base class for creating a new Prompt.
14 * @param {Object} `options` Question object.
17 class Prompt extends Events {
18 constructor(options = {}) {
20 this.name = options.name;
21 this.type = options.type;
22 this.options = options;
25 this.state = new State(this);
26 this.initial = [options.initial, options.default].find(v => v != null);
27 this.stdout = options.stdout || process.stdout;
28 this.stdin = options.stdin || process.stdin;
29 this.scale = options.scale || 1;
30 this.term = this.options.term || process.env.TERM_PROGRAM;
31 this.margin = margin(this.options.margin);
32 this.setMaxListeners(0);
36 async keypress(input, event = {}) {
37 this.keypressed = true;
38 let key = keypress.action(input, keypress(input, event), this.options.actions);
39 this.state.keypress = key;
40 this.emit('keypress', input, key);
41 this.emit('state', this.state.clone());
42 let fn = this.options[key.action] || this[key.action] || this.dispatch;
43 if (typeof fn === 'function') {
44 return await fn.call(this, input, key);
50 delete this.state.alert;
51 if (this.options.show === false) {
54 this.stdout.write(ansi.code.beep);
59 this.stdout.write(ansi.cursor.hide());
60 utils.onExit(() => this.cursorShow());
64 this.stdout.write(ansi.cursor.show());
69 if (this.stdout && this.state.show !== false) {
70 this.stdout.write(str);
72 this.state.buffer += str;
76 let buffer = this.state.buffer;
77 this.state.buffer = '';
78 if ((!buffer && !lines) || this.options.show === false) return;
79 this.stdout.write(ansi.cursor.down(lines) + ansi.clear(buffer, this.width));
83 if (this.state.closed || this.options.show === false) return;
85 let { prompt, after, rest } = this.sections();
86 let { cursor, initial = '', input = '', value = '' } = this;
88 let size = this.state.size = rest.length;
89 let state = { after, cursor, initial, input, prompt, size, value };
90 let codes = ansi.cursor.restore(state);
92 this.stdout.write(codes);
97 let { buffer, input, prompt } = this.state;
98 prompt = colors.unstyle(prompt);
99 let buf = colors.unstyle(buffer);
100 let idx = buf.indexOf(prompt);
101 let header = buf.slice(0, idx);
102 let rest = buf.slice(idx);
103 let lines = rest.split('\n');
104 let first = lines[0];
105 let last = lines[lines.length - 1];
106 let promptLine = prompt + (input ? ' ' + input : '');
107 let len = promptLine.length;
108 let after = len < first.length ? first.slice(len + 1) : '';
109 return { header, prompt: first, after, rest: lines.slice(1), last };
113 this.state.submitted = true;
114 this.state.validating = true;
116 // this will only be called when the prompt is directly submitted
117 // without initializing, i.e. when the prompt is skipped, etc. Otherwize,
118 // "options.onSubmit" is will be handled by the "initialize()" method.
119 if (this.options.onSubmit) {
120 await this.options.onSubmit.call(this, this.name, this.value, this);
123 let result = this.state.error || await this.validate(this.value, this.state);
124 if (result !== true) {
125 let error = '\n' + this.symbols.pointer + ' ';
127 if (typeof result === 'string') {
128 error += result.trim();
130 error += 'Invalid input';
133 this.state.error = '\n' + this.styles.danger(error);
134 this.state.submitted = false;
137 this.state.validating = false;
138 this.state.error = void 0;
142 this.state.validating = false;
146 this.value = await this.result(this.value);
147 this.emit('submit', this.value);
151 this.state.cancelled = this.state.submitted = true;
156 if (typeof this.options.onCancel === 'function') {
157 await this.options.onCancel.call(this, this.name, this.value, this);
160 this.emit('cancel', await this.error(err));
164 this.state.closed = true;
167 let sections = this.sections();
168 let lines = Math.ceil(sections.prompt.length / this.width);
170 this.write(ansi.cursor.down(sections.rest.length));
172 this.write('\n'.repeat(lines));
173 } catch (err) { /* do nothing */ }
179 if (!this.stop && this.options.show !== false) {
180 this.stop = keypress.listen(this, this.keypress.bind(this));
181 this.once('close', this.stop);
186 this.skipped = this.options.skip === true;
187 if (typeof this.options.skip === 'function') {
188 this.skipped = await this.options.skip.call(this, this.name, this.value);
194 let { format, options, result } = this;
196 this.format = () => format.call(this, this.value);
197 this.result = () => result.call(this, this.value);
199 if (typeof options.initial === 'function') {
200 this.initial = await options.initial.call(this, this);
203 if (typeof options.onRun === 'function') {
204 await options.onRun.call(this, this);
207 // if "options.onSubmit" is defined, we wrap the "submit" method to guarantee
208 // that "onSubmit" will always called first thing inside the submit
209 // method, regardless of how it's handled in inheriting prompts.
210 if (typeof options.onSubmit === 'function') {
211 let onSubmit = options.onSubmit.bind(this);
212 let submit = this.submit.bind(this);
213 delete this.options.onSubmit;
214 this.submit = async() => {
215 await onSubmit(this.name, this.value, this);
225 throw new Error('expected prompt to have a custom render method');
229 return new Promise(async(resolve, reject) => {
230 this.once('submit', resolve);
231 this.once('cancel', reject);
232 if (await this.skip()) {
233 this.render = () => {};
234 return this.submit();
236 await this.initialize();
241 async element(name, choice, i) {
242 let { options, state, symbols, timers } = this;
243 let timer = timers && timers[name];
245 let value = options[name] || state[name] || symbols[name];
246 let val = choice && choice[name] != null ? choice[name] : await value;
247 if (val === '') return val;
249 let res = await this.resolve(val, state, choice, i);
250 if (!res && choice && choice[name]) {
251 return this.resolve(value, state, choice, i);
257 let element = await this.element('prefix') || this.symbols;
258 let timer = this.timers && this.timers.prefix;
259 let state = this.state;
261 if (utils.isObject(element)) element = element[state.status] || element.pending;
262 if (!utils.hasColor(element)) {
263 let style = this.styles[state.status] || this.styles.pending;
264 return style(element);
270 let message = await this.element('message');
271 if (!utils.hasColor(message)) {
272 return this.styles.strong(message);
278 let element = await this.element('separator') || this.symbols;
279 let timer = this.timers && this.timers.separator;
280 let state = this.state;
282 let value = element[state.status] || element.pending || state.separator;
283 let ele = await this.resolve(value, state);
284 if (utils.isObject(ele)) ele = ele[state.status] || ele.pending;
285 if (!utils.hasColor(ele)) {
286 return this.styles.muted(ele);
291 async pointer(choice, i) {
292 let val = await this.element('pointer', choice, i);
294 if (typeof val === 'string' && utils.hasColor(val)) {
299 let styles = this.styles;
300 let focused = this.index === i;
301 let style = focused ? styles.primary : val => val;
302 let ele = await this.resolve(val[focused ? 'on' : 'off'] || val, this.state);
303 let styled = !utils.hasColor(ele) ? style(ele) : ele;
304 return focused ? styled : ' '.repeat(ele.length);
308 async indicator(choice, i) {
309 let val = await this.element('indicator', choice, i);
310 if (typeof val === 'string' && utils.hasColor(val)) {
314 let styles = this.styles;
315 let enabled = choice.enabled === true;
316 let style = enabled ? styles.success : styles.dark;
317 let ele = val[enabled ? 'on' : 'off'] || val;
318 return !utils.hasColor(ele) ? style(ele) : ele;
328 if (this.state.status === 'pending') {
329 return this.element('footer');
334 if (this.state.status === 'pending') {
335 return this.element('header');
340 if (this.state.status === 'pending' && !this.isValue(this.state.input)) {
341 let hint = await this.element('hint');
342 if (!utils.hasColor(hint)) {
343 return this.styles.muted(hint);
350 return !this.state.submitted ? (err || this.state.error) : '';
362 if (this.options.required === true) {
363 return this.isValue(value);
369 return value != null && value !== '';
372 resolve(value, ...args) {
373 return utils.resolve(this, value, ...args);
377 return Prompt.prototype;
381 return this.styles[this.state.status];
385 return this.options.rows || utils.height(this.stdout, 25);
388 return this.options.columns || utils.width(this.stdout, 80);
391 return { width: this.width, height: this.height };
395 this.state.cursor = value;
398 return this.state.cursor;
402 this.state.input = value;
405 return this.state.input;
409 this.state.value = value;
412 let { input, value } = this.state;
413 let result = [value, input].find(this.isValue.bind(this));
414 return this.isValue(result) ? result : this.initial;
417 static get prompt() {
418 return options => new this(options).run();
422 function setOptions(prompt) {
423 let isValidKey = key => {
424 return prompt[key] === void 0 || typeof prompt[key] === 'function';
453 for (let key of Object.keys(prompt.options)) {
454 if (ignore.includes(key)) continue;
455 if (/^on[A-Z]/.test(key)) continue;
456 let option = prompt.options[key];
457 if (typeof option === 'function' && isValidKey(key)) {
458 if (!ignoreFn.includes(key)) {
459 prompt[key] = option.bind(prompt);
461 } else if (typeof prompt[key] !== 'function') {
462 prompt[key] = option;
467 function margin(value) {
468 if (typeof value === 'number') {
469 value = [value, value, value, value];
471 let arr = [].concat(value || []);
472 let pad = i => i % 2 === 0 ? '\n' : ' ';
474 for (let i = 0; i < 4; i++) {
477 res.push(char.repeat(arr[i]));
485 module.exports = Prompt;