3 const ArrayPrompt = require('../types/array');
4 const utils = require('../utils');
6 class SelectPrompt extends ArrayPrompt {
9 this.emptyError = this.options.emptyError || 'No items were selected';
12 async dispatch(s, key) {
14 return this[key.name] ? await this[key.name](s, key) : await super.dispatch(s, key);
20 if (this.options.separator) return super.separator();
21 let sep = this.styles.muted(this.symbols.ellipsis);
22 return this.state.submitted ? super.separator() : sep;
26 return (!this.multiple || this.options.pointer) ? super.pointer(choice, i) : '';
29 indicator(choice, i) {
30 return this.multiple ? super.indicator(choice, i) : '';
33 choiceMessage(choice, i) {
34 let message = this.resolve(choice.message, this.state, choice, i);
35 if (choice.role === 'heading' && !utils.hasColor(message)) {
36 message = this.styles.strong(message);
38 return this.resolve(message, this.state, choice, i);
45 async renderChoice(choice, i) {
46 await this.onChoice(choice, i);
48 let focused = this.index === i;
49 let pointer = await this.pointer(choice, i);
50 let check = await this.indicator(choice, i) + (choice.pad || '');
51 let hint = await this.resolve(choice.hint, this.state, choice, i);
53 if (hint && !utils.hasColor(hint)) {
54 hint = this.styles.muted(hint);
57 let ind = this.indent(choice);
58 let msg = await this.choiceMessage(choice, i);
59 let line = () => [this.margin[3], ind + pointer + check, msg, this.margin[1], hint].filter(Boolean).join(' ');
61 if (choice.role === 'heading') {
65 if (choice.disabled) {
66 if (!utils.hasColor(msg)) {
67 msg = this.styles.disabled(msg);
73 msg = this.styles.em(msg);
79 async renderChoices() {
80 if (this.state.loading === 'choices') {
81 return this.styles.warning('Loading choices');
84 if (this.state.submitted) return '';
85 let choices = this.visible.map(async(ch, i) => await this.renderChoice(ch, i));
86 let visible = await Promise.all(choices);
87 if (!visible.length) visible.push(this.styles.danger('No matching choices'));
88 let result = this.margin[0] + visible.join('\n');
91 if (this.options.choicesHeader) {
92 header = await this.resolve(this.options.choicesHeader, this.state);
95 return [header, result].filter(Boolean).join('\n');
99 if (!this.state.submitted || this.state.cancelled) return '';
100 if (Array.isArray(this.selected)) {
101 return this.selected.map(choice => this.styles.primary(choice.name)).join(', ');
103 return this.styles.primary(this.selected.name);
107 let { submitted, size } = this.state;
110 let header = await this.header();
111 let prefix = await this.prefix();
112 let separator = await this.separator();
113 let message = await this.message();
115 if (this.options.promptLine !== false) {
116 prompt = [prefix, message, separator, ''].join(' ');
117 this.state.prompt = prompt;
120 let output = await this.format();
121 let help = (await this.error()) || (await this.hint());
122 let body = await this.renderChoices();
123 let footer = await this.footer();
125 if (output) prompt += output;
126 if (help && !prompt.includes(help)) prompt += ' ' + help;
128 if (submitted && !output && !body.trim() && this.multiple && this.emptyError != null) {
129 prompt += this.styles.danger(this.emptyError);
133 this.write([header, prompt, body, footer].filter(Boolean).join('\n'));
134 this.write(this.margin[2]);
139 module.exports = SelectPrompt;