.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / lib / prompts / select.js
1 'use strict';
2
3 const ArrayPrompt = require('../types/array');
4 const utils = require('../utils');
5
6 class SelectPrompt extends ArrayPrompt {
7   constructor(options) {
8     super(options);
9     this.emptyError = this.options.emptyError || 'No items were selected';
10   }
11
12   async dispatch(s, key) {
13     if (this.multiple) {
14       return this[key.name] ? await this[key.name](s, key) : await super.dispatch(s, key);
15     }
16     this.alert();
17   }
18
19   separator() {
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;
23   }
24
25   pointer(choice, i) {
26     return (!this.multiple || this.options.pointer) ? super.pointer(choice, i) : '';
27   }
28
29   indicator(choice, i) {
30     return this.multiple ? super.indicator(choice, i) : '';
31   }
32
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);
37     }
38     return this.resolve(message, this.state, choice, i);
39   }
40
41   choiceSeparator() {
42     return ':';
43   }
44
45   async renderChoice(choice, i) {
46     await this.onChoice(choice, i);
47
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);
52
53     if (hint && !utils.hasColor(hint)) {
54       hint = this.styles.muted(hint);
55     }
56
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(' ');
60
61     if (choice.role === 'heading') {
62       return line();
63     }
64
65     if (choice.disabled) {
66       if (!utils.hasColor(msg)) {
67         msg = this.styles.disabled(msg);
68       }
69       return line();
70     }
71
72     if (focused) {
73       msg = this.styles.em(msg);
74     }
75
76     return line();
77   }
78
79   async renderChoices() {
80     if (this.state.loading === 'choices') {
81       return this.styles.warning('Loading choices');
82     }
83
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');
89     let header;
90
91     if (this.options.choicesHeader) {
92       header = await this.resolve(this.options.choicesHeader, this.state);
93     }
94
95     return [header, result].filter(Boolean).join('\n');
96   }
97
98   format() {
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(', ');
102     }
103     return this.styles.primary(this.selected.name);
104   }
105
106   async render() {
107     let { submitted, size } = this.state;
108
109     let prompt = '';
110     let header = await this.header();
111     let prefix = await this.prefix();
112     let separator = await this.separator();
113     let message = await this.message();
114
115     if (this.options.promptLine !== false) {
116       prompt = [prefix, message, separator, ''].join(' ');
117       this.state.prompt = prompt;
118     }
119
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();
124
125     if (output) prompt += output;
126     if (help && !prompt.includes(help)) prompt += ' ' + help;
127
128     if (submitted && !output && !body.trim() && this.multiple && this.emptyError != null) {
129       prompt += this.styles.danger(this.emptyError);
130     }
131
132     this.clear(size);
133     this.write([header, prompt, body, footer].filter(Boolean).join('\n'));
134     this.write(this.margin[2]);
135     this.restore();
136   }
137 }
138
139 module.exports = SelectPrompt;