3 const Prompt = require('../prompt');
4 const { isPrimitive, hasColor } = require('../utils');
6 class BooleanPrompt extends Prompt {
13 let initial = await this.resolve(this.initial, this.state);
14 this.input = await this.cast(initial);
15 await super.initialize();
19 if (!this.isValue(ch)) return this.alert();
25 let { styles, state } = this;
26 return !state.submitted ? styles.primary(value) : styles.success(value);
30 return this.isTrue(input);
34 return /^[ty1]/i.test(input);
38 return /^[fn0]/i.test(input);
42 return isPrimitive(value) && (this.isTrue(value) || this.isFalse(value));
46 if (this.state.status === 'pending') {
47 let hint = await this.element('hint');
48 if (!hasColor(hint)) {
49 return this.styles.muted(hint);
56 let { input, size } = this.state;
58 let prefix = await this.prefix();
59 let sep = await this.separator();
60 let msg = await this.message();
61 let hint = this.styles.muted(this.default);
63 let promptLine = [prefix, msg, hint, sep].filter(Boolean).join(' ');
64 this.state.prompt = promptLine;
66 let header = await this.header();
67 let value = this.value = this.cast(input);
68 let output = await this.format(value);
69 let help = (await this.error()) || (await this.hint());
70 let footer = await this.footer();
72 if (help && !promptLine.includes(help)) output += ' ' + help;
73 promptLine += ' ' + output;
76 this.write([header, promptLine, footer].filter(Boolean).join('\n'));
84 return this.cast(super.value);
88 module.exports = BooleanPrompt;